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