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