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