clang 23.0.0git
CGExprScalar.cpp
Go to the documentation of this file.
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 "CGHLSLRuntime.h"
17#include "CGObjCRuntime.h"
18#include "CGOpenMPRuntime.h"
19#include "CGRecordLayout.h"
20#include "CodeGenFunction.h"
21#include "CodeGenModule.h"
22#include "ConstantEmitter.h"
23#include "TargetInfo.h"
24#include "TrapReasonBuilder.h"
26#include "clang/AST/Attr.h"
27#include "clang/AST/DeclObjC.h"
28#include "clang/AST/Expr.h"
35#include "llvm/ADT/APFixedPoint.h"
36#include "llvm/ADT/ScopeExit.h"
37#include "llvm/IR/Argument.h"
38#include "llvm/IR/CFG.h"
39#include "llvm/IR/Constants.h"
40#include "llvm/IR/DataLayout.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/IR/FixedPointBuilder.h"
43#include "llvm/IR/Function.h"
44#include "llvm/IR/GEPNoWrapFlags.h"
45#include "llvm/IR/GetElementPtrTypeIterator.h"
46#include "llvm/IR/GlobalVariable.h"
47#include "llvm/IR/Intrinsics.h"
48#include "llvm/IR/IntrinsicsPowerPC.h"
49#include "llvm/IR/MatrixBuilder.h"
50#include "llvm/IR/Module.h"
51#include "llvm/Support/TypeSize.h"
52#include <cstdarg>
53#include <optional>
54
55using namespace clang;
56using namespace CodeGen;
57using llvm::Value;
58
59//===----------------------------------------------------------------------===//
60// Scalar Expression Emitter
61//===----------------------------------------------------------------------===//
62
63namespace llvm {
64extern cl::opt<bool> EnableSingleByteCoverage;
65} // namespace llvm
66
67namespace {
68
69/// Determine whether the given binary operation may overflow.
70/// Sets \p Result to the value of the operation for BO_Add, BO_Sub, BO_Mul,
71/// and signed BO_{Div,Rem}. For these opcodes, and for unsigned BO_{Div,Rem},
72/// the returned overflow check is precise. The returned value is 'true' for
73/// all other opcodes, to be conservative.
74bool mayHaveIntegerOverflow(llvm::ConstantInt *LHS, llvm::ConstantInt *RHS,
75 BinaryOperator::Opcode Opcode, bool Signed,
76 llvm::APInt &Result) {
77 // Assume overflow is possible, unless we can prove otherwise.
78 bool Overflow = true;
79 const auto &LHSAP = LHS->getValue();
80 const auto &RHSAP = RHS->getValue();
81 if (Opcode == BO_Add) {
82 Result = Signed ? LHSAP.sadd_ov(RHSAP, Overflow)
83 : LHSAP.uadd_ov(RHSAP, Overflow);
84 } else if (Opcode == BO_Sub) {
85 Result = Signed ? LHSAP.ssub_ov(RHSAP, Overflow)
86 : LHSAP.usub_ov(RHSAP, Overflow);
87 } else if (Opcode == BO_Mul) {
88 Result = Signed ? LHSAP.smul_ov(RHSAP, Overflow)
89 : LHSAP.umul_ov(RHSAP, Overflow);
90 } else if (Opcode == BO_Div || Opcode == BO_Rem) {
91 if (Signed && !RHS->isZero())
92 Result = LHSAP.sdiv_ov(RHSAP, Overflow);
93 else
94 return false;
95 }
96 return Overflow;
97}
98
99struct BinOpInfo {
100 Value *LHS;
101 Value *RHS;
102 QualType Ty; // Computation Type.
103 BinaryOperator::Opcode Opcode; // Opcode of BinOp to perform
104 FPOptions FPFeatures;
105 const Expr *E; // Entire expr, for error unsupported. May not be binop.
106
107 /// Check if the binop can result in integer overflow.
108 bool mayHaveIntegerOverflow() const {
109 // Without constant input, we can't rule out overflow.
110 auto *LHSCI = dyn_cast<llvm::ConstantInt>(LHS);
111 auto *RHSCI = dyn_cast<llvm::ConstantInt>(RHS);
112 if (!LHSCI || !RHSCI)
113 return true;
114
115 llvm::APInt Result;
116 return ::mayHaveIntegerOverflow(
117 LHSCI, RHSCI, Opcode, Ty->hasSignedIntegerRepresentation(), Result);
118 }
119
120 /// Check if the binop computes a division or a remainder.
121 bool isDivremOp() const {
122 return Opcode == BO_Div || Opcode == BO_Rem || Opcode == BO_DivAssign ||
123 Opcode == BO_RemAssign;
124 }
125
126 /// Check if the binop can result in an integer division by zero.
127 bool mayHaveIntegerDivisionByZero() const {
128 if (isDivremOp())
129 if (auto *CI = dyn_cast<llvm::ConstantInt>(RHS))
130 return CI->isZero();
131 return true;
132 }
133
134 /// Check if the binop can result in a float division by zero.
135 bool mayHaveFloatDivisionByZero() const {
136 if (isDivremOp())
137 if (auto *CFP = dyn_cast<llvm::ConstantFP>(RHS))
138 return CFP->isZero();
139 return true;
140 }
141
142 /// Check if at least one operand is a fixed point type. In such cases, this
143 /// operation did not follow usual arithmetic conversion and both operands
144 /// might not be of the same type.
145 bool isFixedPointOp() const {
146 // We cannot simply check the result type since comparison operations return
147 // an int.
148 if (const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
149 QualType LHSType = BinOp->getLHS()->getType();
150 QualType RHSType = BinOp->getRHS()->getType();
151 return LHSType->isFixedPointType() || RHSType->isFixedPointType();
152 }
153 if (const auto *UnOp = dyn_cast<UnaryOperator>(E))
154 return UnOp->getSubExpr()->getType()->isFixedPointType();
155 return false;
156 }
157
158 /// Check if the RHS has a signed integer representation.
159 bool rhsHasSignedIntegerRepresentation() const {
160 if (const auto *BinOp = dyn_cast<BinaryOperator>(E)) {
161 QualType RHSType = BinOp->getRHS()->getType();
162 return RHSType->hasSignedIntegerRepresentation();
163 }
164 return false;
165 }
166};
167
168static bool MustVisitNullValue(const Expr *E) {
169 // If a null pointer expression's type is the C++0x nullptr_t, then
170 // it's not necessarily a simple constant and it must be evaluated
171 // for its potential side effects.
172 return E->getType()->isNullPtrType();
173}
174
175/// If \p E is a widened promoted integer, get its base (unpromoted) type.
176static std::optional<QualType> getUnwidenedIntegerType(const ASTContext &Ctx,
177 const Expr *E) {
178 const Expr *Base = E->IgnoreImpCasts();
179 if (E == Base)
180 return std::nullopt;
181
182 QualType BaseTy = Base->getType();
183 if (!Ctx.isPromotableIntegerType(BaseTy) ||
184 Ctx.getTypeSize(BaseTy) >= Ctx.getTypeSize(E->getType()))
185 return std::nullopt;
186
187 return BaseTy;
188}
189
190/// Check if \p E is a widened promoted integer.
191static bool IsWidenedIntegerOp(const ASTContext &Ctx, const Expr *E) {
192 return getUnwidenedIntegerType(Ctx, E).has_value();
193}
194
195/// Consider OverflowBehaviorType and language options to calculate the final
196/// overflow behavior for an expression. There are no language options for
197/// unsigned overflow semantics so there is nothing to consider there.
199getOverflowBehaviorConsideringType(const CodeGenFunction &CGF,
200 const QualType Ty) {
201 const OverflowBehaviorType *OBT = Ty->getAs<OverflowBehaviorType>();
202 /// FIXME: Having two enums named `OverflowBehaviorKind` is not ideal, these
203 /// should be unified into one coherent enum that supports both unsigned and
204 /// signed overflow behavior semantics.
205 if (OBT) {
206 switch (OBT->getBehaviorKind()) {
207 case OverflowBehaviorType::OverflowBehaviorKind::Wrap:
209 case OverflowBehaviorType::OverflowBehaviorKind::Trap:
211 }
212 llvm_unreachable("Unknown OverflowBehaviorKind");
213 }
214
215 if (Ty->isUnsignedIntegerType()) {
217 }
218
219 switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
226 }
227 llvm_unreachable("Unknown SignedOverflowBehaviorTy");
228}
229
230/// Check if we can skip the overflow check for \p Op.
231static bool CanElideOverflowCheck(ASTContext &Ctx, const BinOpInfo &Op) {
232 assert((isa<UnaryOperator>(Op.E) || isa<BinaryOperator>(Op.E)) &&
233 "Expected a unary or binary operator");
234
235 // If the binop has constant inputs and we can prove there is no overflow,
236 // we can elide the overflow check.
237 if (!Op.mayHaveIntegerOverflow())
238 return true;
239
240 const UnaryOperator *UO = dyn_cast<UnaryOperator>(Op.E);
241 if (UO && Ctx.isUnaryOverflowPatternExcluded(UO))
242 return true;
243
244 const auto *BO = dyn_cast<BinaryOperator>(Op.E);
245 if (BO && BO->hasExcludedOverflowPattern())
246 return true;
247
248 if (Op.Ty.isWrapType())
249 return true;
250 if (Op.Ty.isTrapType())
251 return false;
252
253 if (Op.Ty->isSignedIntegerType() &&
254 Ctx.isTypeIgnoredBySanitizer(SanitizerKind::SignedIntegerOverflow,
255 Op.Ty)) {
256 return true;
257 }
258
259 if (Op.Ty->isUnsignedIntegerType() &&
260 Ctx.isTypeIgnoredBySanitizer(SanitizerKind::UnsignedIntegerOverflow,
261 Op.Ty)) {
262 return true;
263 }
264
265 // If a unary op has a widened operand, the op cannot overflow.
266 if (UO)
267 return !UO->canOverflow();
268
269 // We usually don't need overflow checks for binops with widened operands.
270 // Multiplication with promoted unsigned operands is a special case.
271 auto OptionalLHSTy = getUnwidenedIntegerType(Ctx, BO->getLHS());
272 if (!OptionalLHSTy)
273 return false;
274
275 auto OptionalRHSTy = getUnwidenedIntegerType(Ctx, BO->getRHS());
276 if (!OptionalRHSTy)
277 return false;
278
279 QualType LHSTy = *OptionalLHSTy;
280 QualType RHSTy = *OptionalRHSTy;
281
282 // This is the simple case: binops without unsigned multiplication, and with
283 // widened operands. No overflow check is needed here.
284 if ((Op.Opcode != BO_Mul && Op.Opcode != BO_MulAssign) ||
285 !LHSTy->isUnsignedIntegerType() || !RHSTy->isUnsignedIntegerType())
286 return true;
287
288 // For unsigned multiplication the overflow check can be elided if either one
289 // of the unpromoted types are less than half the size of the promoted type.
290 unsigned PromotedSize = Ctx.getTypeSize(Op.E->getType());
291 return (2 * Ctx.getTypeSize(LHSTy)) < PromotedSize ||
292 (2 * Ctx.getTypeSize(RHSTy)) < PromotedSize;
293}
294
295class ScalarExprEmitter
296 : public StmtVisitor<ScalarExprEmitter, Value*> {
297 CodeGenFunction &CGF;
298 CGBuilderTy &Builder;
299 bool IgnoreResultAssign;
300 llvm::LLVMContext &VMContext;
301public:
302
303 ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false)
304 : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira),
305 VMContext(cgf.getLLVMContext()) {
306 }
307
308 //===--------------------------------------------------------------------===//
309 // Utilities
310 //===--------------------------------------------------------------------===//
311
312 bool TestAndClearIgnoreResultAssign() {
313 bool I = IgnoreResultAssign;
314 IgnoreResultAssign = false;
315 return I;
316 }
317
318 llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
319 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
320 LValue EmitCheckedLValue(const Expr *E, CodeGenFunction::TypeCheckKind TCK) {
321 return CGF.EmitCheckedLValue(E, TCK);
322 }
323
324 void EmitBinOpCheck(
325 ArrayRef<std::pair<Value *, SanitizerKind::SanitizerOrdinal>> Checks,
326 const BinOpInfo &Info);
327
328 Value *EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
329 return CGF.EmitLoadOfLValue(LV, Loc).getScalarVal();
330 }
331
332 void EmitLValueAlignmentAssumption(const Expr *E, Value *V) {
333 const AlignValueAttr *AVAttr = nullptr;
334 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
335 const ValueDecl *VD = DRE->getDecl();
336
337 if (VD->getType()->isReferenceType()) {
338 if (const auto *TTy =
339 VD->getType().getNonReferenceType()->getAs<TypedefType>())
340 AVAttr = TTy->getDecl()->getAttr<AlignValueAttr>();
341 } else {
342 // Assumptions for function parameters are emitted at the start of the
343 // function, so there is no need to repeat that here,
344 // unless the alignment-assumption sanitizer is enabled,
345 // then we prefer the assumption over alignment attribute
346 // on IR function param.
347 if (isa<ParmVarDecl>(VD) && !CGF.SanOpts.has(SanitizerKind::Alignment))
348 return;
349
350 AVAttr = VD->getAttr<AlignValueAttr>();
351 }
352 }
353
354 if (!AVAttr)
355 if (const auto *TTy = E->getType()->getAs<TypedefType>())
356 AVAttr = TTy->getDecl()->getAttr<AlignValueAttr>();
357
358 if (!AVAttr)
359 return;
360
361 Value *AlignmentValue = CGF.EmitScalarExpr(AVAttr->getAlignment());
362 llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(AlignmentValue);
363 CGF.emitAlignmentAssumption(V, E, AVAttr->getLocation(), AlignmentCI);
364 }
365
366 /// EmitLoadOfLValue - Given an expression with complex type that represents a
367 /// value l-value, this method emits the address of the l-value, then loads
368 /// and returns the result.
369 Value *EmitLoadOfLValue(const Expr *E) {
370 Value *V = EmitLoadOfLValue(EmitCheckedLValue(E, CodeGenFunction::TCK_Load),
371 E->getExprLoc());
372
373 EmitLValueAlignmentAssumption(E, V);
374 return V;
375 }
376
377 /// EmitConversionToBool - Convert the specified expression value to a
378 /// boolean (i1) truth value. This is equivalent to "Val != 0".
379 Value *EmitConversionToBool(Value *Src, QualType DstTy);
380
381 /// Emit a check that a conversion from a floating-point type does not
382 /// overflow.
383 void EmitFloatConversionCheck(Value *OrigSrc, QualType OrigSrcType,
384 Value *Src, QualType SrcType, QualType DstType,
385 llvm::Type *DstTy, SourceLocation Loc);
386
387 /// Known implicit conversion check kinds.
388 /// This is used for bitfield conversion checks as well.
389 /// Keep in sync with the enum of the same name in ubsan_handlers.h
390 enum ImplicitConversionCheckKind : unsigned char {
391 ICCK_IntegerTruncation = 0, // Legacy, was only used by clang 7.
392 ICCK_UnsignedIntegerTruncation = 1,
393 ICCK_SignedIntegerTruncation = 2,
394 ICCK_IntegerSignChange = 3,
395 ICCK_SignedIntegerTruncationOrSignChange = 4,
396 };
397
398 /// Emit a check that an [implicit] truncation of an integer does not
399 /// discard any bits. It is not UB, so we use the value after truncation.
400 void EmitIntegerTruncationCheck(Value *Src, QualType SrcType, Value *Dst,
401 QualType DstType, SourceLocation Loc,
402 bool OBTrapInvolved = false);
403
404 /// Emit a check that an [implicit] conversion of an integer does not change
405 /// the sign of the value. It is not UB, so we use the value after conversion.
406 /// NOTE: Src and Dst may be the exact same value! (point to the same thing)
407 void EmitIntegerSignChangeCheck(Value *Src, QualType SrcType, Value *Dst,
408 QualType DstType, SourceLocation Loc,
409 bool OBTrapInvolved = false);
410
411 /// Emit a conversion from the specified type to the specified destination
412 /// type, both of which are LLVM scalar types.
413 struct ScalarConversionOpts {
414 bool TreatBooleanAsSigned;
415 bool EmitImplicitIntegerTruncationChecks;
416 bool EmitImplicitIntegerSignChangeChecks;
417 /* Potential -fsanitize-undefined-ignore-overflow-pattern= */
418 bool PatternExcluded;
419
420 ScalarConversionOpts()
421 : TreatBooleanAsSigned(false),
422 EmitImplicitIntegerTruncationChecks(false),
423 EmitImplicitIntegerSignChangeChecks(false), PatternExcluded(false) {}
424
425 ScalarConversionOpts(clang::SanitizerSet SanOpts)
426 : TreatBooleanAsSigned(false),
427 EmitImplicitIntegerTruncationChecks(
428 SanOpts.hasOneOf(SanitizerKind::ImplicitIntegerTruncation)),
429 EmitImplicitIntegerSignChangeChecks(
430 SanOpts.has(SanitizerKind::ImplicitIntegerSignChange)),
431 PatternExcluded(false) {}
432 };
433 Value *EmitScalarCast(Value *Src, QualType SrcType, QualType DstType,
434 llvm::Type *SrcTy, llvm::Type *DstTy,
435 ScalarConversionOpts Opts);
436 Value *
437 EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy,
438 SourceLocation Loc,
439 ScalarConversionOpts Opts = ScalarConversionOpts());
440
441 /// Convert between either a fixed point and other fixed point or fixed point
442 /// and an integer.
443 Value *EmitFixedPointConversion(Value *Src, QualType SrcTy, QualType DstTy,
444 SourceLocation Loc);
445
446 /// Emit a conversion from the specified complex type to the specified
447 /// destination type, where the destination type is an LLVM scalar type.
448 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
449 QualType SrcTy, QualType DstTy,
450 SourceLocation Loc);
451
452 /// EmitNullValue - Emit a value that corresponds to null for the given type.
453 Value *EmitNullValue(QualType Ty);
454
455 /// EmitFloatToBoolConversion - Perform an FP to boolean conversion.
456 Value *EmitFloatToBoolConversion(Value *V) {
457 // Compare against 0.0 for fp scalars.
458 llvm::Value *Zero = llvm::Constant::getNullValue(V->getType());
459 return Builder.CreateFCmpUNE(V, Zero, "tobool");
460 }
461
462 /// EmitPointerToBoolConversion - Perform a pointer to boolean conversion.
463 Value *EmitPointerToBoolConversion(Value *V, QualType QT) {
464 Value *Zero = CGF.CGM.getNullPointer(cast<llvm::PointerType>(V->getType()), QT);
465
466 return Builder.CreateICmpNE(V, Zero, "tobool");
467 }
468
469 Value *EmitIntToBoolConversion(Value *V) {
470 // Because of the type rules of C, we often end up computing a
471 // logical value, then zero extending it to int, then wanting it
472 // as a logical value again. Optimize this common case.
473 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(V)) {
474 if (ZI->getOperand(0)->getType() == Builder.getInt1Ty()) {
475 Value *Result = ZI->getOperand(0);
476 // If there aren't any more uses, zap the instruction to save space.
477 // Note that there can be more uses, for example if this
478 // is the result of an assignment.
479 if (ZI->use_empty())
480 ZI->eraseFromParent();
481 return Result;
482 }
483 }
484
485 return Builder.CreateIsNotNull(V, "tobool");
486 }
487
488 //===--------------------------------------------------------------------===//
489 // Visitor Methods
490 //===--------------------------------------------------------------------===//
491
492 Value *Visit(Expr *E) {
493 ApplyDebugLocation DL(CGF, E);
494 return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E);
495 }
496
497 Value *VisitStmt(Stmt *S) {
498 S->dump(llvm::errs(), CGF.getContext());
499 llvm_unreachable("Stmt can't have complex result type!");
500 }
501 Value *VisitExpr(Expr *S);
502
503 Value *VisitConstantExpr(ConstantExpr *E) {
504 // A constant expression of type 'void' generates no code and produces no
505 // value.
506 if (E->getType()->isVoidType())
507 return nullptr;
508
509 if (Value *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E)) {
510 if (E->isGLValue()) {
511 // This was already converted to an rvalue when it was constant
512 // evaluated.
513 if (E->hasAPValueResult() && !E->getAPValueResult().isLValue())
514 return Result;
515 return CGF.EmitLoadOfScalar(
518 /*Volatile*/ false, E->getType(), E->getExprLoc());
519 }
520 return Result;
521 }
522 return Visit(E->getSubExpr());
523 }
524 Value *VisitParenExpr(ParenExpr *PE) {
525 return Visit(PE->getSubExpr());
526 }
527 Value *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
528 return Visit(E->getReplacement());
529 }
530 Value *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
531 return Visit(GE->getResultExpr());
532 }
533 Value *VisitCoawaitExpr(CoawaitExpr *S) {
534 return CGF.EmitCoawaitExpr(*S).getScalarVal();
535 }
536 Value *VisitCoyieldExpr(CoyieldExpr *S) {
537 return CGF.EmitCoyieldExpr(*S).getScalarVal();
538 }
539 Value *VisitUnaryCoawait(const UnaryOperator *E) {
540 return Visit(E->getSubExpr());
541 }
542
543 // Leaves.
544 Value *VisitIntegerLiteral(const IntegerLiteral *E) {
545 return Builder.getInt(E->getValue());
546 }
547 Value *VisitFixedPointLiteral(const FixedPointLiteral *E) {
548 return Builder.getInt(E->getValue());
549 }
550 Value *VisitFloatingLiteral(const FloatingLiteral *E) {
551 return llvm::ConstantFP::get(VMContext, E->getValue());
552 }
553 Value *VisitCharacterLiteral(const CharacterLiteral *E) {
554 // Character literals are always stored in an unsigned (even for signed
555 // char), so allow implicit truncation here.
556 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue(),
557 /*IsSigned=*/false, /*ImplicitTrunc=*/true);
558 }
559 Value *VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
560 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
561 }
562 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
563 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
564 }
565 Value *VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
566 if (E->getType()->isVoidType())
567 return nullptr;
568
569 return EmitNullValue(E->getType());
570 }
571 Value *VisitGNUNullExpr(const GNUNullExpr *E) {
572 return EmitNullValue(E->getType());
573 }
574 Value *VisitOffsetOfExpr(OffsetOfExpr *E);
575 Value *VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
576 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
577 llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel());
578 return Builder.CreateBitCast(V, ConvertType(E->getType()));
579 }
580
581 Value *VisitSizeOfPackExpr(SizeOfPackExpr *E) {
582 return llvm::ConstantInt::get(ConvertType(E->getType()),E->getPackLength());
583 }
584
585 Value *VisitPseudoObjectExpr(PseudoObjectExpr *E) {
586 return CGF.EmitPseudoObjectRValue(E).getScalarVal();
587 }
588
589 Value *VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr *E);
590 Value *VisitEmbedExpr(EmbedExpr *E);
591
592 Value *VisitOpaqueValueExpr(OpaqueValueExpr *E) {
593 if (E->isGLValue())
594 return EmitLoadOfLValue(CGF.getOrCreateOpaqueLValueMapping(E),
595 E->getExprLoc());
596
597 // Otherwise, assume the mapping is the scalar directly.
599 }
600
601 Value *VisitOpenACCAsteriskSizeExpr(OpenACCAsteriskSizeExpr *E) {
602 llvm_unreachable("Codegen for this isn't defined/implemented");
603 }
604
605 // l-values.
606 Value *VisitDeclRefExpr(DeclRefExpr *E) {
607 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E))
608 return CGF.emitScalarConstant(Constant, E);
609 return EmitLoadOfLValue(E);
610 }
611
612 Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
613 return CGF.EmitObjCSelectorExpr(E);
614 }
615 Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
616 return CGF.EmitObjCProtocolExpr(E);
617 }
618 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
619 return EmitLoadOfLValue(E);
620 }
621 Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
622 if (E->getMethodDecl() &&
624 return EmitLoadOfLValue(E);
625 return CGF.EmitObjCMessageExpr(E).getScalarVal();
626 }
627
628 Value *VisitObjCIsaExpr(ObjCIsaExpr *E) {
629 LValue LV = CGF.EmitObjCIsaExpr(E);
631 return V;
632 }
633
634 Value *VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
635 VersionTuple Version = E->getVersion();
636
637 // If we're checking for a platform older than our minimum deployment
638 // target, we can fold the check away.
639 if (Version <= CGF.CGM.getTarget().getPlatformMinVersion())
640 return llvm::ConstantInt::get(Builder.getInt1Ty(), 1);
641
642 return CGF.EmitBuiltinAvailable(Version);
643 }
644
645 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
646 Value *VisitMatrixSingleSubscriptExpr(MatrixSingleSubscriptExpr *E);
647 Value *VisitMatrixSubscriptExpr(MatrixSubscriptExpr *E);
648 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
649 Value *VisitConvertVectorExpr(ConvertVectorExpr *E);
650 Value *VisitMemberExpr(MemberExpr *E);
651 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
652 Value *VisitMatrixElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
653 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
654 // Strictly speaking, we shouldn't be calling EmitLoadOfLValue, which
655 // transitively calls EmitCompoundLiteralLValue, here in C++ since compound
656 // literals aren't l-values in C++. We do so simply because that's the
657 // cleanest way to handle compound literals in C++.
658 // See the discussion here: https://reviews.llvm.org/D64464
659 return EmitLoadOfLValue(E);
660 }
661
662 Value *VisitInitListExpr(InitListExpr *E);
663
664 Value *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
665 assert(CGF.getArrayInitIndex() &&
666 "ArrayInitIndexExpr not inside an ArrayInitLoopExpr?");
667 return CGF.getArrayInitIndex();
668 }
669
670 Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
671 return EmitNullValue(E->getType());
672 }
673 Value *VisitExplicitCastExpr(ExplicitCastExpr *E) {
674 CGF.CGM.EmitExplicitCastExprType(E, &CGF);
675 return VisitCastExpr(E);
676 }
677 Value *VisitCastExpr(CastExpr *E);
678
679 Value *VisitCallExpr(const CallExpr *E) {
681 return EmitLoadOfLValue(E);
682
683 Value *V = CGF.EmitCallExpr(E).getScalarVal();
684
685 EmitLValueAlignmentAssumption(E, V);
686 return V;
687 }
688
689 Value *VisitStmtExpr(const StmtExpr *E);
690
691 // Unary Operators.
692 Value *VisitUnaryPostDec(const UnaryOperator *E) {
693 LValue LV = EmitLValue(E->getSubExpr());
694 return EmitScalarPrePostIncDec(E, LV, false, false);
695 }
696 Value *VisitUnaryPostInc(const UnaryOperator *E) {
697 LValue LV = EmitLValue(E->getSubExpr());
698 return EmitScalarPrePostIncDec(E, LV, true, false);
699 }
700 Value *VisitUnaryPreDec(const UnaryOperator *E) {
701 LValue LV = EmitLValue(E->getSubExpr());
702 return EmitScalarPrePostIncDec(E, LV, false, true);
703 }
704 Value *VisitUnaryPreInc(const UnaryOperator *E) {
705 LValue LV = EmitLValue(E->getSubExpr());
706 return EmitScalarPrePostIncDec(E, LV, true, true);
707 }
708
709 llvm::Value *EmitIncDecConsiderOverflowBehavior(const UnaryOperator *E,
710 llvm::Value *InVal,
711 bool IsInc);
712
713 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
714 bool isInc, bool isPre);
715
716
717 Value *VisitUnaryAddrOf(const UnaryOperator *E) {
718 if (isa<MemberPointerType>(E->getType())) // never sugared
719 return CGF.CGM.getMemberPointerConstant(E);
720
721 return EmitLValue(E->getSubExpr()).getPointer(CGF);
722 }
723 Value *VisitUnaryDeref(const UnaryOperator *E) {
724 if (E->getType()->isVoidType())
725 return Visit(E->getSubExpr()); // the actual value should be unused
726 return EmitLoadOfLValue(E);
727 }
728
729 Value *VisitUnaryPlus(const UnaryOperator *E,
730 QualType PromotionType = QualType());
731 Value *VisitPlus(const UnaryOperator *E, QualType PromotionType);
732 Value *VisitUnaryMinus(const UnaryOperator *E,
733 QualType PromotionType = QualType());
734 Value *VisitMinus(const UnaryOperator *E, QualType PromotionType);
735
736 Value *VisitUnaryNot (const UnaryOperator *E);
737 Value *VisitUnaryLNot (const UnaryOperator *E);
738 Value *VisitUnaryReal(const UnaryOperator *E,
739 QualType PromotionType = QualType());
740 Value *VisitReal(const UnaryOperator *E, QualType PromotionType);
741 Value *VisitUnaryImag(const UnaryOperator *E,
742 QualType PromotionType = QualType());
743 Value *VisitImag(const UnaryOperator *E, QualType PromotionType);
744 Value *VisitUnaryExtension(const UnaryOperator *E) {
745 return Visit(E->getSubExpr());
746 }
747
748 // C++
749 Value *VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E) {
750 return EmitLoadOfLValue(E);
751 }
752 Value *VisitSourceLocExpr(SourceLocExpr *SLE) {
753 auto &Ctx = CGF.getContext();
756 return ConstantEmitter(CGF).emitAbstract(SLE->getLocation(), Evaluated,
757 SLE->getType());
758 }
759
760 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
761 CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
762 return Visit(DAE->getExpr());
763 }
764 Value *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
765 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE);
766 return Visit(DIE->getExpr());
767 }
768 Value *VisitCXXThisExpr(CXXThisExpr *TE) {
769 return CGF.LoadCXXThis();
770 }
771
772 Value *VisitExprWithCleanups(ExprWithCleanups *E);
773 Value *VisitCXXNewExpr(const CXXNewExpr *E) {
774 return CGF.EmitCXXNewExpr(E);
775 }
776 Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
777 CGF.EmitCXXDeleteExpr(E);
778 return nullptr;
779 }
780
781 Value *VisitTypeTraitExpr(const TypeTraitExpr *E) {
782 if (E->isStoredAsBoolean())
783 return llvm::ConstantInt::get(ConvertType(E->getType()),
784 E->getBoolValue());
785 assert(E->getAPValue().isInt() && "APValue type not supported");
786 return llvm::ConstantInt::get(ConvertType(E->getType()),
787 E->getAPValue().getInt());
788 }
789
790 Value *VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) {
791 return Builder.getInt1(E->isSatisfied());
792 }
793
794 Value *VisitRequiresExpr(const RequiresExpr *E) {
795 return Builder.getInt1(E->isSatisfied());
796 }
797
798 Value *VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
799 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
800 }
801
802 Value *VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
803 return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue());
804 }
805
806 Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) {
807 // C++ [expr.pseudo]p1:
808 // The result shall only be used as the operand for the function call
809 // operator (), and the result of such a call has type void. The only
810 // effect is the evaluation of the postfix-expression before the dot or
811 // arrow.
812 CGF.EmitScalarExpr(E->getBase());
813 return nullptr;
814 }
815
816 Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
817 return EmitNullValue(E->getType());
818 }
819
820 Value *VisitCXXThrowExpr(const CXXThrowExpr *E) {
821 CGF.EmitCXXThrowExpr(E);
822 return nullptr;
823 }
824
825 Value *VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
826 return Builder.getInt1(E->getValue());
827 }
828
829 // Binary Operators.
830 Value *EmitMul(const BinOpInfo &Ops) {
831 if (Ops.Ty->isSignedIntegerOrEnumerationType() ||
832 Ops.Ty->isUnsignedIntegerType()) {
833 const bool isSigned = Ops.Ty->isSignedIntegerOrEnumerationType();
834 const bool hasSan =
835 isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)
836 : CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow);
837 switch (getOverflowBehaviorConsideringType(CGF, Ops.Ty)) {
838 case LangOptions::OB_Wrap:
839 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
840 case LangOptions::OB_SignedAndDefined:
841 if (!hasSan)
842 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
843 [[fallthrough]];
844 case LangOptions::OB_Unset:
845 if (!hasSan)
846 return isSigned ? Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul")
847 : Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
848 [[fallthrough]];
849 case LangOptions::OB_Trap:
850 if (CanElideOverflowCheck(CGF.getContext(), Ops))
851 return isSigned ? Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul")
852 : Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
853 return EmitOverflowCheckedBinOp(Ops);
854 }
855 }
856
857 if (Ops.Ty->isConstantMatrixType()) {
858 llvm::MatrixBuilder MB(Builder);
859 // We need to check the types of the operands of the operator to get the
860 // correct matrix dimensions.
861 auto *BO = cast<BinaryOperator>(Ops.E);
862 auto *LHSMatTy = dyn_cast<ConstantMatrixType>(
863 BO->getLHS()->getType().getCanonicalType());
864 auto *RHSMatTy = dyn_cast<ConstantMatrixType>(
865 BO->getRHS()->getType().getCanonicalType());
866 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures);
867 if (LHSMatTy && RHSMatTy)
868 return MB.CreateMatrixMultiply(Ops.LHS, Ops.RHS, LHSMatTy->getNumRows(),
869 LHSMatTy->getNumColumns(),
870 RHSMatTy->getNumColumns());
871 return MB.CreateScalarMultiply(Ops.LHS, Ops.RHS);
872 }
873
874 if (Ops.LHS->getType()->isFPOrFPVectorTy()) {
875 // Preserve the old values
876 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures);
877 return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul");
878 }
879 if (Ops.isFixedPointOp())
880 return EmitFixedPointBinOp(Ops);
881 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
882 }
883 /// Create a binary op that checks for overflow.
884 /// Currently only supports +, - and *.
885 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
886
887 // Check for undefined division and modulus behaviors.
888 void EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo &Ops,
889 llvm::Value *Zero,bool isDiv);
890 // Common helper for getting how wide LHS of shift is.
891 static Value *GetMaximumShiftAmount(Value *LHS, Value *RHS, bool RHSIsSigned);
892
893 // Used for shifting constraints for OpenCL, do mask for powers of 2, URem for
894 // non powers of two.
895 Value *ConstrainShiftValue(Value *LHS, Value *RHS, const Twine &Name);
896
897 Value *EmitDiv(const BinOpInfo &Ops);
898 Value *EmitRem(const BinOpInfo &Ops);
899 Value *EmitAdd(const BinOpInfo &Ops);
900 Value *EmitSub(const BinOpInfo &Ops);
901 Value *EmitShl(const BinOpInfo &Ops);
902 Value *EmitShr(const BinOpInfo &Ops);
903 Value *EmitAnd(const BinOpInfo &Ops) {
904 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
905 }
906 Value *EmitXor(const BinOpInfo &Ops) {
907 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
908 }
909 Value *EmitOr (const BinOpInfo &Ops) {
910 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
911 }
912
913 // Helper functions for fixed point binary operations.
914 Value *EmitFixedPointBinOp(const BinOpInfo &Ops);
915
916 BinOpInfo EmitBinOps(const BinaryOperator *E,
917 QualType PromotionTy = QualType());
918
919 Value *EmitPromotedValue(Value *result, QualType PromotionType);
920 Value *EmitUnPromotedValue(Value *result, QualType ExprType);
921 Value *EmitPromoted(const Expr *E, QualType PromotionType);
922
923 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
924 Value *(ScalarExprEmitter::*F)(const BinOpInfo &),
925 Value *&Result);
926
927 Value *EmitCompoundAssign(const CompoundAssignOperator *E,
928 Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
929
930 QualType getPromotionType(QualType Ty) {
931 const auto &Ctx = CGF.getContext();
932 if (auto *CT = Ty->getAs<ComplexType>()) {
933 QualType ElementType = CT->getElementType();
934 if (ElementType.UseExcessPrecision(Ctx))
935 return Ctx.getComplexType(Ctx.FloatTy);
936 }
937
938 if (Ty.UseExcessPrecision(Ctx)) {
939 if (auto *VT = Ty->getAs<VectorType>()) {
940 unsigned NumElements = VT->getNumElements();
941 return Ctx.getVectorType(Ctx.FloatTy, NumElements, VT->getVectorKind());
942 }
943 return Ctx.FloatTy;
944 }
945
946 return QualType();
947 }
948
949 // Binary operators and binary compound assignment operators.
950#define HANDLEBINOP(OP) \
951 Value *VisitBin##OP(const BinaryOperator *E) { \
952 QualType promotionTy = getPromotionType(E->getType()); \
953 auto result = Emit##OP(EmitBinOps(E, promotionTy)); \
954 if (result && !promotionTy.isNull()) \
955 result = EmitUnPromotedValue(result, E->getType()); \
956 return result; \
957 } \
958 Value *VisitBin##OP##Assign(const CompoundAssignOperator *E) { \
959 ApplyAtomGroup Grp(CGF.getDebugInfo()); \
960 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit##OP); \
961 }
962 HANDLEBINOP(Mul)
963 HANDLEBINOP(Div)
964 HANDLEBINOP(Rem)
965 HANDLEBINOP(Add)
966 HANDLEBINOP(Sub)
967 HANDLEBINOP(Shl)
968 HANDLEBINOP(Shr)
970 HANDLEBINOP(Xor)
972#undef HANDLEBINOP
973
974 // Comparisons.
975 Value *EmitCompare(const BinaryOperator *E, llvm::CmpInst::Predicate UICmpOpc,
976 llvm::CmpInst::Predicate SICmpOpc,
977 llvm::CmpInst::Predicate FCmpOpc, bool IsSignaling);
978#define VISITCOMP(CODE, UI, SI, FP, SIG) \
979 Value *VisitBin##CODE(const BinaryOperator *E) { \
980 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
981 llvm::FCmpInst::FP, SIG); }
982 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT, true)
983 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT, true)
984 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE, true)
985 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE, true)
986 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ, false)
987 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE, false)
988#undef VISITCOMP
989
990 Value *VisitBinAssign (const BinaryOperator *E);
991
992 Value *VisitBinLAnd (const BinaryOperator *E);
993 Value *VisitBinLOr (const BinaryOperator *E);
994 Value *VisitBinComma (const BinaryOperator *E);
995
996 Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); }
997 Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); }
998
999 Value *VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) {
1000 return Visit(E->getSemanticForm());
1001 }
1002
1003 // Other Operators.
1004 Value *VisitBlockExpr(const BlockExpr *BE);
1005 Value *VisitAbstractConditionalOperator(const AbstractConditionalOperator *);
1006 Value *VisitChooseExpr(ChooseExpr *CE);
1007 Value *VisitVAArgExpr(VAArgExpr *VE);
1008 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
1009 return CGF.EmitObjCStringLiteral(E);
1010 }
1011 Value *VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
1012 return CGF.EmitObjCBoxedExpr(E);
1013 }
1014 Value *VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
1015 return CGF.EmitObjCArrayLiteral(E);
1016 }
1017 Value *VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
1018 return CGF.EmitObjCDictionaryLiteral(E);
1019 }
1020 Value *VisitAsTypeExpr(AsTypeExpr *CE);
1021 Value *VisitAtomicExpr(AtomicExpr *AE);
1022 Value *VisitPackIndexingExpr(PackIndexingExpr *E) {
1023 return Visit(E->getSelectedExpr());
1024 }
1025};
1026} // end anonymous namespace.
1027
1028//===----------------------------------------------------------------------===//
1029// Utilities
1030//===----------------------------------------------------------------------===//
1031
1032/// EmitConversionToBool - Convert the specified expression value to a
1033/// boolean (i1) truth value. This is equivalent to "Val != 0".
1034Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
1035 assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs");
1036
1037 if (SrcType->isRealFloatingType())
1038 return EmitFloatToBoolConversion(Src);
1039
1040 if (const MemberPointerType *MPT = dyn_cast<MemberPointerType>(SrcType))
1041 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT);
1042
1043 // The conversion is a NOP, and will be done when CodeGening the builtin.
1044 if (SrcType == CGF.getContext().AMDGPUFeaturePredicateTy)
1045 return Src;
1046
1047 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
1048 "Unknown scalar type to convert");
1049
1050 if (isa<llvm::IntegerType>(Src->getType()))
1051 return EmitIntToBoolConversion(Src);
1052
1053 assert(isa<llvm::PointerType>(Src->getType()));
1054 return EmitPointerToBoolConversion(Src, SrcType);
1055}
1056
1057void ScalarExprEmitter::EmitFloatConversionCheck(
1058 Value *OrigSrc, QualType OrigSrcType, Value *Src, QualType SrcType,
1059 QualType DstType, llvm::Type *DstTy, SourceLocation Loc) {
1060 assert(SrcType->isFloatingType() && "not a conversion from floating point");
1061 if (!isa<llvm::IntegerType>(DstTy))
1062 return;
1063
1064 auto CheckOrdinal = SanitizerKind::SO_FloatCastOverflow;
1065 auto CheckHandler = SanitizerHandler::FloatCastOverflow;
1066 SanitizerDebugLocation SanScope(&CGF, {CheckOrdinal}, CheckHandler);
1067 using llvm::APFloat;
1068 using llvm::APSInt;
1069
1070 llvm::Value *Check = nullptr;
1071 const llvm::fltSemantics &SrcSema =
1072 CGF.getContext().getFloatTypeSemantics(OrigSrcType);
1073
1074 // Floating-point to integer. This has undefined behavior if the source is
1075 // +-Inf, NaN, or doesn't fit into the destination type (after truncation
1076 // to an integer).
1077 unsigned Width = CGF.getContext().getIntWidth(DstType);
1079
1080 APSInt Min = APSInt::getMinValue(Width, Unsigned);
1081 APFloat MinSrc(SrcSema, APFloat::uninitialized);
1082 if (MinSrc.convertFromAPInt(Min, !Unsigned, APFloat::rmTowardZero) &
1083 APFloat::opOverflow)
1084 // Don't need an overflow check for lower bound. Just check for
1085 // -Inf/NaN.
1086 MinSrc = APFloat::getInf(SrcSema, true);
1087 else
1088 // Find the largest value which is too small to represent (before
1089 // truncation toward zero).
1090 MinSrc.subtract(APFloat(SrcSema, 1), APFloat::rmTowardNegative);
1091
1092 APSInt Max = APSInt::getMaxValue(Width, Unsigned);
1093 APFloat MaxSrc(SrcSema, APFloat::uninitialized);
1094 if (MaxSrc.convertFromAPInt(Max, !Unsigned, APFloat::rmTowardZero) &
1095 APFloat::opOverflow)
1096 // Don't need an overflow check for upper bound. Just check for
1097 // +Inf/NaN.
1098 MaxSrc = APFloat::getInf(SrcSema, false);
1099 else
1100 // Find the smallest value which is too large to represent (before
1101 // truncation toward zero).
1102 MaxSrc.add(APFloat(SrcSema, 1), APFloat::rmTowardPositive);
1103
1104 // If we're converting from __half, convert the range to float to match
1105 // the type of src.
1106 if (OrigSrcType->isHalfType()) {
1107 const llvm::fltSemantics &Sema =
1108 CGF.getContext().getFloatTypeSemantics(SrcType);
1109 bool IsInexact;
1110 MinSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact);
1111 MaxSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact);
1112 }
1113
1114 llvm::Value *GE =
1115 Builder.CreateFCmpOGT(Src, llvm::ConstantFP::get(VMContext, MinSrc));
1116 llvm::Value *LE =
1117 Builder.CreateFCmpOLT(Src, llvm::ConstantFP::get(VMContext, MaxSrc));
1118 Check = Builder.CreateAnd(GE, LE);
1119
1120 llvm::Constant *StaticArgs[] = {CGF.EmitCheckSourceLocation(Loc),
1121 CGF.EmitCheckTypeDescriptor(OrigSrcType),
1122 CGF.EmitCheckTypeDescriptor(DstType)};
1123 CGF.EmitCheck(std::make_pair(Check, CheckOrdinal), CheckHandler, StaticArgs,
1124 OrigSrc);
1125}
1126
1127// Should be called within CodeGenFunction::SanitizerScope RAII scope.
1128// Returns 'i1 false' when the truncation Src -> Dst was lossy.
1129static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
1130 std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>>
1132 QualType DstType, CGBuilderTy &Builder) {
1133 llvm::Type *SrcTy = Src->getType();
1134 llvm::Type *DstTy = Dst->getType();
1135 (void)DstTy; // Only used in assert()
1136
1137 // This should be truncation of integral types.
1138 assert(Src != Dst);
1139 assert(SrcTy->getScalarSizeInBits() > Dst->getType()->getScalarSizeInBits());
1140 assert(isa<llvm::IntegerType>(SrcTy) && isa<llvm::IntegerType>(DstTy) &&
1141 "non-integer llvm type");
1142
1143 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType();
1144 bool DstSigned = DstType->isSignedIntegerOrEnumerationType();
1145
1146 // If both (src and dst) types are unsigned, then it's an unsigned truncation.
1147 // Else, it is a signed truncation.
1148 ScalarExprEmitter::ImplicitConversionCheckKind Kind;
1150 if (!SrcSigned && !DstSigned) {
1151 Kind = ScalarExprEmitter::ICCK_UnsignedIntegerTruncation;
1152 Ordinal = SanitizerKind::SO_ImplicitUnsignedIntegerTruncation;
1153 } else {
1154 Kind = ScalarExprEmitter::ICCK_SignedIntegerTruncation;
1155 Ordinal = SanitizerKind::SO_ImplicitSignedIntegerTruncation;
1156 }
1157
1158 llvm::Value *Check = nullptr;
1159 // 1. Extend the truncated value back to the same width as the Src.
1160 Check = Builder.CreateIntCast(Dst, SrcTy, DstSigned, "anyext");
1161 // 2. Equality-compare with the original source value
1162 Check = Builder.CreateICmpEQ(Check, Src, "truncheck");
1163 // If the comparison result is 'i1 false', then the truncation was lossy.
1164 return std::make_pair(Kind, std::make_pair(Check, Ordinal));
1165}
1166
1168 QualType SrcType, QualType DstType) {
1169 return SrcType->isIntegerType() && DstType->isIntegerType();
1170}
1171
1172void ScalarExprEmitter::EmitIntegerTruncationCheck(Value *Src, QualType SrcType,
1173 Value *Dst, QualType DstType,
1174 SourceLocation Loc,
1175 bool OBTrapInvolved) {
1176 if (!CGF.SanOpts.hasOneOf(SanitizerKind::ImplicitIntegerTruncation) &&
1177 !OBTrapInvolved)
1178 return;
1179
1180 // We only care about int->int conversions here.
1181 // We ignore conversions to/from pointer and/or bool.
1183 DstType))
1184 return;
1185
1186 unsigned SrcBits = Src->getType()->getScalarSizeInBits();
1187 unsigned DstBits = Dst->getType()->getScalarSizeInBits();
1188 // This must be truncation. Else we do not care.
1189 if (SrcBits <= DstBits)
1190 return;
1191
1192 assert(!DstType->isBooleanType() && "we should not get here with booleans.");
1193
1194 // If the integer sign change sanitizer is enabled,
1195 // and we are truncating from larger unsigned type to smaller signed type,
1196 // let that next sanitizer deal with it.
1197 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType();
1198 bool DstSigned = DstType->isSignedIntegerOrEnumerationType();
1199 if (CGF.SanOpts.has(SanitizerKind::ImplicitIntegerSignChange) &&
1200 (!SrcSigned && DstSigned))
1201 return;
1202
1203 std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
1204 std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>>
1205 Check;
1206
1207 auto CheckHandler = SanitizerHandler::ImplicitConversion;
1208 {
1209 // We don't know the check kind until we call
1210 // EmitIntegerTruncationCheckHelper, but we want to annotate
1211 // EmitIntegerTruncationCheckHelper's instructions too.
1212 SanitizerDebugLocation SanScope(
1213 &CGF,
1214 {SanitizerKind::SO_ImplicitUnsignedIntegerTruncation,
1215 SanitizerKind::SO_ImplicitSignedIntegerTruncation},
1216 CheckHandler);
1217 Check =
1218 EmitIntegerTruncationCheckHelper(Src, SrcType, Dst, DstType, Builder);
1219 // If the comparison result is 'i1 false', then the truncation was lossy.
1220 }
1221
1222 // Do we care about this type of truncation?
1223 if (!CGF.SanOpts.has(Check.second.second)) {
1224 // Just emit a trap check if an __ob_trap was involved but appropriate
1225 // sanitizer isn't enabled.
1226 if (OBTrapInvolved)
1227 CGF.EmitTrapCheck(Check.second.first, CheckHandler);
1228 return;
1229 }
1230
1231 SanitizerDebugLocation SanScope(&CGF, {Check.second.second}, CheckHandler);
1232
1233 // Does some SSCL ignore this type?
1234 const bool ignoredBySanitizer = CGF.getContext().isTypeIgnoredBySanitizer(
1235 SanitizerMask::bitPosToMask(Check.second.second), DstType);
1236
1237 // Consider OverflowBehaviorTypes which override SSCL type entries for
1238 // truncation sanitizers.
1239 if (const auto *OBT = DstType->getAs<OverflowBehaviorType>()) {
1240 if (OBT->isWrapKind())
1241 return;
1242 }
1243 if (ignoredBySanitizer && !OBTrapInvolved)
1244 return;
1245
1246 llvm::Constant *StaticArgs[] = {
1247 CGF.EmitCheckSourceLocation(Loc), CGF.EmitCheckTypeDescriptor(SrcType),
1248 CGF.EmitCheckTypeDescriptor(DstType),
1249 llvm::ConstantInt::get(Builder.getInt8Ty(), Check.first),
1250 llvm::ConstantInt::get(Builder.getInt32Ty(), 0)};
1251
1252 CGF.EmitCheck(Check.second, CheckHandler, StaticArgs, {Src, Dst});
1253}
1254
1255static llvm::Value *EmitIsNegativeTestHelper(Value *V, QualType VType,
1256 const char *Name,
1257 CGBuilderTy &Builder) {
1258 bool VSigned = VType->isSignedIntegerOrEnumerationType();
1259 llvm::Type *VTy = V->getType();
1260 if (!VSigned) {
1261 // If the value is unsigned, then it is never negative.
1262 return llvm::ConstantInt::getFalse(VTy->getContext());
1263 }
1264 llvm::Constant *Zero = llvm::ConstantInt::get(VTy, 0);
1265 return Builder.CreateICmp(llvm::ICmpInst::ICMP_SLT, V, Zero,
1266 llvm::Twine(Name) + "." + V->getName() +
1267 ".negativitycheck");
1268}
1269
1270// Should be called within CodeGenFunction::SanitizerScope RAII scope.
1271// Returns 'i1 false' when the conversion Src -> Dst changed the sign.
1272static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
1273 std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>>
1275 QualType DstType, CGBuilderTy &Builder) {
1276 llvm::Type *SrcTy = Src->getType();
1277 llvm::Type *DstTy = Dst->getType();
1278
1279 assert(isa<llvm::IntegerType>(SrcTy) && isa<llvm::IntegerType>(DstTy) &&
1280 "non-integer llvm type");
1281
1282 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType();
1283 bool DstSigned = DstType->isSignedIntegerOrEnumerationType();
1284 (void)SrcSigned; // Only used in assert()
1285 (void)DstSigned; // Only used in assert()
1286 unsigned SrcBits = SrcTy->getScalarSizeInBits();
1287 unsigned DstBits = DstTy->getScalarSizeInBits();
1288 (void)SrcBits; // Only used in assert()
1289 (void)DstBits; // Only used in assert()
1290
1291 assert(((SrcBits != DstBits) || (SrcSigned != DstSigned)) &&
1292 "either the widths should be different, or the signednesses.");
1293
1294 // 1. Was the old Value negative?
1295 llvm::Value *SrcIsNegative =
1296 EmitIsNegativeTestHelper(Src, SrcType, "src", Builder);
1297 // 2. Is the new Value negative?
1298 llvm::Value *DstIsNegative =
1299 EmitIsNegativeTestHelper(Dst, DstType, "dst", Builder);
1300 // 3. Now, was the 'negativity status' preserved during the conversion?
1301 // NOTE: conversion from negative to zero is considered to change the sign.
1302 // (We want to get 'false' when the conversion changed the sign)
1303 // So we should just equality-compare the negativity statuses.
1304 llvm::Value *Check = nullptr;
1305 Check = Builder.CreateICmpEQ(SrcIsNegative, DstIsNegative, "signchangecheck");
1306 // If the comparison result is 'false', then the conversion changed the sign.
1307 return std::make_pair(
1308 ScalarExprEmitter::ICCK_IntegerSignChange,
1309 std::make_pair(Check, SanitizerKind::SO_ImplicitIntegerSignChange));
1310}
1311
1312void ScalarExprEmitter::EmitIntegerSignChangeCheck(Value *Src, QualType SrcType,
1313 Value *Dst, QualType DstType,
1314 SourceLocation Loc,
1315 bool OBTrapInvolved) {
1316 if (!CGF.SanOpts.has(SanitizerKind::SO_ImplicitIntegerSignChange) &&
1317 !OBTrapInvolved)
1318 return;
1319
1320 llvm::Type *SrcTy = Src->getType();
1321 llvm::Type *DstTy = Dst->getType();
1322
1323 // We only care about int->int conversions here.
1324 // We ignore conversions to/from pointer and/or bool.
1326 DstType))
1327 return;
1328
1329 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType();
1330 bool DstSigned = DstType->isSignedIntegerOrEnumerationType();
1331 unsigned SrcBits = SrcTy->getScalarSizeInBits();
1332 unsigned DstBits = DstTy->getScalarSizeInBits();
1333
1334 // Now, we do not need to emit the check in *all* of the cases.
1335 // We can avoid emitting it in some obvious cases where it would have been
1336 // dropped by the opt passes (instcombine) always anyways.
1337 // If it's a cast between effectively the same type, no check.
1338 // NOTE: this is *not* equivalent to checking the canonical types.
1339 if (SrcSigned == DstSigned && SrcBits == DstBits)
1340 return;
1341 // At least one of the values needs to have signed type.
1342 // If both are unsigned, then obviously, neither of them can be negative.
1343 if (!SrcSigned && !DstSigned)
1344 return;
1345 // If the conversion is to *larger* *signed* type, then no check is needed.
1346 // Because either sign-extension happens (so the sign will remain),
1347 // or zero-extension will happen (the sign bit will be zero.)
1348 if ((DstBits > SrcBits) && DstSigned)
1349 return;
1350 if (CGF.SanOpts.has(SanitizerKind::ImplicitSignedIntegerTruncation) &&
1351 (SrcBits > DstBits) && SrcSigned) {
1352 // If the signed integer truncation sanitizer is enabled,
1353 // and this is a truncation from signed type, then no check is needed.
1354 // Because here sign change check is interchangeable with truncation check.
1355 return;
1356 }
1357 // Does an SSCL have an entry for the DstType under its respective sanitizer
1358 // section? Don't check this if an __ob_trap type is involved as it has
1359 // priority to emit checks regardless of sanitizer case lists.
1360 if (!OBTrapInvolved) {
1361 if (DstSigned &&
1363 SanitizerKind::ImplicitSignedIntegerTruncation, DstType))
1364 return;
1365 if (!DstSigned &&
1367 SanitizerKind::ImplicitUnsignedIntegerTruncation, DstType))
1368 return;
1369 }
1370 // That's it. We can't rule out any more cases with the data we have.
1371
1372 auto CheckHandler = SanitizerHandler::ImplicitConversion;
1373 SanitizerDebugLocation SanScope(
1374 &CGF,
1375 {SanitizerKind::SO_ImplicitIntegerSignChange,
1376 SanitizerKind::SO_ImplicitUnsignedIntegerTruncation,
1377 SanitizerKind::SO_ImplicitSignedIntegerTruncation},
1378 CheckHandler);
1379
1380 std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
1381 std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>>
1382 Check;
1383
1384 // Each of these checks needs to return 'false' when an issue was detected.
1385 ImplicitConversionCheckKind CheckKind;
1386 llvm::SmallVector<std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>,
1387 2>
1388 Checks;
1389 // So we can 'and' all the checks together, and still get 'false',
1390 // if at least one of the checks detected an issue.
1391
1392 Check = EmitIntegerSignChangeCheckHelper(Src, SrcType, Dst, DstType, Builder);
1393 CheckKind = Check.first;
1394 Checks.emplace_back(Check.second);
1395
1396 if (CGF.SanOpts.has(SanitizerKind::ImplicitSignedIntegerTruncation) &&
1397 (SrcBits > DstBits) && !SrcSigned && DstSigned) {
1398 // If the signed integer truncation sanitizer was enabled,
1399 // and we are truncating from larger unsigned type to smaller signed type,
1400 // let's handle the case we skipped in that check.
1401 Check =
1402 EmitIntegerTruncationCheckHelper(Src, SrcType, Dst, DstType, Builder);
1403 CheckKind = ICCK_SignedIntegerTruncationOrSignChange;
1404 Checks.emplace_back(Check.second);
1405 // If the comparison result is 'i1 false', then the truncation was lossy.
1406 }
1407
1408 if (!CGF.SanOpts.has(SanitizerKind::SO_ImplicitIntegerSignChange)) {
1409 if (OBTrapInvolved) {
1410 llvm::Value *Combined = Check.second.first;
1411 for (const auto &C : Checks)
1412 Combined = Builder.CreateAnd(Combined, C.first);
1413 CGF.EmitTrapCheck(Combined, CheckHandler);
1414 }
1415 return;
1416 }
1417
1418 llvm::Constant *StaticArgs[] = {
1419 CGF.EmitCheckSourceLocation(Loc), CGF.EmitCheckTypeDescriptor(SrcType),
1420 CGF.EmitCheckTypeDescriptor(DstType),
1421 llvm::ConstantInt::get(Builder.getInt8Ty(), CheckKind),
1422 llvm::ConstantInt::get(Builder.getInt32Ty(), 0)};
1423 // EmitCheck() will 'and' all the checks together.
1424 CGF.EmitCheck(Checks, CheckHandler, StaticArgs, {Src, Dst});
1425}
1426
1427// Should be called within CodeGenFunction::SanitizerScope RAII scope.
1428// Returns 'i1 false' when the truncation Src -> Dst was lossy.
1429static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
1430 std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>>
1432 QualType DstType, CGBuilderTy &Builder) {
1433 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType();
1434 bool DstSigned = DstType->isSignedIntegerOrEnumerationType();
1435
1436 ScalarExprEmitter::ImplicitConversionCheckKind Kind;
1437 if (!SrcSigned && !DstSigned)
1438 Kind = ScalarExprEmitter::ICCK_UnsignedIntegerTruncation;
1439 else
1440 Kind = ScalarExprEmitter::ICCK_SignedIntegerTruncation;
1441
1442 llvm::Value *Check = nullptr;
1443 // 1. Extend the truncated value back to the same width as the Src.
1444 Check = Builder.CreateIntCast(Dst, Src->getType(), DstSigned, "bf.anyext");
1445 // 2. Equality-compare with the original source value
1446 Check = Builder.CreateICmpEQ(Check, Src, "bf.truncheck");
1447 // If the comparison result is 'i1 false', then the truncation was lossy.
1448
1449 return std::make_pair(
1450 Kind,
1451 std::make_pair(Check, SanitizerKind::SO_ImplicitBitfieldConversion));
1452}
1453
1454// Should be called within CodeGenFunction::SanitizerScope RAII scope.
1455// Returns 'i1 false' when the conversion Src -> Dst changed the sign.
1456static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
1457 std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>>
1459 QualType DstType, CGBuilderTy &Builder) {
1460 // 1. Was the old Value negative?
1461 llvm::Value *SrcIsNegative =
1462 EmitIsNegativeTestHelper(Src, SrcType, "bf.src", Builder);
1463 // 2. Is the new Value negative?
1464 llvm::Value *DstIsNegative =
1465 EmitIsNegativeTestHelper(Dst, DstType, "bf.dst", Builder);
1466 // 3. Now, was the 'negativity status' preserved during the conversion?
1467 // NOTE: conversion from negative to zero is considered to change the sign.
1468 // (We want to get 'false' when the conversion changed the sign)
1469 // So we should just equality-compare the negativity statuses.
1470 llvm::Value *Check = nullptr;
1471 Check =
1472 Builder.CreateICmpEQ(SrcIsNegative, DstIsNegative, "bf.signchangecheck");
1473 // If the comparison result is 'false', then the conversion changed the sign.
1474 return std::make_pair(
1475 ScalarExprEmitter::ICCK_IntegerSignChange,
1476 std::make_pair(Check, SanitizerKind::SO_ImplicitBitfieldConversion));
1477}
1478
1480 Value *Dst, QualType DstType,
1481 const CGBitFieldInfo &Info,
1482 SourceLocation Loc) {
1483
1484 if (!SanOpts.has(SanitizerKind::ImplicitBitfieldConversion))
1485 return;
1486
1487 // We only care about int->int conversions here.
1488 // We ignore conversions to/from pointer and/or bool.
1490 DstType))
1491 return;
1492
1493 if (DstType->isBooleanType() || SrcType->isBooleanType())
1494 return;
1495
1496 // This should be truncation of integral types.
1497 assert(isa<llvm::IntegerType>(Src->getType()) &&
1498 isa<llvm::IntegerType>(Dst->getType()) && "non-integer llvm type");
1499
1500 // TODO: Calculate src width to avoid emitting code
1501 // for unecessary cases.
1502 unsigned SrcBits = ConvertType(SrcType)->getScalarSizeInBits();
1503 unsigned DstBits = Info.Size;
1504
1505 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType();
1506 bool DstSigned = DstType->isSignedIntegerOrEnumerationType();
1507
1508 auto CheckHandler = SanitizerHandler::ImplicitConversion;
1509 SanitizerDebugLocation SanScope(
1510 this, {SanitizerKind::SO_ImplicitBitfieldConversion}, CheckHandler);
1511
1512 std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
1513 std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>>
1514 Check;
1515
1516 // Truncation
1517 bool EmitTruncation = DstBits < SrcBits;
1518 // If Dst is signed and Src unsigned, we want to be more specific
1519 // about the CheckKind we emit, in this case we want to emit
1520 // ICCK_SignedIntegerTruncationOrSignChange.
1521 bool EmitTruncationFromUnsignedToSigned =
1522 EmitTruncation && DstSigned && !SrcSigned;
1523 // Sign change
1524 bool SameTypeSameSize = SrcSigned == DstSigned && SrcBits == DstBits;
1525 bool BothUnsigned = !SrcSigned && !DstSigned;
1526 bool LargerSigned = (DstBits > SrcBits) && DstSigned;
1527 // We can avoid emitting sign change checks in some obvious cases
1528 // 1. If Src and Dst have the same signedness and size
1529 // 2. If both are unsigned sign check is unecessary!
1530 // 3. If Dst is signed and bigger than Src, either
1531 // sign-extension or zero-extension will make sure
1532 // the sign remains.
1533 bool EmitSignChange = !SameTypeSameSize && !BothUnsigned && !LargerSigned;
1534
1535 if (EmitTruncation)
1536 Check =
1537 EmitBitfieldTruncationCheckHelper(Src, SrcType, Dst, DstType, Builder);
1538 else if (EmitSignChange) {
1539 assert(((SrcBits != DstBits) || (SrcSigned != DstSigned)) &&
1540 "either the widths should be different, or the signednesses.");
1541 Check =
1542 EmitBitfieldSignChangeCheckHelper(Src, SrcType, Dst, DstType, Builder);
1543 } else
1544 return;
1545
1546 ScalarExprEmitter::ImplicitConversionCheckKind CheckKind = Check.first;
1547 if (EmitTruncationFromUnsignedToSigned)
1548 CheckKind = ScalarExprEmitter::ICCK_SignedIntegerTruncationOrSignChange;
1549
1550 llvm::Constant *StaticArgs[] = {
1552 EmitCheckTypeDescriptor(DstType),
1553 llvm::ConstantInt::get(Builder.getInt8Ty(), CheckKind),
1554 llvm::ConstantInt::get(Builder.getInt32Ty(), Info.Size)};
1555
1556 EmitCheck(Check.second, CheckHandler, StaticArgs, {Src, Dst});
1557}
1558
1559Value *ScalarExprEmitter::EmitScalarCast(Value *Src, QualType SrcType,
1560 QualType DstType, llvm::Type *SrcTy,
1561 llvm::Type *DstTy,
1562 ScalarConversionOpts Opts) {
1563 // The Element types determine the type of cast to perform.
1564 llvm::Type *SrcElementTy;
1565 llvm::Type *DstElementTy;
1566 QualType SrcElementType;
1567 QualType DstElementType;
1568 if (SrcType->isMatrixType() && DstType->isMatrixType()) {
1569 SrcElementTy = cast<llvm::VectorType>(SrcTy)->getElementType();
1570 DstElementTy = cast<llvm::VectorType>(DstTy)->getElementType();
1571 SrcElementType = SrcType->castAs<MatrixType>()->getElementType();
1572 DstElementType = DstType->castAs<MatrixType>()->getElementType();
1573 } else {
1574 assert(!SrcType->isMatrixType() && !DstType->isMatrixType() &&
1575 "cannot cast between matrix and non-matrix types");
1576 SrcElementTy = SrcTy;
1577 DstElementTy = DstTy;
1578 SrcElementType = SrcType;
1579 DstElementType = DstType;
1580 }
1581
1582 if (isa<llvm::IntegerType>(SrcElementTy)) {
1583 bool InputSigned = SrcElementType->isSignedIntegerOrEnumerationType();
1584 if (SrcElementType->isBooleanType() && Opts.TreatBooleanAsSigned) {
1585 InputSigned = true;
1586 }
1587
1588 if (isa<llvm::IntegerType>(DstElementTy))
1589 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
1590 if (InputSigned)
1591 return Builder.CreateSIToFP(Src, DstTy, "conv");
1592 return Builder.CreateUIToFP(Src, DstTy, "conv");
1593 }
1594
1595 if (isa<llvm::IntegerType>(DstElementTy)) {
1596 assert(SrcElementTy->isFloatingPointTy() && "Unknown real conversion");
1597 bool IsSigned = DstElementType->isSignedIntegerOrEnumerationType();
1598
1599 // If we can't recognize overflow as undefined behavior, assume that
1600 // overflow saturates. This protects against normal optimizations if we are
1601 // compiling with non-standard FP semantics.
1602 if (!CGF.CGM.getCodeGenOpts().StrictFloatCastOverflow) {
1603 llvm::Intrinsic::ID IID =
1604 IsSigned ? llvm::Intrinsic::fptosi_sat : llvm::Intrinsic::fptoui_sat;
1605 return Builder.CreateCall(CGF.CGM.getIntrinsic(IID, {DstTy, SrcTy}), Src);
1606 }
1607
1608 if (IsSigned)
1609 return Builder.CreateFPToSI(Src, DstTy, "conv");
1610 return Builder.CreateFPToUI(Src, DstTy, "conv");
1611 }
1612
1613 if ((DstElementTy->is16bitFPTy() && SrcElementTy->is16bitFPTy())) {
1614 Value *FloatVal = Builder.CreateFPExt(Src, Builder.getFloatTy(), "fpext");
1615 return Builder.CreateFPTrunc(FloatVal, DstTy, "fptrunc");
1616 }
1617 if (DstElementTy->getTypeID() < SrcElementTy->getTypeID())
1618 return Builder.CreateFPTrunc(Src, DstTy, "conv");
1619 return Builder.CreateFPExt(Src, DstTy, "conv");
1620}
1621
1622/// Emit a conversion from the specified type to the specified destination type,
1623/// both of which are LLVM scalar types.
1624Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
1625 QualType DstType,
1626 SourceLocation Loc,
1627 ScalarConversionOpts Opts) {
1628 // All conversions involving fixed point types should be handled by the
1629 // EmitFixedPoint family functions. This is done to prevent bloating up this
1630 // function more, and although fixed point numbers are represented by
1631 // integers, we do not want to follow any logic that assumes they should be
1632 // treated as integers.
1633 // TODO(leonardchan): When necessary, add another if statement checking for
1634 // conversions to fixed point types from other types.
1635 if (SrcType->isFixedPointType()) {
1636 if (DstType->isBooleanType())
1637 // It is important that we check this before checking if the dest type is
1638 // an integer because booleans are technically integer types.
1639 // We do not need to check the padding bit on unsigned types if unsigned
1640 // padding is enabled because overflow into this bit is undefined
1641 // behavior.
1642 return Builder.CreateIsNotNull(Src, "tobool");
1643 if (DstType->isFixedPointType() || DstType->isIntegerType() ||
1644 DstType->isRealFloatingType())
1645 return EmitFixedPointConversion(Src, SrcType, DstType, Loc);
1646
1647 llvm_unreachable(
1648 "Unhandled scalar conversion from a fixed point type to another type.");
1649 } else if (DstType->isFixedPointType()) {
1650 if (SrcType->isIntegerType() || SrcType->isRealFloatingType())
1651 // This also includes converting booleans and enums to fixed point types.
1652 return EmitFixedPointConversion(Src, SrcType, DstType, Loc);
1653
1654 llvm_unreachable(
1655 "Unhandled scalar conversion to a fixed point type from another type.");
1656 }
1657
1658 QualType NoncanonicalSrcType = SrcType;
1659 QualType NoncanonicalDstType = DstType;
1660
1661 SrcType = CGF.getContext().getCanonicalType(SrcType);
1662 DstType = CGF.getContext().getCanonicalType(DstType);
1663 if (SrcType == DstType) return Src;
1664
1665 if (DstType->isVoidType()) return nullptr;
1666
1667 llvm::Value *OrigSrc = Src;
1668 QualType OrigSrcType = SrcType;
1669 llvm::Type *SrcTy = Src->getType();
1670
1671 // Handle conversions to bool first, they are special: comparisons against 0.
1672 if (DstType->isBooleanType())
1673 return EmitConversionToBool(Src, SrcType);
1674
1675 llvm::Type *DstTy = ConvertType(DstType);
1676
1677 // Determine whether an overflow behavior of 'trap' has been specified for
1678 // either the destination or the source types. If so, we can elide sanitizer
1679 // capability checks as this overflow behavior kind is also capable of
1680 // emitting traps without runtime sanitizer support.
1681 // Also skip instrumentation if either source or destination has 'wrap'
1682 // behavior - the user has explicitly indicated they accept wrapping
1683 // semantics. Use non-canonical types to preserve OBT annotations.
1684 const auto *DstOBT = NoncanonicalDstType->getAs<OverflowBehaviorType>();
1685 const auto *SrcOBT = NoncanonicalSrcType->getAs<OverflowBehaviorType>();
1686 bool OBTrapInvolved =
1687 (DstOBT && DstOBT->isTrapKind()) || (SrcOBT && SrcOBT->isTrapKind());
1688 bool OBWrapInvolved =
1689 (DstOBT && DstOBT->isWrapKind()) || (SrcOBT && SrcOBT->isWrapKind());
1690
1691 // Cast from half through float if half isn't a native type.
1692 if (SrcType->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
1693 // Cast to FP using the intrinsic if the half type itself isn't supported.
1694 if (DstTy->isFloatingPointTy()) {
1696 Value *BitCast = Builder.CreateBitCast(Src, CGF.CGM.HalfTy);
1697 return Builder.CreateFPExt(BitCast, DstTy, "conv");
1698 }
1699 } else {
1700 // Cast to other types through float, using either the intrinsic or FPExt,
1701 // depending on whether the half type itself is supported
1702 // (as opposed to operations on half, available with NativeHalfType).
1703
1704 if (Src->getType() != CGF.CGM.HalfTy) {
1706 Src = Builder.CreateBitCast(Src, CGF.CGM.HalfTy);
1707 }
1708
1709 Src = Builder.CreateFPExt(Src, CGF.CGM.FloatTy, "conv");
1710 SrcType = CGF.getContext().FloatTy;
1711 SrcTy = CGF.FloatTy;
1712 }
1713 }
1714
1715 // Ignore conversions like int -> uint.
1716 if (SrcTy == DstTy) {
1717 if (Opts.EmitImplicitIntegerSignChangeChecks ||
1718 (OBTrapInvolved && !OBWrapInvolved))
1719 EmitIntegerSignChangeCheck(Src, NoncanonicalSrcType, Src,
1720 NoncanonicalDstType, Loc, OBTrapInvolved);
1721
1722 return Src;
1723 }
1724
1725 // Handle pointer conversions next: pointers can only be converted to/from
1726 // other pointers and integers. Check for pointer types in terms of LLVM, as
1727 // some native types (like Obj-C id) may map to a pointer type.
1728 if (auto DstPT = dyn_cast<llvm::PointerType>(DstTy)) {
1729 // The source value may be an integer, or a pointer.
1730 if (isa<llvm::PointerType>(SrcTy))
1731 return Src;
1732
1733 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
1734 // First, convert to the correct width so that we control the kind of
1735 // extension.
1736 llvm::Type *MiddleTy = CGF.CGM.getDataLayout().getIntPtrType(DstPT);
1737 bool InputSigned = SrcType->isSignedIntegerOrEnumerationType();
1738 llvm::Value* IntResult =
1739 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
1740 // Then, cast to pointer.
1741 return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
1742 }
1743
1744 if (isa<llvm::PointerType>(SrcTy)) {
1745 // Must be an ptr to int cast.
1746 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
1747 return Builder.CreatePtrToInt(Src, DstTy, "conv");
1748 }
1749
1750 // A scalar can be splatted to an extended vector of the same element type
1751 if (DstType->isExtVectorType() && !SrcType->isVectorType()) {
1752 // Sema should add casts to make sure that the source expression's type is
1753 // the same as the vector's element type (sans qualifiers)
1754 assert(DstType->castAs<ExtVectorType>()->getElementType().getTypePtr() ==
1755 SrcType.getTypePtr() &&
1756 "Splatted expr doesn't match with vector element type?");
1757
1758 // Splat the element across to all elements
1759 unsigned NumElements = cast<llvm::FixedVectorType>(DstTy)->getNumElements();
1760 return Builder.CreateVectorSplat(NumElements, Src, "splat");
1761 }
1762
1763 if (SrcType->isMatrixType() && DstType->isMatrixType())
1764 return EmitScalarCast(Src, SrcType, DstType, SrcTy, DstTy, Opts);
1765
1766 if (isa<llvm::VectorType>(SrcTy) || isa<llvm::VectorType>(DstTy)) {
1767 // Allow bitcast from vector to integer/fp of the same size.
1768 llvm::TypeSize SrcSize = SrcTy->getPrimitiveSizeInBits();
1769 llvm::TypeSize DstSize = DstTy->getPrimitiveSizeInBits();
1770 if (SrcSize == DstSize)
1771 return Builder.CreateBitCast(Src, DstTy, "conv");
1772
1773 // Conversions between vectors of different sizes are not allowed except
1774 // when vectors of half are involved. Operations on storage-only half
1775 // vectors require promoting half vector operands to float vectors and
1776 // truncating the result, which is either an int or float vector, to a
1777 // short or half vector.
1778
1779 // Source and destination are both expected to be vectors.
1780 llvm::Type *SrcElementTy = cast<llvm::VectorType>(SrcTy)->getElementType();
1781 llvm::Type *DstElementTy = cast<llvm::VectorType>(DstTy)->getElementType();
1782 (void)DstElementTy;
1783
1784 assert(((SrcElementTy->isIntegerTy() &&
1785 DstElementTy->isIntegerTy()) ||
1786 (SrcElementTy->isFloatingPointTy() &&
1787 DstElementTy->isFloatingPointTy())) &&
1788 "unexpected conversion between a floating-point vector and an "
1789 "integer vector");
1790
1791 // Truncate an i32 vector to an i16 vector.
1792 if (SrcElementTy->isIntegerTy())
1793 return Builder.CreateIntCast(Src, DstTy, false, "conv");
1794
1795 // Truncate a float vector to a half vector.
1796 if (SrcSize > DstSize)
1797 return Builder.CreateFPTrunc(Src, DstTy, "conv");
1798
1799 // Promote a half vector to a float vector.
1800 return Builder.CreateFPExt(Src, DstTy, "conv");
1801 }
1802
1803 // Finally, we have the arithmetic types: real int/float.
1804 Value *Res = nullptr;
1805 llvm::Type *ResTy = DstTy;
1806
1807 // An overflowing conversion has undefined behavior if either the source type
1808 // or the destination type is a floating-point type. However, we consider the
1809 // range of representable values for all floating-point types to be
1810 // [-inf,+inf], so no overflow can ever happen when the destination type is a
1811 // floating-point type.
1812 if (CGF.SanOpts.has(SanitizerKind::FloatCastOverflow) &&
1813 OrigSrcType->isFloatingType())
1814 EmitFloatConversionCheck(OrigSrc, OrigSrcType, Src, SrcType, DstType, DstTy,
1815 Loc);
1816
1817 // Cast to half through float if half isn't a native type.
1818 if (DstType->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
1819 // Make sure we cast in a single step if from another FP type.
1820 if (SrcTy->isFloatingPointTy()) {
1821 // Handle the case where the half type is represented as an integer (as
1822 // opposed to operations on half, available with NativeHalfType).
1823
1824 // If the half type is supported, just use an fptrunc.
1825 Value *Res = Builder.CreateFPTrunc(Src, CGF.CGM.HalfTy, "conv");
1826 if (DstTy == CGF.CGM.HalfTy)
1827 return Res;
1828
1829 assert(DstTy->isIntegerTy(16) &&
1831 "Only half FP requires extra conversion");
1832 return Builder.CreateBitCast(Res, DstTy);
1833 }
1834
1835 DstTy = CGF.FloatTy;
1836 }
1837
1838 Res = EmitScalarCast(Src, SrcType, DstType, SrcTy, DstTy, Opts);
1839
1840 if (DstTy != ResTy) {
1841 Res = Builder.CreateFPTrunc(Res, CGF.CGM.HalfTy, "conv");
1842
1843 if (ResTy != CGF.CGM.HalfTy) {
1844 assert(ResTy->isIntegerTy(16) &&
1846 "Only half FP requires extra conversion");
1847 Res = Builder.CreateBitCast(Res, ResTy);
1848 }
1849 }
1850
1851 if ((Opts.EmitImplicitIntegerTruncationChecks || OBTrapInvolved) &&
1852 !OBWrapInvolved && !Opts.PatternExcluded)
1853 EmitIntegerTruncationCheck(Src, NoncanonicalSrcType, Res,
1854 NoncanonicalDstType, Loc, OBTrapInvolved);
1855
1856 if (Opts.EmitImplicitIntegerSignChangeChecks ||
1857 (OBTrapInvolved && !OBWrapInvolved))
1858 EmitIntegerSignChangeCheck(Src, NoncanonicalSrcType, Res,
1859 NoncanonicalDstType, Loc, OBTrapInvolved);
1860
1861 return Res;
1862}
1863
1864Value *ScalarExprEmitter::EmitFixedPointConversion(Value *Src, QualType SrcTy,
1865 QualType DstTy,
1866 SourceLocation Loc) {
1867 llvm::FixedPointBuilder<CGBuilderTy> FPBuilder(Builder);
1868 llvm::Value *Result;
1869 if (SrcTy->isRealFloatingType())
1870 Result = FPBuilder.CreateFloatingToFixed(Src,
1871 CGF.getContext().getFixedPointSemantics(DstTy));
1872 else if (DstTy->isRealFloatingType())
1873 Result = FPBuilder.CreateFixedToFloating(Src,
1875 ConvertType(DstTy));
1876 else {
1877 auto SrcFPSema = CGF.getContext().getFixedPointSemantics(SrcTy);
1878 auto DstFPSema = CGF.getContext().getFixedPointSemantics(DstTy);
1879
1880 if (DstTy->isIntegerType())
1881 Result = FPBuilder.CreateFixedToInteger(Src, SrcFPSema,
1882 DstFPSema.getWidth(),
1883 DstFPSema.isSigned());
1884 else if (SrcTy->isIntegerType())
1885 Result = FPBuilder.CreateIntegerToFixed(Src, SrcFPSema.isSigned(),
1886 DstFPSema);
1887 else
1888 Result = FPBuilder.CreateFixedToFixed(Src, SrcFPSema, DstFPSema);
1889 }
1890 return Result;
1891}
1892
1893/// Emit a conversion from the specified complex type to the specified
1894/// destination type, where the destination type is an LLVM scalar type.
1895Value *ScalarExprEmitter::EmitComplexToScalarConversion(
1896 CodeGenFunction::ComplexPairTy Src, QualType SrcTy, QualType DstTy,
1897 SourceLocation Loc) {
1898 // Get the source element type.
1899 SrcTy = SrcTy->castAs<ComplexType>()->getElementType();
1900
1901 // Handle conversions to bool first, they are special: comparisons against 0.
1902 if (DstTy->isBooleanType()) {
1903 // Complex != 0 -> (Real != 0) | (Imag != 0)
1904 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy, Loc);
1905 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy, Loc);
1906 return Builder.CreateOr(Src.first, Src.second, "tobool");
1907 }
1908
1909 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
1910 // the imaginary part of the complex value is discarded and the value of the
1911 // real part is converted according to the conversion rules for the
1912 // corresponding real type.
1913 return EmitScalarConversion(Src.first, SrcTy, DstTy, Loc);
1914}
1915
1916Value *ScalarExprEmitter::EmitNullValue(QualType Ty) {
1917 return CGF.EmitFromMemory(CGF.CGM.EmitNullConstant(Ty), Ty);
1918}
1919
1920/// Emit a sanitization check for the given "binary" operation (which
1921/// might actually be a unary increment which has been lowered to a binary
1922/// operation). The check passes if all values in \p Checks (which are \c i1),
1923/// are \c true.
1924void ScalarExprEmitter::EmitBinOpCheck(
1925 ArrayRef<std::pair<Value *, SanitizerKind::SanitizerOrdinal>> Checks,
1926 const BinOpInfo &Info) {
1927 assert(CGF.IsSanitizerScope);
1928 SanitizerHandler Check;
1929 SmallVector<llvm::Constant *, 4> StaticData;
1930 SmallVector<llvm::Value *, 2> DynamicData;
1931 TrapReason TR;
1932
1933 BinaryOperatorKind Opcode = Info.Opcode;
1936
1937 StaticData.push_back(CGF.EmitCheckSourceLocation(Info.E->getExprLoc()));
1938 const UnaryOperator *UO = dyn_cast<UnaryOperator>(Info.E);
1939 if (UO && UO->getOpcode() == UO_Minus) {
1940 Check = SanitizerHandler::NegateOverflow;
1941 StaticData.push_back(CGF.EmitCheckTypeDescriptor(UO->getType()));
1942 DynamicData.push_back(Info.RHS);
1943 } else {
1944 if (BinaryOperator::isShiftOp(Opcode)) {
1945 // Shift LHS negative or too large, or RHS out of bounds.
1946 Check = SanitizerHandler::ShiftOutOfBounds;
1947 const BinaryOperator *BO = cast<BinaryOperator>(Info.E);
1948 StaticData.push_back(
1949 CGF.EmitCheckTypeDescriptor(BO->getLHS()->getType()));
1950 StaticData.push_back(
1951 CGF.EmitCheckTypeDescriptor(BO->getRHS()->getType()));
1952 } else if (Opcode == BO_Div || Opcode == BO_Rem) {
1953 // Divide or modulo by zero, or signed overflow (eg INT_MAX / -1).
1954 Check = SanitizerHandler::DivremOverflow;
1955 StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty));
1956 } else {
1957 // Arithmetic overflow (+, -, *).
1958 int ArithOverflowKind = 0;
1959 switch (Opcode) {
1960 case BO_Add: {
1961 Check = SanitizerHandler::AddOverflow;
1962 ArithOverflowKind = diag::UBSanArithKind::Add;
1963 break;
1964 }
1965 case BO_Sub: {
1966 Check = SanitizerHandler::SubOverflow;
1967 ArithOverflowKind = diag::UBSanArithKind::Sub;
1968 break;
1969 }
1970 case BO_Mul: {
1971 Check = SanitizerHandler::MulOverflow;
1972 ArithOverflowKind = diag::UBSanArithKind::Mul;
1973 break;
1974 }
1975 default:
1976 llvm_unreachable("unexpected opcode for bin op check");
1977 }
1978 StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty));
1980 SanitizerKind::UnsignedIntegerOverflow) ||
1982 SanitizerKind::SignedIntegerOverflow)) {
1983 // Only pay the cost for constructing the trap diagnostic if they are
1984 // going to be used.
1985 CGF.CGM.BuildTrapReason(diag::trap_ubsan_arith_overflow, TR)
1986 << Info.Ty->isSignedIntegerOrEnumerationType() << ArithOverflowKind
1987 << Info.E;
1988 }
1989 }
1990 DynamicData.push_back(Info.LHS);
1991 DynamicData.push_back(Info.RHS);
1992 }
1993
1994 CGF.EmitCheck(Checks, Check, StaticData, DynamicData, &TR);
1995}
1996
1997//===----------------------------------------------------------------------===//
1998// Visitor Methods
1999//===----------------------------------------------------------------------===//
2000
2001Value *ScalarExprEmitter::VisitExpr(Expr *E) {
2002 CGF.ErrorUnsupported(E, "scalar expression");
2003 if (E->getType()->isVoidType())
2004 return nullptr;
2005 return llvm::PoisonValue::get(CGF.ConvertType(E->getType()));
2006}
2007
2008Value *
2009ScalarExprEmitter::VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr *E) {
2010 ASTContext &Context = CGF.getContext();
2011 unsigned AddrSpace =
2013 llvm::Constant *GlobalConstStr = Builder.CreateGlobalString(
2014 E->ComputeName(Context), "__usn_str", AddrSpace);
2015
2016 llvm::Type *ExprTy = ConvertType(E->getType());
2017 return Builder.CreatePointerBitCastOrAddrSpaceCast(GlobalConstStr, ExprTy,
2018 "usn_addr_cast");
2019}
2020
2021Value *ScalarExprEmitter::VisitEmbedExpr(EmbedExpr *E) {
2022 assert(E->getDataElementCount() == 1);
2023 auto It = E->begin();
2024 return Builder.getInt((*It)->getValue());
2025}
2026
2027Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
2028 // Vector Mask Case
2029 if (E->getNumSubExprs() == 2) {
2030 Value *LHS = CGF.EmitScalarExpr(E->getExpr(0));
2031 Value *RHS = CGF.EmitScalarExpr(E->getExpr(1));
2032 Value *Mask;
2033
2034 auto *LTy = cast<llvm::FixedVectorType>(LHS->getType());
2035 unsigned LHSElts = LTy->getNumElements();
2036
2037 Mask = RHS;
2038
2039 auto *MTy = cast<llvm::FixedVectorType>(Mask->getType());
2040
2041 // Mask off the high bits of each shuffle index.
2042 Value *MaskBits =
2043 llvm::ConstantInt::get(MTy, llvm::NextPowerOf2(LHSElts - 1) - 1);
2044 Mask = Builder.CreateAnd(Mask, MaskBits, "mask");
2045
2046 // newv = undef
2047 // mask = mask & maskbits
2048 // for each elt
2049 // n = extract mask i
2050 // x = extract val n
2051 // newv = insert newv, x, i
2052 auto *RTy = llvm::FixedVectorType::get(LTy->getElementType(),
2053 MTy->getNumElements());
2054 Value* NewV = llvm::PoisonValue::get(RTy);
2055 for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) {
2056 Value *IIndx = llvm::ConstantInt::get(CGF.SizeTy, i);
2057 Value *Indx = Builder.CreateExtractElement(Mask, IIndx, "shuf_idx");
2058
2059 Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt");
2060 NewV = Builder.CreateInsertElement(NewV, VExt, IIndx, "shuf_ins");
2061 }
2062 return NewV;
2063 }
2064
2065 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
2066 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
2067
2068 SmallVector<int, 32> Indices;
2069 for (unsigned i = 2; i < E->getNumSubExprs(); ++i) {
2070 llvm::APSInt Idx = E->getShuffleMaskIdx(i - 2);
2071 // Check for -1 and output it as undef in the IR.
2072 if (Idx.isSigned() && Idx.isAllOnes())
2073 Indices.push_back(-1);
2074 else
2075 Indices.push_back(Idx.getZExtValue());
2076 }
2077
2078 return Builder.CreateShuffleVector(V1, V2, Indices, "shuffle");
2079}
2080
2081Value *ScalarExprEmitter::VisitConvertVectorExpr(ConvertVectorExpr *E) {
2082 QualType SrcType = E->getSrcExpr()->getType(),
2083 DstType = E->getType();
2084
2085 Value *Src = CGF.EmitScalarExpr(E->getSrcExpr());
2086
2087 SrcType = CGF.getContext().getCanonicalType(SrcType);
2088 DstType = CGF.getContext().getCanonicalType(DstType);
2089 if (SrcType == DstType) return Src;
2090
2091 assert(SrcType->isVectorType() &&
2092 "ConvertVector source type must be a vector");
2093 assert(DstType->isVectorType() &&
2094 "ConvertVector destination type must be a vector");
2095
2096 llvm::Type *SrcTy = Src->getType();
2097 llvm::Type *DstTy = ConvertType(DstType);
2098
2099 // Ignore conversions like int -> uint.
2100 if (SrcTy == DstTy)
2101 return Src;
2102
2103 QualType SrcEltType = SrcType->castAs<VectorType>()->getElementType(),
2104 DstEltType = DstType->castAs<VectorType>()->getElementType();
2105
2106 assert(SrcTy->isVectorTy() &&
2107 "ConvertVector source IR type must be a vector");
2108 assert(DstTy->isVectorTy() &&
2109 "ConvertVector destination IR type must be a vector");
2110
2111 llvm::Type *SrcEltTy = cast<llvm::VectorType>(SrcTy)->getElementType(),
2112 *DstEltTy = cast<llvm::VectorType>(DstTy)->getElementType();
2113
2114 if (DstEltType->isBooleanType()) {
2115 assert((SrcEltTy->isFloatingPointTy() ||
2116 isa<llvm::IntegerType>(SrcEltTy)) && "Unknown boolean conversion");
2117
2118 llvm::Value *Zero = llvm::Constant::getNullValue(SrcTy);
2119 if (SrcEltTy->isFloatingPointTy()) {
2120 CodeGenFunction::CGFPOptionsRAII FPOptions(CGF, E);
2121 return Builder.CreateFCmpUNE(Src, Zero, "tobool");
2122 } else {
2123 return Builder.CreateICmpNE(Src, Zero, "tobool");
2124 }
2125 }
2126
2127 // We have the arithmetic types: real int/float.
2128 Value *Res = nullptr;
2129
2130 if (isa<llvm::IntegerType>(SrcEltTy)) {
2131 bool InputSigned = SrcEltType->isSignedIntegerOrEnumerationType();
2132 if (isa<llvm::IntegerType>(DstEltTy))
2133 Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
2134 else {
2135 CodeGenFunction::CGFPOptionsRAII FPOptions(CGF, E);
2136 if (InputSigned)
2137 Res = Builder.CreateSIToFP(Src, DstTy, "conv");
2138 else
2139 Res = Builder.CreateUIToFP(Src, DstTy, "conv");
2140 }
2141 } else if (isa<llvm::IntegerType>(DstEltTy)) {
2142 assert(SrcEltTy->isFloatingPointTy() && "Unknown real conversion");
2143 CodeGenFunction::CGFPOptionsRAII FPOptions(CGF, E);
2144 if (DstEltType->isSignedIntegerOrEnumerationType())
2145 Res = Builder.CreateFPToSI(Src, DstTy, "conv");
2146 else
2147 Res = Builder.CreateFPToUI(Src, DstTy, "conv");
2148 } else {
2149 assert(SrcEltTy->isFloatingPointTy() && DstEltTy->isFloatingPointTy() &&
2150 "Unknown real conversion");
2151 CodeGenFunction::CGFPOptionsRAII FPOptions(CGF, E);
2152 if (DstEltTy->getTypeID() < SrcEltTy->getTypeID())
2153 Res = Builder.CreateFPTrunc(Src, DstTy, "conv");
2154 else
2155 Res = Builder.CreateFPExt(Src, DstTy, "conv");
2156 }
2157
2158 return Res;
2159}
2160
2161Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) {
2162 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E)) {
2163 CGF.EmitIgnoredExpr(E->getBase());
2164 return CGF.emitScalarConstant(Constant, E);
2165 } else {
2166 Expr::EvalResult Result;
2168 llvm::APSInt Value = Result.Val.getInt();
2169 CGF.EmitIgnoredExpr(E->getBase());
2170 return Builder.getInt(Value);
2171 }
2172 }
2173
2174 llvm::Value *Result = EmitLoadOfLValue(E);
2175
2176 // If -fdebug-info-for-profiling is specified, emit a pseudo variable and its
2177 // debug info for the pointer, even if there is no variable associated with
2178 // the pointer's expression.
2179 if (CGF.CGM.getCodeGenOpts().DebugInfoForProfiling && CGF.getDebugInfo()) {
2180 if (llvm::LoadInst *Load = dyn_cast<llvm::LoadInst>(Result)) {
2181 if (llvm::GetElementPtrInst *GEP =
2182 dyn_cast<llvm::GetElementPtrInst>(Load->getPointerOperand())) {
2183 if (llvm::Instruction *Pointer =
2184 dyn_cast<llvm::Instruction>(GEP->getPointerOperand())) {
2185 QualType Ty = E->getBase()->getType();
2186 if (!E->isArrow())
2187 Ty = CGF.getContext().getPointerType(Ty);
2188 CGF.getDebugInfo()->EmitPseudoVariable(Builder, Pointer, Ty);
2189 }
2190 }
2191 }
2192 }
2193 return Result;
2194}
2195
2196Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
2197 TestAndClearIgnoreResultAssign();
2198
2199 // Emit subscript expressions in rvalue context's. For most cases, this just
2200 // loads the lvalue formed by the subscript expr. However, we have to be
2201 // careful, because the base of a vector subscript is occasionally an rvalue,
2202 // so we can't get it as an lvalue.
2203 if (!E->getBase()->getType()->isVectorType() &&
2205 return EmitLoadOfLValue(E);
2206
2207 // Handle the vector case. The base must be a vector, the index must be an
2208 // integer value.
2209 Value *Base = Visit(E->getBase());
2210 Value *Idx = Visit(E->getIdx());
2211 QualType IdxTy = E->getIdx()->getType();
2212
2213 if (CGF.SanOpts.has(SanitizerKind::ArrayBounds))
2214 CGF.EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, /*Accessed*/true);
2215
2216 Value *Ret = Builder.CreateExtractElement(Base, Idx, "vecext");
2217
2218 // Even being a scalar the `__mfp8` type corresponds to `<1 x i8>` in LLVM IR.
2219 if (E->getType()->isMFloat8Type())
2220 Ret = Builder.CreateInsertElement(
2221 llvm::PoisonValue::get(llvm::FixedVectorType::get(CGF.Int8Ty, 1)), Ret,
2222 uint64_t(0), "mfp8ext");
2223
2224 return Ret;
2225}
2226
2227Value *ScalarExprEmitter::VisitMatrixSingleSubscriptExpr(
2228 MatrixSingleSubscriptExpr *E) {
2229 TestAndClearIgnoreResultAssign();
2230
2231 auto *MatrixTy = E->getBase()->getType()->castAs<ConstantMatrixType>();
2232 unsigned NumRows = MatrixTy->getNumRows();
2233 unsigned NumColumns = MatrixTy->getNumColumns();
2234
2235 // Row index
2236 Value *RowIdx = CGF.EmitMatrixIndexExpr(E->getRowIdx());
2237 llvm::MatrixBuilder MB(Builder);
2238
2239 // The row index must be in [0, NumRows)
2240 if (CGF.CGM.getCodeGenOpts().OptimizationLevel > 0)
2241 MB.CreateIndexAssumption(RowIdx, NumRows);
2242
2243 Value *FlatMatrix = Visit(E->getBase());
2244 llvm::Type *ElemTy = CGF.ConvertTypeForMem(MatrixTy->getElementType());
2245 auto *ResultTy = llvm::FixedVectorType::get(ElemTy, NumColumns);
2246 Value *RowVec = llvm::PoisonValue::get(ResultTy);
2247
2248 for (unsigned Col = 0; Col != NumColumns; ++Col) {
2249 Value *ColVal = llvm::ConstantInt::get(RowIdx->getType(), Col);
2250 bool IsMatrixRowMajor = CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
2251 LangOptions::MatrixMemoryLayout::MatrixRowMajor;
2252 Value *EltIdx = MB.CreateIndex(RowIdx, ColVal, NumRows, NumColumns,
2253 IsMatrixRowMajor, "matrix_row_idx");
2254 Value *Elt =
2255 Builder.CreateExtractElement(FlatMatrix, EltIdx, "matrix_elem");
2256 Value *Lane = llvm::ConstantInt::get(Builder.getInt32Ty(), Col);
2257 RowVec = Builder.CreateInsertElement(RowVec, Elt, Lane, "matrix_row_ins");
2258 }
2259
2260 return CGF.EmitFromMemory(RowVec, E->getType());
2261}
2262
2263Value *ScalarExprEmitter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *E) {
2264 TestAndClearIgnoreResultAssign();
2265
2266 // Handle the vector case. The base must be a vector, the index must be an
2267 // integer value.
2268 Value *RowIdx = CGF.EmitMatrixIndexExpr(E->getRowIdx());
2269 Value *ColumnIdx = CGF.EmitMatrixIndexExpr(E->getColumnIdx());
2270
2271 const auto *MatrixTy = E->getBase()->getType()->castAs<ConstantMatrixType>();
2272 llvm::MatrixBuilder MB(Builder);
2273
2274 Value *Idx;
2275 unsigned NumCols = MatrixTy->getNumColumns();
2276 unsigned NumRows = MatrixTy->getNumRows();
2277 bool IsMatrixRowMajor = CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
2278 LangOptions::MatrixMemoryLayout::MatrixRowMajor;
2279 Idx = MB.CreateIndex(RowIdx, ColumnIdx, NumRows, NumCols, IsMatrixRowMajor);
2280
2281 if (CGF.CGM.getCodeGenOpts().OptimizationLevel > 0)
2282 MB.CreateIndexAssumption(Idx, MatrixTy->getNumElementsFlattened());
2283
2284 Value *Matrix = Visit(E->getBase());
2285
2286 // TODO: Should we emit bounds checks with SanitizerKind::ArrayBounds?
2287 return Builder.CreateExtractElement(Matrix, Idx, "matrixext");
2288}
2289
2290static int getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
2291 unsigned Off) {
2292 int MV = SVI->getMaskValue(Idx);
2293 if (MV == -1)
2294 return -1;
2295 return Off + MV;
2296}
2297
2298static int getAsInt32(llvm::ConstantInt *C, llvm::Type *I32Ty) {
2299 assert(llvm::ConstantInt::isValueValidForType(I32Ty, C->getZExtValue()) &&
2300 "Index operand too large for shufflevector mask!");
2301 return C->getZExtValue();
2302}
2303
2304Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
2305 bool Ignore = TestAndClearIgnoreResultAssign();
2306 (void)Ignore;
2307 unsigned NumInitElements = E->getNumInits();
2308 assert((Ignore == false ||
2309 (NumInitElements == 0 && E->getType()->isVoidType())) &&
2310 "init list ignored");
2311
2312 // HLSL initialization lists in the AST are an expansion which can contain
2313 // side-effecting expressions wrapped in opaque value expressions. To properly
2314 // emit these we need to emit the opaque values before we emit the argument
2315 // expressions themselves. This is a little hacky, but it prevents us needing
2316 // to do a bigger AST-level change for a language feature that we need
2317 // deprecate in the near future. See related HLSL language proposals in the
2318 // proposals (https://github.com/microsoft/hlsl-specs/blob/main/proposals):
2319 // * 0005-strict-initializer-lists.md
2320 // * 0032-constructors.md
2321 if (CGF.getLangOpts().HLSL)
2323
2324 if (E->hadArrayRangeDesignator())
2325 CGF.ErrorUnsupported(E, "GNU array range designator extension");
2326
2327 llvm::VectorType *VType =
2328 dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
2329
2330 if (!VType) {
2331 if (NumInitElements == 0) {
2332 // C++11 value-initialization for the scalar.
2333 return EmitNullValue(E->getType());
2334 }
2335 // We have a scalar in braces. Just use the first element.
2336 return Visit(E->getInit(0));
2337 }
2338
2339 if (isa<llvm::ScalableVectorType>(VType)) {
2340 if (NumInitElements == 0) {
2341 // C++11 value-initialization for the vector.
2342 return EmitNullValue(E->getType());
2343 }
2344
2345 if (NumInitElements == 1) {
2346 Expr *InitVector = E->getInit(0);
2347
2348 // Initialize from another scalable vector of the same type.
2349 if (InitVector->getType().getCanonicalType() ==
2351 return Visit(InitVector);
2352 }
2353
2354 llvm_unreachable("Unexpected initialization of a scalable vector!");
2355 }
2356
2357 unsigned ResElts = cast<llvm::FixedVectorType>(VType)->getNumElements();
2358
2359 // For column-major matrix types, we insert elements directly at their
2360 // column-major positions rather than inserting sequentially and shuffling.
2361 const ConstantMatrixType *ColMajorMT = nullptr;
2362 if (const auto *MT = E->getType()->getAs<ConstantMatrixType>();
2363 MT && CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
2364 LangOptions::MatrixMemoryLayout::MatrixColMajor)
2365 ColMajorMT = MT;
2366
2367 // Loop over initializers collecting the Value for each, and remembering
2368 // whether the source was swizzle (ExtVectorElementExpr). This will allow
2369 // us to fold the shuffle for the swizzle into the shuffle for the vector
2370 // initializer, since LLVM optimizers generally do not want to touch
2371 // shuffles.
2372 unsigned CurIdx = 0;
2373 bool VIsPoisonShuffle = false;
2374 llvm::Value *V = llvm::PoisonValue::get(VType);
2375 for (unsigned i = 0; i != NumInitElements; ++i) {
2376 Expr *IE = E->getInit(i);
2377 Value *Init = Visit(IE);
2378 SmallVector<int, 16> Args;
2379
2380 llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType());
2381
2382 // Handle scalar elements. If the scalar initializer is actually one
2383 // element of a different vector of the same width, use shuffle instead of
2384 // extract+insert.
2385 if (!VVT) {
2386 if (isa<ExtVectorElementExpr>(IE)) {
2387 llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init);
2388
2389 if (cast<llvm::FixedVectorType>(EI->getVectorOperandType())
2390 ->getNumElements() == ResElts) {
2391 llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand());
2392 Value *LHS = nullptr, *RHS = nullptr;
2393 if (CurIdx == 0) {
2394 // insert into poison -> shuffle (src, poison)
2395 // shufflemask must use an i32
2396 Args.push_back(getAsInt32(C, CGF.Int32Ty));
2397 Args.resize(ResElts, -1);
2398
2399 LHS = EI->getVectorOperand();
2400 RHS = V;
2401 VIsPoisonShuffle = true;
2402 } else if (VIsPoisonShuffle) {
2403 // insert into poison shuffle && size match -> shuffle (v, src)
2404 llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V);
2405 for (unsigned j = 0; j != CurIdx; ++j)
2406 Args.push_back(getMaskElt(SVV, j, 0));
2407 Args.push_back(ResElts + C->getZExtValue());
2408 Args.resize(ResElts, -1);
2409
2410 LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
2411 RHS = EI->getVectorOperand();
2412 VIsPoisonShuffle = false;
2413 }
2414 if (!Args.empty()) {
2415 V = Builder.CreateShuffleVector(LHS, RHS, Args);
2416 ++CurIdx;
2417 continue;
2418 }
2419 }
2420 }
2421 unsigned InsertIdx =
2422 ColMajorMT
2423 ? ColMajorMT->mapRowMajorToColumnMajorFlattenedIndex(CurIdx)
2424 : CurIdx;
2425 V = Builder.CreateInsertElement(V, Init, Builder.getInt32(InsertIdx),
2426 "vecinit");
2427 VIsPoisonShuffle = false;
2428 ++CurIdx;
2429 continue;
2430 }
2431
2432 unsigned InitElts = cast<llvm::FixedVectorType>(VVT)->getNumElements();
2433
2434 // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's
2435 // input is the same width as the vector being constructed, generate an
2436 // optimized shuffle of the swizzle input into the result.
2437 unsigned Offset = (CurIdx == 0) ? 0 : ResElts;
2438 if (isa<ExtVectorElementExpr>(IE)) {
2439 llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init);
2440 Value *SVOp = SVI->getOperand(0);
2441 auto *OpTy = cast<llvm::FixedVectorType>(SVOp->getType());
2442
2443 if (OpTy->getNumElements() == ResElts) {
2444 for (unsigned j = 0; j != CurIdx; ++j) {
2445 // If the current vector initializer is a shuffle with poison, merge
2446 // this shuffle directly into it.
2447 if (VIsPoisonShuffle) {
2448 Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0));
2449 } else {
2450 Args.push_back(j);
2451 }
2452 }
2453 for (unsigned j = 0, je = InitElts; j != je; ++j)
2454 Args.push_back(getMaskElt(SVI, j, Offset));
2455 Args.resize(ResElts, -1);
2456
2457 if (VIsPoisonShuffle)
2458 V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
2459
2460 Init = SVOp;
2461 }
2462 }
2463
2464 // Extend init to result vector length, and then shuffle its contribution
2465 // to the vector initializer into V.
2466 if (Args.empty()) {
2467 for (unsigned j = 0; j != InitElts; ++j)
2468 Args.push_back(j);
2469 Args.resize(ResElts, -1);
2470 Init = Builder.CreateShuffleVector(Init, Args, "vext");
2471
2472 Args.clear();
2473 for (unsigned j = 0; j != CurIdx; ++j)
2474 Args.push_back(j);
2475 for (unsigned j = 0; j != InitElts; ++j)
2476 Args.push_back(j + Offset);
2477 Args.resize(ResElts, -1);
2478 }
2479
2480 // If V is poison, make sure it ends up on the RHS of the shuffle to aid
2481 // merging subsequent shuffles into this one.
2482 if (CurIdx == 0)
2483 std::swap(V, Init);
2484 V = Builder.CreateShuffleVector(V, Init, Args, "vecinit");
2485 VIsPoisonShuffle = isa<llvm::PoisonValue>(Init);
2486 CurIdx += InitElts;
2487 }
2488
2489 // FIXME: evaluate codegen vs. shuffling against constant null vector.
2490 // Emit remaining default initializers.
2491 llvm::Type *EltTy = VType->getElementType();
2492
2493 // Emit remaining default initializers
2494 for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) {
2495 unsigned InsertIdx =
2496 ColMajorMT ? ColMajorMT->mapRowMajorToColumnMajorFlattenedIndex(CurIdx)
2497 : CurIdx;
2498 Value *Idx = Builder.getInt32(InsertIdx);
2499 llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
2500 V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
2501 }
2502
2503 return V;
2504}
2505
2507 return !D->isWeak();
2508}
2509
2510static bool isLValueKnownNonNull(CodeGenFunction &CGF, const Expr *E) {
2511 E = E->IgnoreParens();
2512
2513 if (const auto *UO = dyn_cast<UnaryOperator>(E))
2514 if (UO->getOpcode() == UO_Deref)
2515 return CGF.isPointerKnownNonNull(UO->getSubExpr());
2516
2517 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
2518 return isDeclRefKnownNonNull(CGF, DRE->getDecl());
2519
2520 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
2521 if (isa<FieldDecl>(ME->getMemberDecl()))
2522 return true;
2523 return isDeclRefKnownNonNull(CGF, ME->getMemberDecl());
2524 }
2525
2526 // Array subscripts? Anything else?
2527
2528 return false;
2529}
2530
2532 assert(E->getType()->isSignableType(getContext()));
2533
2534 E = E->IgnoreParens();
2535
2536 if (isa<CXXThisExpr>(E))
2537 return true;
2538
2539 if (const auto *UO = dyn_cast<UnaryOperator>(E))
2540 if (UO->getOpcode() == UO_AddrOf)
2541 return isLValueKnownNonNull(*this, UO->getSubExpr());
2542
2543 if (const auto *CE = dyn_cast<CastExpr>(E))
2544 if (CE->getCastKind() == CK_FunctionToPointerDecay ||
2545 CE->getCastKind() == CK_ArrayToPointerDecay)
2546 return isLValueKnownNonNull(*this, CE->getSubExpr());
2547
2548 // Maybe honor __nonnull?
2549
2550 return false;
2551}
2552
2554 const Expr *E = CE->getSubExpr();
2555
2556 if (CE->getCastKind() == CK_UncheckedDerivedToBase)
2557 return false;
2558
2559 if (isa<CXXThisExpr>(E->IgnoreParens())) {
2560 // We always assume that 'this' is never null.
2561 return false;
2562 }
2563
2564 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) {
2565 // And that glvalue casts are never null.
2566 if (ICE->isGLValue())
2567 return false;
2568 }
2569
2570 return true;
2571}
2572
2573// RHS is an aggregate type
2575 QualType DestTy, SourceLocation Loc) {
2576 SmallVector<LValue, 16> LoadList;
2577 CGF.FlattenAccessAndTypeLValue(SrcVal, LoadList);
2578 // Dest is either a vector, constant matrix, or a builtin
2579 // if its a vector create a temp alloca to store into and return that
2580 if (auto *VecTy = DestTy->getAs<VectorType>()) {
2581 assert(LoadList.size() >= VecTy->getNumElements() &&
2582 "Flattened type on RHS must have the same number or more elements "
2583 "than vector on LHS.");
2584 llvm::Value *V = CGF.Builder.CreateLoad(
2585 CGF.CreateIRTempWithoutCast(DestTy, "flatcast.tmp"));
2586 // write to V.
2587 for (unsigned I = 0, E = VecTy->getNumElements(); I < E; I++) {
2588 RValue RVal = CGF.EmitLoadOfLValue(LoadList[I], Loc);
2589 assert(RVal.isScalar() &&
2590 "All flattened source values should be scalars.");
2591 llvm::Value *Cast =
2592 CGF.EmitScalarConversion(RVal.getScalarVal(), LoadList[I].getType(),
2593 VecTy->getElementType(), Loc);
2594 V = CGF.Builder.CreateInsertElement(V, Cast, I);
2595 }
2596 return V;
2597 }
2598 if (auto *MatTy = DestTy->getAs<ConstantMatrixType>()) {
2599 assert(LoadList.size() >= MatTy->getNumElementsFlattened() &&
2600 "Flattened type on RHS must have the same number or more elements "
2601 "than vector on LHS.");
2602
2603 bool IsRowMajor = CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
2605
2606 llvm::Value *V = CGF.Builder.CreateLoad(
2607 CGF.CreateIRTempWithoutCast(DestTy, "flatcast.tmp"));
2608 // V is an allocated temporary for constructing the matrix.
2609 for (unsigned Row = 0, RE = MatTy->getNumRows(); Row < RE; Row++) {
2610 for (unsigned Col = 0, CE = MatTy->getNumColumns(); Col < CE; Col++) {
2611 // When interpreted as a matrix, \p LoadList is *always* row-major order
2612 // regardless of the default matrix memory layout.
2613 unsigned LoadIdx = MatTy->getRowMajorFlattenedIndex(Row, Col);
2614 RValue RVal = CGF.EmitLoadOfLValue(LoadList[LoadIdx], Loc);
2615 assert(RVal.isScalar() &&
2616 "All flattened source values should be scalars.");
2617 llvm::Value *Cast = CGF.EmitScalarConversion(
2618 RVal.getScalarVal(), LoadList[LoadIdx].getType(),
2619 MatTy->getElementType(), Loc);
2620 unsigned MatrixIdx = MatTy->getFlattenedIndex(Row, Col, IsRowMajor);
2621 V = CGF.Builder.CreateInsertElement(V, Cast, MatrixIdx);
2622 }
2623 }
2624 return V;
2625 }
2626 // if its a builtin just do an extract element or load.
2627 assert(DestTy->isBuiltinType() &&
2628 "Destination type must be a vector, matrix, or builtin type.");
2629 RValue RVal = CGF.EmitLoadOfLValue(LoadList[0], Loc);
2630 assert(RVal.isScalar() && "All flattened source values should be scalars.");
2631 return CGF.EmitScalarConversion(RVal.getScalarVal(), LoadList[0].getType(),
2632 DestTy, Loc);
2633}
2634
2635// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
2636// have to handle a more broad range of conversions than explicit casts, as they
2637// handle things like function to ptr-to-function decay etc.
2638Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
2639 llvm::scope_exit RestoreCurCast(
2640 [this, Prev = CGF.CurCast] { CGF.CurCast = Prev; });
2641 CGF.CurCast = CE;
2642
2643 Expr *E = CE->getSubExpr();
2644 QualType DestTy = CE->getType();
2645 CastKind Kind = CE->getCastKind();
2646 CodeGenFunction::CGFPOptionsRAII FPOptions(CGF, CE);
2647
2648 // These cases are generally not written to ignore the result of
2649 // evaluating their sub-expressions, so we clear this now.
2650 bool Ignored = TestAndClearIgnoreResultAssign();
2651
2652 // Since almost all cast kinds apply to scalars, this switch doesn't have
2653 // a default case, so the compiler will warn on a missing case. The cases
2654 // are in the same order as in the CastKind enum.
2655 switch (Kind) {
2656 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
2657 case CK_BuiltinFnToFnPtr:
2658 llvm_unreachable("builtin functions are handled elsewhere");
2659
2660 case CK_LValueBitCast:
2661 case CK_ObjCObjectLValueCast: {
2662 Address Addr = EmitLValue(E).getAddress();
2663 Addr = Addr.withElementType(CGF.ConvertTypeForMem(DestTy));
2664 LValue LV = CGF.MakeAddrLValue(Addr, DestTy);
2665 return EmitLoadOfLValue(LV, CE->getExprLoc());
2666 }
2667
2668 case CK_LValueToRValueBitCast: {
2669 LValue SourceLVal = CGF.EmitLValue(E);
2670 Address Addr =
2671 SourceLVal.getAddress().withElementType(CGF.ConvertTypeForMem(DestTy));
2672 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
2673 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo());
2674 return EmitLoadOfLValue(DestLV, CE->getExprLoc());
2675 }
2676
2677 case CK_CPointerToObjCPointerCast:
2678 case CK_BlockPointerToObjCPointerCast:
2679 case CK_AnyPointerToBlockPointerCast:
2680 case CK_BitCast: {
2681 Value *Src = Visit(E);
2682 llvm::Type *SrcTy = Src->getType();
2683 llvm::Type *DstTy = ConvertType(DestTy);
2684
2685 // FIXME: this is a gross but seemingly necessary workaround for an issue
2686 // manifesting when a target uses a non-default AS for indirect sret args,
2687 // but the source HLL is generic, wherein a valid C-cast or reinterpret_cast
2688 // on the address of a local struct that gets returned by value yields an
2689 // invalid bitcast from the a pointer to the IndirectAS to a pointer to the
2690 // DefaultAS. We can only do this subversive thing because sret args are
2691 // manufactured and them residing in the IndirectAS is a target specific
2692 // detail, and doing an AS cast here still retains the semantics the user
2693 // expects. It is desirable to remove this iff a better solution is found.
2694 if (auto A = dyn_cast<llvm::Argument>(Src); A && A->hasStructRetAttr())
2695 return CGF.performAddrSpaceCast(Src, DstTy);
2696
2697 // FIXME: Similarly to the sret case above, we need to handle BitCasts that
2698 // involve implicit address space conversions. This arises when the source
2699 // language lacks explicit address spaces, but the target's data layout
2700 // assigns different address spaces (e.g., program address space for
2701 // function pointers). Since Sema operates on Clang types (which don't carry
2702 // this information) and selects CK_BitCast, we must detect the address
2703 // space mismatch here in CodeGen when lowering to LLVM types. The most
2704 // common case is casting function pointers (which get the program AS from
2705 // the data layout) to/from object pointers (which use the default AS).
2706 // Ideally, this would be resolved at a higher level, but that would require
2707 // exposing data layout details to Sema.
2708 if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() &&
2709 SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) {
2710 return CGF.performAddrSpaceCast(Src, DstTy);
2711 }
2712
2713 assert(
2714 (!SrcTy->isPtrOrPtrVectorTy() || !DstTy->isPtrOrPtrVectorTy() ||
2715 SrcTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace()) &&
2716 "Address-space cast must be used to convert address spaces");
2717
2718 if (CGF.SanOpts.has(SanitizerKind::CFIUnrelatedCast)) {
2719 if (auto *PT = DestTy->getAs<PointerType>()) {
2721 PT->getPointeeType(),
2722 Address(Src,
2724 E->getType()->castAs<PointerType>()->getPointeeType()),
2725 CGF.getPointerAlign()),
2726 /*MayBeNull=*/true, CodeGenFunction::CFITCK_UnrelatedCast,
2727 CE->getBeginLoc());
2728 }
2729 }
2730
2731 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers) {
2732 const QualType SrcType = E->getType();
2733
2734 if (SrcType.mayBeNotDynamicClass() && DestTy.mayBeDynamicClass()) {
2735 // Casting to pointer that could carry dynamic information (provided by
2736 // invariant.group) requires launder.
2737 Src = Builder.CreateLaunderInvariantGroup(Src);
2738 } else if (SrcType.mayBeDynamicClass() && DestTy.mayBeNotDynamicClass()) {
2739 // Casting to pointer that does not carry dynamic information (provided
2740 // by invariant.group) requires stripping it. Note that we don't do it
2741 // if the source could not be dynamic type and destination could be
2742 // dynamic because dynamic information is already laundered. It is
2743 // because launder(strip(src)) == launder(src), so there is no need to
2744 // add extra strip before launder.
2745 Src = Builder.CreateStripInvariantGroup(Src);
2746 }
2747 }
2748
2749 // Update heapallocsite metadata when there is an explicit pointer cast.
2750 if (auto *CI = dyn_cast<llvm::CallBase>(Src)) {
2751 if (CI->getMetadata("heapallocsite") && isa<ExplicitCastExpr>(CE) &&
2752 !isa<CastExpr>(E)) {
2753 QualType PointeeType = DestTy->getPointeeType();
2754 if (!PointeeType.isNull())
2755 CGF.getDebugInfo()->addHeapAllocSiteMetadata(CI, PointeeType,
2756 CE->getExprLoc());
2757 }
2758 }
2759
2760 // If Src is a fixed vector and Dst is a scalable vector, and both have the
2761 // same element type, use the llvm.vector.insert intrinsic to perform the
2762 // bitcast.
2763 if (auto *FixedSrcTy = dyn_cast<llvm::FixedVectorType>(SrcTy)) {
2764 if (auto *ScalableDstTy = dyn_cast<llvm::ScalableVectorType>(DstTy)) {
2765 // If we are casting a fixed i8 vector to a scalable i1 predicate
2766 // vector, use a vector insert and bitcast the result.
2767 if (ScalableDstTy->getElementType()->isIntegerTy(1) &&
2768 FixedSrcTy->getElementType()->isIntegerTy(8)) {
2769 ScalableDstTy = llvm::ScalableVectorType::get(
2770 FixedSrcTy->getElementType(),
2771 llvm::divideCeil(
2772 ScalableDstTy->getElementCount().getKnownMinValue(), 8));
2773 }
2774 if (FixedSrcTy->getElementType() == ScalableDstTy->getElementType()) {
2775 llvm::Value *PoisonVec = llvm::PoisonValue::get(ScalableDstTy);
2776 llvm::Value *Result = Builder.CreateInsertVector(
2777 ScalableDstTy, PoisonVec, Src, uint64_t(0), "cast.scalable");
2778 ScalableDstTy = cast<llvm::ScalableVectorType>(
2779 llvm::VectorType::getWithSizeAndScalar(ScalableDstTy, DstTy));
2780 if (Result->getType() != ScalableDstTy)
2781 Result = Builder.CreateBitCast(Result, ScalableDstTy);
2782 if (Result->getType() != DstTy)
2783 Result = Builder.CreateExtractVector(DstTy, Result, uint64_t(0));
2784 return Result;
2785 }
2786 }
2787 }
2788
2789 // If Src is a scalable vector and Dst is a fixed vector, and both have the
2790 // same element type, use the llvm.vector.extract intrinsic to perform the
2791 // bitcast.
2792 if (auto *ScalableSrcTy = dyn_cast<llvm::ScalableVectorType>(SrcTy)) {
2793 if (auto *FixedDstTy = dyn_cast<llvm::FixedVectorType>(DstTy)) {
2794 // If we are casting a scalable i1 predicate vector to a fixed i8
2795 // vector, bitcast the source and use a vector extract.
2796 if (ScalableSrcTy->getElementType()->isIntegerTy(1) &&
2797 FixedDstTy->getElementType()->isIntegerTy(8)) {
2798 if (!ScalableSrcTy->getElementCount().isKnownMultipleOf(8)) {
2799 ScalableSrcTy = llvm::ScalableVectorType::get(
2800 ScalableSrcTy->getElementType(),
2801 llvm::alignTo<8>(
2802 ScalableSrcTy->getElementCount().getKnownMinValue()));
2803 llvm::Value *ZeroVec = llvm::Constant::getNullValue(ScalableSrcTy);
2804 Src = Builder.CreateInsertVector(ScalableSrcTy, ZeroVec, Src,
2805 uint64_t(0));
2806 }
2807
2808 ScalableSrcTy = llvm::ScalableVectorType::get(
2809 FixedDstTy->getElementType(),
2810 ScalableSrcTy->getElementCount().getKnownMinValue() / 8);
2811 Src = Builder.CreateBitCast(Src, ScalableSrcTy);
2812 }
2813 if (ScalableSrcTy->getElementType() == FixedDstTy->getElementType())
2814 return Builder.CreateExtractVector(DstTy, Src, uint64_t(0),
2815 "cast.fixed");
2816 }
2817 }
2818
2819 // Perform VLAT <-> VLST bitcast through memory.
2820 // TODO: since the llvm.vector.{insert,extract} intrinsics
2821 // require the element types of the vectors to be the same, we
2822 // need to keep this around for bitcasts between VLAT <-> VLST where
2823 // the element types of the vectors are not the same, until we figure
2824 // out a better way of doing these casts.
2825 if ((isa<llvm::FixedVectorType>(SrcTy) &&
2829 Address Addr = CGF.CreateDefaultAlignTempAlloca(SrcTy, "saved-value");
2830 LValue LV = CGF.MakeAddrLValue(Addr, E->getType());
2831 CGF.EmitStoreOfScalar(Src, LV);
2832 Addr = Addr.withElementType(CGF.ConvertTypeForMem(DestTy));
2833 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
2834 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo());
2835 return EmitLoadOfLValue(DestLV, CE->getExprLoc());
2836 }
2837
2838 llvm::Value *Result = Builder.CreateBitCast(Src, DstTy);
2839 return CGF.authPointerToPointerCast(Result, E->getType(), DestTy);
2840 }
2841 case CK_AddressSpaceConversion: {
2842 Expr::EvalResult Result;
2843 if (E->EvaluateAsRValue(Result, CGF.getContext()) &&
2844 Result.Val.isNullPointer()) {
2845 // If E has side effect, it is emitted even if its final result is a
2846 // null pointer. In that case, a DCE pass should be able to
2847 // eliminate the useless instructions emitted during translating E.
2848 if (Result.HasSideEffects)
2849 Visit(E);
2851 ConvertType(DestTy)), DestTy);
2852 }
2853 // Since target may map different address spaces in AST to the same address
2854 // space, an address space conversion may end up as a bitcast.
2855 return CGF.performAddrSpaceCast(Visit(E), ConvertType(DestTy));
2856 }
2857 case CK_AtomicToNonAtomic:
2858 case CK_NonAtomicToAtomic:
2859 case CK_UserDefinedConversion:
2860 return Visit(E);
2861
2862 case CK_NoOp: {
2863 return CE->changesVolatileQualification() ? EmitLoadOfLValue(CE) : Visit(E);
2864 }
2865
2866 case CK_BaseToDerived: {
2867 const CXXRecordDecl *DerivedClassDecl = DestTy->getPointeeCXXRecordDecl();
2868 assert(DerivedClassDecl && "BaseToDerived arg isn't a C++ object pointer!");
2869
2871 Address Derived =
2872 CGF.GetAddressOfDerivedClass(Base, DerivedClassDecl,
2873 CE->path_begin(), CE->path_end(),
2875
2876 // C++11 [expr.static.cast]p11: Behavior is undefined if a downcast is
2877 // performed and the object is not of the derived type.
2878 if (CGF.sanitizePerformTypeCheck())
2880 Derived, DestTy->getPointeeType());
2881
2882 if (CGF.SanOpts.has(SanitizerKind::CFIDerivedCast))
2883 CGF.EmitVTablePtrCheckForCast(DestTy->getPointeeType(), Derived,
2884 /*MayBeNull=*/true,
2886 CE->getBeginLoc());
2887
2888 return CGF.getAsNaturalPointerTo(Derived, CE->getType()->getPointeeType());
2889 }
2890 case CK_UncheckedDerivedToBase:
2891 case CK_DerivedToBase: {
2892 // The EmitPointerWithAlignment path does this fine; just discard
2893 // the alignment.
2895 CE->getType()->getPointeeType());
2896 }
2897
2898 case CK_Dynamic: {
2900 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE);
2901 return CGF.EmitDynamicCast(V, DCE);
2902 }
2903
2904 case CK_ArrayToPointerDecay:
2906 CE->getType()->getPointeeType());
2907 case CK_FunctionToPointerDecay:
2908 return EmitLValue(E).getPointer(CGF);
2909
2910 case CK_NullToPointer:
2911 if (MustVisitNullValue(E))
2912 CGF.EmitIgnoredExpr(E);
2913
2914 return CGF.CGM.getNullPointer(cast<llvm::PointerType>(ConvertType(DestTy)),
2915 DestTy);
2916
2917 case CK_NullToMemberPointer: {
2918 if (MustVisitNullValue(E))
2919 CGF.EmitIgnoredExpr(E);
2920
2921 const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>();
2922 return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT);
2923 }
2924
2925 case CK_ReinterpretMemberPointer:
2926 case CK_BaseToDerivedMemberPointer:
2927 case CK_DerivedToBaseMemberPointer: {
2928 Value *Src = Visit(E);
2929
2930 // Note that the AST doesn't distinguish between checked and
2931 // unchecked member pointer conversions, so we always have to
2932 // implement checked conversions here. This is inefficient when
2933 // actual control flow may be required in order to perform the
2934 // check, which it is for data member pointers (but not member
2935 // function pointers on Itanium and ARM).
2936 return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src);
2937 }
2938
2939 case CK_ARCProduceObject:
2940 return CGF.EmitARCRetainScalarExpr(E);
2941 case CK_ARCConsumeObject:
2942 return CGF.EmitObjCConsumeObject(E->getType(), Visit(E));
2943 case CK_ARCReclaimReturnedObject:
2944 return CGF.EmitARCReclaimReturnedObject(E, /*allowUnsafe*/ Ignored);
2945 case CK_ARCExtendBlockObject:
2946 return CGF.EmitARCExtendBlockObject(E);
2947
2948 case CK_CopyAndAutoreleaseBlockObject:
2949 return CGF.EmitBlockCopyAndAutorelease(Visit(E), E->getType());
2950
2951 case CK_FloatingRealToComplex:
2952 case CK_FloatingComplexCast:
2953 case CK_IntegralRealToComplex:
2954 case CK_IntegralComplexCast:
2955 case CK_IntegralComplexToFloatingComplex:
2956 case CK_FloatingComplexToIntegralComplex:
2957 case CK_ConstructorConversion:
2958 case CK_ToUnion:
2959 case CK_HLSLArrayRValue:
2960 llvm_unreachable("scalar cast to non-scalar value");
2961
2962 case CK_LValueToRValue:
2963 assert(CGF.getContext().hasSameUnqualifiedType(E->getType(), DestTy));
2964 assert(E->isGLValue() && "lvalue-to-rvalue applied to r-value!");
2965 return Visit(E);
2966
2967 case CK_IntegralToPointer: {
2968 Value *Src = Visit(E);
2969
2970 // First, convert to the correct width so that we control the kind of
2971 // extension.
2972 auto DestLLVMTy = ConvertType(DestTy);
2973 llvm::Type *MiddleTy = CGF.CGM.getDataLayout().getIntPtrType(DestLLVMTy);
2974 bool InputSigned = E->getType()->isSignedIntegerOrEnumerationType();
2975 llvm::Value* IntResult =
2976 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
2977
2978 auto *IntToPtr = Builder.CreateIntToPtr(IntResult, DestLLVMTy);
2979
2980 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers) {
2981 // Going from integer to pointer that could be dynamic requires reloading
2982 // dynamic information from invariant.group.
2983 if (DestTy.mayBeDynamicClass())
2984 IntToPtr = Builder.CreateLaunderInvariantGroup(IntToPtr);
2985 }
2986
2987 IntToPtr = CGF.authPointerToPointerCast(IntToPtr, E->getType(), DestTy);
2988 return IntToPtr;
2989 }
2990 case CK_PointerToIntegral: {
2991 assert(!DestTy->isBooleanType() && "bool should use PointerToBool");
2992 auto *PtrExpr = Visit(E);
2993
2994 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers) {
2995 const QualType SrcType = E->getType();
2996
2997 // Casting to integer requires stripping dynamic information as it does
2998 // not carries it.
2999 if (SrcType.mayBeDynamicClass())
3000 PtrExpr = Builder.CreateStripInvariantGroup(PtrExpr);
3001 }
3002
3003 PtrExpr = CGF.authPointerToPointerCast(PtrExpr, E->getType(), DestTy);
3004 return Builder.CreatePtrToInt(PtrExpr, ConvertType(DestTy));
3005 }
3006 case CK_ToVoid: {
3007 CGF.EmitIgnoredExpr(E);
3008 return nullptr;
3009 }
3010 case CK_MatrixCast: {
3011 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3012 CE->getExprLoc());
3013 }
3014 // CK_HLSLAggregateSplatCast only handles splatting to vectors from a vec1
3015 // Casts were inserted in Sema to Cast the Src Expr to a Scalar and
3016 // To perform any necessary Scalar Cast, so this Cast can be handled
3017 // by the regular Vector Splat cast code.
3018 case CK_HLSLAggregateSplatCast:
3019 case CK_VectorSplat: {
3020 llvm::Type *DstTy = ConvertType(DestTy);
3021 Value *Elt = Visit(E);
3022 // Splat the element across to all elements
3023 llvm::ElementCount NumElements =
3024 cast<llvm::VectorType>(DstTy)->getElementCount();
3025 return Builder.CreateVectorSplat(NumElements, Elt, "splat");
3026 }
3027
3028 case CK_FixedPointCast:
3029 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3030 CE->getExprLoc());
3031
3032 case CK_FixedPointToBoolean:
3033 assert(E->getType()->isFixedPointType() &&
3034 "Expected src type to be fixed point type");
3035 assert(DestTy->isBooleanType() && "Expected dest type to be boolean type");
3036 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3037 CE->getExprLoc());
3038
3039 case CK_FixedPointToIntegral:
3040 assert(E->getType()->isFixedPointType() &&
3041 "Expected src type to be fixed point type");
3042 assert(DestTy->isIntegerType() && "Expected dest type to be an integer");
3043 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3044 CE->getExprLoc());
3045
3046 case CK_IntegralToFixedPoint:
3047 assert(E->getType()->isIntegerType() &&
3048 "Expected src type to be an integer");
3049 assert(DestTy->isFixedPointType() &&
3050 "Expected dest type to be fixed point type");
3051 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3052 CE->getExprLoc());
3053
3054 case CK_IntegralCast: {
3055 if (E->getType()->isExtVectorType() && DestTy->isExtVectorType()) {
3056 QualType SrcElTy = E->getType()->castAs<VectorType>()->getElementType();
3057 return Builder.CreateIntCast(Visit(E), ConvertType(DestTy),
3059 "conv");
3060 }
3061 ScalarConversionOpts Opts;
3062 if (auto *ICE = dyn_cast<ImplicitCastExpr>(CE)) {
3063 if (!ICE->isPartOfExplicitCast())
3064 Opts = ScalarConversionOpts(CGF.SanOpts);
3065 }
3066 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3067 CE->getExprLoc(), Opts);
3068 }
3069 case CK_IntegralToFloating: {
3070 if (E->getType()->isVectorType() && DestTy->isVectorType()) {
3071 // TODO: Support constrained FP intrinsics.
3072 QualType SrcElTy = E->getType()->castAs<VectorType>()->getElementType();
3073 if (SrcElTy->isSignedIntegerOrEnumerationType())
3074 return Builder.CreateSIToFP(Visit(E), ConvertType(DestTy), "conv");
3075 return Builder.CreateUIToFP(Visit(E), ConvertType(DestTy), "conv");
3076 }
3077 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE);
3078 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3079 CE->getExprLoc());
3080 }
3081 case CK_FloatingToIntegral: {
3082 if (E->getType()->isVectorType() && DestTy->isVectorType()) {
3083 // TODO: Support constrained FP intrinsics.
3084 QualType DstElTy = DestTy->castAs<VectorType>()->getElementType();
3085 if (DstElTy->isSignedIntegerOrEnumerationType())
3086 return Builder.CreateFPToSI(Visit(E), ConvertType(DestTy), "conv");
3087 return Builder.CreateFPToUI(Visit(E), ConvertType(DestTy), "conv");
3088 }
3089 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE);
3090 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3091 CE->getExprLoc());
3092 }
3093 case CK_FloatingCast: {
3094 if (E->getType()->isVectorType() && DestTy->isVectorType()) {
3095 // TODO: Support constrained FP intrinsics.
3096 QualType SrcElTy = E->getType()->castAs<VectorType>()->getElementType();
3097 QualType DstElTy = DestTy->castAs<VectorType>()->getElementType();
3098 if (DstElTy->castAs<BuiltinType>()->getKind() <
3099 SrcElTy->castAs<BuiltinType>()->getKind())
3100 return Builder.CreateFPTrunc(Visit(E), ConvertType(DestTy), "conv");
3101 return Builder.CreateFPExt(Visit(E), ConvertType(DestTy), "conv");
3102 }
3103 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE);
3104 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3105 CE->getExprLoc());
3106 }
3107 case CK_FixedPointToFloating:
3108 case CK_FloatingToFixedPoint: {
3109 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE);
3110 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3111 CE->getExprLoc());
3112 }
3113 case CK_BooleanToSignedIntegral: {
3114 ScalarConversionOpts Opts;
3115 Opts.TreatBooleanAsSigned = true;
3116 return EmitScalarConversion(Visit(E), E->getType(), DestTy,
3117 CE->getExprLoc(), Opts);
3118 }
3119 case CK_IntegralToBoolean:
3120 return EmitIntToBoolConversion(Visit(E));
3121 case CK_PointerToBoolean:
3122 return EmitPointerToBoolConversion(Visit(E), E->getType());
3123 case CK_FloatingToBoolean: {
3124 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, CE);
3125 return EmitFloatToBoolConversion(Visit(E));
3126 }
3127 case CK_MemberPointerToBoolean: {
3128 llvm::Value *MemPtr = Visit(E);
3129 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
3130 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT);
3131 }
3132
3133 case CK_FloatingComplexToReal:
3134 case CK_IntegralComplexToReal:
3135 return CGF.EmitComplexExpr(E, false, true).first;
3136
3137 case CK_FloatingComplexToBoolean:
3138 case CK_IntegralComplexToBoolean: {
3140
3141 // TODO: kill this function off, inline appropriate case here
3142 return EmitComplexToScalarConversion(V, E->getType(), DestTy,
3143 CE->getExprLoc());
3144 }
3145
3146 case CK_ZeroToOCLOpaqueType: {
3147 assert((DestTy->isEventT() || DestTy->isQueueT() ||
3148 DestTy->isOCLIntelSubgroupAVCType()) &&
3149 "CK_ZeroToOCLEvent cast on non-event type");
3150 return llvm::Constant::getNullValue(ConvertType(DestTy));
3151 }
3152
3153 case CK_IntToOCLSampler:
3154 return CGF.CGM.createOpenCLIntToSamplerConversion(E, CGF);
3155
3156 case CK_HLSLVectorTruncation: {
3157 assert((DestTy->isVectorType() || DestTy->isBuiltinType()) &&
3158 "Destination type must be a vector or builtin type.");
3159 Value *Vec = Visit(E);
3160 if (auto *VecTy = DestTy->getAs<VectorType>()) {
3161 SmallVector<int> Mask;
3162 unsigned NumElts = VecTy->getNumElements();
3163 for (unsigned I = 0; I != NumElts; ++I)
3164 Mask.push_back(I);
3165
3166 return Builder.CreateShuffleVector(Vec, Mask, "trunc");
3167 }
3168 llvm::Value *Zero = llvm::Constant::getNullValue(CGF.SizeTy);
3169 return Builder.CreateExtractElement(Vec, Zero, "cast.vtrunc");
3170 }
3171 case CK_HLSLMatrixTruncation: {
3172 assert((DestTy->isMatrixType() || DestTy->isBuiltinType()) &&
3173 "Destination type must be a matrix or builtin type.");
3174 Value *Mat = Visit(E);
3175 if (auto *MatTy = DestTy->getAs<ConstantMatrixType>()) {
3176 SmallVector<int> Mask(MatTy->getNumElementsFlattened());
3177 unsigned NumCols = MatTy->getNumColumns();
3178 unsigned NumRows = MatTy->getNumRows();
3179 auto *SrcMatTy = E->getType()->getAs<ConstantMatrixType>();
3180 assert(SrcMatTy && "Source type must be a matrix type.");
3181 assert(NumRows <= SrcMatTy->getNumRows());
3182 assert(NumCols <= SrcMatTy->getNumColumns());
3183 bool IsRowMajor = CGF.getLangOpts().getDefaultMatrixMemoryLayout() ==
3184 LangOptions::MatrixMemoryLayout::MatrixRowMajor;
3185 for (unsigned R = 0; R < NumRows; R++)
3186 for (unsigned C = 0; C < NumCols; C++)
3187 Mask[MatTy->getFlattenedIndex(R, C, IsRowMajor)] =
3188 SrcMatTy->getFlattenedIndex(R, C, IsRowMajor);
3189
3190 return Builder.CreateShuffleVector(Mat, Mask, "trunc");
3191 }
3192 llvm::Value *Zero = llvm::Constant::getNullValue(CGF.SizeTy);
3193 return Builder.CreateExtractElement(Mat, Zero, "cast.mtrunc");
3194 }
3195 case CK_HLSLElementwiseCast: {
3196 RValue RV = CGF.EmitAnyExpr(E);
3197 SourceLocation Loc = CE->getExprLoc();
3198
3199 Address SrcAddr = Address::invalid();
3200
3201 if (RV.isAggregate()) {
3202 SrcAddr = RV.getAggregateAddress();
3203 } else {
3204 SrcAddr = CGF.CreateMemTemp(E->getType(), "hlsl.ewcast.src");
3205 LValue TmpLV = CGF.MakeAddrLValue(SrcAddr, E->getType());
3206 CGF.EmitStoreThroughLValue(RV, TmpLV);
3207 }
3208
3209 LValue SrcVal = CGF.MakeAddrLValue(SrcAddr, E->getType());
3210 return EmitHLSLElementwiseCast(CGF, SrcVal, DestTy, Loc);
3211 }
3212
3213 } // end of switch
3214
3215 llvm_unreachable("unknown scalar cast");
3216}
3217
3218Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
3219 CodeGenFunction::StmtExprEvaluation eval(CGF);
3220 Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(),
3221 !E->getType()->isVoidType());
3222 if (!RetAlloca.isValid())
3223 return nullptr;
3224 return CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(RetAlloca, E->getType()),
3225 E->getExprLoc());
3226}
3227
3228Value *ScalarExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) {
3229 CodeGenFunction::RunCleanupsScope Scope(CGF);
3230 Value *V = Visit(E->getSubExpr());
3231 // Defend against dominance problems caused by jumps out of expression
3232 // evaluation through the shared cleanup block.
3233 Scope.ForceCleanup({&V});
3234 return V;
3235}
3236
3237//===----------------------------------------------------------------------===//
3238// Unary Operators
3239//===----------------------------------------------------------------------===//
3240
3242 llvm::Value *InVal, bool IsInc,
3243 FPOptions FPFeatures) {
3244 BinOpInfo BinOp;
3245 BinOp.LHS = InVal;
3246 BinOp.RHS = llvm::ConstantInt::get(InVal->getType(), 1, false);
3247 BinOp.Ty = E->getType();
3248 BinOp.Opcode = IsInc ? BO_Add : BO_Sub;
3249 BinOp.FPFeatures = FPFeatures;
3250 BinOp.E = E;
3251 return BinOp;
3252}
3253
3254llvm::Value *ScalarExprEmitter::EmitIncDecConsiderOverflowBehavior(
3255 const UnaryOperator *E, llvm::Value *InVal, bool IsInc) {
3256 // Treat positive amount as unsigned to support inc of i1 (needed for
3257 // unsigned _BitInt(1)).
3258 llvm::Value *Amount =
3259 llvm::ConstantInt::get(InVal->getType(), IsInc ? 1 : -1, !IsInc);
3260 StringRef Name = IsInc ? "inc" : "dec";
3261 QualType Ty = E->getType();
3262 const bool isSigned = Ty->isSignedIntegerOrEnumerationType();
3263 const bool hasSan =
3264 isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)
3265 : CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow);
3266
3267 switch (getOverflowBehaviorConsideringType(CGF, Ty)) {
3268 case LangOptions::OB_Wrap:
3269 return Builder.CreateAdd(InVal, Amount, Name);
3270 case LangOptions::OB_SignedAndDefined:
3271 if (!hasSan)
3272 return Builder.CreateAdd(InVal, Amount, Name);
3273 [[fallthrough]];
3274 case LangOptions::OB_Unset:
3275 if (!E->canOverflow())
3276 return Builder.CreateAdd(InVal, Amount, Name);
3277 if (!hasSan)
3278 return isSigned ? Builder.CreateNSWAdd(InVal, Amount, Name)
3279 : Builder.CreateAdd(InVal, Amount, Name);
3280 [[fallthrough]];
3281 case LangOptions::OB_Trap:
3282 if (!Ty->getAs<OverflowBehaviorType>() && !E->canOverflow())
3283 return Builder.CreateAdd(InVal, Amount, Name);
3284 BinOpInfo Info = createBinOpInfoFromIncDec(
3285 E, InVal, IsInc, E->getFPFeaturesInEffect(CGF.getLangOpts()));
3286 if (CanElideOverflowCheck(CGF.getContext(), Info))
3287 return isSigned ? Builder.CreateNSWAdd(InVal, Amount, Name)
3288 : Builder.CreateAdd(InVal, Amount, Name);
3289 return EmitOverflowCheckedBinOp(Info);
3290 }
3291 llvm_unreachable("Unknown OverflowBehaviorKind");
3292}
3293
3294namespace {
3295/// Handles check and update for lastprivate conditional variables.
3296class OMPLastprivateConditionalUpdateRAII {
3297private:
3298 CodeGenFunction &CGF;
3299 const UnaryOperator *E;
3300
3301public:
3302 OMPLastprivateConditionalUpdateRAII(CodeGenFunction &CGF,
3303 const UnaryOperator *E)
3304 : CGF(CGF), E(E) {}
3305 ~OMPLastprivateConditionalUpdateRAII() {
3306 if (CGF.getLangOpts().OpenMP)
3308 CGF, E->getSubExpr());
3309 }
3310};
3311} // namespace
3312
3313llvm::Value *
3314ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
3315 bool isInc, bool isPre) {
3316 ApplyAtomGroup Grp(CGF.getDebugInfo());
3317 OMPLastprivateConditionalUpdateRAII OMPRegion(CGF, E);
3318 QualType type = E->getSubExpr()->getType();
3319 llvm::PHINode *atomicPHI = nullptr;
3320 llvm::Value *value;
3321 llvm::Value *input;
3322 llvm::Value *Previous = nullptr;
3323 QualType SrcType = E->getType();
3324
3325 int amount = (isInc ? 1 : -1);
3326 bool isSubtraction = !isInc;
3327
3328 if (const AtomicType *atomicTy = type->getAs<AtomicType>()) {
3329 type = atomicTy->getValueType();
3330 if (isInc && type->isBooleanType()) {
3331 llvm::Value *True = CGF.EmitToMemory(Builder.getTrue(), type);
3332 if (isPre) {
3333 Builder.CreateStore(True, LV.getAddress(), LV.isVolatileQualified())
3334 ->setAtomic(llvm::AtomicOrdering::SequentiallyConsistent);
3335 return Builder.getTrue();
3336 }
3337 // For atomic bool increment, we just store true and return it for
3338 // preincrement, do an atomic swap with true for postincrement
3339 return Builder.CreateAtomicRMW(
3340 llvm::AtomicRMWInst::Xchg, LV.getAddress(), True,
3341 llvm::AtomicOrdering::SequentiallyConsistent);
3342 }
3343 // Special case for atomic increment / decrement on integers, emit
3344 // atomicrmw instructions. We skip this if we want to be doing overflow
3345 // checking, and fall into the slow path with the atomic cmpxchg loop.
3346 if (!type->isBooleanType() && type->isIntegerType() &&
3347 !(type->isUnsignedIntegerType() &&
3348 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
3349 CGF.getLangOpts().getSignedOverflowBehavior() !=
3350 LangOptions::SOB_Trapping) {
3351 llvm::AtomicRMWInst::BinOp aop = isInc ? llvm::AtomicRMWInst::Add :
3352 llvm::AtomicRMWInst::Sub;
3353 llvm::Instruction::BinaryOps op = isInc ? llvm::Instruction::Add :
3354 llvm::Instruction::Sub;
3355 llvm::Value *amt = CGF.EmitToMemory(
3356 llvm::ConstantInt::get(ConvertType(type), 1, true), type);
3357 llvm::Value *old =
3358 Builder.CreateAtomicRMW(aop, LV.getAddress(), amt,
3359 llvm::AtomicOrdering::SequentiallyConsistent);
3360 return isPre ? Builder.CreateBinOp(op, old, amt) : old;
3361 }
3362 // Special case for atomic increment/decrement on floats.
3363 // Bail out non-power-of-2-sized floating point types (e.g., x86_fp80).
3364 if (type->isFloatingType()) {
3365 llvm::Type *Ty = ConvertType(type);
3366 if (llvm::has_single_bit(Ty->getScalarSizeInBits())) {
3367 llvm::AtomicRMWInst::BinOp aop =
3368 isInc ? llvm::AtomicRMWInst::FAdd : llvm::AtomicRMWInst::FSub;
3369 llvm::Instruction::BinaryOps op =
3370 isInc ? llvm::Instruction::FAdd : llvm::Instruction::FSub;
3371 llvm::Value *amt = llvm::ConstantFP::get(Ty, 1.0);
3372 llvm::AtomicRMWInst *old =
3373 CGF.emitAtomicRMWInst(aop, LV.getAddress(), amt,
3374 llvm::AtomicOrdering::SequentiallyConsistent);
3375
3376 return isPre ? Builder.CreateBinOp(op, old, amt) : old;
3377 }
3378 }
3379 value = EmitLoadOfLValue(LV, E->getExprLoc());
3380 input = value;
3381 // For every other atomic operation, we need to emit a load-op-cmpxchg loop
3382 llvm::BasicBlock *startBB = Builder.GetInsertBlock();
3383 llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn);
3384 value = CGF.EmitToMemory(value, type);
3385 Builder.CreateBr(opBB);
3386 Builder.SetInsertPoint(opBB);
3387 atomicPHI = Builder.CreatePHI(value->getType(), 2);
3388 atomicPHI->addIncoming(value, startBB);
3389 value = atomicPHI;
3390 } else {
3391 value = EmitLoadOfLValue(LV, E->getExprLoc());
3392 input = value;
3393 }
3394
3395 // Special case of integer increment that we have to check first: bool++.
3396 // Due to promotion rules, we get:
3397 // bool++ -> bool = bool + 1
3398 // -> bool = (int)bool + 1
3399 // -> bool = ((int)bool + 1 != 0)
3400 // An interesting aspect of this is that increment is always true.
3401 // Decrement does not have this property.
3402 if (isInc && type->isBooleanType()) {
3403 value = Builder.getTrue();
3404
3405 // Most common case by far: integer increment.
3406 } else if (type->isIntegerType()) {
3407 QualType promotedType;
3408 bool canPerformLossyDemotionCheck = false;
3409
3411 promotedType = CGF.getContext().getPromotedIntegerType(type);
3412 assert(promotedType != type && "Shouldn't promote to the same type.");
3413 canPerformLossyDemotionCheck = true;
3414 canPerformLossyDemotionCheck &=
3416 CGF.getContext().getCanonicalType(promotedType);
3417 canPerformLossyDemotionCheck &=
3419 type, promotedType);
3420 assert((!canPerformLossyDemotionCheck ||
3421 type->isSignedIntegerOrEnumerationType() ||
3422 promotedType->isSignedIntegerOrEnumerationType() ||
3423 ConvertType(type)->getScalarSizeInBits() ==
3424 ConvertType(promotedType)->getScalarSizeInBits()) &&
3425 "The following check expects that if we do promotion to different "
3426 "underlying canonical type, at least one of the types (either "
3427 "base or promoted) will be signed, or the bitwidths will match.");
3428 }
3429 if (CGF.SanOpts.hasOneOf(
3430 SanitizerKind::ImplicitIntegerArithmeticValueChange |
3431 SanitizerKind::ImplicitBitfieldConversion) &&
3432 canPerformLossyDemotionCheck) {
3433 // While `x += 1` (for `x` with width less than int) is modeled as
3434 // promotion+arithmetics+demotion, and we can catch lossy demotion with
3435 // ease; inc/dec with width less than int can't overflow because of
3436 // promotion rules, so we omit promotion+demotion, which means that we can
3437 // not catch lossy "demotion". Because we still want to catch these cases
3438 // when the sanitizer is enabled, we perform the promotion, then perform
3439 // the increment/decrement in the wider type, and finally
3440 // perform the demotion. This will catch lossy demotions.
3441
3442 // We have a special case for bitfields defined using all the bits of the
3443 // type. In this case we need to do the same trick as for the integer
3444 // sanitizer checks, i.e., promotion -> increment/decrement -> demotion.
3445
3446 value = EmitScalarConversion(value, type, promotedType, E->getExprLoc());
3447 Value *amt = llvm::ConstantInt::get(value->getType(), amount, true);
3448 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
3449 // Do pass non-default ScalarConversionOpts so that sanitizer check is
3450 // emitted if LV is not a bitfield, otherwise the bitfield sanitizer
3451 // checks will take care of the conversion.
3452 ScalarConversionOpts Opts;
3453 if (!LV.isBitField())
3454 Opts = ScalarConversionOpts(CGF.SanOpts);
3455 else if (CGF.SanOpts.has(SanitizerKind::ImplicitBitfieldConversion)) {
3456 Previous = value;
3457 SrcType = promotedType;
3458 }
3459
3460 Opts.PatternExcluded = CGF.getContext().isUnaryOverflowPatternExcluded(E);
3461 value = EmitScalarConversion(value, promotedType, type, E->getExprLoc(),
3462 Opts);
3463
3464 // Note that signed integer inc/dec with width less than int can't
3465 // overflow because of promotion rules; we're just eliding a few steps
3466 // here.
3467 } else if (type->isSignedIntegerOrEnumerationType() ||
3468 type->isUnsignedIntegerType()) {
3469 value = EmitIncDecConsiderOverflowBehavior(E, value, isInc);
3470 } else {
3471 // Treat positive amount as unsigned to support inc of i1 (needed for
3472 // unsigned _BitInt(1)).
3473 llvm::Value *amt =
3474 llvm::ConstantInt::get(value->getType(), amount, !isInc);
3475 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
3476 }
3477
3478 // Next most common: pointer increment.
3479 } else if (const PointerType *ptr = type->getAs<PointerType>()) {
3480 QualType type = ptr->getPointeeType();
3481
3482 // VLA types don't have constant size.
3483 if (const VariableArrayType *vla
3485 llvm::Value *numElts = CGF.getVLASize(vla).NumElts;
3486 if (!isInc) numElts = Builder.CreateNSWNeg(numElts, "vla.negsize");
3487 llvm::Type *elemTy = CGF.ConvertTypeForMem(vla->getElementType());
3488 if (CGF.getLangOpts().PointerOverflowDefined)
3489 value = Builder.CreateGEP(elemTy, value, numElts, "vla.inc");
3490 else
3491 value = CGF.EmitCheckedInBoundsGEP(
3492 elemTy, value, numElts, /*SignedIndices=*/false, isSubtraction,
3493 E->getExprLoc(), "vla.inc");
3494
3495 // Arithmetic on function pointers (!) is just +-1.
3496 } else if (type->isFunctionType()) {
3497 llvm::Value *amt = Builder.getInt32(amount);
3498
3499 if (CGF.getLangOpts().PointerOverflowDefined)
3500 value = Builder.CreateGEP(CGF.Int8Ty, value, amt, "incdec.funcptr");
3501 else
3502 value =
3503 CGF.EmitCheckedInBoundsGEP(CGF.Int8Ty, value, amt,
3504 /*SignedIndices=*/false, isSubtraction,
3505 E->getExprLoc(), "incdec.funcptr");
3506
3507 // For everything else, we can just do a simple increment.
3508 } else {
3509 llvm::Value *amt = Builder.getInt32(amount);
3510 llvm::Type *elemTy = CGF.ConvertTypeForMem(type);
3511 if (CGF.getLangOpts().PointerOverflowDefined)
3512 value = Builder.CreateGEP(elemTy, value, amt, "incdec.ptr");
3513 else
3514 value = CGF.EmitCheckedInBoundsGEP(
3515 elemTy, value, amt, /*SignedIndices=*/false, isSubtraction,
3516 E->getExprLoc(), "incdec.ptr");
3517 }
3518
3519 // Vector increment/decrement.
3520 } else if (type->isVectorType()) {
3521 if (type->hasIntegerRepresentation()) {
3522 llvm::Value *amt = llvm::ConstantInt::getSigned(value->getType(), amount);
3523
3524 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
3525 } else {
3526 value = Builder.CreateFAdd(
3527 value,
3528 llvm::ConstantFP::get(value->getType(), amount),
3529 isInc ? "inc" : "dec");
3530 }
3531
3532 // Floating point.
3533 } else if (type->isRealFloatingType()) {
3534 // Add the inc/dec to the real part.
3535 llvm::Value *amt;
3536 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
3537
3538 if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
3539 // Another special case: half FP increment should be done via float. If
3540 // the input isn't already half, it may be i16.
3541 Value *bitcast = Builder.CreateBitCast(input, CGF.CGM.HalfTy);
3542 value = Builder.CreateFPExt(bitcast, CGF.CGM.FloatTy, "incdec.conv");
3543 }
3544
3545 if (value->getType()->isFloatTy())
3546 amt = llvm::ConstantFP::get(VMContext,
3547 llvm::APFloat(static_cast<float>(amount)));
3548 else if (value->getType()->isDoubleTy())
3549 amt = llvm::ConstantFP::get(VMContext,
3550 llvm::APFloat(static_cast<double>(amount)));
3551 else {
3552 // Remaining types are Half, Bfloat16, LongDouble, __ibm128 or __float128.
3553 // Convert from float.
3554 llvm::APFloat F(static_cast<float>(amount));
3555 bool ignored;
3556 const llvm::fltSemantics *FS;
3557 // Don't use getFloatTypeSemantics because Half isn't
3558 // necessarily represented using the "half" LLVM type.
3559 if (value->getType()->isFP128Ty())
3560 FS = &CGF.getTarget().getFloat128Format();
3561 else if (value->getType()->isHalfTy())
3562 FS = &CGF.getTarget().getHalfFormat();
3563 else if (value->getType()->isBFloatTy())
3564 FS = &CGF.getTarget().getBFloat16Format();
3565 else if (value->getType()->isPPC_FP128Ty())
3566 FS = &CGF.getTarget().getIbm128Format();
3567 else
3568 FS = &CGF.getTarget().getLongDoubleFormat();
3569 F.convert(*FS, llvm::APFloat::rmTowardZero, &ignored);
3570 amt = llvm::ConstantFP::get(VMContext, F);
3571 }
3572 value = Builder.CreateFAdd(value, amt, isInc ? "inc" : "dec");
3573
3574 if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
3575 value = Builder.CreateFPTrunc(value, CGF.CGM.HalfTy, "incdec.conv");
3576 value = Builder.CreateBitCast(value, input->getType());
3577 }
3578
3579 // Fixed-point types.
3580 } else if (type->isFixedPointType()) {
3581 // Fixed-point types are tricky. In some cases, it isn't possible to
3582 // represent a 1 or a -1 in the type at all. Piggyback off of
3583 // EmitFixedPointBinOp to avoid having to reimplement saturation.
3584 BinOpInfo Info;
3585 Info.E = E;
3586 Info.Ty = E->getType();
3587 Info.Opcode = isInc ? BO_Add : BO_Sub;
3588 Info.LHS = value;
3589 Info.RHS = llvm::ConstantInt::get(value->getType(), 1, false);
3590 // If the type is signed, it's better to represent this as +(-1) or -(-1),
3591 // since -1 is guaranteed to be representable.
3592 if (type->isSignedFixedPointType()) {
3593 Info.Opcode = isInc ? BO_Sub : BO_Add;
3594 Info.RHS = Builder.CreateNeg(Info.RHS);
3595 }
3596 // Now, convert from our invented integer literal to the type of the unary
3597 // op. This will upscale and saturate if necessary. This value can become
3598 // undef in some cases.
3599 llvm::FixedPointBuilder<CGBuilderTy> FPBuilder(Builder);
3600 auto DstSema = CGF.getContext().getFixedPointSemantics(Info.Ty);
3601 Info.RHS = FPBuilder.CreateIntegerToFixed(Info.RHS, true, DstSema);
3602 value = EmitFixedPointBinOp(Info);
3603
3604 // Objective-C pointer types.
3605 } else {
3606 const ObjCObjectPointerType *OPT = type->castAs<ObjCObjectPointerType>();
3607
3608 CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType());
3609 if (!isInc) size = -size;
3610 llvm::Value *sizeValue =
3611 llvm::ConstantInt::getSigned(CGF.SizeTy, size.getQuantity());
3612
3613 if (CGF.getLangOpts().PointerOverflowDefined)
3614 value = Builder.CreateGEP(CGF.Int8Ty, value, sizeValue, "incdec.objptr");
3615 else
3616 value = CGF.EmitCheckedInBoundsGEP(
3617 CGF.Int8Ty, value, sizeValue, /*SignedIndices=*/false, isSubtraction,
3618 E->getExprLoc(), "incdec.objptr");
3619 value = Builder.CreateBitCast(value, input->getType());
3620 }
3621
3622 if (atomicPHI) {
3623 llvm::BasicBlock *curBlock = Builder.GetInsertBlock();
3624 llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
3625 auto Pair = CGF.EmitAtomicCompareExchange(
3626 LV, RValue::get(atomicPHI), RValue::get(value), E->getExprLoc());
3627 llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), type);
3628 llvm::Value *success = Pair.second;
3629 atomicPHI->addIncoming(old, curBlock);
3630 Builder.CreateCondBr(success, contBB, atomicPHI->getParent());
3631 Builder.SetInsertPoint(contBB);
3632 return isPre ? value : input;
3633 }
3634
3635 // Store the updated result through the lvalue.
3636 if (LV.isBitField()) {
3637 Value *Src = Previous ? Previous : value;
3638 CGF.EmitStoreThroughBitfieldLValue(RValue::get(value), LV, &value);
3639 CGF.EmitBitfieldConversionCheck(Src, SrcType, value, E->getType(),
3640 LV.getBitFieldInfo(), E->getExprLoc());
3641 } else
3642 CGF.EmitStoreThroughLValue(RValue::get(value), LV);
3643
3644 // If this is a postinc, return the value read from memory, otherwise use the
3645 // updated value.
3646 return isPre ? value : input;
3647}
3648
3649
3650Value *ScalarExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
3651 QualType PromotionType) {
3652 QualType promotionTy = PromotionType.isNull()
3653 ? getPromotionType(E->getSubExpr()->getType())
3654 : PromotionType;
3655 Value *result = VisitPlus(E, promotionTy);
3656 if (result && !promotionTy.isNull())
3657 result = EmitUnPromotedValue(result, E->getType());
3658 return result;
3659}
3660
3661Value *ScalarExprEmitter::VisitPlus(const UnaryOperator *E,
3662 QualType PromotionType) {
3663 // This differs from gcc, though, most likely due to a bug in gcc.
3664 TestAndClearIgnoreResultAssign();
3665 if (!PromotionType.isNull())
3666 return CGF.EmitPromotedScalarExpr(E->getSubExpr(), PromotionType);
3667 return Visit(E->getSubExpr());
3668}
3669
3670Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
3671 QualType PromotionType) {
3672 QualType promotionTy = PromotionType.isNull()
3673 ? getPromotionType(E->getSubExpr()->getType())
3674 : PromotionType;
3675 Value *result = VisitMinus(E, promotionTy);
3676 if (result && !promotionTy.isNull())
3677 result = EmitUnPromotedValue(result, E->getType());
3678 return result;
3679}
3680
3681Value *ScalarExprEmitter::VisitMinus(const UnaryOperator *E,
3682 QualType PromotionType) {
3683 TestAndClearIgnoreResultAssign();
3684 Value *Op;
3685 if (!PromotionType.isNull())
3686 Op = CGF.EmitPromotedScalarExpr(E->getSubExpr(), PromotionType);
3687 else
3688 Op = Visit(E->getSubExpr());
3689
3690 // Generate a unary FNeg for FP ops.
3691 if (Op->getType()->isFPOrFPVectorTy())
3692 return Builder.CreateFNeg(Op, "fneg");
3693
3694 // Emit unary minus with EmitSub so we handle overflow cases etc.
3695 BinOpInfo BinOp;
3696 BinOp.RHS = Op;
3697 BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType());
3698 BinOp.Ty = E->getType();
3699 BinOp.Opcode = BO_Sub;
3700 BinOp.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
3701 BinOp.E = E;
3702 return EmitSub(BinOp);
3703}
3704
3705Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
3706 TestAndClearIgnoreResultAssign();
3707 Value *Op = Visit(E->getSubExpr());
3708 return Builder.CreateNot(Op, "not");
3709}
3710
3711Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
3712 // Perform vector logical not on comparison with zero vector.
3713 if (E->getType()->isVectorType() &&
3714 E->getType()->castAs<VectorType>()->getVectorKind() ==
3715 VectorKind::Generic) {
3716 Value *Oper = Visit(E->getSubExpr());
3717 Value *Zero = llvm::Constant::getNullValue(Oper->getType());
3718 Value *Result;
3719 if (Oper->getType()->isFPOrFPVectorTy()) {
3720 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(
3721 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts()));
3722 Result = Builder.CreateFCmp(llvm::CmpInst::FCMP_OEQ, Oper, Zero, "cmp");
3723 } else
3724 Result = Builder.CreateICmp(llvm::CmpInst::ICMP_EQ, Oper, Zero, "cmp");
3725 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
3726 }
3727
3728 // Compare operand to zero.
3729 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
3730
3731 // Invert value.
3732 // TODO: Could dynamically modify easy computations here. For example, if
3733 // the operand is an icmp ne, turn into icmp eq.
3734 BoolVal = Builder.CreateNot(BoolVal, "lnot");
3735
3736 // ZExt result to the expr type.
3737 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext");
3738}
3739
3740Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) {
3741 // Try folding the offsetof to a constant.
3742 Expr::EvalResult EVResult;
3743 if (E->EvaluateAsInt(EVResult, CGF.getContext())) {
3744 llvm::APSInt Value = EVResult.Val.getInt();
3745 return Builder.getInt(Value);
3746 }
3747
3748 // Loop over the components of the offsetof to compute the value.
3749 unsigned n = E->getNumComponents();
3750 llvm::Type* ResultType = ConvertType(E->getType());
3751 llvm::Value* Result = llvm::Constant::getNullValue(ResultType);
3752 QualType CurrentType = E->getTypeSourceInfo()->getType();
3753 for (unsigned i = 0; i != n; ++i) {
3754 OffsetOfNode ON = E->getComponent(i);
3755 llvm::Value *Offset = nullptr;
3756 switch (ON.getKind()) {
3757 case OffsetOfNode::Array: {
3758 // Compute the index
3759 Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex());
3760 llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr);
3761 bool IdxSigned = IdxExpr->getType()->isSignedIntegerOrEnumerationType();
3762 Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv");
3763
3764 // Save the element type
3765 CurrentType =
3766 CGF.getContext().getAsArrayType(CurrentType)->getElementType();
3767
3768 // Compute the element size
3769 llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType,
3770 CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity());
3771
3772 // Multiply out to compute the result
3773 Offset = Builder.CreateMul(Idx, ElemSize);
3774 break;
3775 }
3776
3777 case OffsetOfNode::Field: {
3778 FieldDecl *MemberDecl = ON.getField();
3779 auto *RD = CurrentType->castAsRecordDecl();
3780 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD);
3781
3782 // Get the index of the field in its parent.
3783 unsigned FieldIndex = MemberDecl->getFieldIndex();
3784
3785 // Compute the offset to the field
3786 int64_t OffsetInt =
3787 RL.getFieldOffset(FieldIndex) / CGF.getContext().getCharWidth();
3788 Offset = llvm::ConstantInt::get(ResultType, OffsetInt);
3789
3790 // Save the element type.
3791 CurrentType = MemberDecl->getType();
3792 break;
3793 }
3794
3796 llvm_unreachable("dependent __builtin_offsetof");
3797
3798 case OffsetOfNode::Base: {
3799 if (ON.getBase()->isVirtual()) {
3800 CGF.ErrorUnsupported(E, "virtual base in offsetof");
3801 continue;
3802 }
3803
3804 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(
3805 CurrentType->castAsCanonical<RecordType>()->getDecl());
3806
3807 // Save the element type.
3808 CurrentType = ON.getBase()->getType();
3809
3810 // Compute the offset to the base.
3811 auto *BaseRD = CurrentType->castAsCXXRecordDecl();
3812 CharUnits OffsetInt = RL.getBaseClassOffset(BaseRD);
3813 Offset = llvm::ConstantInt::get(ResultType, OffsetInt.getQuantity());
3814 break;
3815 }
3816 }
3817 Result = Builder.CreateAdd(Result, Offset);
3818 }
3819 return Result;
3820}
3821
3822/// VisitUnaryExprOrTypeTraitExpr - Return the size or alignment of the type of
3823/// argument of the sizeof expression as an integer.
3824Value *
3825ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
3826 const UnaryExprOrTypeTraitExpr *E) {
3827 QualType TypeToSize = E->getTypeOfArgument();
3828 if (auto Kind = E->getKind();
3829 Kind == UETT_SizeOf || Kind == UETT_DataSizeOf || Kind == UETT_CountOf) {
3830 if (const VariableArrayType *VAT =
3831 CGF.getContext().getAsVariableArrayType(TypeToSize)) {
3832 // For _Countof, we only want to evaluate if the extent is actually
3833 // variable as opposed to a multi-dimensional array whose extent is
3834 // constant but whose element type is variable.
3835 bool EvaluateExtent = true;
3836 if (Kind == UETT_CountOf && VAT->getElementType()->isArrayType()) {
3837 EvaluateExtent =
3838 !VAT->getSizeExpr()->isIntegerConstantExpr(CGF.getContext());
3839 }
3840 if (EvaluateExtent) {
3841 if (E->isArgumentType()) {
3842 // sizeof(type) - make sure to emit the VLA size.
3843 CGF.EmitVariablyModifiedType(TypeToSize);
3844 } else {
3845 // C99 6.5.3.4p2: If the argument is an expression of type
3846 // VLA, it is evaluated.
3848 }
3849
3850 // For _Countof, we just want to return the size of a single dimension.
3851 if (Kind == UETT_CountOf)
3852 return CGF.getVLAElements1D(VAT).NumElts;
3853
3854 // For sizeof and __datasizeof, we need to scale the number of elements
3855 // by the size of the array element type.
3856 auto VlaSize = CGF.getVLASize(VAT);
3857
3858 // Scale the number of non-VLA elements by the non-VLA element size.
3859 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(VlaSize.Type);
3860 if (!eltSize.isOne())
3861 return CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize),
3862 VlaSize.NumElts);
3863 return VlaSize.NumElts;
3864 }
3865 }
3866 } else if (E->getKind() == UETT_OpenMPRequiredSimdAlign) {
3867 auto Alignment =
3868 CGF.getContext()
3871 .getQuantity();
3872 return llvm::ConstantInt::get(CGF.SizeTy, Alignment);
3873 } else if (E->getKind() == UETT_VectorElements) {
3874 auto *VecTy = cast<llvm::VectorType>(ConvertType(E->getTypeOfArgument()));
3875 return Builder.CreateElementCount(CGF.SizeTy, VecTy->getElementCount());
3876 }
3877
3878 // If this isn't sizeof(vla), the result must be constant; use the constant
3879 // folding logic so we don't have to duplicate it here.
3880 return Builder.getInt(E->EvaluateKnownConstInt(CGF.getContext()));
3881}
3882
3883Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E,
3884 QualType PromotionType) {
3885 QualType promotionTy = PromotionType.isNull()
3886 ? getPromotionType(E->getSubExpr()->getType())
3887 : PromotionType;
3888 Value *result = VisitReal(E, promotionTy);
3889 if (result && !promotionTy.isNull())
3890 result = EmitUnPromotedValue(result, E->getType());
3891 return result;
3892}
3893
3894Value *ScalarExprEmitter::VisitReal(const UnaryOperator *E,
3895 QualType PromotionType) {
3896 Expr *Op = E->getSubExpr();
3897 if (Op->getType()->isAnyComplexType()) {
3898 // If it's an l-value, load through the appropriate subobject l-value.
3899 // Note that we have to ask E because Op might be an l-value that
3900 // this won't work for, e.g. an Obj-C property.
3901 if (E->isGLValue()) {
3902 if (!PromotionType.isNull()) {
3904 Op, /*IgnoreReal*/ IgnoreResultAssign, /*IgnoreImag*/ true);
3905 PromotionType = PromotionType->isAnyComplexType()
3906 ? PromotionType
3907 : CGF.getContext().getComplexType(PromotionType);
3908 return result.first ? CGF.EmitPromotedValue(result, PromotionType).first
3909 : result.first;
3910 }
3911
3912 return CGF.EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc())
3913 .getScalarVal();
3914 }
3915 // Otherwise, calculate and project.
3916 return CGF.EmitComplexExpr(Op, false, true).first;
3917 }
3918
3919 if (!PromotionType.isNull())
3920 return CGF.EmitPromotedScalarExpr(Op, PromotionType);
3921 return Visit(Op);
3922}
3923
3924Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E,
3925 QualType PromotionType) {
3926 QualType promotionTy = PromotionType.isNull()
3927 ? getPromotionType(E->getSubExpr()->getType())
3928 : PromotionType;
3929 Value *result = VisitImag(E, promotionTy);
3930 if (result && !promotionTy.isNull())
3931 result = EmitUnPromotedValue(result, E->getType());
3932 return result;
3933}
3934
3935Value *ScalarExprEmitter::VisitImag(const UnaryOperator *E,
3936 QualType PromotionType) {
3937 Expr *Op = E->getSubExpr();
3938 if (Op->getType()->isAnyComplexType()) {
3939 // If it's an l-value, load through the appropriate subobject l-value.
3940 // Note that we have to ask E because Op might be an l-value that
3941 // this won't work for, e.g. an Obj-C property.
3942 if (Op->isGLValue()) {
3943 if (!PromotionType.isNull()) {
3945 Op, /*IgnoreReal*/ true, /*IgnoreImag*/ IgnoreResultAssign);
3946 PromotionType = PromotionType->isAnyComplexType()
3947 ? PromotionType
3948 : CGF.getContext().getComplexType(PromotionType);
3949 return result.second
3950 ? CGF.EmitPromotedValue(result, PromotionType).second
3951 : result.second;
3952 }
3953
3954 return CGF.EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc())
3955 .getScalarVal();
3956 }
3957 // Otherwise, calculate and project.
3958 return CGF.EmitComplexExpr(Op, true, false).second;
3959 }
3960
3961 // __imag on a scalar returns zero. Emit the subexpr to ensure side
3962 // effects are evaluated, but not the actual value.
3963 if (Op->isGLValue())
3964 CGF.EmitLValue(Op);
3965 else if (!PromotionType.isNull())
3966 CGF.EmitPromotedScalarExpr(Op, PromotionType);
3967 else
3968 CGF.EmitScalarExpr(Op, true);
3969 if (!PromotionType.isNull())
3970 return llvm::Constant::getNullValue(ConvertType(PromotionType));
3971 return llvm::Constant::getNullValue(ConvertType(E->getType()));
3972}
3973
3974//===----------------------------------------------------------------------===//
3975// Binary Operators
3976//===----------------------------------------------------------------------===//
3977
3978Value *ScalarExprEmitter::EmitPromotedValue(Value *result,
3979 QualType PromotionType) {
3980 return CGF.Builder.CreateFPExt(result, ConvertType(PromotionType), "ext");
3981}
3982
3983Value *ScalarExprEmitter::EmitUnPromotedValue(Value *result,
3984 QualType ExprType) {
3985 return CGF.Builder.CreateFPTrunc(result, ConvertType(ExprType), "unpromotion");
3986}
3987
3988Value *ScalarExprEmitter::EmitPromoted(const Expr *E, QualType PromotionType) {
3989 E = E->IgnoreParens();
3990 if (auto BO = dyn_cast<BinaryOperator>(E)) {
3991 switch (BO->getOpcode()) {
3992#define HANDLE_BINOP(OP) \
3993 case BO_##OP: \
3994 return Emit##OP(EmitBinOps(BO, PromotionType));
3995 HANDLE_BINOP(Add)
3996 HANDLE_BINOP(Sub)
3997 HANDLE_BINOP(Mul)
3998 HANDLE_BINOP(Div)
3999#undef HANDLE_BINOP
4000 default:
4001 break;
4002 }
4003 } else if (auto UO = dyn_cast<UnaryOperator>(E)) {
4004 switch (UO->getOpcode()) {
4005 case UO_Imag:
4006 return VisitImag(UO, PromotionType);
4007 case UO_Real:
4008 return VisitReal(UO, PromotionType);
4009 case UO_Minus:
4010 return VisitMinus(UO, PromotionType);
4011 case UO_Plus:
4012 return VisitPlus(UO, PromotionType);
4013 default:
4014 break;
4015 }
4016 }
4017 auto result = Visit(const_cast<Expr *>(E));
4018 if (result) {
4019 if (!PromotionType.isNull())
4020 return EmitPromotedValue(result, PromotionType);
4021 else
4022 return EmitUnPromotedValue(result, E->getType());
4023 }
4024 return result;
4025}
4026
4027BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E,
4028 QualType PromotionType) {
4029 TestAndClearIgnoreResultAssign();
4030 BinOpInfo Result;
4031 Result.LHS = CGF.EmitPromotedScalarExpr(E->getLHS(), PromotionType);
4032 Result.RHS = CGF.EmitPromotedScalarExpr(E->getRHS(), PromotionType);
4033 if (!PromotionType.isNull())
4034 Result.Ty = PromotionType;
4035 else
4036 Result.Ty = E->getType();
4037 Result.Opcode = E->getOpcode();
4038 Result.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
4039 Result.E = E;
4040 return Result;
4041}
4042
4043LValue ScalarExprEmitter::EmitCompoundAssignLValue(
4044 const CompoundAssignOperator *E,
4045 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &),
4046 Value *&Result) {
4047 QualType LHSTy = E->getLHS()->getType();
4048 BinOpInfo OpInfo;
4049
4052
4053 // Emit the RHS first. __block variables need to have the rhs evaluated
4054 // first, plus this should improve codegen a little.
4055
4056 QualType PromotionTypeCR;
4057 PromotionTypeCR = getPromotionType(E->getComputationResultType());
4058 if (PromotionTypeCR.isNull())
4059 PromotionTypeCR = E->getComputationResultType();
4060 QualType PromotionTypeLHS = getPromotionType(E->getComputationLHSType());
4061 QualType PromotionTypeRHS = getPromotionType(E->getRHS()->getType());
4062 if (!PromotionTypeRHS.isNull())
4063 OpInfo.RHS = CGF.EmitPromotedScalarExpr(E->getRHS(), PromotionTypeRHS);
4064 else
4065 OpInfo.RHS = Visit(E->getRHS());
4066 OpInfo.Ty = PromotionTypeCR;
4067 OpInfo.Opcode = E->getOpcode();
4068 OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
4069 OpInfo.E = E;
4070 // Load/convert the LHS.
4071 LValue LHSLV = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
4072
4073 llvm::PHINode *atomicPHI = nullptr;
4074 if (const AtomicType *atomicTy = LHSTy->getAs<AtomicType>()) {
4075 QualType type = atomicTy->getValueType();
4076 if (!type->isBooleanType() && type->isIntegerType() &&
4077 !(type->isUnsignedIntegerType() &&
4078 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
4079 CGF.getLangOpts().getSignedOverflowBehavior() !=
4080 LangOptions::SOB_Trapping) {
4081 llvm::AtomicRMWInst::BinOp AtomicOp = llvm::AtomicRMWInst::BAD_BINOP;
4082 llvm::Instruction::BinaryOps Op;
4083 switch (OpInfo.Opcode) {
4084 // We don't have atomicrmw operands for *, %, /, <<, >>
4085 case BO_MulAssign: case BO_DivAssign:
4086 case BO_RemAssign:
4087 case BO_ShlAssign:
4088 case BO_ShrAssign:
4089 break;
4090 case BO_AddAssign:
4091 AtomicOp = llvm::AtomicRMWInst::Add;
4092 Op = llvm::Instruction::Add;
4093 break;
4094 case BO_SubAssign:
4095 AtomicOp = llvm::AtomicRMWInst::Sub;
4096 Op = llvm::Instruction::Sub;
4097 break;
4098 case BO_AndAssign:
4099 AtomicOp = llvm::AtomicRMWInst::And;
4100 Op = llvm::Instruction::And;
4101 break;
4102 case BO_XorAssign:
4103 AtomicOp = llvm::AtomicRMWInst::Xor;
4104 Op = llvm::Instruction::Xor;
4105 break;
4106 case BO_OrAssign:
4107 AtomicOp = llvm::AtomicRMWInst::Or;
4108 Op = llvm::Instruction::Or;
4109 break;
4110 default:
4111 llvm_unreachable("Invalid compound assignment type");
4112 }
4113 if (AtomicOp != llvm::AtomicRMWInst::BAD_BINOP) {
4114 llvm::Value *Amt = CGF.EmitToMemory(
4115 EmitScalarConversion(OpInfo.RHS, E->getRHS()->getType(), LHSTy,
4116 E->getExprLoc()),
4117 LHSTy);
4118
4119 llvm::AtomicRMWInst *OldVal =
4120 CGF.emitAtomicRMWInst(AtomicOp, LHSLV.getAddress(), Amt);
4121
4122 // Since operation is atomic, the result type is guaranteed to be the
4123 // same as the input in LLVM terms.
4124 Result = Builder.CreateBinOp(Op, OldVal, Amt);
4125 return LHSLV;
4126 }
4127 }
4128 // FIXME: For floating point types, we should be saving and restoring the
4129 // floating point environment in the loop.
4130 llvm::BasicBlock *startBB = Builder.GetInsertBlock();
4131 llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn);
4132 OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc());
4133 OpInfo.LHS = CGF.EmitToMemory(OpInfo.LHS, type);
4134 Builder.CreateBr(opBB);
4135 Builder.SetInsertPoint(opBB);
4136 atomicPHI = Builder.CreatePHI(OpInfo.LHS->getType(), 2);
4137 atomicPHI->addIncoming(OpInfo.LHS, startBB);
4138 OpInfo.LHS = atomicPHI;
4139 }
4140 else
4141 OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc());
4142
4143 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures);
4144 SourceLocation Loc = E->getExprLoc();
4145 if (!PromotionTypeLHS.isNull())
4146 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, PromotionTypeLHS,
4147 E->getExprLoc());
4148 else
4149 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
4150 E->getComputationLHSType(), Loc);
4151
4152 // Expand the binary operator.
4153 Result = (this->*Func)(OpInfo);
4154
4155 // Convert the result back to the LHS type,
4156 // potentially with Implicit Conversion sanitizer check.
4157 // If LHSLV is a bitfield, use default ScalarConversionOpts
4158 // to avoid emit any implicit integer checks.
4159 Value *Previous = nullptr;
4160 if (LHSLV.isBitField()) {
4161 Previous = Result;
4162 Result = EmitScalarConversion(Result, PromotionTypeCR, LHSTy, Loc);
4163 } else if (const auto *atomicTy = LHSTy->getAs<AtomicType>()) {
4164 Result =
4165 EmitScalarConversion(Result, PromotionTypeCR, atomicTy->getValueType(),
4166 Loc, ScalarConversionOpts(CGF.SanOpts));
4167 } else {
4168 Result = EmitScalarConversion(Result, PromotionTypeCR, LHSTy, Loc,
4169 ScalarConversionOpts(CGF.SanOpts));
4170 }
4171
4172 if (atomicPHI) {
4173 llvm::BasicBlock *curBlock = Builder.GetInsertBlock();
4174 llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
4175 auto Pair = CGF.EmitAtomicCompareExchange(
4176 LHSLV, RValue::get(atomicPHI), RValue::get(Result), E->getExprLoc());
4177 llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), LHSTy);
4178 llvm::Value *success = Pair.second;
4179 atomicPHI->addIncoming(old, curBlock);
4180 Builder.CreateCondBr(success, contBB, atomicPHI->getParent());
4181 Builder.SetInsertPoint(contBB);
4182 return LHSLV;
4183 }
4184
4185 // Store the result value into the LHS lvalue. Bit-fields are handled
4186 // specially because the result is altered by the store, i.e., [C99 6.5.16p1]
4187 // 'An assignment expression has the value of the left operand after the
4188 // assignment...'.
4189 if (LHSLV.isBitField()) {
4190 Value *Src = Previous ? Previous : Result;
4191 QualType SrcType = E->getRHS()->getType();
4192 QualType DstType = E->getLHS()->getType();
4194 CGF.EmitBitfieldConversionCheck(Src, SrcType, Result, DstType,
4195 LHSLV.getBitFieldInfo(), E->getExprLoc());
4196 } else
4198
4199 if (CGF.getLangOpts().OpenMP)
4201 E->getLHS());
4202 return LHSLV;
4203}
4204
4205Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
4206 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
4207 bool Ignore = TestAndClearIgnoreResultAssign();
4208 Value *RHS = nullptr;
4209 LValue LHS = EmitCompoundAssignLValue(E, Func, RHS);
4210
4211 // If the result is clearly ignored, return now.
4212 if (Ignore)
4213 return nullptr;
4214
4215 // The result of an assignment in C is the assigned r-value.
4216 if (!CGF.getLangOpts().CPlusPlus)
4217 return RHS;
4218
4219 // If the lvalue is non-volatile, return the computed value of the assignment.
4220 if (!LHS.isVolatileQualified())
4221 return RHS;
4222
4223 // Otherwise, reload the value.
4224 return EmitLoadOfLValue(LHS, E->getExprLoc());
4225}
4226
4227void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
4228 const BinOpInfo &Ops, llvm::Value *Zero, bool isDiv) {
4229 SmallVector<std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>, 2>
4230 Checks;
4231
4232 if (CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero)) {
4233 Checks.push_back(std::make_pair(Builder.CreateICmpNE(Ops.RHS, Zero),
4234 SanitizerKind::SO_IntegerDivideByZero));
4235 }
4236
4237 const auto *BO = cast<BinaryOperator>(Ops.E);
4238 if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow) &&
4239 Ops.Ty->hasSignedIntegerRepresentation() &&
4240 !IsWidenedIntegerOp(CGF.getContext(), BO->getLHS()) &&
4241 Ops.mayHaveIntegerOverflow() &&
4243 SanitizerKind::SignedIntegerOverflow, Ops.Ty)) {
4244 llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType());
4245
4246 llvm::Value *IntMin =
4247 Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth()));
4248 llvm::Value *NegOne = llvm::Constant::getAllOnesValue(Ty);
4249
4250 llvm::Value *LHSCmp = Builder.CreateICmpNE(Ops.LHS, IntMin);
4251 llvm::Value *RHSCmp = Builder.CreateICmpNE(Ops.RHS, NegOne);
4252 llvm::Value *NotOverflow = Builder.CreateOr(LHSCmp, RHSCmp, "or");
4253 Checks.push_back(
4254 std::make_pair(NotOverflow, SanitizerKind::SO_SignedIntegerOverflow));
4255 }
4256
4257 if (Checks.size() > 0)
4258 EmitBinOpCheck(Checks, Ops);
4259}
4260
4261Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
4262 {
4263 SanitizerDebugLocation SanScope(&CGF,
4264 {SanitizerKind::SO_IntegerDivideByZero,
4265 SanitizerKind::SO_SignedIntegerOverflow,
4266 SanitizerKind::SO_FloatDivideByZero},
4267 SanitizerHandler::DivremOverflow);
4268 if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) ||
4269 CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) &&
4270 Ops.Ty->isIntegerType() &&
4271 (Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) {
4272 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
4273 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true);
4274 } else if (CGF.SanOpts.has(SanitizerKind::FloatDivideByZero) &&
4275 Ops.Ty->isRealFloatingType() &&
4276 Ops.mayHaveFloatDivisionByZero()) {
4277 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
4278 llvm::Value *NonZero = Builder.CreateFCmpUNE(Ops.RHS, Zero);
4279 EmitBinOpCheck(
4280 std::make_pair(NonZero, SanitizerKind::SO_FloatDivideByZero), Ops);
4281 }
4282 }
4283
4284 if (Ops.Ty->isConstantMatrixType()) {
4285 llvm::MatrixBuilder MB(Builder);
4286 // We need to check the types of the operands of the operator to get the
4287 // correct matrix dimensions.
4288 auto *BO = cast<BinaryOperator>(Ops.E);
4289 (void)BO;
4290 assert(
4292 "first operand must be a matrix");
4293 assert(BO->getRHS()->getType().getCanonicalType()->isArithmeticType() &&
4294 "second operand must be an arithmetic type");
4295 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures);
4296 return MB.CreateScalarDiv(Ops.LHS, Ops.RHS,
4297 Ops.Ty->hasUnsignedIntegerRepresentation());
4298 }
4299
4300 if (Ops.LHS->getType()->isFPOrFPVectorTy()) {
4301 llvm::Value *Val;
4302 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures);
4303 Val = Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
4304 CGF.SetDivFPAccuracy(Val);
4305 return Val;
4306 }
4307 else if (Ops.isFixedPointOp())
4308 return EmitFixedPointBinOp(Ops);
4309 else if (Ops.Ty->hasUnsignedIntegerRepresentation())
4310 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
4311 else
4312 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
4313}
4314
4315Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
4316 // Rem in C can't be a floating point type: C99 6.5.5p2.
4317 if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) ||
4318 CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) &&
4319 Ops.Ty->isIntegerType() &&
4320 (Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) {
4321 SanitizerDebugLocation SanScope(&CGF,
4322 {SanitizerKind::SO_IntegerDivideByZero,
4323 SanitizerKind::SO_SignedIntegerOverflow},
4324 SanitizerHandler::DivremOverflow);
4325 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
4326 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false);
4327 }
4328
4329 if (Ops.Ty->hasUnsignedIntegerRepresentation())
4330 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
4331
4332 if (CGF.getLangOpts().HLSL && Ops.Ty->hasFloatingRepresentation())
4333 return Builder.CreateFRem(Ops.LHS, Ops.RHS, "rem");
4334
4335 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
4336}
4337
4338Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
4339 unsigned IID;
4340 unsigned OpID = 0;
4341 SanitizerHandler OverflowKind;
4342
4343 bool isSigned = Ops.Ty->isSignedIntegerOrEnumerationType();
4344 switch (Ops.Opcode) {
4345 case BO_Add:
4346 case BO_AddAssign:
4347 OpID = 1;
4348 IID = isSigned ? llvm::Intrinsic::sadd_with_overflow :
4349 llvm::Intrinsic::uadd_with_overflow;
4350 OverflowKind = SanitizerHandler::AddOverflow;
4351 break;
4352 case BO_Sub:
4353 case BO_SubAssign:
4354 OpID = 2;
4355 IID = isSigned ? llvm::Intrinsic::ssub_with_overflow :
4356 llvm::Intrinsic::usub_with_overflow;
4357 OverflowKind = SanitizerHandler::SubOverflow;
4358 break;
4359 case BO_Mul:
4360 case BO_MulAssign:
4361 OpID = 3;
4362 IID = isSigned ? llvm::Intrinsic::smul_with_overflow :
4363 llvm::Intrinsic::umul_with_overflow;
4364 OverflowKind = SanitizerHandler::MulOverflow;
4365 break;
4366 default:
4367 llvm_unreachable("Unsupported operation for overflow detection");
4368 }
4369 OpID <<= 1;
4370 if (isSigned)
4371 OpID |= 1;
4372
4373 SanitizerDebugLocation SanScope(&CGF,
4374 {SanitizerKind::SO_SignedIntegerOverflow,
4375 SanitizerKind::SO_UnsignedIntegerOverflow},
4376 OverflowKind);
4377 llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
4378
4379 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, opTy);
4380
4381 Value *resultAndOverflow = Builder.CreateCall(intrinsic, {Ops.LHS, Ops.RHS});
4382 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
4383 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
4384
4385 // Handle overflow with llvm.trap if no custom handler has been specified.
4386 const std::string *handlerName =
4388 if (handlerName->empty()) {
4389 // If no -ftrapv handler has been specified, try to use sanitizer runtimes
4390 // if available otherwise just emit a trap. It is possible for unsigned
4391 // arithmetic to result in a trap due to the OverflowBehaviorType attribute
4392 // which describes overflow behavior on a per-type basis.
4393 if (isSigned) {
4394 if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) {
4395 llvm::Value *NotOf = Builder.CreateNot(overflow);
4396 EmitBinOpCheck(
4397 std::make_pair(NotOf, SanitizerKind::SO_SignedIntegerOverflow),
4398 Ops);
4399 } else
4400 CGF.EmitTrapCheck(Builder.CreateNot(overflow), OverflowKind);
4401 return result;
4402 }
4403 if (CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) {
4404 llvm::Value *NotOf = Builder.CreateNot(overflow);
4405 EmitBinOpCheck(
4406 std::make_pair(NotOf, SanitizerKind::SO_UnsignedIntegerOverflow),
4407 Ops);
4408 } else
4409 CGF.EmitTrapCheck(Builder.CreateNot(overflow), OverflowKind);
4410 return result;
4411 }
4412
4413 // Branch in case of overflow.
4414 llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
4415 llvm::BasicBlock *continueBB =
4416 CGF.createBasicBlock("nooverflow", CGF.CurFn, initialBB->getNextNode());
4417 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn);
4418
4419 Builder.CreateCondBr(overflow, overflowBB, continueBB);
4420
4421 // If an overflow handler is set, then we want to call it and then use its
4422 // result, if it returns.
4423 Builder.SetInsertPoint(overflowBB);
4424
4425 // Get the overflow handler.
4426 llvm::Type *Int8Ty = CGF.Int8Ty;
4427 llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty };
4428 llvm::FunctionType *handlerTy =
4429 llvm::FunctionType::get(CGF.Int64Ty, argTypes, true);
4430 llvm::FunctionCallee handler =
4431 CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName);
4432
4433 // Sign extend the args to 64-bit, so that we can use the same handler for
4434 // all types of overflow.
4435 llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty);
4436 llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty);
4437
4438 // Call the handler with the two arguments, the operation, and the size of
4439 // the result.
4440 llvm::Value *handlerArgs[] = {
4441 lhs,
4442 rhs,
4443 Builder.getInt8(OpID),
4444 Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth())
4445 };
4446 llvm::Value *handlerResult =
4447 CGF.EmitNounwindRuntimeCall(handler, handlerArgs);
4448
4449 // Truncate the result back to the desired size.
4450 handlerResult = Builder.CreateTrunc(handlerResult, opTy);
4451 Builder.CreateBr(continueBB);
4452
4453 Builder.SetInsertPoint(continueBB);
4454 llvm::PHINode *phi = Builder.CreatePHI(opTy, 2);
4455 phi->addIncoming(result, initialBB);
4456 phi->addIncoming(handlerResult, overflowBB);
4457
4458 return phi;
4459}
4460
4461/// BO_Add/BO_Sub are handled by EmitPointerWithAlignment to preserve alignment
4462/// information.
4463/// This function is used for BO_AddAssign/BO_SubAssign.
4464static Value *emitPointerArithmetic(CodeGenFunction &CGF, const BinOpInfo &op,
4465 bool isSubtraction) {
4466 // Must have binary (not unary) expr here. Unary pointer
4467 // increment/decrement doesn't use this path.
4469
4470 Value *pointer = op.LHS;
4471 Expr *pointerOperand = expr->getLHS();
4472 Value *index = op.RHS;
4473 Expr *indexOperand = expr->getRHS();
4474
4475 // In a subtraction, the LHS is always the pointer.
4476 if (!isSubtraction && !pointer->getType()->isPointerTy()) {
4477 std::swap(pointer, index);
4478 std::swap(pointerOperand, indexOperand);
4479 }
4480
4481 return CGF.EmitPointerArithmetic(expr, pointerOperand, pointer, indexOperand,
4482 index, isSubtraction);
4483}
4484
4485/// Emit pointer + index arithmetic.
4487 const BinaryOperator *BO, Expr *pointerOperand, llvm::Value *pointer,
4488 Expr *indexOperand, llvm::Value *index, bool isSubtraction) {
4489 bool isSigned = indexOperand->getType()->isSignedIntegerOrEnumerationType();
4490
4491 unsigned width = cast<llvm::IntegerType>(index->getType())->getBitWidth();
4492 auto &DL = CGM.getDataLayout();
4493 auto *PtrTy = cast<llvm::PointerType>(pointer->getType());
4494
4495 // Some versions of glibc and gcc use idioms (particularly in their malloc
4496 // routines) that add a pointer-sized integer (known to be a pointer value)
4497 // to a null pointer in order to cast the value back to an integer or as
4498 // part of a pointer alignment algorithm. This is undefined behavior, but
4499 // we'd like to be able to compile programs that use it.
4500 //
4501 // Normally, we'd generate a GEP with a null-pointer base here in response
4502 // to that code, but it's also UB to dereference a pointer created that
4503 // way. Instead (as an acknowledged hack to tolerate the idiom) we will
4504 // generate a direct cast of the integer value to a pointer.
4505 //
4506 // The idiom (p = nullptr + N) is not met if any of the following are true:
4507 //
4508 // The operation is subtraction.
4509 // The index is not pointer-sized.
4510 // The pointer type is not byte-sized.
4511 //
4512 // Note that we do not suppress the pointer overflow check in this case.
4514 getContext(), BO->getOpcode(), pointerOperand, indexOperand)) {
4515 llvm::Value *Ptr = Builder.CreateIntToPtr(index, pointer->getType());
4516 if (getLangOpts().PointerOverflowDefined ||
4517 !SanOpts.has(SanitizerKind::PointerOverflow) ||
4518 NullPointerIsDefined(Builder.GetInsertBlock()->getParent(),
4519 PtrTy->getPointerAddressSpace()))
4520 return Ptr;
4521 // The inbounds GEP of null is valid iff the index is zero.
4522 auto CheckOrdinal = SanitizerKind::SO_PointerOverflow;
4523 auto CheckHandler = SanitizerHandler::PointerOverflow;
4524 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
4525 llvm::Value *IsZeroIndex = Builder.CreateIsNull(index);
4526 llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(BO->getExprLoc())};
4527 llvm::Type *IntPtrTy = DL.getIntPtrType(PtrTy);
4528 llvm::Value *IntPtr = llvm::Constant::getNullValue(IntPtrTy);
4529 llvm::Value *ComputedGEP = Builder.CreateZExtOrTrunc(index, IntPtrTy);
4530 llvm::Value *DynamicArgs[] = {IntPtr, ComputedGEP};
4531 EmitCheck({{IsZeroIndex, CheckOrdinal}}, CheckHandler, StaticArgs,
4532 DynamicArgs);
4533 return Ptr;
4534 }
4535
4536 if (width != DL.getIndexTypeSizeInBits(PtrTy)) {
4537 // Zero-extend or sign-extend the pointer value according to
4538 // whether the index is signed or not.
4539 index = Builder.CreateIntCast(index, DL.getIndexType(PtrTy), isSigned,
4540 "idx.ext");
4541 }
4542
4543 // If this is subtraction, negate the index.
4544 if (isSubtraction)
4545 index = Builder.CreateNeg(index, "idx.neg");
4546
4547 if (SanOpts.has(SanitizerKind::ArrayBounds))
4548 EmitBoundsCheck(BO, pointerOperand, index, indexOperand->getType(),
4549 /*Accessed*/ false);
4550
4551 const PointerType *pointerType =
4552 pointerOperand->getType()->getAs<PointerType>();
4553 if (!pointerType) {
4554 QualType objectType = pointerOperand->getType()
4556 ->getPointeeType();
4557 llvm::Value *objectSize =
4558 CGM.getSize(getContext().getTypeSizeInChars(objectType));
4559
4560 index = Builder.CreateMul(index, objectSize);
4561
4562 llvm::Value *result = Builder.CreateGEP(Int8Ty, pointer, index, "add.ptr");
4563 return Builder.CreateBitCast(result, pointer->getType());
4564 }
4565
4566 QualType elementType = pointerType->getPointeeType();
4567 if (const VariableArrayType *vla =
4568 getContext().getAsVariableArrayType(elementType)) {
4569 // The element count here is the total number of non-VLA elements.
4570 llvm::Value *numElements = getVLASize(vla).NumElts;
4571
4572 // Effectively, the multiply by the VLA size is part of the GEP.
4573 // GEP indexes are signed, and scaling an index isn't permitted to
4574 // signed-overflow, so we use the same semantics for our explicit
4575 // multiply. We suppress this if overflow is not undefined behavior.
4576 llvm::Type *elemTy = ConvertTypeForMem(vla->getElementType());
4577 if (getLangOpts().PointerOverflowDefined) {
4578 index = Builder.CreateMul(index, numElements, "vla.index");
4579 pointer = Builder.CreateGEP(elemTy, pointer, index, "add.ptr");
4580 } else {
4581 index = Builder.CreateNSWMul(index, numElements, "vla.index");
4582 pointer =
4583 EmitCheckedInBoundsGEP(elemTy, pointer, index, isSigned,
4584 isSubtraction, BO->getExprLoc(), "add.ptr");
4585 }
4586 return pointer;
4587 }
4588
4589 // Explicitly handle GNU void* and function pointer arithmetic extensions. The
4590 // GNU void* casts amount to no-ops since our void* type is i8*, but this is
4591 // future proof.
4592 llvm::Type *elemTy;
4593 if (elementType->isVoidType() || elementType->isFunctionType())
4594 elemTy = Int8Ty;
4595 else
4596 elemTy = ConvertTypeForMem(elementType);
4597
4598 if (getLangOpts().PointerOverflowDefined)
4599 return Builder.CreateGEP(elemTy, pointer, index, "add.ptr");
4600
4601 return EmitCheckedInBoundsGEP(elemTy, pointer, index, isSigned, isSubtraction,
4602 BO->getExprLoc(), "add.ptr");
4603}
4604
4605// Construct an fmuladd intrinsic to represent a fused mul-add of MulOp and
4606// Addend. Use negMul and negAdd to negate the first operand of the Mul or
4607// the add operand respectively. This allows fmuladd to represent a*b-c, or
4608// c-a*b. Patterns in LLVM should catch the negated forms and translate them to
4609// efficient operations.
4610static Value* buildFMulAdd(llvm::Instruction *MulOp, Value *Addend,
4611 const CodeGenFunction &CGF, CGBuilderTy &Builder,
4612 bool negMul, bool negAdd) {
4613 Value *MulOp0 = MulOp->getOperand(0);
4614 Value *MulOp1 = MulOp->getOperand(1);
4615 if (negMul)
4616 MulOp0 = Builder.CreateFNeg(MulOp0, "neg");
4617 if (negAdd)
4618 Addend = Builder.CreateFNeg(Addend, "neg");
4619
4620 Value *FMulAdd = nullptr;
4621 if (Builder.getIsFPConstrained()) {
4622 assert(isa<llvm::ConstrainedFPIntrinsic>(MulOp) &&
4623 "Only constrained operation should be created when Builder is in FP "
4624 "constrained mode");
4625 FMulAdd = Builder.CreateConstrainedFPCall(
4626 CGF.CGM.getIntrinsic(llvm::Intrinsic::experimental_constrained_fmuladd,
4627 Addend->getType()),
4628 {MulOp0, MulOp1, Addend});
4629 } else {
4630 FMulAdd = Builder.CreateCall(
4631 CGF.CGM.getIntrinsic(llvm::Intrinsic::fmuladd, Addend->getType()),
4632 {MulOp0, MulOp1, Addend});
4633 }
4634 MulOp->eraseFromParent();
4635
4636 return FMulAdd;
4637}
4638
4639// Check whether it would be legal to emit an fmuladd intrinsic call to
4640// represent op and if so, build the fmuladd.
4641//
4642// Checks that (a) the operation is fusable, and (b) -ffp-contract=on.
4643// Does NOT check the type of the operation - it's assumed that this function
4644// will be called from contexts where it's known that the type is contractable.
4645static Value* tryEmitFMulAdd(const BinOpInfo &op,
4646 const CodeGenFunction &CGF, CGBuilderTy &Builder,
4647 bool isSub=false) {
4648
4649 assert((op.Opcode == BO_Add || op.Opcode == BO_AddAssign ||
4650 op.Opcode == BO_Sub || op.Opcode == BO_SubAssign) &&
4651 "Only fadd/fsub can be the root of an fmuladd.");
4652
4653 // Check whether this op is marked as fusable.
4654 if (!op.FPFeatures.allowFPContractWithinStatement())
4655 return nullptr;
4656
4657 Value *LHS = op.LHS;
4658 Value *RHS = op.RHS;
4659
4660 // Peek through fneg to look for fmul. Make sure fneg has no users, and that
4661 // it is the only use of its operand.
4662 bool NegLHS = false;
4663 if (auto *LHSUnOp = dyn_cast<llvm::UnaryOperator>(LHS)) {
4664 if (LHSUnOp->getOpcode() == llvm::Instruction::FNeg &&
4665 LHSUnOp->use_empty() && LHSUnOp->getOperand(0)->hasOneUse()) {
4666 LHS = LHSUnOp->getOperand(0);
4667 NegLHS = true;
4668 }
4669 }
4670
4671 bool NegRHS = false;
4672 if (auto *RHSUnOp = dyn_cast<llvm::UnaryOperator>(RHS)) {
4673 if (RHSUnOp->getOpcode() == llvm::Instruction::FNeg &&
4674 RHSUnOp->use_empty() && RHSUnOp->getOperand(0)->hasOneUse()) {
4675 RHS = RHSUnOp->getOperand(0);
4676 NegRHS = true;
4677 }
4678 }
4679
4680 // We have a potentially fusable op. Look for a mul on one of the operands.
4681 // Also, make sure that the mul result isn't used directly. In that case,
4682 // there's no point creating a muladd operation.
4683 if (auto *LHSBinOp = dyn_cast<llvm::BinaryOperator>(LHS)) {
4684 if (LHSBinOp->getOpcode() == llvm::Instruction::FMul &&
4685 (LHSBinOp->use_empty() || NegLHS)) {
4686 // If we looked through fneg, erase it.
4687 if (NegLHS)
4688 cast<llvm::Instruction>(op.LHS)->eraseFromParent();
4689 return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder, NegLHS, isSub);
4690 }
4691 }
4692 if (auto *RHSBinOp = dyn_cast<llvm::BinaryOperator>(RHS)) {
4693 if (RHSBinOp->getOpcode() == llvm::Instruction::FMul &&
4694 (RHSBinOp->use_empty() || NegRHS)) {
4695 // If we looked through fneg, erase it.
4696 if (NegRHS)
4697 cast<llvm::Instruction>(op.RHS)->eraseFromParent();
4698 return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub ^ NegRHS, false);
4699 }
4700 }
4701
4702 if (auto *LHSBinOp = dyn_cast<llvm::CallBase>(LHS)) {
4703 if (LHSBinOp->getIntrinsicID() ==
4704 llvm::Intrinsic::experimental_constrained_fmul &&
4705 (LHSBinOp->use_empty() || NegLHS)) {
4706 // If we looked through fneg, erase it.
4707 if (NegLHS)
4708 cast<llvm::Instruction>(op.LHS)->eraseFromParent();
4709 return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder, NegLHS, isSub);
4710 }
4711 }
4712 if (auto *RHSBinOp = dyn_cast<llvm::CallBase>(RHS)) {
4713 if (RHSBinOp->getIntrinsicID() ==
4714 llvm::Intrinsic::experimental_constrained_fmul &&
4715 (RHSBinOp->use_empty() || NegRHS)) {
4716 // If we looked through fneg, erase it.
4717 if (NegRHS)
4718 cast<llvm::Instruction>(op.RHS)->eraseFromParent();
4719 return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub ^ NegRHS, false);
4720 }
4721 }
4722
4723 return nullptr;
4724}
4725
4726Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &op) {
4727 if (op.LHS->getType()->isPointerTy() ||
4728 op.RHS->getType()->isPointerTy())
4730
4731 if (op.Ty->isSignedIntegerOrEnumerationType() ||
4732 op.Ty->isUnsignedIntegerType()) {
4733 const bool isSigned = op.Ty->isSignedIntegerOrEnumerationType();
4734 const bool hasSan =
4735 isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)
4736 : CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow);
4737 switch (getOverflowBehaviorConsideringType(CGF, op.Ty)) {
4738 case LangOptions::OB_Wrap:
4739 return Builder.CreateAdd(op.LHS, op.RHS, "add");
4740 case LangOptions::OB_SignedAndDefined:
4741 if (!hasSan)
4742 return Builder.CreateAdd(op.LHS, op.RHS, "add");
4743 [[fallthrough]];
4744 case LangOptions::OB_Unset:
4745 if (!hasSan)
4746 return isSigned ? Builder.CreateNSWAdd(op.LHS, op.RHS, "add")
4747 : Builder.CreateAdd(op.LHS, op.RHS, "add");
4748 [[fallthrough]];
4749 case LangOptions::OB_Trap:
4750 if (CanElideOverflowCheck(CGF.getContext(), op))
4751 return isSigned ? Builder.CreateNSWAdd(op.LHS, op.RHS, "add")
4752 : Builder.CreateAdd(op.LHS, op.RHS, "add");
4753 return EmitOverflowCheckedBinOp(op);
4754 }
4755 }
4756
4757 // For vector and matrix adds, try to fold into a fmuladd.
4758 if (op.LHS->getType()->isFPOrFPVectorTy()) {
4759 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures);
4760 // Try to form an fmuladd.
4761 if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder))
4762 return FMulAdd;
4763 }
4764
4765 if (op.Ty->isConstantMatrixType()) {
4766 llvm::MatrixBuilder MB(Builder);
4767 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures);
4768 return MB.CreateAdd(op.LHS, op.RHS);
4769 }
4770
4771 if (op.LHS->getType()->isFPOrFPVectorTy()) {
4772 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures);
4773 return Builder.CreateFAdd(op.LHS, op.RHS, "add");
4774 }
4775
4776 if (op.isFixedPointOp())
4777 return EmitFixedPointBinOp(op);
4778
4779 return Builder.CreateAdd(op.LHS, op.RHS, "add");
4780}
4781
4782/// The resulting value must be calculated with exact precision, so the operands
4783/// may not be the same type.
4784Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) {
4785 using llvm::APSInt;
4786 using llvm::ConstantInt;
4787
4788 // This is either a binary operation where at least one of the operands is
4789 // a fixed-point type, or a unary operation where the operand is a fixed-point
4790 // type. The result type of a binary operation is determined by
4791 // Sema::handleFixedPointConversions().
4792 QualType ResultTy = op.Ty;
4793 QualType LHSTy, RHSTy;
4794 if (const auto *BinOp = dyn_cast<BinaryOperator>(op.E)) {
4795 RHSTy = BinOp->getRHS()->getType();
4796 if (const auto *CAO = dyn_cast<CompoundAssignOperator>(BinOp)) {
4797 // For compound assignment, the effective type of the LHS at this point
4798 // is the computation LHS type, not the actual LHS type, and the final
4799 // result type is not the type of the expression but rather the
4800 // computation result type.
4801 LHSTy = CAO->getComputationLHSType();
4802 ResultTy = CAO->getComputationResultType();
4803 } else
4804 LHSTy = BinOp->getLHS()->getType();
4805 } else if (const auto *UnOp = dyn_cast<UnaryOperator>(op.E)) {
4806 LHSTy = UnOp->getSubExpr()->getType();
4807 RHSTy = UnOp->getSubExpr()->getType();
4808 }
4809 ASTContext &Ctx = CGF.getContext();
4810 Value *LHS = op.LHS;
4811 Value *RHS = op.RHS;
4812
4813 auto LHSFixedSema = Ctx.getFixedPointSemantics(LHSTy);
4814 auto RHSFixedSema = Ctx.getFixedPointSemantics(RHSTy);
4815 auto ResultFixedSema = Ctx.getFixedPointSemantics(ResultTy);
4816 auto CommonFixedSema = LHSFixedSema.getCommonSemantics(RHSFixedSema);
4817
4818 // Perform the actual operation.
4819 Value *Result;
4820 llvm::FixedPointBuilder<CGBuilderTy> FPBuilder(Builder);
4821 switch (op.Opcode) {
4822 case BO_AddAssign:
4823 case BO_Add:
4824 Result = FPBuilder.CreateAdd(LHS, LHSFixedSema, RHS, RHSFixedSema);
4825 break;
4826 case BO_SubAssign:
4827 case BO_Sub:
4828 Result = FPBuilder.CreateSub(LHS, LHSFixedSema, RHS, RHSFixedSema);
4829 break;
4830 case BO_MulAssign:
4831 case BO_Mul:
4832 Result = FPBuilder.CreateMul(LHS, LHSFixedSema, RHS, RHSFixedSema);
4833 break;
4834 case BO_DivAssign:
4835 case BO_Div:
4836 Result = FPBuilder.CreateDiv(LHS, LHSFixedSema, RHS, RHSFixedSema);
4837 break;
4838 case BO_ShlAssign:
4839 case BO_Shl:
4840 Result = FPBuilder.CreateShl(LHS, LHSFixedSema, RHS);
4841 break;
4842 case BO_ShrAssign:
4843 case BO_Shr:
4844 Result = FPBuilder.CreateShr(LHS, LHSFixedSema, RHS);
4845 break;
4846 case BO_LT:
4847 return FPBuilder.CreateLT(LHS, LHSFixedSema, RHS, RHSFixedSema);
4848 case BO_GT:
4849 return FPBuilder.CreateGT(LHS, LHSFixedSema, RHS, RHSFixedSema);
4850 case BO_LE:
4851 return FPBuilder.CreateLE(LHS, LHSFixedSema, RHS, RHSFixedSema);
4852 case BO_GE:
4853 return FPBuilder.CreateGE(LHS, LHSFixedSema, RHS, RHSFixedSema);
4854 case BO_EQ:
4855 // For equality operations, we assume any padding bits on unsigned types are
4856 // zero'd out. They could be overwritten through non-saturating operations
4857 // that cause overflow, but this leads to undefined behavior.
4858 return FPBuilder.CreateEQ(LHS, LHSFixedSema, RHS, RHSFixedSema);
4859 case BO_NE:
4860 return FPBuilder.CreateNE(LHS, LHSFixedSema, RHS, RHSFixedSema);
4861 case BO_Cmp:
4862 case BO_LAnd:
4863 case BO_LOr:
4864 llvm_unreachable("Found unimplemented fixed point binary operation");
4865 case BO_PtrMemD:
4866 case BO_PtrMemI:
4867 case BO_Rem:
4868 case BO_Xor:
4869 case BO_And:
4870 case BO_Or:
4871 case BO_Assign:
4872 case BO_RemAssign:
4873 case BO_AndAssign:
4874 case BO_XorAssign:
4875 case BO_OrAssign:
4876 case BO_Comma:
4877 llvm_unreachable("Found unsupported binary operation for fixed point types.");
4878 }
4879
4880 bool IsShift = BinaryOperator::isShiftOp(op.Opcode) ||
4882 // Convert to the result type.
4883 return FPBuilder.CreateFixedToFixed(Result, IsShift ? LHSFixedSema
4884 : CommonFixedSema,
4885 ResultFixedSema);
4886}
4887
4888Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) {
4889 // The LHS is always a pointer if either side is.
4890 if (!op.LHS->getType()->isPointerTy()) {
4891 if (op.Ty->isSignedIntegerOrEnumerationType() ||
4892 op.Ty->isUnsignedIntegerType()) {
4893 const bool isSigned = op.Ty->isSignedIntegerOrEnumerationType();
4894 const bool hasSan =
4895 isSigned ? CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)
4896 : CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow);
4897 switch (getOverflowBehaviorConsideringType(CGF, op.Ty)) {
4898 case LangOptions::OB_Wrap:
4899 return Builder.CreateSub(op.LHS, op.RHS, "sub");
4900 case LangOptions::OB_SignedAndDefined:
4901 if (!hasSan)
4902 return Builder.CreateSub(op.LHS, op.RHS, "sub");
4903 [[fallthrough]];
4904 case LangOptions::OB_Unset:
4905 if (!hasSan)
4906 return isSigned ? Builder.CreateNSWSub(op.LHS, op.RHS, "sub")
4907 : Builder.CreateSub(op.LHS, op.RHS, "sub");
4908 [[fallthrough]];
4909 case LangOptions::OB_Trap:
4910 if (CanElideOverflowCheck(CGF.getContext(), op))
4911 return isSigned ? Builder.CreateNSWSub(op.LHS, op.RHS, "sub")
4912 : Builder.CreateSub(op.LHS, op.RHS, "sub");
4913 return EmitOverflowCheckedBinOp(op);
4914 }
4915 }
4916
4917 // For vector and matrix subs, try to fold into a fmuladd.
4918 if (op.LHS->getType()->isFPOrFPVectorTy()) {
4919 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures);
4920 // Try to form an fmuladd.
4921 if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder, true))
4922 return FMulAdd;
4923 }
4924
4925 if (op.Ty->isConstantMatrixType()) {
4926 llvm::MatrixBuilder MB(Builder);
4927 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures);
4928 return MB.CreateSub(op.LHS, op.RHS);
4929 }
4930
4931 if (op.LHS->getType()->isFPOrFPVectorTy()) {
4932 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures);
4933 return Builder.CreateFSub(op.LHS, op.RHS, "sub");
4934 }
4935
4936 if (op.isFixedPointOp())
4937 return EmitFixedPointBinOp(op);
4938
4939 return Builder.CreateSub(op.LHS, op.RHS, "sub");
4940 }
4941
4942 // If the RHS is not a pointer, then we have normal pointer
4943 // arithmetic.
4944 if (!op.RHS->getType()->isPointerTy())
4946
4947 // Otherwise, this is a pointer subtraction.
4948
4949 // Do the raw subtraction part.
4950 llvm::Value *LHS
4951 = Builder.CreatePtrToInt(op.LHS, CGF.PtrDiffTy, "sub.ptr.lhs.cast");
4952 llvm::Value *RHS
4953 = Builder.CreatePtrToInt(op.RHS, CGF.PtrDiffTy, "sub.ptr.rhs.cast");
4954 Value *diffInChars = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
4955
4956 // Okay, figure out the element size.
4957 const BinaryOperator *expr = cast<BinaryOperator>(op.E);
4958 QualType elementType = expr->getLHS()->getType()->getPointeeType();
4959
4960 llvm::Value *divisor = nullptr;
4961
4962 // For a variable-length array, this is going to be non-constant.
4963 if (const VariableArrayType *vla
4964 = CGF.getContext().getAsVariableArrayType(elementType)) {
4965 auto VlaSize = CGF.getVLASize(vla);
4966 elementType = VlaSize.Type;
4967 divisor = VlaSize.NumElts;
4968
4969 // Scale the number of non-VLA elements by the non-VLA element size.
4970 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(elementType);
4971 if (!eltSize.isOne())
4972 divisor = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), divisor);
4973
4974 // For everything elese, we can just compute it, safe in the
4975 // assumption that Sema won't let anything through that we can't
4976 // safely compute the size of.
4977 } else {
4978 CharUnits elementSize;
4979 // Handle GCC extension for pointer arithmetic on void* and
4980 // function pointer types.
4981 if (elementType->isVoidType() || elementType->isFunctionType())
4982 elementSize = CharUnits::One();
4983 else
4984 elementSize = CGF.getContext().getTypeSizeInChars(elementType);
4985
4986 // Don't even emit the divide for element size of 1.
4987 if (elementSize.isOne())
4988 return diffInChars;
4989
4990 divisor = CGF.CGM.getSize(elementSize);
4991 }
4992
4993 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
4994 // pointer difference in C is only defined in the case where both operands
4995 // are pointing to elements of an array.
4996 return Builder.CreateExactSDiv(diffInChars, divisor, "sub.ptr.div");
4997}
4998
4999Value *ScalarExprEmitter::GetMaximumShiftAmount(Value *LHS, Value *RHS,
5000 bool RHSIsSigned) {
5001 llvm::IntegerType *Ty;
5002 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(LHS->getType()))
5003 Ty = cast<llvm::IntegerType>(VT->getElementType());
5004 else
5005 Ty = cast<llvm::IntegerType>(LHS->getType());
5006 // For a given type of LHS the maximum shift amount is width(LHS)-1, however
5007 // it can occur that width(LHS)-1 > range(RHS). Since there is no check for
5008 // this in ConstantInt::get, this results in the value getting truncated.
5009 // Constrain the return value to be max(RHS) in this case.
5010 llvm::Type *RHSTy = RHS->getType();
5011 llvm::APInt RHSMax =
5012 RHSIsSigned ? llvm::APInt::getSignedMaxValue(RHSTy->getScalarSizeInBits())
5013 : llvm::APInt::getMaxValue(RHSTy->getScalarSizeInBits());
5014 if (RHSMax.ult(Ty->getBitWidth()))
5015 return llvm::ConstantInt::get(RHSTy, RHSMax);
5016 return llvm::ConstantInt::get(RHSTy, Ty->getBitWidth() - 1);
5017}
5018
5019Value *ScalarExprEmitter::ConstrainShiftValue(Value *LHS, Value *RHS,
5020 const Twine &Name) {
5021 llvm::IntegerType *Ty;
5022 if (auto *VT = dyn_cast<llvm::VectorType>(LHS->getType()))
5023 Ty = cast<llvm::IntegerType>(VT->getElementType());
5024 else
5025 Ty = cast<llvm::IntegerType>(LHS->getType());
5026
5027 if (llvm::isPowerOf2_64(Ty->getBitWidth()))
5028 return Builder.CreateAnd(RHS, GetMaximumShiftAmount(LHS, RHS, false), Name);
5029
5030 return Builder.CreateURem(
5031 RHS, llvm::ConstantInt::get(RHS->getType(), Ty->getBitWidth()), Name);
5032}
5033
5034Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
5035 // TODO: This misses out on the sanitizer check below.
5036 if (Ops.isFixedPointOp())
5037 return EmitFixedPointBinOp(Ops);
5038
5039 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
5040 // RHS to the same size as the LHS.
5041 Value *RHS = Ops.RHS;
5042 if (Ops.LHS->getType() != RHS->getType())
5043 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
5044
5045 bool SanitizeSignedBase = CGF.SanOpts.has(SanitizerKind::ShiftBase) &&
5046 Ops.Ty->hasSignedIntegerRepresentation() &&
5048 !CGF.getLangOpts().CPlusPlus20;
5049 bool SanitizeUnsignedBase =
5050 CGF.SanOpts.has(SanitizerKind::UnsignedShiftBase) &&
5051 Ops.Ty->hasUnsignedIntegerRepresentation();
5052 bool SanitizeBase = SanitizeSignedBase || SanitizeUnsignedBase;
5053 bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent);
5054 // OpenCL 6.3j: shift values are effectively % word size of LHS.
5055 if (CGF.getLangOpts().OpenCL || CGF.getLangOpts().HLSL)
5056 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shl.mask");
5057 else if ((SanitizeBase || SanitizeExponent) &&
5058 isa<llvm::IntegerType>(Ops.LHS->getType())) {
5059 SmallVector<SanitizerKind::SanitizerOrdinal, 3> Ordinals;
5060 if (SanitizeSignedBase)
5061 Ordinals.push_back(SanitizerKind::SO_ShiftBase);
5062 if (SanitizeUnsignedBase)
5063 Ordinals.push_back(SanitizerKind::SO_UnsignedShiftBase);
5064 if (SanitizeExponent)
5065 Ordinals.push_back(SanitizerKind::SO_ShiftExponent);
5066
5067 SanitizerDebugLocation SanScope(&CGF, Ordinals,
5068 SanitizerHandler::ShiftOutOfBounds);
5069 SmallVector<std::pair<Value *, SanitizerKind::SanitizerOrdinal>, 2> Checks;
5070 bool RHSIsSigned = Ops.rhsHasSignedIntegerRepresentation();
5071 llvm::Value *WidthMinusOne =
5072 GetMaximumShiftAmount(Ops.LHS, Ops.RHS, RHSIsSigned);
5073 llvm::Value *ValidExponent = Builder.CreateICmpULE(Ops.RHS, WidthMinusOne);
5074
5075 if (SanitizeExponent) {
5076 Checks.push_back(
5077 std::make_pair(ValidExponent, SanitizerKind::SO_ShiftExponent));
5078 }
5079
5080 if (SanitizeBase) {
5081 // Check whether we are shifting any non-zero bits off the top of the
5082 // integer. We only emit this check if exponent is valid - otherwise
5083 // instructions below will have undefined behavior themselves.
5084 llvm::BasicBlock *Orig = Builder.GetInsertBlock();
5085 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
5086 llvm::BasicBlock *CheckShiftBase = CGF.createBasicBlock("check");
5087 Builder.CreateCondBr(ValidExponent, CheckShiftBase, Cont);
5088 llvm::Value *PromotedWidthMinusOne =
5089 (RHS == Ops.RHS) ? WidthMinusOne
5090 : GetMaximumShiftAmount(Ops.LHS, RHS, RHSIsSigned);
5091 CGF.EmitBlock(CheckShiftBase);
5092 llvm::Value *BitsShiftedOff = Builder.CreateLShr(
5093 Ops.LHS, Builder.CreateSub(PromotedWidthMinusOne, RHS, "shl.zeros",
5094 /*NUW*/ true, /*NSW*/ true),
5095 "shl.check");
5096 if (SanitizeUnsignedBase || CGF.getLangOpts().CPlusPlus) {
5097 // In C99, we are not permitted to shift a 1 bit into the sign bit.
5098 // Under C++11's rules, shifting a 1 bit into the sign bit is
5099 // OK, but shifting a 1 bit out of it is not. (C89 and C++03 don't
5100 // define signed left shifts, so we use the C99 and C++11 rules there).
5101 // Unsigned shifts can always shift into the top bit.
5102 llvm::Value *One = llvm::ConstantInt::get(BitsShiftedOff->getType(), 1);
5103 BitsShiftedOff = Builder.CreateLShr(BitsShiftedOff, One);
5104 }
5105 llvm::Value *Zero = llvm::ConstantInt::get(BitsShiftedOff->getType(), 0);
5106 llvm::Value *ValidBase = Builder.CreateICmpEQ(BitsShiftedOff, Zero);
5107 CGF.EmitBlock(Cont);
5108 llvm::PHINode *BaseCheck = Builder.CreatePHI(ValidBase->getType(), 2);
5109 BaseCheck->addIncoming(Builder.getTrue(), Orig);
5110 BaseCheck->addIncoming(ValidBase, CheckShiftBase);
5111 Checks.push_back(std::make_pair(
5112 BaseCheck, SanitizeSignedBase ? SanitizerKind::SO_ShiftBase
5113 : SanitizerKind::SO_UnsignedShiftBase));
5114 }
5115
5116 assert(!Checks.empty());
5117 EmitBinOpCheck(Checks, Ops);
5118 }
5119
5120 return Builder.CreateShl(Ops.LHS, RHS, "shl");
5121}
5122
5123Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
5124 // TODO: This misses out on the sanitizer check below.
5125 if (Ops.isFixedPointOp())
5126 return EmitFixedPointBinOp(Ops);
5127
5128 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
5129 // RHS to the same size as the LHS.
5130 Value *RHS = Ops.RHS;
5131 if (Ops.LHS->getType() != RHS->getType())
5132 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
5133
5134 // OpenCL 6.3j: shift values are effectively % word size of LHS.
5135 if (CGF.getLangOpts().OpenCL || CGF.getLangOpts().HLSL)
5136 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shr.mask");
5137 else if (CGF.SanOpts.has(SanitizerKind::ShiftExponent) &&
5138 isa<llvm::IntegerType>(Ops.LHS->getType())) {
5139 SanitizerDebugLocation SanScope(&CGF, {SanitizerKind::SO_ShiftExponent},
5140 SanitizerHandler::ShiftOutOfBounds);
5141 bool RHSIsSigned = Ops.rhsHasSignedIntegerRepresentation();
5142 llvm::Value *Valid = Builder.CreateICmpULE(
5143 Ops.RHS, GetMaximumShiftAmount(Ops.LHS, Ops.RHS, RHSIsSigned));
5144 EmitBinOpCheck(std::make_pair(Valid, SanitizerKind::SO_ShiftExponent), Ops);
5145 }
5146
5147 if (Ops.Ty->hasUnsignedIntegerRepresentation())
5148 return Builder.CreateLShr(Ops.LHS, RHS, "shr");
5149 return Builder.CreateAShr(Ops.LHS, RHS, "shr");
5150}
5151
5153// return corresponding comparison intrinsic for given vector type
5154static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT,
5155 BuiltinType::Kind ElemKind) {
5156 switch (ElemKind) {
5157 default: llvm_unreachable("unexpected element type");
5158 case BuiltinType::Char_U:
5159 case BuiltinType::UChar:
5160 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
5161 llvm::Intrinsic::ppc_altivec_vcmpgtub_p;
5162 case BuiltinType::Char_S:
5163 case BuiltinType::SChar:
5164 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
5165 llvm::Intrinsic::ppc_altivec_vcmpgtsb_p;
5166 case BuiltinType::UShort:
5167 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
5168 llvm::Intrinsic::ppc_altivec_vcmpgtuh_p;
5169 case BuiltinType::Short:
5170 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
5171 llvm::Intrinsic::ppc_altivec_vcmpgtsh_p;
5172 case BuiltinType::UInt:
5173 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
5174 llvm::Intrinsic::ppc_altivec_vcmpgtuw_p;
5175 case BuiltinType::Int:
5176 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
5177 llvm::Intrinsic::ppc_altivec_vcmpgtsw_p;
5178 case BuiltinType::ULong:
5179 case BuiltinType::ULongLong:
5180 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequd_p :
5181 llvm::Intrinsic::ppc_altivec_vcmpgtud_p;
5182 case BuiltinType::Long:
5183 case BuiltinType::LongLong:
5184 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequd_p :
5185 llvm::Intrinsic::ppc_altivec_vcmpgtsd_p;
5186 case BuiltinType::Float:
5187 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p :
5188 llvm::Intrinsic::ppc_altivec_vcmpgtfp_p;
5189 case BuiltinType::Double:
5190 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_vsx_xvcmpeqdp_p :
5191 llvm::Intrinsic::ppc_vsx_xvcmpgtdp_p;
5192 case BuiltinType::UInt128:
5193 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequq_p
5194 : llvm::Intrinsic::ppc_altivec_vcmpgtuq_p;
5195 case BuiltinType::Int128:
5196 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequq_p
5197 : llvm::Intrinsic::ppc_altivec_vcmpgtsq_p;
5198 }
5199}
5200
5201Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,
5202 llvm::CmpInst::Predicate UICmpOpc,
5203 llvm::CmpInst::Predicate SICmpOpc,
5204 llvm::CmpInst::Predicate FCmpOpc,
5205 bool IsSignaling) {
5206 TestAndClearIgnoreResultAssign();
5207 Value *Result;
5208 QualType LHSTy = E->getLHS()->getType();
5209 QualType RHSTy = E->getRHS()->getType();
5210 if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) {
5211 assert(E->getOpcode() == BO_EQ ||
5212 E->getOpcode() == BO_NE);
5213 Value *LHS = CGF.EmitScalarExpr(E->getLHS());
5214 Value *RHS = CGF.EmitScalarExpr(E->getRHS());
5216 CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE);
5217 } else if (!LHSTy->isAnyComplexType() && !RHSTy->isAnyComplexType()) {
5218 BinOpInfo BOInfo = EmitBinOps(E);
5219 Value *LHS = BOInfo.LHS;
5220 Value *RHS = BOInfo.RHS;
5221
5222 // If AltiVec, the comparison results in a numeric type, so we use
5223 // intrinsics comparing vectors and giving 0 or 1 as a result
5224 if (LHSTy->isVectorType() && !E->getType()->isVectorType()) {
5225 // constants for mapping CR6 register bits to predicate result
5226 enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6;
5227
5228 llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic;
5229
5230 // in several cases vector arguments order will be reversed
5231 Value *FirstVecArg = LHS,
5232 *SecondVecArg = RHS;
5233
5234 QualType ElTy = LHSTy->castAs<VectorType>()->getElementType();
5235 BuiltinType::Kind ElementKind = ElTy->castAs<BuiltinType>()->getKind();
5236
5237 switch(E->getOpcode()) {
5238 default: llvm_unreachable("is not a comparison operation");
5239 case BO_EQ:
5240 CR6 = CR6_LT;
5241 ID = GetIntrinsic(VCMPEQ, ElementKind);
5242 break;
5243 case BO_NE:
5244 CR6 = CR6_EQ;
5245 ID = GetIntrinsic(VCMPEQ, ElementKind);
5246 break;
5247 case BO_LT:
5248 CR6 = CR6_LT;
5249 ID = GetIntrinsic(VCMPGT, ElementKind);
5250 std::swap(FirstVecArg, SecondVecArg);
5251 break;
5252 case BO_GT:
5253 CR6 = CR6_LT;
5254 ID = GetIntrinsic(VCMPGT, ElementKind);
5255 break;
5256 case BO_LE:
5257 if (ElementKind == BuiltinType::Float) {
5258 CR6 = CR6_LT;
5259 ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
5260 std::swap(FirstVecArg, SecondVecArg);
5261 }
5262 else {
5263 CR6 = CR6_EQ;
5264 ID = GetIntrinsic(VCMPGT, ElementKind);
5265 }
5266 break;
5267 case BO_GE:
5268 if (ElementKind == BuiltinType::Float) {
5269 CR6 = CR6_LT;
5270 ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
5271 }
5272 else {
5273 CR6 = CR6_EQ;
5274 ID = GetIntrinsic(VCMPGT, ElementKind);
5275 std::swap(FirstVecArg, SecondVecArg);
5276 }
5277 break;
5278 }
5279
5280 Value *CR6Param = Builder.getInt32(CR6);
5281 llvm::Function *F = CGF.CGM.getIntrinsic(ID);
5282 Result = Builder.CreateCall(F, {CR6Param, FirstVecArg, SecondVecArg});
5283
5284 // The result type of intrinsic may not be same as E->getType().
5285 // If E->getType() is not BoolTy, EmitScalarConversion will do the
5286 // conversion work. If E->getType() is BoolTy, EmitScalarConversion will
5287 // do nothing, if ResultTy is not i1 at the same time, it will cause
5288 // crash later.
5289 llvm::IntegerType *ResultTy = cast<llvm::IntegerType>(Result->getType());
5290 if (ResultTy->getBitWidth() > 1 &&
5291 E->getType() == CGF.getContext().BoolTy)
5292 Result = Builder.CreateTrunc(Result, Builder.getInt1Ty());
5293 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType(),
5294 E->getExprLoc());
5295 }
5296
5297 if (BOInfo.isFixedPointOp()) {
5298 Result = EmitFixedPointBinOp(BOInfo);
5299 } else if (LHS->getType()->isFPOrFPVectorTy()) {
5300 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, BOInfo.FPFeatures);
5301 if (!IsSignaling)
5302 Result = Builder.CreateFCmp(FCmpOpc, LHS, RHS, "cmp");
5303 else
5304 Result = Builder.CreateFCmpS(FCmpOpc, LHS, RHS, "cmp");
5305 } else if (LHSTy->hasSignedIntegerRepresentation()) {
5306 Result = Builder.CreateICmp(SICmpOpc, LHS, RHS, "cmp");
5307 } else {
5308 // Unsigned integers and pointers.
5309
5310 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers &&
5313
5314 // Dynamic information is required to be stripped for comparisons,
5315 // because it could leak the dynamic information. Based on comparisons
5316 // of pointers to dynamic objects, the optimizer can replace one pointer
5317 // with another, which might be incorrect in presence of invariant
5318 // groups. Comparison with null is safe because null does not carry any
5319 // dynamic information.
5320 if (LHSTy.mayBeDynamicClass())
5321 LHS = Builder.CreateStripInvariantGroup(LHS);
5322 if (RHSTy.mayBeDynamicClass())
5323 RHS = Builder.CreateStripInvariantGroup(RHS);
5324 }
5325
5326 Result = Builder.CreateICmp(UICmpOpc, LHS, RHS, "cmp");
5327 }
5328
5329 // If this is a vector comparison, sign extend the result to the appropriate
5330 // vector integer type and return it (don't convert to bool).
5331 if (LHSTy->isVectorType() || LHSTy->isSveVLSBuiltinType())
5332 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
5333
5334 } else {
5335 // Complex Comparison: can only be an equality comparison.
5337 QualType CETy;
5338 if (auto *CTy = LHSTy->getAs<ComplexType>()) {
5339 LHS = CGF.EmitComplexExpr(E->getLHS());
5340 CETy = CTy->getElementType();
5341 } else {
5342 LHS.first = Visit(E->getLHS());
5343 LHS.second = llvm::Constant::getNullValue(LHS.first->getType());
5344 CETy = LHSTy;
5345 }
5346 if (auto *CTy = RHSTy->getAs<ComplexType>()) {
5347 RHS = CGF.EmitComplexExpr(E->getRHS());
5348 assert(CGF.getContext().hasSameUnqualifiedType(CETy,
5349 CTy->getElementType()) &&
5350 "The element types must always match.");
5351 (void)CTy;
5352 } else {
5353 RHS.first = Visit(E->getRHS());
5354 RHS.second = llvm::Constant::getNullValue(RHS.first->getType());
5355 assert(CGF.getContext().hasSameUnqualifiedType(CETy, RHSTy) &&
5356 "The element types must always match.");
5357 }
5358
5359 Value *ResultR, *ResultI;
5360 if (CETy->isRealFloatingType()) {
5361 // As complex comparisons can only be equality comparisons, they
5362 // are never signaling comparisons.
5363 ResultR = Builder.CreateFCmp(FCmpOpc, LHS.first, RHS.first, "cmp.r");
5364 ResultI = Builder.CreateFCmp(FCmpOpc, LHS.second, RHS.second, "cmp.i");
5365 } else {
5366 // Complex comparisons can only be equality comparisons. As such, signed
5367 // and unsigned opcodes are the same.
5368 ResultR = Builder.CreateICmp(UICmpOpc, LHS.first, RHS.first, "cmp.r");
5369 ResultI = Builder.CreateICmp(UICmpOpc, LHS.second, RHS.second, "cmp.i");
5370 }
5371
5372 if (E->getOpcode() == BO_EQ) {
5373 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
5374 } else {
5375 assert(E->getOpcode() == BO_NE &&
5376 "Complex comparison other than == or != ?");
5377 Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
5378 }
5379 }
5380
5381 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType(),
5382 E->getExprLoc());
5383}
5384
5386 const BinaryOperator *E, Value **Previous, QualType *SrcType) {
5387 // In case we have the integer or bitfield sanitizer checks enabled
5388 // we want to get the expression before scalar conversion.
5389 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E->getRHS())) {
5390 CastKind Kind = ICE->getCastKind();
5391 if (Kind == CK_IntegralCast || Kind == CK_LValueToRValue) {
5392 *SrcType = ICE->getSubExpr()->getType();
5393 *Previous = EmitScalarExpr(ICE->getSubExpr());
5394 // Pass default ScalarConversionOpts to avoid emitting
5395 // integer sanitizer checks as E refers to bitfield.
5396 return EmitScalarConversion(*Previous, *SrcType, ICE->getType(),
5397 ICE->getExprLoc());
5398 }
5399 }
5400 return EmitScalarExpr(E->getRHS());
5401}
5402
5403Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
5404 ApplyAtomGroup Grp(CGF.getDebugInfo());
5405 bool Ignore = TestAndClearIgnoreResultAssign();
5406
5407 Value *RHS;
5408 LValue LHS;
5409
5410 if (PointerAuthQualifier PtrAuth = E->getLHS()->getType().getPointerAuth()) {
5413 llvm::Value *RV =
5414 CGF.EmitPointerAuthQualify(PtrAuth, E->getRHS(), LV.getAddress());
5415 CGF.EmitNullabilityCheck(LV, RV, E->getExprLoc());
5417
5418 if (Ignore)
5419 return nullptr;
5420 RV = CGF.EmitPointerAuthUnqualify(PtrAuth, RV, LV.getType(),
5421 LV.getAddress(), /*nonnull*/ false);
5422 return RV;
5423 }
5424
5425 switch (E->getLHS()->getType().getObjCLifetime()) {
5427 std::tie(LHS, RHS) = CGF.EmitARCStoreStrong(E, Ignore);
5428 break;
5429
5431 std::tie(LHS, RHS) = CGF.EmitARCStoreAutoreleasing(E);
5432 break;
5433
5435 std::tie(LHS, RHS) = CGF.EmitARCStoreUnsafeUnretained(E, Ignore);
5436 break;
5437
5439 RHS = Visit(E->getRHS());
5440 LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
5441 RHS = CGF.EmitARCStoreWeak(LHS.getAddress(), RHS, Ignore);
5442 break;
5443
5445 // __block variables need to have the rhs evaluated first, plus
5446 // this should improve codegen just a little.
5447 Value *Previous = nullptr;
5448 QualType SrcType = E->getRHS()->getType();
5449 // Check if LHS is a bitfield, if RHS contains an implicit cast expression
5450 // we want to extract that value and potentially (if the bitfield sanitizer
5451 // is enabled) use it to check for an implicit conversion.
5452 if (E->getLHS()->refersToBitField())
5453 RHS = CGF.EmitWithOriginalRHSBitfieldAssignment(E, &Previous, &SrcType);
5454 else
5455 RHS = Visit(E->getRHS());
5456
5457 LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
5458
5459 // Store the value into the LHS. Bit-fields are handled specially
5460 // because the result is altered by the store, i.e., [C99 6.5.16p1]
5461 // 'An assignment expression has the value of the left operand after
5462 // the assignment...'.
5463 if (LHS.isBitField()) {
5464 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, &RHS);
5465 // If the expression contained an implicit conversion, make sure
5466 // to use the value before the scalar conversion.
5467 Value *Src = Previous ? Previous : RHS;
5468 QualType DstType = E->getLHS()->getType();
5469 CGF.EmitBitfieldConversionCheck(Src, SrcType, RHS, DstType,
5470 LHS.getBitFieldInfo(), E->getExprLoc());
5471 } else {
5472 CGF.EmitNullabilityCheck(LHS, RHS, E->getExprLoc());
5473 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS);
5474 }
5475 }
5476 // OpenMP: Handle lastprivate(condition:) in scalar assignment
5477 if (CGF.getLangOpts().OpenMP) {
5479 E->getLHS());
5480 }
5481
5482 // If the result is clearly ignored, return now.
5483 if (Ignore)
5484 return nullptr;
5485
5486 // The result of an assignment in C is the assigned r-value.
5487 if (!CGF.getLangOpts().CPlusPlus)
5488 return RHS;
5489
5490 // If the lvalue is non-volatile, return the computed value of the assignment.
5491 if (!LHS.isVolatileQualified())
5492 return RHS;
5493
5494 // Otherwise, reload the value.
5495 return EmitLoadOfLValue(LHS, E->getExprLoc());
5496}
5497
5498Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
5499 auto HasLHSSkip = CGF.hasSkipCounter(E);
5500 auto HasRHSSkip = CGF.hasSkipCounter(E->getRHS());
5501
5502 // Perform vector logical and on comparisons with zero vectors.
5503 if (E->getType()->isVectorType()) {
5505
5506 Value *LHS = Visit(E->getLHS());
5507 Value *RHS = Visit(E->getRHS());
5508 Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
5509 if (LHS->getType()->isFPOrFPVectorTy()) {
5510 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(
5511 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts()));
5512 LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp");
5513 RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp");
5514 } else {
5515 LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
5516 RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
5517 }
5518 Value *And = Builder.CreateAnd(LHS, RHS);
5519 return Builder.CreateSExt(And, ConvertType(E->getType()), "sext");
5520 }
5521
5522 bool InstrumentRegions = CGF.CGM.getCodeGenOpts().hasProfileClangInstr();
5523 llvm::Type *ResTy = ConvertType(E->getType());
5524
5525 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
5526 // If we have 1 && X, just emit X without inserting the control flow.
5527 bool LHSCondVal;
5528 if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
5529 if (LHSCondVal) { // If we have 1 && X, just emit X.
5530 CGF.incrementProfileCounter(CGF.UseExecPath, E, /*UseBoth=*/true);
5531
5532 // If the top of the logical operator nest, reset the MCDC temp to 0.
5533 if (CGF.isMCDCDecisionExpr(E))
5535
5536 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
5537
5538 // If we're generating for profiling or coverage, generate a branch to a
5539 // block that increments the RHS counter needed to track branch condition
5540 // coverage. In this case, use "FBlock" as both the final "TrueBlock" and
5541 // "FalseBlock" after the increment is done.
5542 if (InstrumentRegions &&
5544 CGF.maybeUpdateMCDCCondBitmap(E->getRHS(), RHSCond);
5545 llvm::BasicBlock *FBlock = CGF.createBasicBlock("land.end");
5546 llvm::BasicBlock *RHSSkip =
5547 (HasRHSSkip ? CGF.createBasicBlock("land.rhsskip") : FBlock);
5548 llvm::BasicBlock *RHSBlockCnt = CGF.createBasicBlock("land.rhscnt");
5549 Builder.CreateCondBr(RHSCond, RHSBlockCnt, RHSSkip);
5550 CGF.EmitBlock(RHSBlockCnt);
5552 CGF.EmitBranch(FBlock);
5553 if (HasRHSSkip) {
5554 CGF.EmitBlock(RHSSkip);
5556 }
5557 CGF.EmitBlock(FBlock);
5558 } else
5559 CGF.markStmtMaybeUsed(E->getRHS());
5560
5561 // If the top of the logical operator nest, update the MCDC bitmap.
5562 if (CGF.isMCDCDecisionExpr(E))
5564
5565 // ZExt result to int or bool.
5566 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext");
5567 }
5568
5569 // 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
5570 if (!CGF.ContainsLabel(E->getRHS())) {
5571 CGF.markStmtAsUsed(false, E);
5572 if (HasLHSSkip)
5574
5575 CGF.markStmtMaybeUsed(E->getRHS());
5576
5577 return llvm::Constant::getNullValue(ResTy);
5578 }
5579 }
5580
5581 // If the top of the logical operator nest, reset the MCDC temp to 0.
5582 if (CGF.isMCDCDecisionExpr(E))
5584
5585 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
5586 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
5587
5588 llvm::BasicBlock *LHSFalseBlock =
5589 (HasLHSSkip ? CGF.createBasicBlock("land.lhsskip") : ContBlock);
5590
5591 CodeGenFunction::ConditionalEvaluation eval(CGF);
5592
5593 // Branch on the LHS first. If it is false, go to the failure (cont) block.
5594 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, LHSFalseBlock,
5595 CGF.getProfileCount(E->getRHS()));
5596
5597 if (HasLHSSkip) {
5598 CGF.EmitBlock(LHSFalseBlock);
5600 CGF.EmitBranch(ContBlock);
5601 }
5602
5603 // Any edges into the ContBlock are now from an (indeterminate number of)
5604 // edges from this first condition. All of these values will be false. Start
5605 // setting up the PHI node in the Cont Block for this.
5606 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2,
5607 "", ContBlock);
5608 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
5609 PI != PE; ++PI)
5610 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
5611
5612 eval.begin(CGF);
5613 CGF.EmitBlock(RHSBlock);
5615 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
5616 eval.end(CGF);
5617
5618 // Reaquire the RHS block, as there may be subblocks inserted.
5619 RHSBlock = Builder.GetInsertBlock();
5620
5621 // If we're generating for profiling or coverage, generate a branch on the
5622 // RHS to a block that increments the RHS true counter needed to track branch
5623 // condition coverage.
5624 llvm::BasicBlock *ContIncoming = RHSBlock;
5625 if (InstrumentRegions &&
5627 CGF.maybeUpdateMCDCCondBitmap(E->getRHS(), RHSCond);
5628 llvm::BasicBlock *RHSBlockCnt = CGF.createBasicBlock("land.rhscnt");
5629 llvm::BasicBlock *RHSBlockSkip =
5630 (HasRHSSkip ? CGF.createBasicBlock("land.rhsskip") : ContBlock);
5631 Builder.CreateCondBr(RHSCond, RHSBlockCnt, RHSBlockSkip);
5632 CGF.EmitBlock(RHSBlockCnt);
5634 CGF.EmitBranch(ContBlock);
5635 PN->addIncoming(RHSCond, RHSBlockCnt);
5636 if (HasRHSSkip) {
5637 CGF.EmitBlock(RHSBlockSkip);
5639 CGF.EmitBranch(ContBlock);
5640 ContIncoming = RHSBlockSkip;
5641 }
5642 }
5643
5644 // Emit an unconditional branch from this block to ContBlock.
5645 {
5646 // There is no need to emit line number for unconditional branch.
5647 auto NL = ApplyDebugLocation::CreateEmpty(CGF);
5648 CGF.EmitBlock(ContBlock);
5649 }
5650 // Insert an entry into the phi node for the edge with the value of RHSCond.
5651 PN->addIncoming(RHSCond, ContIncoming);
5652
5653 // If the top of the logical operator nest, update the MCDC bitmap.
5654 if (CGF.isMCDCDecisionExpr(E))
5656
5657 // Artificial location to preserve the scope information
5658 {
5660 PN->setDebugLoc(Builder.getCurrentDebugLocation());
5661 }
5662
5663 // ZExt result to int.
5664 return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext");
5665}
5666
5667Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
5668 auto HasLHSSkip = CGF.hasSkipCounter(E);
5669 auto HasRHSSkip = CGF.hasSkipCounter(E->getRHS());
5670
5671 // Perform vector logical or on comparisons with zero vectors.
5672 if (E->getType()->isVectorType()) {
5674
5675 Value *LHS = Visit(E->getLHS());
5676 Value *RHS = Visit(E->getRHS());
5677 Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
5678 if (LHS->getType()->isFPOrFPVectorTy()) {
5679 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(
5680 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts()));
5681 LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp");
5682 RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp");
5683 } else {
5684 LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
5685 RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
5686 }
5687 Value *Or = Builder.CreateOr(LHS, RHS);
5688 return Builder.CreateSExt(Or, ConvertType(E->getType()), "sext");
5689 }
5690
5691 bool InstrumentRegions = CGF.CGM.getCodeGenOpts().hasProfileClangInstr();
5692 llvm::Type *ResTy = ConvertType(E->getType());
5693
5694 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
5695 // If we have 0 || X, just emit X without inserting the control flow.
5696 bool LHSCondVal;
5697 if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
5698 if (!LHSCondVal) { // If we have 0 || X, just emit X.
5699 CGF.incrementProfileCounter(CGF.UseExecPath, E, /*UseBoth=*/true);
5700
5701 // If the top of the logical operator nest, reset the MCDC temp to 0.
5702 if (CGF.isMCDCDecisionExpr(E))
5704
5705 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
5706
5707 // If we're generating for profiling or coverage, generate a branch to a
5708 // block that increments the RHS counter need to track branch condition
5709 // coverage. In this case, use "FBlock" as both the final "TrueBlock" and
5710 // "FalseBlock" after the increment is done.
5711 if (InstrumentRegions &&
5713 CGF.maybeUpdateMCDCCondBitmap(E->getRHS(), RHSCond);
5714 llvm::BasicBlock *FBlock = CGF.createBasicBlock("lor.end");
5715 llvm::BasicBlock *RHSSkip =
5716 (HasRHSSkip ? CGF.createBasicBlock("lor.rhsskip") : FBlock);
5717 llvm::BasicBlock *RHSBlockCnt = CGF.createBasicBlock("lor.rhscnt");
5718 Builder.CreateCondBr(RHSCond, RHSSkip, RHSBlockCnt);
5719 CGF.EmitBlock(RHSBlockCnt);
5721 CGF.EmitBranch(FBlock);
5722 if (HasRHSSkip) {
5723 CGF.EmitBlock(RHSSkip);
5725 }
5726 CGF.EmitBlock(FBlock);
5727 } else
5728 CGF.markStmtMaybeUsed(E->getRHS());
5729
5730 // If the top of the logical operator nest, update the MCDC bitmap.
5731 if (CGF.isMCDCDecisionExpr(E))
5733
5734 // ZExt result to int or bool.
5735 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext");
5736 }
5737
5738 // 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
5739 if (!CGF.ContainsLabel(E->getRHS())) {
5740 CGF.markStmtAsUsed(false, E);
5741 if (HasLHSSkip)
5743
5744 CGF.markStmtMaybeUsed(E->getRHS());
5745
5746 return llvm::ConstantInt::get(ResTy, 1);
5747 }
5748 }
5749
5750 // If the top of the logical operator nest, reset the MCDC temp to 0.
5751 if (CGF.isMCDCDecisionExpr(E))
5753
5754 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
5755 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
5756 llvm::BasicBlock *LHSTrueBlock =
5757 (HasLHSSkip ? CGF.createBasicBlock("lor.lhsskip") : ContBlock);
5758
5759 CodeGenFunction::ConditionalEvaluation eval(CGF);
5760
5761 // Branch on the LHS first. If it is true, go to the success (cont) block.
5762 CGF.EmitBranchOnBoolExpr(E->getLHS(), LHSTrueBlock, RHSBlock,
5764 CGF.getProfileCount(E->getRHS()));
5765
5766 if (HasLHSSkip) {
5767 CGF.EmitBlock(LHSTrueBlock);
5769 CGF.EmitBranch(ContBlock);
5770 }
5771
5772 // Any edges into the ContBlock are now from an (indeterminate number of)
5773 // edges from this first condition. All of these values will be true. Start
5774 // setting up the PHI node in the Cont Block for this.
5775 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2,
5776 "", ContBlock);
5777 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
5778 PI != PE; ++PI)
5779 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
5780
5781 eval.begin(CGF);
5782
5783 // Emit the RHS condition as a bool value.
5784 CGF.EmitBlock(RHSBlock);
5786 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
5787
5788 eval.end(CGF);
5789
5790 // Reaquire the RHS block, as there may be subblocks inserted.
5791 RHSBlock = Builder.GetInsertBlock();
5792
5793 // If we're generating for profiling or coverage, generate a branch on the
5794 // RHS to a block that increments the RHS true counter needed to track branch
5795 // condition coverage.
5796 llvm::BasicBlock *ContIncoming = RHSBlock;
5797 if (InstrumentRegions &&
5799 CGF.maybeUpdateMCDCCondBitmap(E->getRHS(), RHSCond);
5800 llvm::BasicBlock *RHSBlockCnt = CGF.createBasicBlock("lor.rhscnt");
5801 llvm::BasicBlock *RHSTrueBlock =
5802 (HasRHSSkip ? CGF.createBasicBlock("lor.rhsskip") : ContBlock);
5803 Builder.CreateCondBr(RHSCond, RHSTrueBlock, RHSBlockCnt);
5804 CGF.EmitBlock(RHSBlockCnt);
5806 CGF.EmitBranch(ContBlock);
5807 PN->addIncoming(RHSCond, RHSBlockCnt);
5808 if (HasRHSSkip) {
5809 CGF.EmitBlock(RHSTrueBlock);
5811 CGF.EmitBranch(ContBlock);
5812 ContIncoming = RHSTrueBlock;
5813 }
5814 }
5815
5816 // Emit an unconditional branch from this block to ContBlock. Insert an entry
5817 // into the phi node for the edge with the value of RHSCond.
5818 CGF.EmitBlock(ContBlock);
5819 PN->addIncoming(RHSCond, ContIncoming);
5820
5821 // If the top of the logical operator nest, update the MCDC bitmap.
5822 if (CGF.isMCDCDecisionExpr(E))
5824
5825 // ZExt result to int.
5826 return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext");
5827}
5828
5829Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
5830 CGF.EmitIgnoredExpr(E->getLHS());
5831 CGF.EnsureInsertPoint();
5832 return Visit(E->getRHS());
5833}
5834
5835//===----------------------------------------------------------------------===//
5836// Other Operators
5837//===----------------------------------------------------------------------===//
5838
5839/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
5840/// expression is cheap enough and side-effect-free enough to evaluate
5841/// unconditionally instead of conditionally. This is used to convert control
5842/// flow into selects in some cases.
5844 CodeGenFunction &CGF) {
5845 // Anything that is an integer or floating point constant is fine.
5846 return E->IgnoreParens()->isEvaluatable(CGF.getContext());
5847
5848 // Even non-volatile automatic variables can't be evaluated unconditionally.
5849 // Referencing a thread_local may cause non-trivial initialization work to
5850 // occur. If we're inside a lambda and one of the variables is from the scope
5851 // outside the lambda, that function may have returned already. Reading its
5852 // locals is a bad idea. Also, these reads may introduce races there didn't
5853 // exist in the source-level program.
5854}
5855
5856
5857Value *ScalarExprEmitter::
5858VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
5859 TestAndClearIgnoreResultAssign();
5860
5861 // Bind the common expression if necessary.
5862 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
5863
5864 Expr *condExpr = E->getCond();
5865 Expr *lhsExpr = E->getTrueExpr();
5866 Expr *rhsExpr = E->getFalseExpr();
5867
5868 // If the condition constant folds and can be elided, try to avoid emitting
5869 // the condition and the dead arm.
5870 bool CondExprBool;
5871 if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
5872 Expr *live = lhsExpr, *dead = rhsExpr;
5873 if (!CondExprBool) std::swap(live, dead);
5874
5875 // If the dead side doesn't have labels we need, just emit the Live part.
5876 if (!CGF.ContainsLabel(dead)) {
5877 CGF.incrementProfileCounter(CondExprBool ? CGF.UseExecPath
5878 : CGF.UseSkipPath,
5879 E, /*UseBoth=*/true);
5880 Value *Result = Visit(live);
5881 CGF.markStmtMaybeUsed(dead);
5882
5883 // If the live part is a throw expression, it acts like it has a void
5884 // type, so evaluating it returns a null Value*. However, a conditional
5885 // with non-void type must return a non-null Value*.
5886 if (!Result && !E->getType()->isVoidType())
5887 Result = llvm::UndefValue::get(CGF.ConvertType(E->getType()));
5888
5889 return Result;
5890 }
5891 }
5892
5893 // OpenCL: If the condition is a vector, we can treat this condition like
5894 // the select function.
5895 if (CGF.getLangOpts().OpenCL && (condExpr->getType()->isVectorType() ||
5896 condExpr->getType()->isExtVectorType())) {
5898
5899 llvm::Value *CondV = CGF.EmitScalarExpr(condExpr);
5900 llvm::Value *LHS = Visit(lhsExpr);
5901 llvm::Value *RHS = Visit(rhsExpr);
5902
5903 llvm::Type *condType = ConvertType(condExpr->getType());
5904 auto *vecTy = cast<llvm::FixedVectorType>(condType);
5905
5906 unsigned numElem = vecTy->getNumElements();
5907 llvm::Type *elemType = vecTy->getElementType();
5908
5909 llvm::Value *zeroVec = llvm::Constant::getNullValue(vecTy);
5910 llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec);
5911 llvm::Value *tmp = Builder.CreateSExt(
5912 TestMSB, llvm::FixedVectorType::get(elemType, numElem), "sext");
5913 llvm::Value *tmp2 = Builder.CreateNot(tmp);
5914
5915 // Cast float to int to perform ANDs if necessary.
5916 llvm::Value *RHSTmp = RHS;
5917 llvm::Value *LHSTmp = LHS;
5918 bool wasCast = false;
5919 llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType());
5920 if (rhsVTy->getElementType()->isFloatingPointTy()) {
5921 RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType());
5922 LHSTmp = Builder.CreateBitCast(LHS, tmp->getType());
5923 wasCast = true;
5924 }
5925
5926 llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2);
5927 llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp);
5928 llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond");
5929 if (wasCast)
5930 tmp5 = Builder.CreateBitCast(tmp5, RHS->getType());
5931
5932 return tmp5;
5933 }
5934
5935 if (condExpr->getType()->isVectorType() ||
5936 condExpr->getType()->isSveVLSBuiltinType()) {
5938
5939 llvm::Value *CondV = CGF.EmitScalarExpr(condExpr);
5940 llvm::Value *LHS = Visit(lhsExpr);
5941 llvm::Value *RHS = Visit(rhsExpr);
5942
5943 llvm::Type *CondType = ConvertType(condExpr->getType());
5944 auto *VecTy = cast<llvm::VectorType>(CondType);
5945
5946 if (VecTy->getElementType()->isIntegerTy(1))
5947 return Builder.CreateSelect(CondV, LHS, RHS, "vector_select");
5948
5949 // OpenCL uses the MSB of the mask vector.
5950 llvm::Value *ZeroVec = llvm::Constant::getNullValue(VecTy);
5951 if (condExpr->getType()->isExtVectorType())
5952 CondV = Builder.CreateICmpSLT(CondV, ZeroVec, "vector_cond");
5953 else
5954 CondV = Builder.CreateICmpNE(CondV, ZeroVec, "vector_cond");
5955 return Builder.CreateSelect(CondV, LHS, RHS, "vector_select");
5956 }
5957
5958 // If this is a really simple expression (like x ? 4 : 5), emit this as a
5959 // select instead of as control flow. We can only do this if it is cheap and
5960 // safe to evaluate the LHS and RHS unconditionally.
5964 llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr);
5965 llvm::Value *StepV = Builder.CreateZExtOrBitCast(CondV, CGF.Int64Ty);
5966
5967 CGF.incrementProfileCounter(E, StepV);
5968
5969 llvm::Value *LHS = Visit(lhsExpr);
5970 llvm::Value *RHS = Visit(rhsExpr);
5971 if (!LHS) {
5972 // If the conditional has void type, make sure we return a null Value*.
5973 assert(!RHS && "LHS and RHS types must match");
5974 return nullptr;
5975 }
5976 return Builder.CreateSelect(CondV, LHS, RHS, "cond");
5977 }
5978
5979 // If the top of the logical operator nest, reset the MCDC temp to 0.
5980 if (auto E = CGF.stripCond(condExpr); CGF.isMCDCDecisionExpr(E))
5982
5983 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
5984 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
5985 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
5986
5987 CodeGenFunction::ConditionalEvaluation eval(CGF);
5988 CGF.EmitBranchOnBoolExpr(condExpr, LHSBlock, RHSBlock,
5989 CGF.getProfileCount(lhsExpr));
5990
5991 CGF.EmitBlock(LHSBlock);
5992
5993 // If the top of the logical operator nest, update the MCDC bitmap for the
5994 // ConditionalOperator prior to visiting its LHS and RHS blocks, since they
5995 // may also contain a boolean expression.
5996 if (auto E = CGF.stripCond(condExpr); CGF.isMCDCDecisionExpr(E))
5998
6000 eval.begin(CGF);
6001 Value *LHS = Visit(lhsExpr);
6002 eval.end(CGF);
6003
6004 LHSBlock = Builder.GetInsertBlock();
6005 Builder.CreateBr(ContBlock);
6006
6007 CGF.EmitBlock(RHSBlock);
6008
6009 // If the top of the logical operator nest, update the MCDC bitmap for the
6010 // ConditionalOperator prior to visiting its LHS and RHS blocks, since they
6011 // may also contain a boolean expression.
6012 if (auto E = CGF.stripCond(condExpr); CGF.isMCDCDecisionExpr(E))
6014
6016 eval.begin(CGF);
6017 Value *RHS = Visit(rhsExpr);
6018 eval.end(CGF);
6019
6020 RHSBlock = Builder.GetInsertBlock();
6021 CGF.EmitBlock(ContBlock);
6022
6023 // If the LHS or RHS is a throw expression, it will be legitimately null.
6024 if (!LHS)
6025 return RHS;
6026 if (!RHS)
6027 return LHS;
6028
6029 // Create a PHI node for the real part.
6030 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), 2, "cond");
6031 PN->addIncoming(LHS, LHSBlock);
6032 PN->addIncoming(RHS, RHSBlock);
6033
6034 return PN;
6035}
6036
6037Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
6038 return Visit(E->getChosenSubExpr());
6039}
6040
6041Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
6042 Address ArgValue = Address::invalid();
6043 RValue ArgPtr = CGF.EmitVAArg(VE, ArgValue);
6044
6045 return ArgPtr.getScalarVal();
6046}
6047
6048Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *block) {
6049 return CGF.EmitBlockLiteral(block);
6050}
6051
6052// Convert a vec3 to vec4, or vice versa.
6054 Value *Src, unsigned NumElementsDst) {
6055 static constexpr int Mask[] = {0, 1, 2, -1};
6056 return Builder.CreateShuffleVector(Src, llvm::ArrayRef(Mask, NumElementsDst));
6057}
6058
6059// Create cast instructions for converting LLVM value \p Src to LLVM type \p
6060// DstTy. \p Src has the same size as \p DstTy. Both are single value types
6061// but could be scalar or vectors of different lengths, and either can be
6062// pointer.
6063// There are 4 cases:
6064// 1. non-pointer -> non-pointer : needs 1 bitcast
6065// 2. pointer -> pointer : needs 1 bitcast or addrspacecast
6066// 3. pointer -> non-pointer
6067// a) pointer -> intptr_t : needs 1 ptrtoint
6068// b) pointer -> non-intptr_t : needs 1 ptrtoint then 1 bitcast
6069// 4. non-pointer -> pointer
6070// a) intptr_t -> pointer : needs 1 inttoptr
6071// b) non-intptr_t -> pointer : needs 1 bitcast then 1 inttoptr
6072// Note: for cases 3b and 4b two casts are required since LLVM casts do not
6073// allow casting directly between pointer types and non-integer non-pointer
6074// types.
6076 const llvm::DataLayout &DL,
6077 Value *Src, llvm::Type *DstTy,
6078 StringRef Name = "") {
6079 auto SrcTy = Src->getType();
6080
6081 // Case 1.
6082 if (!SrcTy->isPointerTy() && !DstTy->isPointerTy())
6083 return Builder.CreateBitCast(Src, DstTy, Name);
6084
6085 // Case 2.
6086 if (SrcTy->isPointerTy() && DstTy->isPointerTy())
6087 return Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DstTy, Name);
6088
6089 // Case 3.
6090 if (SrcTy->isPointerTy() && !DstTy->isPointerTy()) {
6091 // Case 3b.
6092 if (!DstTy->isIntegerTy())
6093 Src = Builder.CreatePtrToInt(Src, DL.getIntPtrType(SrcTy));
6094 // Cases 3a and 3b.
6095 return Builder.CreateBitOrPointerCast(Src, DstTy, Name);
6096 }
6097
6098 // Case 4b.
6099 if (!SrcTy->isIntegerTy())
6100 Src = Builder.CreateBitCast(Src, DL.getIntPtrType(DstTy));
6101 // Cases 4a and 4b.
6102 return Builder.CreateIntToPtr(Src, DstTy, Name);
6103}
6104
6105Value *ScalarExprEmitter::VisitAsTypeExpr(AsTypeExpr *E) {
6106 Value *Src = CGF.EmitScalarExpr(E->getSrcExpr());
6107 llvm::Type *DstTy = ConvertType(E->getType());
6108
6109 llvm::Type *SrcTy = Src->getType();
6110 unsigned NumElementsSrc =
6112 ? cast<llvm::FixedVectorType>(SrcTy)->getNumElements()
6113 : 0;
6114 unsigned NumElementsDst =
6116 ? cast<llvm::FixedVectorType>(DstTy)->getNumElements()
6117 : 0;
6118
6119 // Use bit vector expansion for ext_vector_type boolean vectors.
6120 if (E->getType()->isExtVectorBoolType())
6121 return CGF.emitBoolVecConversion(Src, NumElementsDst, "astype");
6122
6123 // Going from vec3 to non-vec3 is a special case and requires a shuffle
6124 // vector to get a vec4, then a bitcast if the target type is different.
6125 if (NumElementsSrc == 3 && NumElementsDst != 3) {
6126 Src = ConvertVec3AndVec4(Builder, CGF, Src, 4);
6127 Src = createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), Src,
6128 DstTy);
6129
6130 Src->setName("astype");
6131 return Src;
6132 }
6133
6134 // Going from non-vec3 to vec3 is a special case and requires a bitcast
6135 // to vec4 if the original type is not vec4, then a shuffle vector to
6136 // get a vec3.
6137 if (NumElementsSrc != 3 && NumElementsDst == 3) {
6138 auto *Vec4Ty = llvm::FixedVectorType::get(
6139 cast<llvm::VectorType>(DstTy)->getElementType(), 4);
6140 Src = createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), Src,
6141 Vec4Ty);
6142
6143 Src = ConvertVec3AndVec4(Builder, CGF, Src, 3);
6144 Src->setName("astype");
6145 return Src;
6146 }
6147
6148 return createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(),
6149 Src, DstTy, "astype");
6150}
6151
6152Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) {
6153 return CGF.EmitAtomicExpr(E).getScalarVal();
6154}
6155
6156//===----------------------------------------------------------------------===//
6157// Entry Point into this File
6158//===----------------------------------------------------------------------===//
6159
6160/// Emit the computation of the specified expression of scalar type, ignoring
6161/// the result.
6162Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
6163 assert(E && hasScalarEvaluationKind(E->getType()) &&
6164 "Invalid scalar expression to emit");
6165
6166 return ScalarExprEmitter(*this, IgnoreResultAssign)
6167 .Visit(const_cast<Expr *>(E));
6168}
6169
6170/// Emit a conversion from the specified type to the specified destination type,
6171/// both of which are LLVM scalar types.
6173 QualType DstTy,
6174 SourceLocation Loc) {
6175 assert(hasScalarEvaluationKind(SrcTy) && hasScalarEvaluationKind(DstTy) &&
6176 "Invalid scalar expression to emit");
6177 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy, Loc);
6178}
6179
6180/// Emit a conversion from the specified complex type to the specified
6181/// destination type, where the destination type is an LLVM scalar type.
6183 QualType SrcTy,
6184 QualType DstTy,
6185 SourceLocation Loc) {
6186 assert(SrcTy->isAnyComplexType() && hasScalarEvaluationKind(DstTy) &&
6187 "Invalid complex -> scalar conversion");
6188 return ScalarExprEmitter(*this)
6189 .EmitComplexToScalarConversion(Src, SrcTy, DstTy, Loc);
6190}
6191
6192
6193Value *
6195 QualType PromotionType) {
6196 if (!PromotionType.isNull())
6197 return ScalarExprEmitter(*this).EmitPromoted(E, PromotionType);
6198 else
6199 return ScalarExprEmitter(*this).Visit(const_cast<Expr *>(E));
6200}
6201
6202
6205 bool isInc, bool isPre) {
6206 return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre);
6207}
6208
6210 // object->isa or (*object).isa
6211 // Generate code as for: *(Class*)object
6212
6213 Expr *BaseExpr = E->getBase();
6215 if (BaseExpr->isPRValue()) {
6216 llvm::Type *BaseTy =
6218 Addr = Address(EmitScalarExpr(BaseExpr), BaseTy, getPointerAlign());
6219 } else {
6220 Addr = EmitLValue(BaseExpr).getAddress();
6221 }
6222
6223 // Cast the address to Class*.
6224 Addr = Addr.withElementType(ConvertType(E->getType()));
6225 return MakeAddrLValue(Addr, E->getType());
6226}
6227
6228
6230 const CompoundAssignOperator *E) {
6232 ScalarExprEmitter Scalar(*this);
6233 Value *Result = nullptr;
6234 switch (E->getOpcode()) {
6235#define COMPOUND_OP(Op) \
6236 case BO_##Op##Assign: \
6237 return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \
6238 Result)
6239 COMPOUND_OP(Mul);
6240 COMPOUND_OP(Div);
6241 COMPOUND_OP(Rem);
6242 COMPOUND_OP(Add);
6243 COMPOUND_OP(Sub);
6244 COMPOUND_OP(Shl);
6245 COMPOUND_OP(Shr);
6247 COMPOUND_OP(Xor);
6248 COMPOUND_OP(Or);
6249#undef COMPOUND_OP
6250
6251 case BO_PtrMemD:
6252 case BO_PtrMemI:
6253 case BO_Mul:
6254 case BO_Div:
6255 case BO_Rem:
6256 case BO_Add:
6257 case BO_Sub:
6258 case BO_Shl:
6259 case BO_Shr:
6260 case BO_LT:
6261 case BO_GT:
6262 case BO_LE:
6263 case BO_GE:
6264 case BO_EQ:
6265 case BO_NE:
6266 case BO_Cmp:
6267 case BO_And:
6268 case BO_Xor:
6269 case BO_Or:
6270 case BO_LAnd:
6271 case BO_LOr:
6272 case BO_Assign:
6273 case BO_Comma:
6274 llvm_unreachable("Not valid compound assignment operators");
6275 }
6276
6277 llvm_unreachable("Unhandled compound assignment operator");
6278}
6279
6281 // The total (signed) byte offset for the GEP.
6282 llvm::Value *TotalOffset;
6283 // The offset overflow flag - true if the total offset overflows.
6284 llvm::Value *OffsetOverflows;
6285};
6286
6287/// Evaluate given GEPVal, which is either an inbounds GEP, or a constant,
6288/// and compute the total offset it applies from it's base pointer BasePtr.
6289/// Returns offset in bytes and a boolean flag whether an overflow happened
6290/// during evaluation.
6292 llvm::LLVMContext &VMContext,
6293 CodeGenModule &CGM,
6294 CGBuilderTy &Builder) {
6295 const auto &DL = CGM.getDataLayout();
6296
6297 // The total (signed) byte offset for the GEP.
6298 llvm::Value *TotalOffset = nullptr;
6299
6300 // Was the GEP already reduced to a constant?
6301 if (isa<llvm::Constant>(GEPVal)) {
6302 // Compute the offset by casting both pointers to integers and subtracting:
6303 // GEPVal = BasePtr + ptr(Offset) <--> Offset = int(GEPVal) - int(BasePtr)
6304 Value *BasePtr_int =
6305 Builder.CreatePtrToInt(BasePtr, DL.getIntPtrType(BasePtr->getType()));
6306 Value *GEPVal_int =
6307 Builder.CreatePtrToInt(GEPVal, DL.getIntPtrType(GEPVal->getType()));
6308 TotalOffset = Builder.CreateSub(GEPVal_int, BasePtr_int);
6309 return {TotalOffset, /*OffsetOverflows=*/Builder.getFalse()};
6310 }
6311
6312 auto *GEP = cast<llvm::GEPOperator>(GEPVal);
6313 assert(GEP->getPointerOperand() == BasePtr &&
6314 "BasePtr must be the base of the GEP.");
6315 assert(GEP->isInBounds() && "Expected inbounds GEP");
6316
6317 auto *IntPtrTy = DL.getIntPtrType(GEP->getPointerOperandType());
6318
6319 // Grab references to the signed add/mul overflow intrinsics for intptr_t.
6320 auto *Zero = llvm::ConstantInt::getNullValue(IntPtrTy);
6321 auto *SAddIntrinsic =
6322 CGM.getIntrinsic(llvm::Intrinsic::sadd_with_overflow, IntPtrTy);
6323 auto *SMulIntrinsic =
6324 CGM.getIntrinsic(llvm::Intrinsic::smul_with_overflow, IntPtrTy);
6325
6326 // The offset overflow flag - true if the total offset overflows.
6327 llvm::Value *OffsetOverflows = Builder.getFalse();
6328
6329 /// Return the result of the given binary operation.
6330 auto eval = [&](BinaryOperator::Opcode Opcode, llvm::Value *LHS,
6331 llvm::Value *RHS) -> llvm::Value * {
6332 assert((Opcode == BO_Add || Opcode == BO_Mul) && "Can't eval binop");
6333
6334 // If the operands are constants, return a constant result.
6335 if (auto *LHSCI = dyn_cast<llvm::ConstantInt>(LHS)) {
6336 if (auto *RHSCI = dyn_cast<llvm::ConstantInt>(RHS)) {
6337 llvm::APInt N;
6338 bool HasOverflow = mayHaveIntegerOverflow(LHSCI, RHSCI, Opcode,
6339 /*Signed=*/true, N);
6340 if (HasOverflow)
6341 OffsetOverflows = Builder.getTrue();
6342 return llvm::ConstantInt::get(VMContext, N);
6343 }
6344 }
6345
6346 // Otherwise, compute the result with checked arithmetic.
6347 auto *ResultAndOverflow = Builder.CreateCall(
6348 (Opcode == BO_Add) ? SAddIntrinsic : SMulIntrinsic, {LHS, RHS});
6349 OffsetOverflows = Builder.CreateOr(
6350 Builder.CreateExtractValue(ResultAndOverflow, 1), OffsetOverflows);
6351 return Builder.CreateExtractValue(ResultAndOverflow, 0);
6352 };
6353
6354 // Determine the total byte offset by looking at each GEP operand.
6355 for (auto GTI = llvm::gep_type_begin(GEP), GTE = llvm::gep_type_end(GEP);
6356 GTI != GTE; ++GTI) {
6357 llvm::Value *LocalOffset;
6358 auto *Index = GTI.getOperand();
6359 // Compute the local offset contributed by this indexing step:
6360 if (auto *STy = GTI.getStructTypeOrNull()) {
6361 // For struct indexing, the local offset is the byte position of the
6362 // specified field.
6363 unsigned FieldNo = cast<llvm::ConstantInt>(Index)->getZExtValue();
6364 LocalOffset = llvm::ConstantInt::get(
6365 IntPtrTy, DL.getStructLayout(STy)->getElementOffset(FieldNo));
6366 } else {
6367 // Otherwise this is array-like indexing. The local offset is the index
6368 // multiplied by the element size.
6369 auto *ElementSize =
6370 llvm::ConstantInt::get(IntPtrTy, GTI.getSequentialElementStride(DL));
6371 auto *IndexS = Builder.CreateIntCast(Index, IntPtrTy, /*isSigned=*/true);
6372 LocalOffset = eval(BO_Mul, ElementSize, IndexS);
6373 }
6374
6375 // If this is the first offset, set it as the total offset. Otherwise, add
6376 // the local offset into the running total.
6377 if (!TotalOffset || TotalOffset == Zero)
6378 TotalOffset = LocalOffset;
6379 else
6380 TotalOffset = eval(BO_Add, TotalOffset, LocalOffset);
6381 }
6382
6383 return {TotalOffset, OffsetOverflows};
6384}
6385
6386Value *
6387CodeGenFunction::EmitCheckedInBoundsGEP(llvm::Type *ElemTy, Value *Ptr,
6388 ArrayRef<Value *> IdxList,
6389 bool SignedIndices, bool IsSubtraction,
6390 SourceLocation Loc, const Twine &Name) {
6391 llvm::Type *PtrTy = Ptr->getType();
6392
6393 llvm::GEPNoWrapFlags NWFlags = llvm::GEPNoWrapFlags::inBounds();
6394 if (!SignedIndices && !IsSubtraction)
6395 NWFlags |= llvm::GEPNoWrapFlags::noUnsignedWrap();
6396
6397 Value *GEPVal = Builder.CreateGEP(ElemTy, Ptr, IdxList, Name, NWFlags);
6398
6399 // If the pointer overflow sanitizer isn't enabled, do nothing.
6400 if (!SanOpts.has(SanitizerKind::PointerOverflow))
6401 return GEPVal;
6402
6403 // Perform nullptr-and-offset check unless the nullptr is defined.
6404 bool PerformNullCheck = !NullPointerIsDefined(
6405 Builder.GetInsertBlock()->getParent(), PtrTy->getPointerAddressSpace());
6406 // Check for overflows unless the GEP got constant-folded,
6407 // and only in the default address space
6408 bool PerformOverflowCheck =
6409 !isa<llvm::Constant>(GEPVal) && PtrTy->getPointerAddressSpace() == 0;
6410
6411 if (!(PerformNullCheck || PerformOverflowCheck))
6412 return GEPVal;
6413
6414 const auto &DL = CGM.getDataLayout();
6415
6416 auto CheckOrdinal = SanitizerKind::SO_PointerOverflow;
6417 auto CheckHandler = SanitizerHandler::PointerOverflow;
6418 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
6419 llvm::Type *IntPtrTy = DL.getIntPtrType(PtrTy);
6420
6421 GEPOffsetAndOverflow EvaluatedGEP =
6422 EmitGEPOffsetInBytes(Ptr, GEPVal, getLLVMContext(), CGM, Builder);
6423
6424 assert((!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) ||
6425 EvaluatedGEP.OffsetOverflows == Builder.getFalse()) &&
6426 "If the offset got constant-folded, we don't expect that there was an "
6427 "overflow.");
6428
6429 auto *Zero = llvm::ConstantInt::getNullValue(IntPtrTy);
6430
6431 // Common case: if the total offset is zero, don't emit a check.
6432 if (EvaluatedGEP.TotalOffset == Zero)
6433 return GEPVal;
6434
6435 // Now that we've computed the total offset, add it to the base pointer (with
6436 // wrapping semantics).
6437 auto *IntPtr = Builder.CreatePtrToInt(Ptr, IntPtrTy);
6438 auto *ComputedGEP = Builder.CreateAdd(IntPtr, EvaluatedGEP.TotalOffset);
6439
6440 llvm::SmallVector<std::pair<llvm::Value *, SanitizerKind::SanitizerOrdinal>,
6441 2>
6442 Checks;
6443
6444 if (PerformNullCheck) {
6445 // If the base pointer evaluates to a null pointer value,
6446 // the only valid pointer this inbounds GEP can produce is also
6447 // a null pointer, so the offset must also evaluate to zero.
6448 // Likewise, if we have non-zero base pointer, we can not get null pointer
6449 // as a result, so the offset can not be -intptr_t(BasePtr).
6450 // In other words, both pointers are either null, or both are non-null,
6451 // or the behaviour is undefined.
6452 auto *BaseIsNotNullptr = Builder.CreateIsNotNull(Ptr);
6453 auto *ResultIsNotNullptr = Builder.CreateIsNotNull(ComputedGEP);
6454 auto *Valid = Builder.CreateICmpEQ(BaseIsNotNullptr, ResultIsNotNullptr);
6455 Checks.emplace_back(Valid, CheckOrdinal);
6456 }
6457
6458 if (PerformOverflowCheck) {
6459 // The GEP is valid if:
6460 // 1) The total offset doesn't overflow, and
6461 // 2) The sign of the difference between the computed address and the base
6462 // pointer matches the sign of the total offset.
6463 llvm::Value *ValidGEP;
6464 auto *NoOffsetOverflow = Builder.CreateNot(EvaluatedGEP.OffsetOverflows);
6465 if (SignedIndices) {
6466 // GEP is computed as `unsigned base + signed offset`, therefore:
6467 // * If offset was positive, then the computed pointer can not be
6468 // [unsigned] less than the base pointer, unless it overflowed.
6469 // * If offset was negative, then the computed pointer can not be
6470 // [unsigned] greater than the bas pointere, unless it overflowed.
6471 auto *PosOrZeroValid = Builder.CreateICmpUGE(ComputedGEP, IntPtr);
6472 auto *PosOrZeroOffset =
6473 Builder.CreateICmpSGE(EvaluatedGEP.TotalOffset, Zero);
6474 llvm::Value *NegValid = Builder.CreateICmpULT(ComputedGEP, IntPtr);
6475 ValidGEP =
6476 Builder.CreateSelect(PosOrZeroOffset, PosOrZeroValid, NegValid);
6477 } else if (!IsSubtraction) {
6478 // GEP is computed as `unsigned base + unsigned offset`, therefore the
6479 // computed pointer can not be [unsigned] less than base pointer,
6480 // unless there was an overflow.
6481 // Equivalent to `@llvm.uadd.with.overflow(%base, %offset)`.
6482 ValidGEP = Builder.CreateICmpUGE(ComputedGEP, IntPtr);
6483 } else {
6484 // GEP is computed as `unsigned base - unsigned offset`, therefore the
6485 // computed pointer can not be [unsigned] greater than base pointer,
6486 // unless there was an overflow.
6487 // Equivalent to `@llvm.usub.with.overflow(%base, sub(0, %offset))`.
6488 ValidGEP = Builder.CreateICmpULE(ComputedGEP, IntPtr);
6489 }
6490 ValidGEP = Builder.CreateAnd(ValidGEP, NoOffsetOverflow);
6491 Checks.emplace_back(ValidGEP, CheckOrdinal);
6492 }
6493
6494 assert(!Checks.empty() && "Should have produced some checks.");
6495
6496 llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc)};
6497 // Pass the computed GEP to the runtime to avoid emitting poisoned arguments.
6498 llvm::Value *DynamicArgs[] = {IntPtr, ComputedGEP};
6499 EmitCheck(Checks, CheckHandler, StaticArgs, DynamicArgs);
6500
6501 return GEPVal;
6502}
6503
6505 Address Addr, ArrayRef<Value *> IdxList, llvm::Type *elementType,
6506 bool SignedIndices, bool IsSubtraction, SourceLocation Loc, CharUnits Align,
6507 const Twine &Name) {
6508 if (!SanOpts.has(SanitizerKind::PointerOverflow)) {
6509 llvm::GEPNoWrapFlags NWFlags = llvm::GEPNoWrapFlags::inBounds();
6510 if (!SignedIndices && !IsSubtraction)
6511 NWFlags |= llvm::GEPNoWrapFlags::noUnsignedWrap();
6512
6513 return Builder.CreateGEP(Addr, IdxList, elementType, Align, Name, NWFlags);
6514 }
6515
6516 return RawAddress(
6517 EmitCheckedInBoundsGEP(Addr.getElementType(), Addr.emitRawPointer(*this),
6518 IdxList, SignedIndices, IsSubtraction, Loc, Name),
6519 elementType, Align);
6520}
Defines the clang::ASTContext interface.
#define V(N, I)
static llvm::Value * EmitCompare(CGBuilderTy &Builder, CodeGenFunction &CGF, const BinaryOperator *E, llvm::Value *LHS, llvm::Value *RHS, CompareKind Kind, const char *NameSuffix="")
static void EmitHLSLElementwiseCast(CodeGenFunction &CGF, LValue DestVal, LValue SrcVal, SourceLocation Loc)
static int getAsInt32(llvm::ConstantInt *C, llvm::Type *I32Ty)
static llvm::Value * EmitIsNegativeTestHelper(Value *V, QualType VType, const char *Name, CGBuilderTy &Builder)
static Value * createCastsForTypeOfSameSize(CGBuilderTy &Builder, const llvm::DataLayout &DL, Value *Src, llvm::Type *DstTy, StringRef Name="")
static bool isLValueKnownNonNull(CodeGenFunction &CGF, const Expr *E)
IntrinsicType
@ VCMPGT
@ VCMPEQ
static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT, BuiltinType::Kind ElemKind)
static GEPOffsetAndOverflow EmitGEPOffsetInBytes(Value *BasePtr, Value *GEPVal, llvm::LLVMContext &VMContext, CodeGenModule &CGM, CGBuilderTy &Builder)
Evaluate given GEPVal, which is either an inbounds GEP, or a constant, and compute the total offset i...
static bool isDeclRefKnownNonNull(CodeGenFunction &CGF, const ValueDecl *D)
static bool PromotionIsPotentiallyEligibleForImplicitIntegerConversionCheck(QualType SrcType, QualType DstType)
static std::pair< ScalarExprEmitter::ImplicitConversionCheckKind, std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > EmitBitfieldTruncationCheckHelper(Value *Src, QualType SrcType, Value *Dst, QualType DstType, CGBuilderTy &Builder)
static Value * buildFMulAdd(llvm::Instruction *MulOp, Value *Addend, const CodeGenFunction &CGF, CGBuilderTy &Builder, bool negMul, bool negAdd)
static std::pair< ScalarExprEmitter::ImplicitConversionCheckKind, std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > EmitBitfieldSignChangeCheckHelper(Value *Src, QualType SrcType, Value *Dst, QualType DstType, CGBuilderTy &Builder)
static std::pair< ScalarExprEmitter::ImplicitConversionCheckKind, std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > EmitIntegerSignChangeCheckHelper(Value *Src, QualType SrcType, Value *Dst, QualType DstType, CGBuilderTy &Builder)
static int getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx, unsigned Off)
static std::pair< ScalarExprEmitter::ImplicitConversionCheckKind, std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > EmitIntegerTruncationCheckHelper(Value *Src, QualType SrcType, Value *Dst, QualType DstType, CGBuilderTy &Builder)
static Value * ConvertVec3AndVec4(CGBuilderTy &Builder, CodeGenFunction &CGF, Value *Src, unsigned NumElementsDst)
static Value * tryEmitFMulAdd(const BinOpInfo &op, const CodeGenFunction &CGF, CGBuilderTy &Builder, bool isSub=false)
static BinOpInfo createBinOpInfoFromIncDec(const UnaryOperator *E, llvm::Value *InVal, bool IsInc, FPOptions FPFeatures)
#define HANDLE_BINOP(OP)
#define COMPOUND_OP(Op)
#define HANDLEBINOP(OP)
static mlir::Value emitPointerArithmetic(CIRGenFunction &cgf, const BinOpInfo &op, bool isSubtraction)
Emit pointer + index arithmetic.
static bool isCheapEnoughToEvaluateUnconditionally(const Expr *e, CIRGenFunction &cgf)
Return true if the specified expression is cheap enough and side-effect-free enough to evaluate uncon...
static std::optional< QualType > getUnwidenedIntegerType(const ASTContext &astContext, const Expr *e)
If e is a widened promoted integer, get its base (unpromoted) type.
#define VISITCOMP(CODE)
static uint32_t getBitWidth(const Expr *E)
llvm::APSInt APSInt
Definition Compiler.cpp:24
static Decl::Kind getKind(const Decl *D)
FormatToken * Previous
The previous token in the unwrapped line.
Result
Implement __builtin_bit_cast and related operations.
SanitizerHandler
static QualType getPointeeType(const MemRegion *R)
This file contains the declaration of TrapReasonBuilder and related classes.
llvm::APInt getValue() const
APSInt & getInt()
Definition APValue.h:508
bool isLValue() const
Definition APValue.h:490
bool isInt() const
Definition APValue.h:485
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:228
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
unsigned getIntWidth(QualType T) const
const llvm::fltSemantics & getFloatTypeSemantics(QualType T) const
Return the APFloat 'semantics' for the specified scalar floating point type.
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
CanQualType FloatTy
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
QualType getVectorType(QualType VectorType, unsigned NumElts, VectorKind VecKind) const
Return the unique reference to a vector type of the specified element type and size.
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
const LangOptions & getLangOpts() const
Definition ASTContext.h:960
bool isTypeIgnoredBySanitizer(const SanitizerMask &Mask, const QualType &Ty) const
Check if a type can have its sanitizer instrumentation elided based on its presence within an ignorel...
CanQualType BoolTy
unsigned getOpenMPDefaultSimdAlign(QualType T) const
Get default simd alignment of the specified complete type in bits.
llvm::FixedPointSemantics getFixedPointSemantics(QualType Ty) const
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
QualType getPromotedIntegerType(QualType PromotableType) const
Return the type that PromotableType will promote to: C99 6.3.1.1p2, assuming that PromotableType is a...
const VariableArrayType * getAsVariableArrayType(QualType T) const
QualType getComplexType(QualType T) const
Return the uniqued reference to the type for a complex number with the specified element type.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:925
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
unsigned getTargetAddressSpace(LangAS AS) const
bool isPromotableIntegerType(QualType T) const
More type predicates useful for type checking/promotion.
static bool hasSameUnqualifiedType(QualType T1, QualType T2)
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
bool isUnaryOverflowPatternExcluded(const UnaryOperator *UO)
uint64_t getCharWidth() const
Return the size of the character type, in bits.
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Expr * getCond() const
getCond - Return the expression representing the condition for the ?
Definition Expr.h:4534
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition Expr.h:4540
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition Expr.h:4546
LabelDecl * getLabel() const
Definition Expr.h:4576
uint64_t getValue() const
Definition ExprCXX.h:3048
QualType getElementType() const
Definition TypeBase.h:3796
Expr * getSrcExpr() const
getSrcExpr - Return the Expr to be converted.
Definition Expr.h:6752
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4041
Expr * getLHS() const
Definition Expr.h:4091
static Opcode getOpForCompoundAssignment(Opcode Opc)
Definition Expr.h:4188
bool isCompoundAssignmentOp() const
Definition Expr.h:4185
SourceLocation getExprLoc() const
Definition Expr.h:4082
bool isShiftOp() const
Definition Expr.h:4130
Expr * getRHS() const
Definition Expr.h:4093
bool isShiftAssignOp() const
Definition Expr.h:4199
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Get the FP features status of this operator.
Definition Expr.h:4254
static bool isNullPointerArithmeticExtension(ASTContext &Ctx, Opcode Opc, const Expr *LHS, const Expr *RHS)
Return true if a binary operator using the specified opcode and operands would match the 'p = (i8*)nu...
Definition Expr.cpp:2210
Opcode getOpcode() const
Definition Expr.h:4086
BinaryOperatorKind Opcode
Definition Expr.h:4046
bool isVirtual() const
Determines whether the base class is a virtual base class (or not).
Definition DeclCXX.h:203
QualType getType() const
Retrieves the type of the base class.
Definition DeclCXX.h:249
bool getValue() const
Definition ExprCXX.h:744
Expr * getExpr()
Get the initialization expression that will be used.
Definition ExprCXX.cpp:1112
bool getValue() const
Definition ExprCXX.h:4332
Expr * getSemanticForm()
Get an equivalent semantic form for this expression.
Definition ExprCXX.h:308
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition Expr.cpp:1608
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition Expr.h:3679
path_iterator path_begin()
Definition Expr.h:3749
CastKind getCastKind() const
Definition Expr.h:3723
bool changesVolatileQualification() const
Return.
Definition Expr.h:3813
path_iterator path_end()
Definition Expr.h:3750
Expr * getSubExpr()
Definition Expr.h:3729
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
bool isOne() const
isOne - Test whether the quantity equals one.
Definition CharUnits.h:125
unsigned getValue() const
Definition Expr.h:1632
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition Expr.h:4887
bool hasProfileClangInstr() const
Check if Clang profile instrumenation is on.
SanitizerSet SanitizeTrap
Set of sanitizer checks that trap rather than diagnose.
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition Address.h:128
static Address invalid()
Definition Address.h:176
bool isValid() const
Definition Address.h:177
A scoped helper to set the current source atom group for CGDebugInfo::addInstToCurrentSourceAtom.
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition CGBuilder.h:118
virtual llvm::Constant * EmitNullMemberPointer(const MemberPointerType *MPT)
Create a null member pointer of the given type.
Definition CGCXXABI.cpp:102
virtual llvm::Value * EmitMemberPointerIsNotNull(CodeGenFunction &CGF, llvm::Value *MemPtr, const MemberPointerType *MPT)
Determine if a member pointer is non-null. Returns an i1.
Definition CGCXXABI.cpp:94
virtual llvm::Value * EmitMemberPointerComparison(CodeGenFunction &CGF, llvm::Value *L, llvm::Value *R, const MemberPointerType *MPT, bool Inequality)
Emit a comparison between two member pointers. Returns an i1.
Definition CGCXXABI.cpp:84
virtual llvm::Value * EmitMemberPointerConversion(CodeGenFunction &CGF, const CastExpr *E, llvm::Value *Src)
Perform a derived-to-base, base-to-derived, or bitcast member pointer conversion.
Definition CGCXXABI.cpp:71
void EmitPseudoVariable(CGBuilderTy &Builder, llvm::Instruction *Value, QualType Ty)
Emit a pseudo variable and debug info for an intermediate value if it does not correspond to a variab...
void addHeapAllocSiteMetadata(llvm::CallBase *CallSite, QualType AllocatedTy, SourceLocation Loc)
Add heapallocsite metadata for MSAllocator calls.
void emitInitListOpaqueValues(CodeGenFunction &CGF, InitListExpr *E)
virtual void checkAndEmitLastprivateConditional(CodeGenFunction &CGF, const Expr *LHS)
Checks if the provided LVal is lastprivate conditional and emits the code to update the value of the ...
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Value * EmitObjCConsumeObject(QualType T, llvm::Value *Ptr)
Produce the code for a CK_ARCConsumeObject.
Definition CGObjC.cpp:2184
void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount, Stmt::Likelihood LH=Stmt::LH_None, const Expr *ConditionalOp=nullptr, const VarDecl *ConditionalDecl=nullptr)
EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g.
RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, ReturnValueSlot Return=ReturnValueSlot())
Definition CGObjC.cpp:591
llvm::Value * emitBoolVecConversion(llvm::Value *SrcVec, unsigned NumElementsDst, const llvm::Twine &Name="")
CurrentSourceLocExprScope CurSourceLocExprScope
Source location information about the default argument or member initializer expression we're evaluat...
llvm::Value * performAddrSpaceCast(llvm::Value *Src, llvm::Type *DestTy)
llvm::Value * EmitARCReclaimReturnedObject(const Expr *e, bool allowUnsafeClaim)
Definition CGObjC.cpp:3121
std::pair< LValue, llvm::Value * > EmitARCStoreAutoreleasing(const BinaryOperator *e)
Definition CGObjC.cpp:3711
void SetDivFPAccuracy(llvm::Value *Val)
Set the minimum required accuracy of the given sqrt operation based on CodeGenOpts.
Definition CGExpr.cpp:7235
llvm::Value * EmitObjCSelectorExpr(const ObjCSelectorExpr *E)
Emit a selector.
Definition CGObjC.cpp:275
SanitizerSet SanOpts
Sanitizers enabled for this function.
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
llvm::Value * EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E)
Definition CGObjC.cpp:269
llvm::BlockAddress * GetAddrOfLabel(const LabelDecl *L)
const CastExpr * CurCast
If a cast expression is being visited, this holds the current cast's expression.
static bool hasScalarEvaluationKind(QualType T)
llvm::Type * ConvertType(QualType T)
llvm::Value * EmitObjCProtocolExpr(const ObjCProtocolExpr *E)
Definition CGObjC.cpp:283
llvm::Value * EmitPointerAuthQualify(PointerAuthQualifier Qualifier, llvm::Value *Pointer, QualType ValueType, Address StorageAddress, bool IsKnownNonNull)
void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint=true)
LValue EmitObjCIsaExpr(const ObjCIsaExpr *E)
void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, llvm::Value **Result=nullptr)
EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints as EmitStoreThroughLValue.
Definition CGExpr.cpp:3038
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
Definition CGExpr.cpp:4038
llvm::Value * EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, bool isInc, bool isPre)
RValue EmitVAArg(VAArgExpr *VE, Address &VAListAddr, AggValueSlot Slot=AggValueSlot::ignored())
Generate code to get an argument from the passed in pointer and update it accordingly.
Definition CGCall.cpp:6585
llvm::Value * getAsNaturalPointerTo(Address Addr, QualType PointeeType)
RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e, AggValueSlot slot=AggValueSlot::ignored())
Definition CGExpr.cpp:7336
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
void maybeUpdateMCDCTestVectorBitmap(const Expr *E)
Increment the profiler's counter for the given expression by StepV.
void EmitCXXDeleteExpr(const CXXDeleteExpr *E)
llvm::Value * EmitObjCArrayLiteral(const ObjCArrayLiteral *E)
Definition CGObjC.cpp:265
llvm::Value * EmitPromotedScalarExpr(const Expr *E, QualType PromotionType)
const LangOptions & getLangOpts() const
llvm::Value * EmitARCStoreStrong(LValue lvalue, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition CGObjC.cpp:2577
bool isPointerKnownNonNull(const Expr *E)
Address GetAddressOfDerivedClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue)
Definition CGClass.cpp:388
void EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, SourceLocation Loc)
Given an assignment *LHS = RHS, emit a test that checks if RHS is nonnull, if LHS is marked _Nonnull.
Definition CGDecl.cpp:766
llvm::Value * EmitPointerAuthUnqualify(PointerAuthQualifier Qualifier, llvm::Value *Pointer, QualType PointerType, Address StorageAddress, bool IsKnownNonNull)
std::pair< RValue, llvm::Value * > EmitAtomicCompareExchange(LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc, llvm::AtomicOrdering Success=llvm::AtomicOrdering::SequentiallyConsistent, llvm::AtomicOrdering Failure=llvm::AtomicOrdering::SequentiallyConsistent, bool IsWeak=false, AggValueSlot Slot=AggValueSlot::ignored())
Emit a compare-and-exchange op for atomic type.
void EmitVTablePtrCheckForCast(QualType T, Address Derived, bool MayBeNull, CFITypeCheckKind TCK, SourceLocation Loc)
Derived is the presumed address of an object of type T after a cast.
Definition CGClass.cpp:2953
TypeCheckKind
Situations in which we might emit a check for the suitability of a pointer or glvalue.
@ TCK_DowncastPointer
Checking the operand of a static_cast to a derived pointer type.
@ TCK_Store
Checking the destination of a store. Must be suitably sized and aligned.
@ TCK_Load
Checking the operand of a load. Must be suitably sized and aligned.
llvm::Value * EmitCXXNewExpr(const CXXNewExpr *E)
bool hasSkipCounter(const Stmt *S) const
void EmitBitfieldConversionCheck(llvm::Value *Src, QualType SrcType, llvm::Value *Dst, QualType DstType, const CGBitFieldInfo &Info, SourceLocation Loc)
Emit a check that an [implicit] conversion of a bitfield.
std::pair< LValue, llvm::Value * > EmitARCStoreUnsafeUnretained(const BinaryOperator *e, bool ignored)
Definition CGObjC.cpp:3661
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler.
Definition CGExpr.cpp:3928
LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E, llvm::Value *&Result)
RawAddress CreateDefaultAlignTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateDefaultAlignedTempAlloca - This creates an alloca with the default ABI alignment of the given L...
Definition CGExpr.cpp:181
const TargetInfo & getTarget() const
LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E)
llvm::Value * EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty)
Definition CGObjC.cpp:3985
void EmitIgnoredExpr(const Expr *E)
EmitIgnoredExpr - Emit an expression in a context which ignores the result.
Definition CGExpr.cpp:257
RValue EmitCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue=ReturnValueSlot(), llvm::CallBase **CallOrInvoke=nullptr)
Definition CGExpr.cpp:6448
RValue EmitLoadOfLValue(LValue V, SourceLocation Loc)
EmitLoadOfLValue - Given an expression that represents a value lvalue, this method emits the address ...
Definition CGExpr.cpp:2524
llvm::Value * EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified complex type to the specified destination type,...
static bool isInstrumentedCondition(const Expr *C)
isInstrumentedCondition - Determine whether the given condition is an instrumentable condition (i....
VlaSizePair getVLAElements1D(const VariableArrayType *vla)
Return the number of elements for a single dimension for the given array type.
RawAddress CreateIRTempWithoutCast(QualType T, const Twine &Name="tmp")
CreateIRTempWithoutCast - Create a temporary IR object of the given type, with appropriate alignment.
Definition CGExpr.cpp:188
llvm::Value * EmitObjCBoxedExpr(const ObjCBoxedExpr *E)
EmitObjCBoxedExpr - This routine generates code to call the appropriate expression boxing method.
Definition CGObjC.cpp:65
void EmitBoundsCheck(const Expr *ArrayExpr, const Expr *ArrayExprBase, llvm::Value *Index, QualType IndexType, bool Accessed)
Emit a check that Base points into an array object, which we can access at index Index.
Definition CGExpr.cpp:1264
llvm::Value * EvaluateExprAsBool(const Expr *E)
EvaluateExprAsBool - Perform the usual unary conversions on the specified expression and compare the ...
Definition CGExpr.cpp:238
void maybeResetMCDCCondBitmap(const Expr *E)
Zero-init the MCDC temp value.
RValue EmitCoyieldExpr(const CoyieldExpr &E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerKind::SanitizerOrdinal > > Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs, const TrapReason *TR=nullptr)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
Definition CGExpr.cpp:4186
RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e)
Given an opaque value expression, return its RValue mapping if it exists, otherwise create one.
Definition CGExpr.cpp:6401
llvm::Value * emitScalarConstant(const ConstantEmission &Constant, Expr *E)
Definition CGExpr.cpp:2046
llvm::Value * EmitARCRetainScalarExpr(const Expr *expr)
EmitARCRetainScalarExpr - Semantically equivalent to EmitARCRetainObject(e->getType(),...
Definition CGObjC.cpp:3525
llvm::Value * EmitBlockLiteral(const BlockExpr *)
Emit block literal.
Definition CGBlocks.cpp:764
llvm::Value * EmitToMemory(llvm::Value *Value, QualType Ty)
EmitToMemory - Change a scalar value from its value representation to its in-memory representation.
Definition CGExpr.cpp:2246
void maybeUpdateMCDCCondBitmap(const Expr *E, llvm::Value *Val)
Update the MCDC temp value with the condition's evaluated result.
LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e)
Given an opaque value expression, return its LValue mapping if it exists, otherwise create one.
Definition CGExpr.cpp:6387
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type,...
VlaSizePair getVLASize(const VariableArrayType *vla)
Returns an LLVM value that corresponds to the size, in non-variably-sized elements,...
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
llvm::Value * EmitWithOriginalRHSBitfieldAssignment(const BinaryOperator *E, llvm::Value **Previous, QualType *SrcType)
Retrieve the implicit cast expression of the rhs in a binary operator expression by passing pointers ...
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
static const Expr * stripCond(const Expr *C)
Ignore parentheses and logical-NOT to track conditions consistently.
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
Definition CGExpr.cpp:2776
Address EmitArrayToPointerDecay(const Expr *Array, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
Definition CGExpr.cpp:4618
Address EmitCompoundStmt(const CompoundStmt &S, bool GetLast=false, AggValueSlot AVS=AggValueSlot::ignored())
EmitCompoundStmt - Emit a compound statement {..} node.
Definition CGStmt.cpp:560
llvm::AtomicRMWInst * emitAtomicRMWInst(llvm::AtomicRMWInst::BinOp Op, Address Addr, llvm::Value *Val, llvm::AtomicOrdering Order=llvm::AtomicOrdering::SequentiallyConsistent, llvm::SyncScope::ID SSID=llvm::SyncScope::System, const AtomicExpr *AE=nullptr)
Emit an atomicrmw instruction, and applying relevant metadata when applicable.
llvm::Value * EmitPointerArithmetic(const BinaryOperator *BO, Expr *pointerOperand, llvm::Value *pointer, Expr *indexOperand, llvm::Value *index, bool isSubtraction)
Emit pointer + index arithmetic.
RValue EmitAnyExpr(const Expr *E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
EmitAnyExpr - Emit code to compute the specified expression which can have any type.
Definition CGExpr.cpp:279
uint64_t getCurrentProfileCount()
Get the profiler's current count.
llvm::Type * ConvertTypeForMem(QualType T)
RValue EmitAtomicExpr(AtomicExpr *E)
Definition CGAtomic.cpp:914
void markStmtMaybeUsed(const Stmt *S)
bool IsSanitizerScope
True if CodeGen currently emits code implementing sanitizer checks.
void FlattenAccessAndTypeLValue(LValue LVal, SmallVectorImpl< LValue > &AccessList)
Definition CGExpr.cpp:7345
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, LValue LV, QualType Type, SanitizerSet SkippedChecks=SanitizerSet(), llvm::Value *ArraySize=nullptr)
RValue EmitCoawaitExpr(const CoawaitExpr &E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
llvm::Value * authPointerToPointerCast(llvm::Value *ResultPtr, QualType SourceType, QualType DestType)
Address EmitPointerWithAlignment(const Expr *Addr, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitPointerWithAlignment - Given an expression with a pointer type, emit the value and compute our be...
Definition CGExpr.cpp:1600
void EmitBranch(llvm::BasicBlock *Block)
EmitBranch - Emit a branch to the specified basic block from the current insert block,...
Definition CGStmt.cpp:663
LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK)
Same as EmitLValue but additionally we generate checking code to guard against undefined behavior.
Definition CGExpr.cpp:1681
RawAddress CreateMemTemp(QualType T, const Twine &Name="tmp", RawAddress *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
Definition CGExpr.cpp:194
llvm::Type * convertTypeForLoadStore(QualType ASTTy, llvm::Type *LLVMTy=nullptr)
bool sanitizePerformTypeCheck() const
Whether any type-checking sanitizers are enabled.
Definition CGExpr.cpp:751
llvm::Value * EmitCheckedInBoundsGEP(llvm::Type *ElemTy, llvm::Value *Ptr, ArrayRef< llvm::Value * > IdxList, bool SignedIndices, bool IsSubtraction, SourceLocation Loc, const Twine &Name="")
Same as IRBuilder::CreateInBoundsGEP, but additionally emits a check to detect undefined behavior whe...
llvm::Value * EmitBuiltinAvailable(const VersionTuple &Version)
Definition CGObjC.cpp:4065
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
llvm::Value * EmitMatrixIndexExpr(const Expr *E)
Definition CGExpr.cpp:5188
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitTrapCheck(llvm::Value *Checked, SanitizerHandler CheckHandlerID, bool NoMerge=false, const TrapReason *TR=nullptr)
Create a basic block that will call the trap intrinsic, and emit a conditional branch to it,...
Definition CGExpr.cpp:4525
llvm::Value * LoadCXXThis()
LoadCXXThis - Load the value of 'this'.
llvm::Value * EmitFromMemory(llvm::Value *Value, QualType Ty)
EmitFromMemory - Change a scalar value from its memory representation to its value representation.
Definition CGExpr.cpp:2280
uint64_t getProfileCount(const Stmt *S)
Get the profiler's count for the given statement.
llvm::Value * getArrayInitIndex()
Get the index of the current ArrayInitLoopExpr, if any.
bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result, bool AllowLabels=false)
ConstantFoldsToSimpleInteger - If the specified expression does not fold to a constant,...
llvm::Value * EmitObjCStringLiteral(const ObjCStringLiteral *E)
Emits an instance of NSConstantString representing the object.
Definition CGObjC.cpp:52
void ErrorUnsupported(const Stmt *S, const char *Type)
ErrorUnsupported - Print out an error that codegen doesn't support the specified stmt yet.
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
ConstantEmission tryEmitAsConstant(const DeclRefExpr *RefExpr)
Try to emit a reference to the given value without producing it as an l-value.
Definition CGExpr.cpp:1943
LValue EmitLValue(const Expr *E, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
Definition CGExpr.cpp:1716
llvm::Value * EmitARCExtendBlockObject(const Expr *expr)
Definition CGObjC.cpp:3556
void markStmtAsUsed(bool Skipped, const Stmt *S)
llvm::Value * EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored)
i8* @objc_storeWeak(i8** addr, i8* value) Returns value.
Definition CGObjC.cpp:2683
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go.
ComplexPairTy EmitPromotedValue(ComplexPairTy result, QualType PromotionType)
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
void emitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue=nullptr)
bool isMCDCDecisionExpr(const Expr *E) const
llvm::Value * EmitScalarConversion(llvm::Value *Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified type to the specified destination type, both of which are LLVM s...
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
static bool ShouldNullCheckClassCastValue(const CastExpr *Cast)
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
llvm::Value * EmitDynamicCast(Address V, const CXXDynamicCastExpr *DCE)
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition CGStmt.cpp:643
This class organizes the cross-function state that is used while generating LLVM code.
void EmitExplicitCastExprType(const ExplicitCastExpr *E, CodeGenFunction *CGF=nullptr)
Emit type info if type of an expression is a variably modified type.
Definition CGExpr.cpp:1396
CGHLSLRuntime & getHLSLRuntime()
Return a reference to the configured HLSL runtime.
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
TrapReasonBuilder BuildTrapReason(unsigned DiagID, TrapReason &TR)
Helper function to construct a TrapReasonBuilder.
llvm::Constant * getNullPointer(llvm::PointerType *T, QualType QT)
Get target specific null pointer.
const TargetInfo & getTarget() const
llvm::Constant * getMemberPointerConstant(const UnaryOperator *e)
const llvm::DataLayout & getDataLayout() const
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
const CodeGenOptions & getCodeGenOpts() const
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys={})
llvm::Value * createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF)
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
LangAS GetGlobalConstantAddressSpace() const
Return the AST address space of constant literal, which is used to emit the constant literal as globa...
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
LValue - This represents an lvalue references.
Definition CGValue.h:183
bool isBitField() const
Definition CGValue.h:288
bool isVolatileQualified() const
Definition CGValue.h:297
const Qualifiers & getQuals() const
Definition CGValue.h:350
Address getAddress() const
Definition CGValue.h:373
QualType getType() const
Definition CGValue.h:303
const CGBitFieldInfo & getBitFieldInfo() const
Definition CGValue.h:446
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition CGValue.h:42
bool isScalar() const
Definition CGValue.h:64
static RValue get(llvm::Value *V)
Definition CGValue.h:99
bool isAggregate() const
Definition CGValue.h:66
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
Definition CGValue.h:84
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition CGValue.h:72
CompoundAssignOperator - For compound assignments (e.g.
Definition Expr.h:4303
QualType getComputationLHSType() const
Definition Expr.h:4337
QualType getComputationResultType() const
Definition Expr.h:4340
bool isSatisfied() const
Whether or not the concept with the given arguments was satisfied when the expression was created.
APValue getAPValueResult() const
Definition Expr.cpp:418
bool hasAPValueResult() const
Definition Expr.h:1160
Represents a concrete matrix type with constant number of rows and columns.
Definition TypeBase.h:4449
unsigned mapRowMajorToColumnMajorFlattenedIndex(unsigned RowMajorIdx) const
Given a row-major flattened index RowMajorIdx, return the equivalent column-major flattened index.
Definition TypeBase.h:4508
Expr * getSrcExpr() const
getSrcExpr - Return the Expr to be converted.
Definition Expr.h:4812
T * getAttr() const
Definition DeclBase.h:581
ChildElementIter< false > begin()
Definition Expr.h:5235
size_t getDataElementCount() const
Definition Expr.h:5151
This represents one expression.
Definition Expr.h:112
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
bool isGLValue() const
Definition Expr.h:287
@ SE_AllowSideEffects
Allow any unmodeled side effect.
Definition Expr.h:677
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3093
bool isEvaluatable(const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects) const
isEvaluatable - Call EvaluateAsRValue to see if this expression can be constant folded without side-e...
bool isPRValue() const
Definition Expr.h:285
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3077
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:282
bool refersToBitField() const
Returns true if this expression is a gl-value that potentially refers to a bit-field.
Definition Expr.h:479
QualType getType() const
Definition Expr.h:144
unsigned getFieldIndex() const
Returns the index of this field within its record, as appropriate for passing to ASTRecordLayout::get...
Definition Decl.h:3263
llvm::APInt getValue() const
Returns an internal integer representation of the literal.
Definition Expr.h:1578
llvm::APFloat getValue() const
Definition Expr.h:1669
const Expr * getSubExpr() const
Definition Expr.h:1065
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition Expr.h:3856
unsigned getNumInits() const
Definition Expr.h:5335
bool hadArrayRangeDesignator() const
Definition Expr.h:5483
const Expr * getInit(unsigned Init) const
Definition Expr.h:5357
bool isSignedOverflowDefined() const
std::string OverflowHandler
The name of the handler function to be called when -ftrapv is specified.
Represents a matrix type, as defined in the Matrix Types clang extensions.
Definition TypeBase.h:4399
Expr * getBase() const
Definition Expr.h:3444
bool isArrow() const
Definition Expr.h:3551
VersionTuple getVersion() const
Definition ExprObjC.h:1757
ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type.
Definition ExprObjC.h:1529
Expr * getBase() const
Definition ExprObjC.h:1554
SourceLocation getExprLoc() const LLVM_READONLY
Definition ExprObjC.h:1577
const ObjCMethodDecl * getMethodDecl() const
Definition ExprObjC.h:1395
QualType getReturnType() const
Definition DeclObjC.h:329
Represents a pointer to an Objective C object.
Definition TypeBase.h:8063
const ObjCObjectType * getObjectType() const
Gets the type pointed to by this ObjC pointer.
Definition TypeBase.h:8100
Expr * getIndexExpr(unsigned Idx)
Definition Expr.h:2589
const OffsetOfNode & getComponent(unsigned Idx) const
Definition Expr.h:2577
TypeSourceInfo * getTypeSourceInfo() const
Definition Expr.h:2570
unsigned getNumComponents() const
Definition Expr.h:2585
unsigned getArrayExprIndex() const
For an array element node, returns the index into the array of expressions.
Definition Expr.h:2482
FieldDecl * getField() const
For a field offsetof node, returns the field.
Definition Expr.h:2488
@ Array
An index into an array.
Definition Expr.h:2429
@ Identifier
A field in a dependent type, known only by its name.
Definition Expr.h:2433
@ Field
A field.
Definition Expr.h:2431
@ Base
An implicit indirection through a C++ base class, when the field found is in a base class.
Definition Expr.h:2436
Kind getKind() const
Determine what kind of offsetof node this is.
Definition Expr.h:2478
CXXBaseSpecifier * getBase() const
For a base class node, returns the base specifier.
Definition Expr.h:2498
SourceLocation getExprLoc() const LLVM_READONLY
Definition Expr.h:1211
Expr * getSelectedExpr() const
Definition ExprCXX.h:4639
const Expr * getSubExpr() const
Definition Expr.h:2202
Pointer-authentication qualifiers.
Definition TypeBase.h:152
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3390
A (possibly-)qualified type.
Definition TypeBase.h:937
PointerAuthQualifier getPointerAuth() const
Definition TypeBase.h:1468
bool mayBeDynamicClass() const
Returns true if it is a class and it might be dynamic.
Definition Type.cpp:167
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8445
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition TypeBase.h:1453
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition TypeBase.h:8630
QualType getCanonicalType() const
Definition TypeBase.h:8497
bool UseExcessPrecision(const ASTContext &Ctx)
Definition Type.cpp:1661
bool mayBeNotDynamicClass() const
Returns true if it is not a class or if the class might not be dynamic.
Definition Type.cpp:172
bool isCanonical() const
Definition TypeBase.h:8502
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition TypeBase.h:361
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition TypeBase.h:354
@ OCL_None
There is no lifetime qualification on this type.
Definition TypeBase.h:350
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition TypeBase.h:364
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition TypeBase.h:367
void removePointerAuth()
Definition TypeBase.h:610
bool isSatisfied() const
Whether or not the requires clause is satisfied.
std::string ComputeName(ASTContext &Context) const
Definition Expr.cpp:592
static constexpr SanitizerMask bitPosToMask(const unsigned Pos)
Create a mask with a bit enabled at position Pos.
Definition Sanitizers.h:59
llvm::APSInt getShuffleMaskIdx(unsigned N) const
Definition Expr.h:4698
unsigned getNumSubExprs() const
getNumSubExprs - Return the size of the SubExprs array.
Definition Expr.h:4679
Expr * getExpr(unsigned Index)
getExpr - Return the Expr at the specified index.
Definition Expr.h:4685
unsigned getPackLength() const
Retrieve the length of the parameter pack.
Definition ExprCXX.h:4515
APValue EvaluateInContext(const ASTContext &Ctx, const Expr *DefaultExpr) const
Return the result of evaluating this SourceLocExpr in the specified (and possibly null) default argum...
Definition Expr.cpp:2287
SourceLocation getLocation() const
Definition Expr.h:5064
Encodes a location in the source.
CompoundStmt * getSubStmt()
Definition Expr.h:4615
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
void dump() const
Dumps the specified AST fragment and all subtrees to llvm::errs().
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
virtual bool useFP16ConversionIntrinsics() const
Check whether conversions to and from __fp16 should go through an integer bitcast with i16.
VersionTuple getPlatformMinVersion() const
Retrieve the minimum desired version of the platform, to which the program should be compiled.
const llvm::fltSemantics & getHalfFormat() const
Definition TargetInfo.h:789
const llvm::fltSemantics & getBFloat16Format() const
Definition TargetInfo.h:799
const llvm::fltSemantics & getLongDoubleFormat() const
Definition TargetInfo.h:810
const llvm::fltSemantics & getFloat128Format() const
Definition TargetInfo.h:818
const llvm::fltSemantics & getIbm128Format() const
Definition TargetInfo.h:826
QualType getType() const
Return the type wrapped by this type source info.
Definition TypeBase.h:8427
bool getBoolValue() const
Definition ExprCXX.h:2951
const APValue & getAPValue() const
Definition ExprCXX.h:2956
bool isStoredAsBoolean() const
Definition ExprCXX.h:2947
bool isVoidType() const
Definition TypeBase.h:9048
bool isBooleanType() const
Definition TypeBase.h:9185
bool isSignableType(const ASTContext &Ctx) const
Definition TypeBase.h:8694
bool isMFloat8Type() const
Definition TypeBase.h:9073
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition Type.cpp:2289
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition Type.cpp:2355
CXXRecordDecl * castAsCXXRecordDecl() const
Definition Type.h:36
bool isArithmeticType() const
Definition Type.cpp:2422
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9092
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9342
bool isReferenceType() const
Definition TypeBase.h:8706
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
Definition Type.cpp:1958
bool isSveVLSBuiltinType() const
Determines if this is a sizeless type supported by the 'arm_sve_vector_bits' type attribute,...
Definition Type.cpp:2701
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isExtVectorType() const
Definition TypeBase.h:8825
bool isExtVectorBoolType() const
Definition TypeBase.h:8829
bool isOCLIntelSubgroupAVCType() const
Definition TypeBase.h:8967
bool isBuiltinType() const
Helper methods to distinguish type categories.
Definition TypeBase.h:8805
RecordDecl * castAsRecordDecl() const
Definition Type.h:48
bool isAnyComplexType() const
Definition TypeBase.h:8817
bool isFixedPointType() const
Return true if this is a fixed point type according to ISO/IEC JTC1 SC22 WG14 N1169.
Definition TypeBase.h:9108
bool isHalfType() const
Definition TypeBase.h:9052
bool hasSignedIntegerRepresentation() const
Determine whether this type has an signed integer representation of some sort, e.g....
Definition Type.cpp:2310
bool isQueueT() const
Definition TypeBase.h:8938
bool isMatrixType() const
Definition TypeBase.h:8845
bool isEventT() const
Definition TypeBase.h:8930
bool isFunctionType() const
Definition TypeBase.h:8678
bool isVectorType() const
Definition TypeBase.h:8821
bool isRealFloatingType() const
Floating point categories.
Definition Type.cpp:2405
bool isFloatingType() const
Definition Type.cpp:2389
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
Definition Type.cpp:2332
const T * castAsCanonical() const
Return this type's canonical type cast to the specified type.
Definition TypeBase.h:2990
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9275
bool isNullPtrType() const
Definition TypeBase.h:9085
QualType getTypeOfArgument() const
Gets the argument type, or the type of the argument expression, whichever is appropriate.
Definition Expr.h:2697
UnaryExprOrTypeTrait getKind() const
Definition Expr.h:2660
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
SourceLocation getExprLoc() const
Definition Expr.h:2371
Expr * getSubExpr() const
Definition Expr.h:2288
Opcode getOpcode() const
Definition Expr.h:2283
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Get the FP features status of this operator.
Definition Expr.h:2403
bool canOverflow() const
Returns true if the unary operator can cause an overflow.
Definition Expr.h:2301
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
bool isWeak() const
Determine whether this symbol is weakly-imported, or declared with the weak or weak-ref attr.
Definition Decl.cpp:5555
QualType getType() const
Definition Value.cpp:237
Represents a C array with a specified size that is not an integer-constant-expression.
Definition TypeBase.h:4028
Represents a GCC generic vector type.
Definition TypeBase.h:4237
Defines the clang::TargetInfo interface.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
const AstTypeMatcher< PointerType > pointerType
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
bool BitCast(InterpState &S, CodePtr OpPC)
Definition Interp.h:3996
llvm::APFloat APFloat
Definition Floating.h:27
llvm::APInt APInt
Definition FixedPoint.h:19
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1473
bool Load(InterpState &S, CodePtr OpPC)
Definition Interp.h:2147
bool GE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1488
PRESERVE_NONE bool Ret(InterpState &S, CodePtr &PC)
Definition Interp.h:258
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
@ Result
The result type of a method or function.
Definition TypeBase.h:905
CastKind
CastKind - The kind of operation required for a conversion.
U cast(CodeGen::Address addr)
Definition Address.h:327
unsigned long uint64_t
long int64_t
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
cl::opt< bool > EnableSingleByteCoverage
#define false
Definition stdbool.h:26
#define true
Definition stdbool.h:25
llvm::Value * TotalOffset
llvm::Value * OffsetOverflows
Structure with information about how a bitfield should be accessed.
unsigned Size
The total size of the bit-field, in bits.
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
llvm::Type * HalfTy
half, bfloat, float, double
static TBAAAccessInfo getMayAliasInfo()
Definition CodeGenTBAA.h:63
APValue Val
Val - This is the value the expression can be folded to.
Definition Expr.h:650
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition Sanitizers.h:174
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition Sanitizers.h:184