Bug Summary

File:clang/lib/CodeGen/CGExprScalar.cpp
Warning:line 1728, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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