1f4a2713aSLionel Sambuc //===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This contains code to emit Expr nodes with scalar LLVM types as LLVM code.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "CodeGenFunction.h"
15f4a2713aSLionel Sambuc #include "CGCXXABI.h"
16f4a2713aSLionel Sambuc #include "CGDebugInfo.h"
17f4a2713aSLionel Sambuc #include "CGObjCRuntime.h"
18f4a2713aSLionel Sambuc #include "CodeGenModule.h"
19f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
20f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
21f4a2713aSLionel Sambuc #include "clang/AST/RecordLayout.h"
22f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
23f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
24f4a2713aSLionel Sambuc #include "clang/Frontend/CodeGenOptions.h"
25*0a6a1f1dSLionel Sambuc #include "llvm/IR/CFG.h"
26f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
27f4a2713aSLionel Sambuc #include "llvm/IR/DataLayout.h"
28f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
29f4a2713aSLionel Sambuc #include "llvm/IR/GlobalVariable.h"
30f4a2713aSLionel Sambuc #include "llvm/IR/Intrinsics.h"
31f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
32f4a2713aSLionel Sambuc #include <cstdarg>
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc using namespace clang;
35f4a2713aSLionel Sambuc using namespace CodeGen;
36f4a2713aSLionel Sambuc using llvm::Value;
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
39f4a2713aSLionel Sambuc // Scalar Expression Emitter
40f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc namespace {
43f4a2713aSLionel Sambuc struct BinOpInfo {
44f4a2713aSLionel Sambuc Value *LHS;
45f4a2713aSLionel Sambuc Value *RHS;
46f4a2713aSLionel Sambuc QualType Ty; // Computation Type.
47f4a2713aSLionel Sambuc BinaryOperator::Opcode Opcode; // Opcode of BinOp to perform
48f4a2713aSLionel Sambuc bool FPContractable;
49f4a2713aSLionel Sambuc const Expr *E; // Entire expr, for error unsupported. May not be binop.
50f4a2713aSLionel Sambuc };
51f4a2713aSLionel Sambuc
MustVisitNullValue(const Expr * E)52f4a2713aSLionel Sambuc static bool MustVisitNullValue(const Expr *E) {
53f4a2713aSLionel Sambuc // If a null pointer expression's type is the C++0x nullptr_t, then
54f4a2713aSLionel Sambuc // it's not necessarily a simple constant and it must be evaluated
55f4a2713aSLionel Sambuc // for its potential side effects.
56f4a2713aSLionel Sambuc return E->getType()->isNullPtrType();
57f4a2713aSLionel Sambuc }
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc class ScalarExprEmitter
60f4a2713aSLionel Sambuc : public StmtVisitor<ScalarExprEmitter, Value*> {
61f4a2713aSLionel Sambuc CodeGenFunction &CGF;
62f4a2713aSLionel Sambuc CGBuilderTy &Builder;
63f4a2713aSLionel Sambuc bool IgnoreResultAssign;
64f4a2713aSLionel Sambuc llvm::LLVMContext &VMContext;
65f4a2713aSLionel Sambuc public:
66f4a2713aSLionel Sambuc
ScalarExprEmitter(CodeGenFunction & cgf,bool ira=false)67f4a2713aSLionel Sambuc ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false)
68f4a2713aSLionel Sambuc : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira),
69f4a2713aSLionel Sambuc VMContext(cgf.getLLVMContext()) {
70f4a2713aSLionel Sambuc }
71f4a2713aSLionel Sambuc
72f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
73f4a2713aSLionel Sambuc // Utilities
74f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
75f4a2713aSLionel Sambuc
TestAndClearIgnoreResultAssign()76f4a2713aSLionel Sambuc bool TestAndClearIgnoreResultAssign() {
77f4a2713aSLionel Sambuc bool I = IgnoreResultAssign;
78f4a2713aSLionel Sambuc IgnoreResultAssign = false;
79f4a2713aSLionel Sambuc return I;
80f4a2713aSLionel Sambuc }
81f4a2713aSLionel Sambuc
ConvertType(QualType T)82f4a2713aSLionel Sambuc llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
EmitLValue(const Expr * E)83f4a2713aSLionel Sambuc LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
EmitCheckedLValue(const Expr * E,CodeGenFunction::TypeCheckKind TCK)84f4a2713aSLionel Sambuc LValue EmitCheckedLValue(const Expr *E, CodeGenFunction::TypeCheckKind TCK) {
85f4a2713aSLionel Sambuc return CGF.EmitCheckedLValue(E, TCK);
86f4a2713aSLionel Sambuc }
87f4a2713aSLionel Sambuc
88*0a6a1f1dSLionel Sambuc void EmitBinOpCheck(ArrayRef<std::pair<Value *, SanitizerKind>> Checks,
89*0a6a1f1dSLionel Sambuc const BinOpInfo &Info);
90f4a2713aSLionel Sambuc
EmitLoadOfLValue(LValue LV,SourceLocation Loc)91f4a2713aSLionel Sambuc Value *EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
92f4a2713aSLionel Sambuc return CGF.EmitLoadOfLValue(LV, Loc).getScalarVal();
93f4a2713aSLionel Sambuc }
94f4a2713aSLionel Sambuc
EmitLValueAlignmentAssumption(const Expr * E,Value * V)95*0a6a1f1dSLionel Sambuc void EmitLValueAlignmentAssumption(const Expr *E, Value *V) {
96*0a6a1f1dSLionel Sambuc const AlignValueAttr *AVAttr = nullptr;
97*0a6a1f1dSLionel Sambuc if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) {
98*0a6a1f1dSLionel Sambuc const ValueDecl *VD = DRE->getDecl();
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc if (VD->getType()->isReferenceType()) {
101*0a6a1f1dSLionel Sambuc if (const auto *TTy =
102*0a6a1f1dSLionel Sambuc dyn_cast<TypedefType>(VD->getType().getNonReferenceType()))
103*0a6a1f1dSLionel Sambuc AVAttr = TTy->getDecl()->getAttr<AlignValueAttr>();
104*0a6a1f1dSLionel Sambuc } else {
105*0a6a1f1dSLionel Sambuc // Assumptions for function parameters are emitted at the start of the
106*0a6a1f1dSLionel Sambuc // function, so there is no need to repeat that here.
107*0a6a1f1dSLionel Sambuc if (isa<ParmVarDecl>(VD))
108*0a6a1f1dSLionel Sambuc return;
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambuc AVAttr = VD->getAttr<AlignValueAttr>();
111*0a6a1f1dSLionel Sambuc }
112*0a6a1f1dSLionel Sambuc }
113*0a6a1f1dSLionel Sambuc
114*0a6a1f1dSLionel Sambuc if (!AVAttr)
115*0a6a1f1dSLionel Sambuc if (const auto *TTy =
116*0a6a1f1dSLionel Sambuc dyn_cast<TypedefType>(E->getType()))
117*0a6a1f1dSLionel Sambuc AVAttr = TTy->getDecl()->getAttr<AlignValueAttr>();
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc if (!AVAttr)
120*0a6a1f1dSLionel Sambuc return;
121*0a6a1f1dSLionel Sambuc
122*0a6a1f1dSLionel Sambuc Value *AlignmentValue = CGF.EmitScalarExpr(AVAttr->getAlignment());
123*0a6a1f1dSLionel Sambuc llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(AlignmentValue);
124*0a6a1f1dSLionel Sambuc CGF.EmitAlignmentAssumption(V, AlignmentCI->getZExtValue());
125*0a6a1f1dSLionel Sambuc }
126*0a6a1f1dSLionel Sambuc
127f4a2713aSLionel Sambuc /// EmitLoadOfLValue - Given an expression with complex type that represents a
128f4a2713aSLionel Sambuc /// value l-value, this method emits the address of the l-value, then loads
129f4a2713aSLionel Sambuc /// and returns the result.
EmitLoadOfLValue(const Expr * E)130f4a2713aSLionel Sambuc Value *EmitLoadOfLValue(const Expr *E) {
131*0a6a1f1dSLionel Sambuc Value *V = EmitLoadOfLValue(EmitCheckedLValue(E, CodeGenFunction::TCK_Load),
132f4a2713aSLionel Sambuc E->getExprLoc());
133*0a6a1f1dSLionel Sambuc
134*0a6a1f1dSLionel Sambuc EmitLValueAlignmentAssumption(E, V);
135*0a6a1f1dSLionel Sambuc return V;
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc /// EmitConversionToBool - Convert the specified expression value to a
139f4a2713aSLionel Sambuc /// boolean (i1) truth value. This is equivalent to "Val != 0".
140f4a2713aSLionel Sambuc Value *EmitConversionToBool(Value *Src, QualType DstTy);
141f4a2713aSLionel Sambuc
142f4a2713aSLionel Sambuc /// \brief Emit a check that a conversion to or from a floating-point type
143f4a2713aSLionel Sambuc /// does not overflow.
144f4a2713aSLionel Sambuc void EmitFloatConversionCheck(Value *OrigSrc, QualType OrigSrcType,
145f4a2713aSLionel Sambuc Value *Src, QualType SrcType,
146f4a2713aSLionel Sambuc QualType DstType, llvm::Type *DstTy);
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc /// EmitScalarConversion - Emit a conversion from the specified type to the
149f4a2713aSLionel Sambuc /// specified destination type, both of which are LLVM scalar types.
150f4a2713aSLionel Sambuc Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
151f4a2713aSLionel Sambuc
152f4a2713aSLionel Sambuc /// EmitComplexToScalarConversion - Emit a conversion from the specified
153f4a2713aSLionel Sambuc /// complex type to the specified destination type, where the destination type
154f4a2713aSLionel Sambuc /// is an LLVM scalar type.
155f4a2713aSLionel Sambuc Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
156f4a2713aSLionel Sambuc QualType SrcTy, QualType DstTy);
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc /// EmitNullValue - Emit a value that corresponds to null for the given type.
159f4a2713aSLionel Sambuc Value *EmitNullValue(QualType Ty);
160f4a2713aSLionel Sambuc
161f4a2713aSLionel Sambuc /// EmitFloatToBoolConversion - Perform an FP to boolean conversion.
EmitFloatToBoolConversion(Value * V)162f4a2713aSLionel Sambuc Value *EmitFloatToBoolConversion(Value *V) {
163f4a2713aSLionel Sambuc // Compare against 0.0 for fp scalars.
164f4a2713aSLionel Sambuc llvm::Value *Zero = llvm::Constant::getNullValue(V->getType());
165f4a2713aSLionel Sambuc return Builder.CreateFCmpUNE(V, Zero, "tobool");
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc
168f4a2713aSLionel Sambuc /// EmitPointerToBoolConversion - Perform a pointer to boolean conversion.
EmitPointerToBoolConversion(Value * V)169f4a2713aSLionel Sambuc Value *EmitPointerToBoolConversion(Value *V) {
170f4a2713aSLionel Sambuc Value *Zero = llvm::ConstantPointerNull::get(
171f4a2713aSLionel Sambuc cast<llvm::PointerType>(V->getType()));
172f4a2713aSLionel Sambuc return Builder.CreateICmpNE(V, Zero, "tobool");
173f4a2713aSLionel Sambuc }
174f4a2713aSLionel Sambuc
EmitIntToBoolConversion(Value * V)175f4a2713aSLionel Sambuc Value *EmitIntToBoolConversion(Value *V) {
176f4a2713aSLionel Sambuc // Because of the type rules of C, we often end up computing a
177f4a2713aSLionel Sambuc // logical value, then zero extending it to int, then wanting it
178f4a2713aSLionel Sambuc // as a logical value again. Optimize this common case.
179f4a2713aSLionel Sambuc if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(V)) {
180f4a2713aSLionel Sambuc if (ZI->getOperand(0)->getType() == Builder.getInt1Ty()) {
181f4a2713aSLionel Sambuc Value *Result = ZI->getOperand(0);
182f4a2713aSLionel Sambuc // If there aren't any more uses, zap the instruction to save space.
183f4a2713aSLionel Sambuc // Note that there can be more uses, for example if this
184f4a2713aSLionel Sambuc // is the result of an assignment.
185f4a2713aSLionel Sambuc if (ZI->use_empty())
186f4a2713aSLionel Sambuc ZI->eraseFromParent();
187f4a2713aSLionel Sambuc return Result;
188f4a2713aSLionel Sambuc }
189f4a2713aSLionel Sambuc }
190f4a2713aSLionel Sambuc
191f4a2713aSLionel Sambuc return Builder.CreateIsNotNull(V, "tobool");
192f4a2713aSLionel Sambuc }
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
195f4a2713aSLionel Sambuc // Visitor Methods
196f4a2713aSLionel Sambuc //===--------------------------------------------------------------------===//
197f4a2713aSLionel Sambuc
Visit(Expr * E)198f4a2713aSLionel Sambuc Value *Visit(Expr *E) {
199*0a6a1f1dSLionel Sambuc ApplyDebugLocation DL(CGF, E->getLocStart());
200f4a2713aSLionel Sambuc return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E);
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc
VisitStmt(Stmt * S)203f4a2713aSLionel Sambuc Value *VisitStmt(Stmt *S) {
204f4a2713aSLionel Sambuc S->dump(CGF.getContext().getSourceManager());
205f4a2713aSLionel Sambuc llvm_unreachable("Stmt can't have complex result type!");
206f4a2713aSLionel Sambuc }
207f4a2713aSLionel Sambuc Value *VisitExpr(Expr *S);
208f4a2713aSLionel Sambuc
VisitParenExpr(ParenExpr * PE)209f4a2713aSLionel Sambuc Value *VisitParenExpr(ParenExpr *PE) {
210f4a2713aSLionel Sambuc return Visit(PE->getSubExpr());
211f4a2713aSLionel Sambuc }
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr * E)212f4a2713aSLionel Sambuc Value *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
213f4a2713aSLionel Sambuc return Visit(E->getReplacement());
214f4a2713aSLionel Sambuc }
VisitGenericSelectionExpr(GenericSelectionExpr * GE)215f4a2713aSLionel Sambuc Value *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
216f4a2713aSLionel Sambuc return Visit(GE->getResultExpr());
217f4a2713aSLionel Sambuc }
218f4a2713aSLionel Sambuc
219f4a2713aSLionel Sambuc // Leaves.
VisitIntegerLiteral(const IntegerLiteral * E)220f4a2713aSLionel Sambuc Value *VisitIntegerLiteral(const IntegerLiteral *E) {
221f4a2713aSLionel Sambuc return Builder.getInt(E->getValue());
222f4a2713aSLionel Sambuc }
VisitFloatingLiteral(const FloatingLiteral * E)223f4a2713aSLionel Sambuc Value *VisitFloatingLiteral(const FloatingLiteral *E) {
224f4a2713aSLionel Sambuc return llvm::ConstantFP::get(VMContext, E->getValue());
225f4a2713aSLionel Sambuc }
VisitCharacterLiteral(const CharacterLiteral * E)226f4a2713aSLionel Sambuc Value *VisitCharacterLiteral(const CharacterLiteral *E) {
227f4a2713aSLionel Sambuc return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
228f4a2713aSLionel Sambuc }
VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr * E)229f4a2713aSLionel Sambuc Value *VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
230f4a2713aSLionel Sambuc return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
231f4a2713aSLionel Sambuc }
VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr * E)232f4a2713aSLionel Sambuc Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
233f4a2713aSLionel Sambuc return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
234f4a2713aSLionel Sambuc }
VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr * E)235f4a2713aSLionel Sambuc Value *VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
236f4a2713aSLionel Sambuc return EmitNullValue(E->getType());
237f4a2713aSLionel Sambuc }
VisitGNUNullExpr(const GNUNullExpr * E)238f4a2713aSLionel Sambuc Value *VisitGNUNullExpr(const GNUNullExpr *E) {
239f4a2713aSLionel Sambuc return EmitNullValue(E->getType());
240f4a2713aSLionel Sambuc }
241f4a2713aSLionel Sambuc Value *VisitOffsetOfExpr(OffsetOfExpr *E);
242f4a2713aSLionel Sambuc Value *VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
VisitAddrLabelExpr(const AddrLabelExpr * E)243f4a2713aSLionel Sambuc Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
244f4a2713aSLionel Sambuc llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel());
245f4a2713aSLionel Sambuc return Builder.CreateBitCast(V, ConvertType(E->getType()));
246f4a2713aSLionel Sambuc }
247f4a2713aSLionel Sambuc
VisitSizeOfPackExpr(SizeOfPackExpr * E)248f4a2713aSLionel Sambuc Value *VisitSizeOfPackExpr(SizeOfPackExpr *E) {
249f4a2713aSLionel Sambuc return llvm::ConstantInt::get(ConvertType(E->getType()),E->getPackLength());
250f4a2713aSLionel Sambuc }
251f4a2713aSLionel Sambuc
VisitPseudoObjectExpr(PseudoObjectExpr * E)252f4a2713aSLionel Sambuc Value *VisitPseudoObjectExpr(PseudoObjectExpr *E) {
253f4a2713aSLionel Sambuc return CGF.EmitPseudoObjectRValue(E).getScalarVal();
254f4a2713aSLionel Sambuc }
255f4a2713aSLionel Sambuc
VisitOpaqueValueExpr(OpaqueValueExpr * E)256f4a2713aSLionel Sambuc Value *VisitOpaqueValueExpr(OpaqueValueExpr *E) {
257f4a2713aSLionel Sambuc if (E->isGLValue())
258f4a2713aSLionel Sambuc return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc());
259f4a2713aSLionel Sambuc
260f4a2713aSLionel Sambuc // Otherwise, assume the mapping is the scalar directly.
261f4a2713aSLionel Sambuc return CGF.getOpaqueRValueMapping(E).getScalarVal();
262f4a2713aSLionel Sambuc }
263f4a2713aSLionel Sambuc
264f4a2713aSLionel Sambuc // l-values.
VisitDeclRefExpr(DeclRefExpr * E)265f4a2713aSLionel Sambuc Value *VisitDeclRefExpr(DeclRefExpr *E) {
266f4a2713aSLionel Sambuc if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) {
267f4a2713aSLionel Sambuc if (result.isReference())
268f4a2713aSLionel Sambuc return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
269f4a2713aSLionel Sambuc E->getExprLoc());
270f4a2713aSLionel Sambuc return result.getValue();
271f4a2713aSLionel Sambuc }
272f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
273f4a2713aSLionel Sambuc }
274f4a2713aSLionel Sambuc
VisitObjCSelectorExpr(ObjCSelectorExpr * E)275f4a2713aSLionel Sambuc Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
276f4a2713aSLionel Sambuc return CGF.EmitObjCSelectorExpr(E);
277f4a2713aSLionel Sambuc }
VisitObjCProtocolExpr(ObjCProtocolExpr * E)278f4a2713aSLionel Sambuc Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
279f4a2713aSLionel Sambuc return CGF.EmitObjCProtocolExpr(E);
280f4a2713aSLionel Sambuc }
VisitObjCIvarRefExpr(ObjCIvarRefExpr * E)281f4a2713aSLionel Sambuc Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
282f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
283f4a2713aSLionel Sambuc }
VisitObjCMessageExpr(ObjCMessageExpr * E)284f4a2713aSLionel Sambuc Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
285f4a2713aSLionel Sambuc if (E->getMethodDecl() &&
286*0a6a1f1dSLionel Sambuc E->getMethodDecl()->getReturnType()->isReferenceType())
287f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
288f4a2713aSLionel Sambuc return CGF.EmitObjCMessageExpr(E).getScalarVal();
289f4a2713aSLionel Sambuc }
290f4a2713aSLionel Sambuc
VisitObjCIsaExpr(ObjCIsaExpr * E)291f4a2713aSLionel Sambuc Value *VisitObjCIsaExpr(ObjCIsaExpr *E) {
292f4a2713aSLionel Sambuc LValue LV = CGF.EmitObjCIsaExpr(E);
293f4a2713aSLionel Sambuc Value *V = CGF.EmitLoadOfLValue(LV, E->getExprLoc()).getScalarVal();
294f4a2713aSLionel Sambuc return V;
295f4a2713aSLionel Sambuc }
296f4a2713aSLionel Sambuc
297f4a2713aSLionel Sambuc Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
298f4a2713aSLionel Sambuc Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
299f4a2713aSLionel Sambuc Value *VisitConvertVectorExpr(ConvertVectorExpr *E);
300f4a2713aSLionel Sambuc Value *VisitMemberExpr(MemberExpr *E);
VisitExtVectorElementExpr(Expr * E)301f4a2713aSLionel Sambuc Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
VisitCompoundLiteralExpr(CompoundLiteralExpr * E)302f4a2713aSLionel Sambuc Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
303f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
304f4a2713aSLionel Sambuc }
305f4a2713aSLionel Sambuc
306f4a2713aSLionel Sambuc Value *VisitInitListExpr(InitListExpr *E);
307f4a2713aSLionel Sambuc
VisitImplicitValueInitExpr(const ImplicitValueInitExpr * E)308f4a2713aSLionel Sambuc Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
309f4a2713aSLionel Sambuc return EmitNullValue(E->getType());
310f4a2713aSLionel Sambuc }
VisitExplicitCastExpr(ExplicitCastExpr * E)311f4a2713aSLionel Sambuc Value *VisitExplicitCastExpr(ExplicitCastExpr *E) {
312f4a2713aSLionel Sambuc if (E->getType()->isVariablyModifiedType())
313f4a2713aSLionel Sambuc CGF.EmitVariablyModifiedType(E->getType());
314*0a6a1f1dSLionel Sambuc
315*0a6a1f1dSLionel Sambuc if (CGDebugInfo *DI = CGF.getDebugInfo())
316*0a6a1f1dSLionel Sambuc DI->EmitExplicitCastType(E->getType());
317*0a6a1f1dSLionel Sambuc
318f4a2713aSLionel Sambuc return VisitCastExpr(E);
319f4a2713aSLionel Sambuc }
320f4a2713aSLionel Sambuc Value *VisitCastExpr(CastExpr *E);
321f4a2713aSLionel Sambuc
VisitCallExpr(const CallExpr * E)322f4a2713aSLionel Sambuc Value *VisitCallExpr(const CallExpr *E) {
323f4a2713aSLionel Sambuc if (E->getCallReturnType()->isReferenceType())
324f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
325f4a2713aSLionel Sambuc
326*0a6a1f1dSLionel Sambuc Value *V = CGF.EmitCallExpr(E).getScalarVal();
327*0a6a1f1dSLionel Sambuc
328*0a6a1f1dSLionel Sambuc EmitLValueAlignmentAssumption(E, V);
329*0a6a1f1dSLionel Sambuc return V;
330f4a2713aSLionel Sambuc }
331f4a2713aSLionel Sambuc
332f4a2713aSLionel Sambuc Value *VisitStmtExpr(const StmtExpr *E);
333f4a2713aSLionel Sambuc
334f4a2713aSLionel Sambuc // Unary Operators.
VisitUnaryPostDec(const UnaryOperator * E)335f4a2713aSLionel Sambuc Value *VisitUnaryPostDec(const UnaryOperator *E) {
336f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
337f4a2713aSLionel Sambuc return EmitScalarPrePostIncDec(E, LV, false, false);
338f4a2713aSLionel Sambuc }
VisitUnaryPostInc(const UnaryOperator * E)339f4a2713aSLionel Sambuc Value *VisitUnaryPostInc(const UnaryOperator *E) {
340f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
341f4a2713aSLionel Sambuc return EmitScalarPrePostIncDec(E, LV, true, false);
342f4a2713aSLionel Sambuc }
VisitUnaryPreDec(const UnaryOperator * E)343f4a2713aSLionel Sambuc Value *VisitUnaryPreDec(const UnaryOperator *E) {
344f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
345f4a2713aSLionel Sambuc return EmitScalarPrePostIncDec(E, LV, false, true);
346f4a2713aSLionel Sambuc }
VisitUnaryPreInc(const UnaryOperator * E)347f4a2713aSLionel Sambuc Value *VisitUnaryPreInc(const UnaryOperator *E) {
348f4a2713aSLionel Sambuc LValue LV = EmitLValue(E->getSubExpr());
349f4a2713aSLionel Sambuc return EmitScalarPrePostIncDec(E, LV, true, true);
350f4a2713aSLionel Sambuc }
351f4a2713aSLionel Sambuc
352f4a2713aSLionel Sambuc llvm::Value *EmitAddConsiderOverflowBehavior(const UnaryOperator *E,
353f4a2713aSLionel Sambuc llvm::Value *InVal,
354f4a2713aSLionel Sambuc llvm::Value *NextVal,
355f4a2713aSLionel Sambuc bool IsInc);
356f4a2713aSLionel Sambuc
357f4a2713aSLionel Sambuc llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
358f4a2713aSLionel Sambuc bool isInc, bool isPre);
359f4a2713aSLionel Sambuc
360f4a2713aSLionel Sambuc
VisitUnaryAddrOf(const UnaryOperator * E)361f4a2713aSLionel Sambuc Value *VisitUnaryAddrOf(const UnaryOperator *E) {
362f4a2713aSLionel Sambuc if (isa<MemberPointerType>(E->getType())) // never sugared
363f4a2713aSLionel Sambuc return CGF.CGM.getMemberPointerConstant(E);
364f4a2713aSLionel Sambuc
365f4a2713aSLionel Sambuc return EmitLValue(E->getSubExpr()).getAddress();
366f4a2713aSLionel Sambuc }
VisitUnaryDeref(const UnaryOperator * E)367f4a2713aSLionel Sambuc Value *VisitUnaryDeref(const UnaryOperator *E) {
368f4a2713aSLionel Sambuc if (E->getType()->isVoidType())
369f4a2713aSLionel Sambuc return Visit(E->getSubExpr()); // the actual value should be unused
370f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
371f4a2713aSLionel Sambuc }
VisitUnaryPlus(const UnaryOperator * E)372f4a2713aSLionel Sambuc Value *VisitUnaryPlus(const UnaryOperator *E) {
373f4a2713aSLionel Sambuc // This differs from gcc, though, most likely due to a bug in gcc.
374f4a2713aSLionel Sambuc TestAndClearIgnoreResultAssign();
375f4a2713aSLionel Sambuc return Visit(E->getSubExpr());
376f4a2713aSLionel Sambuc }
377f4a2713aSLionel Sambuc Value *VisitUnaryMinus (const UnaryOperator *E);
378f4a2713aSLionel Sambuc Value *VisitUnaryNot (const UnaryOperator *E);
379f4a2713aSLionel Sambuc Value *VisitUnaryLNot (const UnaryOperator *E);
380f4a2713aSLionel Sambuc Value *VisitUnaryReal (const UnaryOperator *E);
381f4a2713aSLionel Sambuc Value *VisitUnaryImag (const UnaryOperator *E);
VisitUnaryExtension(const UnaryOperator * E)382f4a2713aSLionel Sambuc Value *VisitUnaryExtension(const UnaryOperator *E) {
383f4a2713aSLionel Sambuc return Visit(E->getSubExpr());
384f4a2713aSLionel Sambuc }
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc // C++
VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr * E)387f4a2713aSLionel Sambuc Value *VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E) {
388f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
389f4a2713aSLionel Sambuc }
390f4a2713aSLionel Sambuc
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * DAE)391f4a2713aSLionel Sambuc Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
392f4a2713aSLionel Sambuc return Visit(DAE->getExpr());
393f4a2713aSLionel Sambuc }
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * DIE)394f4a2713aSLionel Sambuc Value *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
395f4a2713aSLionel Sambuc CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
396f4a2713aSLionel Sambuc return Visit(DIE->getExpr());
397f4a2713aSLionel Sambuc }
VisitCXXThisExpr(CXXThisExpr * TE)398f4a2713aSLionel Sambuc Value *VisitCXXThisExpr(CXXThisExpr *TE) {
399f4a2713aSLionel Sambuc return CGF.LoadCXXThis();
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc
VisitExprWithCleanups(ExprWithCleanups * E)402f4a2713aSLionel Sambuc Value *VisitExprWithCleanups(ExprWithCleanups *E) {
403f4a2713aSLionel Sambuc CGF.enterFullExpression(E);
404f4a2713aSLionel Sambuc CodeGenFunction::RunCleanupsScope Scope(CGF);
405f4a2713aSLionel Sambuc return Visit(E->getSubExpr());
406f4a2713aSLionel Sambuc }
VisitCXXNewExpr(const CXXNewExpr * E)407f4a2713aSLionel Sambuc Value *VisitCXXNewExpr(const CXXNewExpr *E) {
408f4a2713aSLionel Sambuc return CGF.EmitCXXNewExpr(E);
409f4a2713aSLionel Sambuc }
VisitCXXDeleteExpr(const CXXDeleteExpr * E)410f4a2713aSLionel Sambuc Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
411f4a2713aSLionel Sambuc CGF.EmitCXXDeleteExpr(E);
412*0a6a1f1dSLionel Sambuc return nullptr;
413f4a2713aSLionel Sambuc }
414f4a2713aSLionel Sambuc
VisitTypeTraitExpr(const TypeTraitExpr * E)415*0a6a1f1dSLionel Sambuc Value *VisitTypeTraitExpr(const TypeTraitExpr *E) {
416f4a2713aSLionel Sambuc return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
417f4a2713aSLionel Sambuc }
418f4a2713aSLionel Sambuc
VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr * E)419f4a2713aSLionel Sambuc Value *VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
420f4a2713aSLionel Sambuc return llvm::ConstantInt::get(Builder.getInt32Ty(), E->getValue());
421f4a2713aSLionel Sambuc }
422f4a2713aSLionel Sambuc
VisitExpressionTraitExpr(const ExpressionTraitExpr * E)423f4a2713aSLionel Sambuc Value *VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
424f4a2713aSLionel Sambuc return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue());
425f4a2713aSLionel Sambuc }
426f4a2713aSLionel Sambuc
VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr * E)427f4a2713aSLionel Sambuc Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) {
428f4a2713aSLionel Sambuc // C++ [expr.pseudo]p1:
429f4a2713aSLionel Sambuc // The result shall only be used as the operand for the function call
430f4a2713aSLionel Sambuc // operator (), and the result of such a call has type void. The only
431f4a2713aSLionel Sambuc // effect is the evaluation of the postfix-expression before the dot or
432f4a2713aSLionel Sambuc // arrow.
433f4a2713aSLionel Sambuc CGF.EmitScalarExpr(E->getBase());
434*0a6a1f1dSLionel Sambuc return nullptr;
435f4a2713aSLionel Sambuc }
436f4a2713aSLionel Sambuc
VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr * E)437f4a2713aSLionel Sambuc Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
438f4a2713aSLionel Sambuc return EmitNullValue(E->getType());
439f4a2713aSLionel Sambuc }
440f4a2713aSLionel Sambuc
VisitCXXThrowExpr(const CXXThrowExpr * E)441f4a2713aSLionel Sambuc Value *VisitCXXThrowExpr(const CXXThrowExpr *E) {
442f4a2713aSLionel Sambuc CGF.EmitCXXThrowExpr(E);
443*0a6a1f1dSLionel Sambuc return nullptr;
444f4a2713aSLionel Sambuc }
445f4a2713aSLionel Sambuc
VisitCXXNoexceptExpr(const CXXNoexceptExpr * E)446f4a2713aSLionel Sambuc Value *VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
447f4a2713aSLionel Sambuc return Builder.getInt1(E->getValue());
448f4a2713aSLionel Sambuc }
449f4a2713aSLionel Sambuc
450f4a2713aSLionel Sambuc // Binary Operators.
EmitMul(const BinOpInfo & Ops)451f4a2713aSLionel Sambuc Value *EmitMul(const BinOpInfo &Ops) {
452f4a2713aSLionel Sambuc if (Ops.Ty->isSignedIntegerOrEnumerationType()) {
453f4a2713aSLionel Sambuc switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
454f4a2713aSLionel Sambuc case LangOptions::SOB_Defined:
455f4a2713aSLionel Sambuc return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
456f4a2713aSLionel Sambuc case LangOptions::SOB_Undefined:
457*0a6a1f1dSLionel Sambuc if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow))
458f4a2713aSLionel Sambuc return Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul");
459f4a2713aSLionel Sambuc // Fall through.
460f4a2713aSLionel Sambuc case LangOptions::SOB_Trapping:
461f4a2713aSLionel Sambuc return EmitOverflowCheckedBinOp(Ops);
462f4a2713aSLionel Sambuc }
463f4a2713aSLionel Sambuc }
464f4a2713aSLionel Sambuc
465*0a6a1f1dSLionel Sambuc if (Ops.Ty->isUnsignedIntegerType() &&
466*0a6a1f1dSLionel Sambuc CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow))
467f4a2713aSLionel Sambuc return EmitOverflowCheckedBinOp(Ops);
468f4a2713aSLionel Sambuc
469f4a2713aSLionel Sambuc if (Ops.LHS->getType()->isFPOrFPVectorTy())
470f4a2713aSLionel Sambuc return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul");
471f4a2713aSLionel Sambuc return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
472f4a2713aSLionel Sambuc }
473f4a2713aSLionel Sambuc /// Create a binary op that checks for overflow.
474f4a2713aSLionel Sambuc /// Currently only supports +, - and *.
475f4a2713aSLionel Sambuc Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
476f4a2713aSLionel Sambuc
477f4a2713aSLionel Sambuc // Check for undefined division and modulus behaviors.
478f4a2713aSLionel Sambuc void EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo &Ops,
479f4a2713aSLionel Sambuc llvm::Value *Zero,bool isDiv);
480f4a2713aSLionel Sambuc // Common helper for getting how wide LHS of shift is.
481f4a2713aSLionel Sambuc static Value *GetWidthMinusOneValue(Value* LHS,Value* RHS);
482f4a2713aSLionel Sambuc Value *EmitDiv(const BinOpInfo &Ops);
483f4a2713aSLionel Sambuc Value *EmitRem(const BinOpInfo &Ops);
484f4a2713aSLionel Sambuc Value *EmitAdd(const BinOpInfo &Ops);
485f4a2713aSLionel Sambuc Value *EmitSub(const BinOpInfo &Ops);
486f4a2713aSLionel Sambuc Value *EmitShl(const BinOpInfo &Ops);
487f4a2713aSLionel Sambuc Value *EmitShr(const BinOpInfo &Ops);
EmitAnd(const BinOpInfo & Ops)488f4a2713aSLionel Sambuc Value *EmitAnd(const BinOpInfo &Ops) {
489f4a2713aSLionel Sambuc return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
490f4a2713aSLionel Sambuc }
EmitXor(const BinOpInfo & Ops)491f4a2713aSLionel Sambuc Value *EmitXor(const BinOpInfo &Ops) {
492f4a2713aSLionel Sambuc return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
493f4a2713aSLionel Sambuc }
EmitOr(const BinOpInfo & Ops)494f4a2713aSLionel Sambuc Value *EmitOr (const BinOpInfo &Ops) {
495f4a2713aSLionel Sambuc return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
496f4a2713aSLionel Sambuc }
497f4a2713aSLionel Sambuc
498f4a2713aSLionel Sambuc BinOpInfo EmitBinOps(const BinaryOperator *E);
499f4a2713aSLionel Sambuc LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
500f4a2713aSLionel Sambuc Value *(ScalarExprEmitter::*F)(const BinOpInfo &),
501f4a2713aSLionel Sambuc Value *&Result);
502f4a2713aSLionel Sambuc
503f4a2713aSLionel Sambuc Value *EmitCompoundAssign(const CompoundAssignOperator *E,
504f4a2713aSLionel Sambuc Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
505f4a2713aSLionel Sambuc
506f4a2713aSLionel Sambuc // Binary operators and binary compound assignment operators.
507f4a2713aSLionel Sambuc #define HANDLEBINOP(OP) \
508f4a2713aSLionel Sambuc Value *VisitBin ## OP(const BinaryOperator *E) { \
509f4a2713aSLionel Sambuc return Emit ## OP(EmitBinOps(E)); \
510f4a2713aSLionel Sambuc } \
511f4a2713aSLionel Sambuc Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
512f4a2713aSLionel Sambuc return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
513f4a2713aSLionel Sambuc }
514f4a2713aSLionel Sambuc HANDLEBINOP(Mul)
515f4a2713aSLionel Sambuc HANDLEBINOP(Div)
516f4a2713aSLionel Sambuc HANDLEBINOP(Rem)
517f4a2713aSLionel Sambuc HANDLEBINOP(Add)
518f4a2713aSLionel Sambuc HANDLEBINOP(Sub)
519f4a2713aSLionel Sambuc HANDLEBINOP(Shl)
520f4a2713aSLionel Sambuc HANDLEBINOP(Shr)
521f4a2713aSLionel Sambuc HANDLEBINOP(And)
522f4a2713aSLionel Sambuc HANDLEBINOP(Xor)
523f4a2713aSLionel Sambuc HANDLEBINOP(Or)
524f4a2713aSLionel Sambuc #undef HANDLEBINOP
525f4a2713aSLionel Sambuc
526f4a2713aSLionel Sambuc // Comparisons.
527f4a2713aSLionel Sambuc Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
528f4a2713aSLionel Sambuc unsigned SICmpOpc, unsigned FCmpOpc);
529f4a2713aSLionel Sambuc #define VISITCOMP(CODE, UI, SI, FP) \
530f4a2713aSLionel Sambuc Value *VisitBin##CODE(const BinaryOperator *E) { \
531f4a2713aSLionel Sambuc return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
532f4a2713aSLionel Sambuc llvm::FCmpInst::FP); }
533f4a2713aSLionel Sambuc VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT)
534f4a2713aSLionel Sambuc VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT)
535f4a2713aSLionel Sambuc VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE)
536f4a2713aSLionel Sambuc VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE)
537f4a2713aSLionel Sambuc VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ)
538f4a2713aSLionel Sambuc VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE)
539f4a2713aSLionel Sambuc #undef VISITCOMP
540f4a2713aSLionel Sambuc
541f4a2713aSLionel Sambuc Value *VisitBinAssign (const BinaryOperator *E);
542f4a2713aSLionel Sambuc
543f4a2713aSLionel Sambuc Value *VisitBinLAnd (const BinaryOperator *E);
544f4a2713aSLionel Sambuc Value *VisitBinLOr (const BinaryOperator *E);
545f4a2713aSLionel Sambuc Value *VisitBinComma (const BinaryOperator *E);
546f4a2713aSLionel Sambuc
VisitBinPtrMemD(const Expr * E)547f4a2713aSLionel Sambuc Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); }
VisitBinPtrMemI(const Expr * E)548f4a2713aSLionel Sambuc Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); }
549f4a2713aSLionel Sambuc
550f4a2713aSLionel Sambuc // Other Operators.
551f4a2713aSLionel Sambuc Value *VisitBlockExpr(const BlockExpr *BE);
552f4a2713aSLionel Sambuc Value *VisitAbstractConditionalOperator(const AbstractConditionalOperator *);
553f4a2713aSLionel Sambuc Value *VisitChooseExpr(ChooseExpr *CE);
554f4a2713aSLionel Sambuc Value *VisitVAArgExpr(VAArgExpr *VE);
VisitObjCStringLiteral(const ObjCStringLiteral * E)555f4a2713aSLionel Sambuc Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
556f4a2713aSLionel Sambuc return CGF.EmitObjCStringLiteral(E);
557f4a2713aSLionel Sambuc }
VisitObjCBoxedExpr(ObjCBoxedExpr * E)558f4a2713aSLionel Sambuc Value *VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
559f4a2713aSLionel Sambuc return CGF.EmitObjCBoxedExpr(E);
560f4a2713aSLionel Sambuc }
VisitObjCArrayLiteral(ObjCArrayLiteral * E)561f4a2713aSLionel Sambuc Value *VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
562f4a2713aSLionel Sambuc return CGF.EmitObjCArrayLiteral(E);
563f4a2713aSLionel Sambuc }
VisitObjCDictionaryLiteral(ObjCDictionaryLiteral * E)564f4a2713aSLionel Sambuc Value *VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
565f4a2713aSLionel Sambuc return CGF.EmitObjCDictionaryLiteral(E);
566f4a2713aSLionel Sambuc }
567f4a2713aSLionel Sambuc Value *VisitAsTypeExpr(AsTypeExpr *CE);
568f4a2713aSLionel Sambuc Value *VisitAtomicExpr(AtomicExpr *AE);
569f4a2713aSLionel Sambuc };
570f4a2713aSLionel Sambuc } // end anonymous namespace.
571f4a2713aSLionel Sambuc
572f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
573f4a2713aSLionel Sambuc // Utilities
574f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
575f4a2713aSLionel Sambuc
576f4a2713aSLionel Sambuc /// EmitConversionToBool - Convert the specified expression value to a
577f4a2713aSLionel Sambuc /// boolean (i1) truth value. This is equivalent to "Val != 0".
EmitConversionToBool(Value * Src,QualType SrcType)578f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
579f4a2713aSLionel Sambuc assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs");
580f4a2713aSLionel Sambuc
581f4a2713aSLionel Sambuc if (SrcType->isRealFloatingType())
582f4a2713aSLionel Sambuc return EmitFloatToBoolConversion(Src);
583f4a2713aSLionel Sambuc
584f4a2713aSLionel Sambuc if (const MemberPointerType *MPT = dyn_cast<MemberPointerType>(SrcType))
585f4a2713aSLionel Sambuc return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT);
586f4a2713aSLionel Sambuc
587f4a2713aSLionel Sambuc assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
588f4a2713aSLionel Sambuc "Unknown scalar type to convert");
589f4a2713aSLionel Sambuc
590f4a2713aSLionel Sambuc if (isa<llvm::IntegerType>(Src->getType()))
591f4a2713aSLionel Sambuc return EmitIntToBoolConversion(Src);
592f4a2713aSLionel Sambuc
593f4a2713aSLionel Sambuc assert(isa<llvm::PointerType>(Src->getType()));
594f4a2713aSLionel Sambuc return EmitPointerToBoolConversion(Src);
595f4a2713aSLionel Sambuc }
596f4a2713aSLionel Sambuc
EmitFloatConversionCheck(Value * OrigSrc,QualType OrigSrcType,Value * Src,QualType SrcType,QualType DstType,llvm::Type * DstTy)597f4a2713aSLionel Sambuc void ScalarExprEmitter::EmitFloatConversionCheck(Value *OrigSrc,
598f4a2713aSLionel Sambuc QualType OrigSrcType,
599f4a2713aSLionel Sambuc Value *Src, QualType SrcType,
600f4a2713aSLionel Sambuc QualType DstType,
601f4a2713aSLionel Sambuc llvm::Type *DstTy) {
602*0a6a1f1dSLionel Sambuc CodeGenFunction::SanitizerScope SanScope(&CGF);
603f4a2713aSLionel Sambuc using llvm::APFloat;
604f4a2713aSLionel Sambuc using llvm::APSInt;
605f4a2713aSLionel Sambuc
606f4a2713aSLionel Sambuc llvm::Type *SrcTy = Src->getType();
607f4a2713aSLionel Sambuc
608*0a6a1f1dSLionel Sambuc llvm::Value *Check = nullptr;
609f4a2713aSLionel Sambuc if (llvm::IntegerType *IntTy = dyn_cast<llvm::IntegerType>(SrcTy)) {
610f4a2713aSLionel Sambuc // Integer to floating-point. This can fail for unsigned short -> __half
611f4a2713aSLionel Sambuc // or unsigned __int128 -> float.
612f4a2713aSLionel Sambuc assert(DstType->isFloatingType());
613f4a2713aSLionel Sambuc bool SrcIsUnsigned = OrigSrcType->isUnsignedIntegerOrEnumerationType();
614f4a2713aSLionel Sambuc
615f4a2713aSLionel Sambuc APFloat LargestFloat =
616f4a2713aSLionel Sambuc APFloat::getLargest(CGF.getContext().getFloatTypeSemantics(DstType));
617f4a2713aSLionel Sambuc APSInt LargestInt(IntTy->getBitWidth(), SrcIsUnsigned);
618f4a2713aSLionel Sambuc
619f4a2713aSLionel Sambuc bool IsExact;
620f4a2713aSLionel Sambuc if (LargestFloat.convertToInteger(LargestInt, APFloat::rmTowardZero,
621f4a2713aSLionel Sambuc &IsExact) != APFloat::opOK)
622f4a2713aSLionel Sambuc // The range of representable values of this floating point type includes
623f4a2713aSLionel Sambuc // all values of this integer type. Don't need an overflow check.
624f4a2713aSLionel Sambuc return;
625f4a2713aSLionel Sambuc
626f4a2713aSLionel Sambuc llvm::Value *Max = llvm::ConstantInt::get(VMContext, LargestInt);
627f4a2713aSLionel Sambuc if (SrcIsUnsigned)
628f4a2713aSLionel Sambuc Check = Builder.CreateICmpULE(Src, Max);
629f4a2713aSLionel Sambuc else {
630f4a2713aSLionel Sambuc llvm::Value *Min = llvm::ConstantInt::get(VMContext, -LargestInt);
631f4a2713aSLionel Sambuc llvm::Value *GE = Builder.CreateICmpSGE(Src, Min);
632f4a2713aSLionel Sambuc llvm::Value *LE = Builder.CreateICmpSLE(Src, Max);
633f4a2713aSLionel Sambuc Check = Builder.CreateAnd(GE, LE);
634f4a2713aSLionel Sambuc }
635f4a2713aSLionel Sambuc } else {
636f4a2713aSLionel Sambuc const llvm::fltSemantics &SrcSema =
637f4a2713aSLionel Sambuc CGF.getContext().getFloatTypeSemantics(OrigSrcType);
638f4a2713aSLionel Sambuc if (isa<llvm::IntegerType>(DstTy)) {
639f4a2713aSLionel Sambuc // Floating-point to integer. This has undefined behavior if the source is
640f4a2713aSLionel Sambuc // +-Inf, NaN, or doesn't fit into the destination type (after truncation
641f4a2713aSLionel Sambuc // to an integer).
642f4a2713aSLionel Sambuc unsigned Width = CGF.getContext().getIntWidth(DstType);
643f4a2713aSLionel Sambuc bool Unsigned = DstType->isUnsignedIntegerOrEnumerationType();
644f4a2713aSLionel Sambuc
645f4a2713aSLionel Sambuc APSInt Min = APSInt::getMinValue(Width, Unsigned);
646f4a2713aSLionel Sambuc APFloat MinSrc(SrcSema, APFloat::uninitialized);
647f4a2713aSLionel Sambuc if (MinSrc.convertFromAPInt(Min, !Unsigned, APFloat::rmTowardZero) &
648f4a2713aSLionel Sambuc APFloat::opOverflow)
649f4a2713aSLionel Sambuc // Don't need an overflow check for lower bound. Just check for
650f4a2713aSLionel Sambuc // -Inf/NaN.
651f4a2713aSLionel Sambuc MinSrc = APFloat::getInf(SrcSema, true);
652f4a2713aSLionel Sambuc else
653f4a2713aSLionel Sambuc // Find the largest value which is too small to represent (before
654f4a2713aSLionel Sambuc // truncation toward zero).
655f4a2713aSLionel Sambuc MinSrc.subtract(APFloat(SrcSema, 1), APFloat::rmTowardNegative);
656f4a2713aSLionel Sambuc
657f4a2713aSLionel Sambuc APSInt Max = APSInt::getMaxValue(Width, Unsigned);
658f4a2713aSLionel Sambuc APFloat MaxSrc(SrcSema, APFloat::uninitialized);
659f4a2713aSLionel Sambuc if (MaxSrc.convertFromAPInt(Max, !Unsigned, APFloat::rmTowardZero) &
660f4a2713aSLionel Sambuc APFloat::opOverflow)
661f4a2713aSLionel Sambuc // Don't need an overflow check for upper bound. Just check for
662f4a2713aSLionel Sambuc // +Inf/NaN.
663f4a2713aSLionel Sambuc MaxSrc = APFloat::getInf(SrcSema, false);
664f4a2713aSLionel Sambuc else
665f4a2713aSLionel Sambuc // Find the smallest value which is too large to represent (before
666f4a2713aSLionel Sambuc // truncation toward zero).
667f4a2713aSLionel Sambuc MaxSrc.add(APFloat(SrcSema, 1), APFloat::rmTowardPositive);
668f4a2713aSLionel Sambuc
669f4a2713aSLionel Sambuc // If we're converting from __half, convert the range to float to match
670f4a2713aSLionel Sambuc // the type of src.
671f4a2713aSLionel Sambuc if (OrigSrcType->isHalfType()) {
672f4a2713aSLionel Sambuc const llvm::fltSemantics &Sema =
673f4a2713aSLionel Sambuc CGF.getContext().getFloatTypeSemantics(SrcType);
674f4a2713aSLionel Sambuc bool IsInexact;
675f4a2713aSLionel Sambuc MinSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact);
676f4a2713aSLionel Sambuc MaxSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact);
677f4a2713aSLionel Sambuc }
678f4a2713aSLionel Sambuc
679f4a2713aSLionel Sambuc llvm::Value *GE =
680f4a2713aSLionel Sambuc Builder.CreateFCmpOGT(Src, llvm::ConstantFP::get(VMContext, MinSrc));
681f4a2713aSLionel Sambuc llvm::Value *LE =
682f4a2713aSLionel Sambuc Builder.CreateFCmpOLT(Src, llvm::ConstantFP::get(VMContext, MaxSrc));
683f4a2713aSLionel Sambuc Check = Builder.CreateAnd(GE, LE);
684f4a2713aSLionel Sambuc } else {
685f4a2713aSLionel Sambuc // FIXME: Maybe split this sanitizer out from float-cast-overflow.
686f4a2713aSLionel Sambuc //
687f4a2713aSLionel Sambuc // Floating-point to floating-point. This has undefined behavior if the
688f4a2713aSLionel Sambuc // source is not in the range of representable values of the destination
689f4a2713aSLionel Sambuc // type. The C and C++ standards are spectacularly unclear here. We
690f4a2713aSLionel Sambuc // diagnose finite out-of-range conversions, but allow infinities and NaNs
691f4a2713aSLionel Sambuc // to convert to the corresponding value in the smaller type.
692f4a2713aSLionel Sambuc //
693f4a2713aSLionel Sambuc // C11 Annex F gives all such conversions defined behavior for IEC 60559
694f4a2713aSLionel Sambuc // conforming implementations. Unfortunately, LLVM's fptrunc instruction
695f4a2713aSLionel Sambuc // does not.
696f4a2713aSLionel Sambuc
697f4a2713aSLionel Sambuc // Converting from a lower rank to a higher rank can never have
698f4a2713aSLionel Sambuc // undefined behavior, since higher-rank types must have a superset
699f4a2713aSLionel Sambuc // of values of lower-rank types.
700f4a2713aSLionel Sambuc if (CGF.getContext().getFloatingTypeOrder(OrigSrcType, DstType) != 1)
701f4a2713aSLionel Sambuc return;
702f4a2713aSLionel Sambuc
703f4a2713aSLionel Sambuc assert(!OrigSrcType->isHalfType() &&
704f4a2713aSLionel Sambuc "should not check conversion from __half, it has the lowest rank");
705f4a2713aSLionel Sambuc
706f4a2713aSLionel Sambuc const llvm::fltSemantics &DstSema =
707f4a2713aSLionel Sambuc CGF.getContext().getFloatTypeSemantics(DstType);
708f4a2713aSLionel Sambuc APFloat MinBad = APFloat::getLargest(DstSema, false);
709f4a2713aSLionel Sambuc APFloat MaxBad = APFloat::getInf(DstSema, false);
710f4a2713aSLionel Sambuc
711f4a2713aSLionel Sambuc bool IsInexact;
712f4a2713aSLionel Sambuc MinBad.convert(SrcSema, APFloat::rmTowardZero, &IsInexact);
713f4a2713aSLionel Sambuc MaxBad.convert(SrcSema, APFloat::rmTowardZero, &IsInexact);
714f4a2713aSLionel Sambuc
715f4a2713aSLionel Sambuc Value *AbsSrc = CGF.EmitNounwindRuntimeCall(
716f4a2713aSLionel Sambuc CGF.CGM.getIntrinsic(llvm::Intrinsic::fabs, Src->getType()), Src);
717f4a2713aSLionel Sambuc llvm::Value *GE =
718f4a2713aSLionel Sambuc Builder.CreateFCmpOGT(AbsSrc, llvm::ConstantFP::get(VMContext, MinBad));
719f4a2713aSLionel Sambuc llvm::Value *LE =
720f4a2713aSLionel Sambuc Builder.CreateFCmpOLT(AbsSrc, llvm::ConstantFP::get(VMContext, MaxBad));
721f4a2713aSLionel Sambuc Check = Builder.CreateNot(Builder.CreateAnd(GE, LE));
722f4a2713aSLionel Sambuc }
723f4a2713aSLionel Sambuc }
724f4a2713aSLionel Sambuc
725f4a2713aSLionel Sambuc // FIXME: Provide a SourceLocation.
726f4a2713aSLionel Sambuc llvm::Constant *StaticArgs[] = {
727f4a2713aSLionel Sambuc CGF.EmitCheckTypeDescriptor(OrigSrcType),
728f4a2713aSLionel Sambuc CGF.EmitCheckTypeDescriptor(DstType)
729f4a2713aSLionel Sambuc };
730*0a6a1f1dSLionel Sambuc CGF.EmitCheck(std::make_pair(Check, SanitizerKind::FloatCastOverflow),
731*0a6a1f1dSLionel Sambuc "float_cast_overflow", StaticArgs, OrigSrc);
732f4a2713aSLionel Sambuc }
733f4a2713aSLionel Sambuc
734f4a2713aSLionel Sambuc /// EmitScalarConversion - Emit a conversion from the specified type to the
735f4a2713aSLionel Sambuc /// specified destination type, both of which are LLVM scalar types.
EmitScalarConversion(Value * Src,QualType SrcType,QualType DstType)736f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
737f4a2713aSLionel Sambuc QualType DstType) {
738f4a2713aSLionel Sambuc SrcType = CGF.getContext().getCanonicalType(SrcType);
739f4a2713aSLionel Sambuc DstType = CGF.getContext().getCanonicalType(DstType);
740f4a2713aSLionel Sambuc if (SrcType == DstType) return Src;
741f4a2713aSLionel Sambuc
742*0a6a1f1dSLionel Sambuc if (DstType->isVoidType()) return nullptr;
743f4a2713aSLionel Sambuc
744f4a2713aSLionel Sambuc llvm::Value *OrigSrc = Src;
745f4a2713aSLionel Sambuc QualType OrigSrcType = SrcType;
746f4a2713aSLionel Sambuc llvm::Type *SrcTy = Src->getType();
747f4a2713aSLionel Sambuc
748f4a2713aSLionel Sambuc // If casting to/from storage-only half FP, use special intrinsics.
749*0a6a1f1dSLionel Sambuc if (SrcType->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType &&
750*0a6a1f1dSLionel Sambuc !CGF.getContext().getLangOpts().HalfArgsAndReturns) {
751*0a6a1f1dSLionel Sambuc Src = Builder.CreateCall(
752*0a6a1f1dSLionel Sambuc CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16,
753*0a6a1f1dSLionel Sambuc CGF.CGM.FloatTy),
754*0a6a1f1dSLionel Sambuc Src);
755f4a2713aSLionel Sambuc SrcType = CGF.getContext().FloatTy;
756f4a2713aSLionel Sambuc SrcTy = CGF.FloatTy;
757f4a2713aSLionel Sambuc }
758f4a2713aSLionel Sambuc
759f4a2713aSLionel Sambuc // Handle conversions to bool first, they are special: comparisons against 0.
760f4a2713aSLionel Sambuc if (DstType->isBooleanType())
761f4a2713aSLionel Sambuc return EmitConversionToBool(Src, SrcType);
762f4a2713aSLionel Sambuc
763f4a2713aSLionel Sambuc llvm::Type *DstTy = ConvertType(DstType);
764f4a2713aSLionel Sambuc
765f4a2713aSLionel Sambuc // Ignore conversions like int -> uint.
766f4a2713aSLionel Sambuc if (SrcTy == DstTy)
767f4a2713aSLionel Sambuc return Src;
768f4a2713aSLionel Sambuc
769f4a2713aSLionel Sambuc // Handle pointer conversions next: pointers can only be converted to/from
770f4a2713aSLionel Sambuc // other pointers and integers. Check for pointer types in terms of LLVM, as
771f4a2713aSLionel Sambuc // some native types (like Obj-C id) may map to a pointer type.
772f4a2713aSLionel Sambuc if (isa<llvm::PointerType>(DstTy)) {
773f4a2713aSLionel Sambuc // The source value may be an integer, or a pointer.
774f4a2713aSLionel Sambuc if (isa<llvm::PointerType>(SrcTy))
775f4a2713aSLionel Sambuc return Builder.CreateBitCast(Src, DstTy, "conv");
776f4a2713aSLionel Sambuc
777f4a2713aSLionel Sambuc assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
778f4a2713aSLionel Sambuc // First, convert to the correct width so that we control the kind of
779f4a2713aSLionel Sambuc // extension.
780f4a2713aSLionel Sambuc llvm::Type *MiddleTy = CGF.IntPtrTy;
781f4a2713aSLionel Sambuc bool InputSigned = SrcType->isSignedIntegerOrEnumerationType();
782f4a2713aSLionel Sambuc llvm::Value* IntResult =
783f4a2713aSLionel Sambuc Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
784f4a2713aSLionel Sambuc // Then, cast to pointer.
785f4a2713aSLionel Sambuc return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
786f4a2713aSLionel Sambuc }
787f4a2713aSLionel Sambuc
788f4a2713aSLionel Sambuc if (isa<llvm::PointerType>(SrcTy)) {
789f4a2713aSLionel Sambuc // Must be an ptr to int cast.
790f4a2713aSLionel Sambuc assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
791f4a2713aSLionel Sambuc return Builder.CreatePtrToInt(Src, DstTy, "conv");
792f4a2713aSLionel Sambuc }
793f4a2713aSLionel Sambuc
794f4a2713aSLionel Sambuc // A scalar can be splatted to an extended vector of the same element type
795f4a2713aSLionel Sambuc if (DstType->isExtVectorType() && !SrcType->isVectorType()) {
796f4a2713aSLionel Sambuc // Cast the scalar to element type
797f4a2713aSLionel Sambuc QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType();
798f4a2713aSLionel Sambuc llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
799f4a2713aSLionel Sambuc
800f4a2713aSLionel Sambuc // Splat the element across to all elements
801f4a2713aSLionel Sambuc unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
802f4a2713aSLionel Sambuc return Builder.CreateVectorSplat(NumElements, Elt, "splat");
803f4a2713aSLionel Sambuc }
804f4a2713aSLionel Sambuc
805f4a2713aSLionel Sambuc // Allow bitcast from vector to integer/fp of the same size.
806f4a2713aSLionel Sambuc if (isa<llvm::VectorType>(SrcTy) ||
807f4a2713aSLionel Sambuc isa<llvm::VectorType>(DstTy))
808f4a2713aSLionel Sambuc return Builder.CreateBitCast(Src, DstTy, "conv");
809f4a2713aSLionel Sambuc
810f4a2713aSLionel Sambuc // Finally, we have the arithmetic types: real int/float.
811*0a6a1f1dSLionel Sambuc Value *Res = nullptr;
812f4a2713aSLionel Sambuc llvm::Type *ResTy = DstTy;
813f4a2713aSLionel Sambuc
814f4a2713aSLionel Sambuc // An overflowing conversion has undefined behavior if either the source type
815f4a2713aSLionel Sambuc // or the destination type is a floating-point type.
816*0a6a1f1dSLionel Sambuc if (CGF.SanOpts.has(SanitizerKind::FloatCastOverflow) &&
817f4a2713aSLionel Sambuc (OrigSrcType->isFloatingType() || DstType->isFloatingType()))
818f4a2713aSLionel Sambuc EmitFloatConversionCheck(OrigSrc, OrigSrcType, Src, SrcType, DstType,
819f4a2713aSLionel Sambuc DstTy);
820f4a2713aSLionel Sambuc
821f4a2713aSLionel Sambuc // Cast to half via float
822*0a6a1f1dSLionel Sambuc if (DstType->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType &&
823*0a6a1f1dSLionel Sambuc !CGF.getContext().getLangOpts().HalfArgsAndReturns)
824f4a2713aSLionel Sambuc DstTy = CGF.FloatTy;
825f4a2713aSLionel Sambuc
826f4a2713aSLionel Sambuc if (isa<llvm::IntegerType>(SrcTy)) {
827f4a2713aSLionel Sambuc bool InputSigned = SrcType->isSignedIntegerOrEnumerationType();
828f4a2713aSLionel Sambuc if (isa<llvm::IntegerType>(DstTy))
829f4a2713aSLionel Sambuc Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
830f4a2713aSLionel Sambuc else if (InputSigned)
831f4a2713aSLionel Sambuc Res = Builder.CreateSIToFP(Src, DstTy, "conv");
832f4a2713aSLionel Sambuc else
833f4a2713aSLionel Sambuc Res = Builder.CreateUIToFP(Src, DstTy, "conv");
834f4a2713aSLionel Sambuc } else if (isa<llvm::IntegerType>(DstTy)) {
835f4a2713aSLionel Sambuc assert(SrcTy->isFloatingPointTy() && "Unknown real conversion");
836f4a2713aSLionel Sambuc if (DstType->isSignedIntegerOrEnumerationType())
837f4a2713aSLionel Sambuc Res = Builder.CreateFPToSI(Src, DstTy, "conv");
838f4a2713aSLionel Sambuc else
839f4a2713aSLionel Sambuc Res = Builder.CreateFPToUI(Src, DstTy, "conv");
840f4a2713aSLionel Sambuc } else {
841f4a2713aSLionel Sambuc assert(SrcTy->isFloatingPointTy() && DstTy->isFloatingPointTy() &&
842f4a2713aSLionel Sambuc "Unknown real conversion");
843f4a2713aSLionel Sambuc if (DstTy->getTypeID() < SrcTy->getTypeID())
844f4a2713aSLionel Sambuc Res = Builder.CreateFPTrunc(Src, DstTy, "conv");
845f4a2713aSLionel Sambuc else
846f4a2713aSLionel Sambuc Res = Builder.CreateFPExt(Src, DstTy, "conv");
847f4a2713aSLionel Sambuc }
848f4a2713aSLionel Sambuc
849f4a2713aSLionel Sambuc if (DstTy != ResTy) {
850f4a2713aSLionel Sambuc assert(ResTy->isIntegerTy(16) && "Only half FP requires extra conversion");
851*0a6a1f1dSLionel Sambuc Res = Builder.CreateCall(
852*0a6a1f1dSLionel Sambuc CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16, CGF.CGM.FloatTy),
853*0a6a1f1dSLionel Sambuc Res);
854f4a2713aSLionel Sambuc }
855f4a2713aSLionel Sambuc
856f4a2713aSLionel Sambuc return Res;
857f4a2713aSLionel Sambuc }
858f4a2713aSLionel Sambuc
859f4a2713aSLionel Sambuc /// EmitComplexToScalarConversion - Emit a conversion from the specified complex
860f4a2713aSLionel Sambuc /// type to the specified destination type, where the destination type is an
861f4a2713aSLionel Sambuc /// LLVM scalar type.
862f4a2713aSLionel Sambuc Value *ScalarExprEmitter::
EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,QualType SrcTy,QualType DstTy)863f4a2713aSLionel Sambuc EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
864f4a2713aSLionel Sambuc QualType SrcTy, QualType DstTy) {
865f4a2713aSLionel Sambuc // Get the source element type.
866f4a2713aSLionel Sambuc SrcTy = SrcTy->castAs<ComplexType>()->getElementType();
867f4a2713aSLionel Sambuc
868f4a2713aSLionel Sambuc // Handle conversions to bool first, they are special: comparisons against 0.
869f4a2713aSLionel Sambuc if (DstTy->isBooleanType()) {
870f4a2713aSLionel Sambuc // Complex != 0 -> (Real != 0) | (Imag != 0)
871f4a2713aSLionel Sambuc Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
872f4a2713aSLionel Sambuc Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
873f4a2713aSLionel Sambuc return Builder.CreateOr(Src.first, Src.second, "tobool");
874f4a2713aSLionel Sambuc }
875f4a2713aSLionel Sambuc
876f4a2713aSLionel Sambuc // C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
877f4a2713aSLionel Sambuc // the imaginary part of the complex value is discarded and the value of the
878f4a2713aSLionel Sambuc // real part is converted according to the conversion rules for the
879f4a2713aSLionel Sambuc // corresponding real type.
880f4a2713aSLionel Sambuc return EmitScalarConversion(Src.first, SrcTy, DstTy);
881f4a2713aSLionel Sambuc }
882f4a2713aSLionel Sambuc
EmitNullValue(QualType Ty)883f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitNullValue(QualType Ty) {
884f4a2713aSLionel Sambuc return CGF.EmitFromMemory(CGF.CGM.EmitNullConstant(Ty), Ty);
885f4a2713aSLionel Sambuc }
886f4a2713aSLionel Sambuc
887f4a2713aSLionel Sambuc /// \brief Emit a sanitization check for the given "binary" operation (which
888f4a2713aSLionel Sambuc /// might actually be a unary increment which has been lowered to a binary
889*0a6a1f1dSLionel Sambuc /// operation). The check passes if all values in \p Checks (which are \c i1),
890*0a6a1f1dSLionel Sambuc /// are \c true.
EmitBinOpCheck(ArrayRef<std::pair<Value *,SanitizerKind>> Checks,const BinOpInfo & Info)891*0a6a1f1dSLionel Sambuc void ScalarExprEmitter::EmitBinOpCheck(
892*0a6a1f1dSLionel Sambuc ArrayRef<std::pair<Value *, SanitizerKind>> Checks, const BinOpInfo &Info) {
893*0a6a1f1dSLionel Sambuc assert(CGF.IsSanitizerScope);
894f4a2713aSLionel Sambuc StringRef CheckName;
895f4a2713aSLionel Sambuc SmallVector<llvm::Constant *, 4> StaticData;
896f4a2713aSLionel Sambuc SmallVector<llvm::Value *, 2> DynamicData;
897f4a2713aSLionel Sambuc
898f4a2713aSLionel Sambuc BinaryOperatorKind Opcode = Info.Opcode;
899f4a2713aSLionel Sambuc if (BinaryOperator::isCompoundAssignmentOp(Opcode))
900f4a2713aSLionel Sambuc Opcode = BinaryOperator::getOpForCompoundAssignment(Opcode);
901f4a2713aSLionel Sambuc
902f4a2713aSLionel Sambuc StaticData.push_back(CGF.EmitCheckSourceLocation(Info.E->getExprLoc()));
903f4a2713aSLionel Sambuc const UnaryOperator *UO = dyn_cast<UnaryOperator>(Info.E);
904f4a2713aSLionel Sambuc if (UO && UO->getOpcode() == UO_Minus) {
905f4a2713aSLionel Sambuc CheckName = "negate_overflow";
906f4a2713aSLionel Sambuc StaticData.push_back(CGF.EmitCheckTypeDescriptor(UO->getType()));
907f4a2713aSLionel Sambuc DynamicData.push_back(Info.RHS);
908f4a2713aSLionel Sambuc } else {
909f4a2713aSLionel Sambuc if (BinaryOperator::isShiftOp(Opcode)) {
910f4a2713aSLionel Sambuc // Shift LHS negative or too large, or RHS out of bounds.
911f4a2713aSLionel Sambuc CheckName = "shift_out_of_bounds";
912f4a2713aSLionel Sambuc const BinaryOperator *BO = cast<BinaryOperator>(Info.E);
913f4a2713aSLionel Sambuc StaticData.push_back(
914f4a2713aSLionel Sambuc CGF.EmitCheckTypeDescriptor(BO->getLHS()->getType()));
915f4a2713aSLionel Sambuc StaticData.push_back(
916f4a2713aSLionel Sambuc CGF.EmitCheckTypeDescriptor(BO->getRHS()->getType()));
917f4a2713aSLionel Sambuc } else if (Opcode == BO_Div || Opcode == BO_Rem) {
918f4a2713aSLionel Sambuc // Divide or modulo by zero, or signed overflow (eg INT_MAX / -1).
919f4a2713aSLionel Sambuc CheckName = "divrem_overflow";
920f4a2713aSLionel Sambuc StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty));
921f4a2713aSLionel Sambuc } else {
922*0a6a1f1dSLionel Sambuc // Arithmetic overflow (+, -, *).
923f4a2713aSLionel Sambuc switch (Opcode) {
924f4a2713aSLionel Sambuc case BO_Add: CheckName = "add_overflow"; break;
925f4a2713aSLionel Sambuc case BO_Sub: CheckName = "sub_overflow"; break;
926f4a2713aSLionel Sambuc case BO_Mul: CheckName = "mul_overflow"; break;
927f4a2713aSLionel Sambuc default: llvm_unreachable("unexpected opcode for bin op check");
928f4a2713aSLionel Sambuc }
929f4a2713aSLionel Sambuc StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty));
930f4a2713aSLionel Sambuc }
931f4a2713aSLionel Sambuc DynamicData.push_back(Info.LHS);
932f4a2713aSLionel Sambuc DynamicData.push_back(Info.RHS);
933f4a2713aSLionel Sambuc }
934f4a2713aSLionel Sambuc
935*0a6a1f1dSLionel Sambuc CGF.EmitCheck(Checks, CheckName, StaticData, DynamicData);
936f4a2713aSLionel Sambuc }
937f4a2713aSLionel Sambuc
938f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
939f4a2713aSLionel Sambuc // Visitor Methods
940f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
941f4a2713aSLionel Sambuc
VisitExpr(Expr * E)942f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitExpr(Expr *E) {
943f4a2713aSLionel Sambuc CGF.ErrorUnsupported(E, "scalar expression");
944f4a2713aSLionel Sambuc if (E->getType()->isVoidType())
945*0a6a1f1dSLionel Sambuc return nullptr;
946f4a2713aSLionel Sambuc return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
947f4a2713aSLionel Sambuc }
948f4a2713aSLionel Sambuc
VisitShuffleVectorExpr(ShuffleVectorExpr * E)949f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
950f4a2713aSLionel Sambuc // Vector Mask Case
951f4a2713aSLionel Sambuc if (E->getNumSubExprs() == 2 ||
952f4a2713aSLionel Sambuc (E->getNumSubExprs() == 3 && E->getExpr(2)->getType()->isVectorType())) {
953f4a2713aSLionel Sambuc Value *LHS = CGF.EmitScalarExpr(E->getExpr(0));
954f4a2713aSLionel Sambuc Value *RHS = CGF.EmitScalarExpr(E->getExpr(1));
955f4a2713aSLionel Sambuc Value *Mask;
956f4a2713aSLionel Sambuc
957f4a2713aSLionel Sambuc llvm::VectorType *LTy = cast<llvm::VectorType>(LHS->getType());
958f4a2713aSLionel Sambuc unsigned LHSElts = LTy->getNumElements();
959f4a2713aSLionel Sambuc
960f4a2713aSLionel Sambuc if (E->getNumSubExprs() == 3) {
961f4a2713aSLionel Sambuc Mask = CGF.EmitScalarExpr(E->getExpr(2));
962f4a2713aSLionel Sambuc
963f4a2713aSLionel Sambuc // Shuffle LHS & RHS into one input vector.
964f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 32> concat;
965f4a2713aSLionel Sambuc for (unsigned i = 0; i != LHSElts; ++i) {
966f4a2713aSLionel Sambuc concat.push_back(Builder.getInt32(2*i));
967f4a2713aSLionel Sambuc concat.push_back(Builder.getInt32(2*i+1));
968f4a2713aSLionel Sambuc }
969f4a2713aSLionel Sambuc
970f4a2713aSLionel Sambuc Value* CV = llvm::ConstantVector::get(concat);
971f4a2713aSLionel Sambuc LHS = Builder.CreateShuffleVector(LHS, RHS, CV, "concat");
972f4a2713aSLionel Sambuc LHSElts *= 2;
973f4a2713aSLionel Sambuc } else {
974f4a2713aSLionel Sambuc Mask = RHS;
975f4a2713aSLionel Sambuc }
976f4a2713aSLionel Sambuc
977f4a2713aSLionel Sambuc llvm::VectorType *MTy = cast<llvm::VectorType>(Mask->getType());
978f4a2713aSLionel Sambuc llvm::Constant* EltMask;
979f4a2713aSLionel Sambuc
980f4a2713aSLionel Sambuc EltMask = llvm::ConstantInt::get(MTy->getElementType(),
981f4a2713aSLionel Sambuc llvm::NextPowerOf2(LHSElts-1)-1);
982f4a2713aSLionel Sambuc
983f4a2713aSLionel Sambuc // Mask off the high bits of each shuffle index.
984f4a2713aSLionel Sambuc Value *MaskBits = llvm::ConstantVector::getSplat(MTy->getNumElements(),
985f4a2713aSLionel Sambuc EltMask);
986f4a2713aSLionel Sambuc Mask = Builder.CreateAnd(Mask, MaskBits, "mask");
987f4a2713aSLionel Sambuc
988f4a2713aSLionel Sambuc // newv = undef
989f4a2713aSLionel Sambuc // mask = mask & maskbits
990f4a2713aSLionel Sambuc // for each elt
991f4a2713aSLionel Sambuc // n = extract mask i
992f4a2713aSLionel Sambuc // x = extract val n
993f4a2713aSLionel Sambuc // newv = insert newv, x, i
994f4a2713aSLionel Sambuc llvm::VectorType *RTy = llvm::VectorType::get(LTy->getElementType(),
995f4a2713aSLionel Sambuc MTy->getNumElements());
996f4a2713aSLionel Sambuc Value* NewV = llvm::UndefValue::get(RTy);
997f4a2713aSLionel Sambuc for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) {
998*0a6a1f1dSLionel Sambuc Value *IIndx = llvm::ConstantInt::get(CGF.SizeTy, i);
999f4a2713aSLionel Sambuc Value *Indx = Builder.CreateExtractElement(Mask, IIndx, "shuf_idx");
1000f4a2713aSLionel Sambuc
1001f4a2713aSLionel Sambuc Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt");
1002f4a2713aSLionel Sambuc NewV = Builder.CreateInsertElement(NewV, VExt, IIndx, "shuf_ins");
1003f4a2713aSLionel Sambuc }
1004f4a2713aSLionel Sambuc return NewV;
1005f4a2713aSLionel Sambuc }
1006f4a2713aSLionel Sambuc
1007f4a2713aSLionel Sambuc Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
1008f4a2713aSLionel Sambuc Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
1009f4a2713aSLionel Sambuc
1010f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 32> indices;
1011f4a2713aSLionel Sambuc for (unsigned i = 2; i < E->getNumSubExprs(); ++i) {
1012f4a2713aSLionel Sambuc llvm::APSInt Idx = E->getShuffleMaskIdx(CGF.getContext(), i-2);
1013f4a2713aSLionel Sambuc // Check for -1 and output it as undef in the IR.
1014f4a2713aSLionel Sambuc if (Idx.isSigned() && Idx.isAllOnesValue())
1015f4a2713aSLionel Sambuc indices.push_back(llvm::UndefValue::get(CGF.Int32Ty));
1016f4a2713aSLionel Sambuc else
1017f4a2713aSLionel Sambuc indices.push_back(Builder.getInt32(Idx.getZExtValue()));
1018f4a2713aSLionel Sambuc }
1019f4a2713aSLionel Sambuc
1020f4a2713aSLionel Sambuc Value *SV = llvm::ConstantVector::get(indices);
1021f4a2713aSLionel Sambuc return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
1022f4a2713aSLionel Sambuc }
1023f4a2713aSLionel Sambuc
VisitConvertVectorExpr(ConvertVectorExpr * E)1024f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitConvertVectorExpr(ConvertVectorExpr *E) {
1025f4a2713aSLionel Sambuc QualType SrcType = E->getSrcExpr()->getType(),
1026f4a2713aSLionel Sambuc DstType = E->getType();
1027f4a2713aSLionel Sambuc
1028f4a2713aSLionel Sambuc Value *Src = CGF.EmitScalarExpr(E->getSrcExpr());
1029f4a2713aSLionel Sambuc
1030f4a2713aSLionel Sambuc SrcType = CGF.getContext().getCanonicalType(SrcType);
1031f4a2713aSLionel Sambuc DstType = CGF.getContext().getCanonicalType(DstType);
1032f4a2713aSLionel Sambuc if (SrcType == DstType) return Src;
1033f4a2713aSLionel Sambuc
1034f4a2713aSLionel Sambuc assert(SrcType->isVectorType() &&
1035f4a2713aSLionel Sambuc "ConvertVector source type must be a vector");
1036f4a2713aSLionel Sambuc assert(DstType->isVectorType() &&
1037f4a2713aSLionel Sambuc "ConvertVector destination type must be a vector");
1038f4a2713aSLionel Sambuc
1039f4a2713aSLionel Sambuc llvm::Type *SrcTy = Src->getType();
1040f4a2713aSLionel Sambuc llvm::Type *DstTy = ConvertType(DstType);
1041f4a2713aSLionel Sambuc
1042f4a2713aSLionel Sambuc // Ignore conversions like int -> uint.
1043f4a2713aSLionel Sambuc if (SrcTy == DstTy)
1044f4a2713aSLionel Sambuc return Src;
1045f4a2713aSLionel Sambuc
1046f4a2713aSLionel Sambuc QualType SrcEltType = SrcType->getAs<VectorType>()->getElementType(),
1047f4a2713aSLionel Sambuc DstEltType = DstType->getAs<VectorType>()->getElementType();
1048f4a2713aSLionel Sambuc
1049f4a2713aSLionel Sambuc assert(SrcTy->isVectorTy() &&
1050f4a2713aSLionel Sambuc "ConvertVector source IR type must be a vector");
1051f4a2713aSLionel Sambuc assert(DstTy->isVectorTy() &&
1052f4a2713aSLionel Sambuc "ConvertVector destination IR type must be a vector");
1053f4a2713aSLionel Sambuc
1054f4a2713aSLionel Sambuc llvm::Type *SrcEltTy = SrcTy->getVectorElementType(),
1055f4a2713aSLionel Sambuc *DstEltTy = DstTy->getVectorElementType();
1056f4a2713aSLionel Sambuc
1057f4a2713aSLionel Sambuc if (DstEltType->isBooleanType()) {
1058f4a2713aSLionel Sambuc assert((SrcEltTy->isFloatingPointTy() ||
1059f4a2713aSLionel Sambuc isa<llvm::IntegerType>(SrcEltTy)) && "Unknown boolean conversion");
1060f4a2713aSLionel Sambuc
1061f4a2713aSLionel Sambuc llvm::Value *Zero = llvm::Constant::getNullValue(SrcTy);
1062f4a2713aSLionel Sambuc if (SrcEltTy->isFloatingPointTy()) {
1063f4a2713aSLionel Sambuc return Builder.CreateFCmpUNE(Src, Zero, "tobool");
1064f4a2713aSLionel Sambuc } else {
1065f4a2713aSLionel Sambuc return Builder.CreateICmpNE(Src, Zero, "tobool");
1066f4a2713aSLionel Sambuc }
1067f4a2713aSLionel Sambuc }
1068f4a2713aSLionel Sambuc
1069f4a2713aSLionel Sambuc // We have the arithmetic types: real int/float.
1070*0a6a1f1dSLionel Sambuc Value *Res = nullptr;
1071f4a2713aSLionel Sambuc
1072f4a2713aSLionel Sambuc if (isa<llvm::IntegerType>(SrcEltTy)) {
1073f4a2713aSLionel Sambuc bool InputSigned = SrcEltType->isSignedIntegerOrEnumerationType();
1074f4a2713aSLionel Sambuc if (isa<llvm::IntegerType>(DstEltTy))
1075f4a2713aSLionel Sambuc Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
1076f4a2713aSLionel Sambuc else if (InputSigned)
1077f4a2713aSLionel Sambuc Res = Builder.CreateSIToFP(Src, DstTy, "conv");
1078f4a2713aSLionel Sambuc else
1079f4a2713aSLionel Sambuc Res = Builder.CreateUIToFP(Src, DstTy, "conv");
1080f4a2713aSLionel Sambuc } else if (isa<llvm::IntegerType>(DstEltTy)) {
1081f4a2713aSLionel Sambuc assert(SrcEltTy->isFloatingPointTy() && "Unknown real conversion");
1082f4a2713aSLionel Sambuc if (DstEltType->isSignedIntegerOrEnumerationType())
1083f4a2713aSLionel Sambuc Res = Builder.CreateFPToSI(Src, DstTy, "conv");
1084f4a2713aSLionel Sambuc else
1085f4a2713aSLionel Sambuc Res = Builder.CreateFPToUI(Src, DstTy, "conv");
1086f4a2713aSLionel Sambuc } else {
1087f4a2713aSLionel Sambuc assert(SrcEltTy->isFloatingPointTy() && DstEltTy->isFloatingPointTy() &&
1088f4a2713aSLionel Sambuc "Unknown real conversion");
1089f4a2713aSLionel Sambuc if (DstEltTy->getTypeID() < SrcEltTy->getTypeID())
1090f4a2713aSLionel Sambuc Res = Builder.CreateFPTrunc(Src, DstTy, "conv");
1091f4a2713aSLionel Sambuc else
1092f4a2713aSLionel Sambuc Res = Builder.CreateFPExt(Src, DstTy, "conv");
1093f4a2713aSLionel Sambuc }
1094f4a2713aSLionel Sambuc
1095f4a2713aSLionel Sambuc return Res;
1096f4a2713aSLionel Sambuc }
1097f4a2713aSLionel Sambuc
VisitMemberExpr(MemberExpr * E)1098f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) {
1099f4a2713aSLionel Sambuc llvm::APSInt Value;
1100f4a2713aSLionel Sambuc if (E->EvaluateAsInt(Value, CGF.getContext(), Expr::SE_AllowSideEffects)) {
1101f4a2713aSLionel Sambuc if (E->isArrow())
1102f4a2713aSLionel Sambuc CGF.EmitScalarExpr(E->getBase());
1103f4a2713aSLionel Sambuc else
1104f4a2713aSLionel Sambuc EmitLValue(E->getBase());
1105f4a2713aSLionel Sambuc return Builder.getInt(Value);
1106f4a2713aSLionel Sambuc }
1107f4a2713aSLionel Sambuc
1108f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
1109f4a2713aSLionel Sambuc }
1110f4a2713aSLionel Sambuc
VisitArraySubscriptExpr(ArraySubscriptExpr * E)1111f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
1112f4a2713aSLionel Sambuc TestAndClearIgnoreResultAssign();
1113f4a2713aSLionel Sambuc
1114f4a2713aSLionel Sambuc // Emit subscript expressions in rvalue context's. For most cases, this just
1115f4a2713aSLionel Sambuc // loads the lvalue formed by the subscript expr. However, we have to be
1116f4a2713aSLionel Sambuc // careful, because the base of a vector subscript is occasionally an rvalue,
1117f4a2713aSLionel Sambuc // so we can't get it as an lvalue.
1118f4a2713aSLionel Sambuc if (!E->getBase()->getType()->isVectorType())
1119f4a2713aSLionel Sambuc return EmitLoadOfLValue(E);
1120f4a2713aSLionel Sambuc
1121f4a2713aSLionel Sambuc // Handle the vector case. The base must be a vector, the index must be an
1122f4a2713aSLionel Sambuc // integer value.
1123f4a2713aSLionel Sambuc Value *Base = Visit(E->getBase());
1124f4a2713aSLionel Sambuc Value *Idx = Visit(E->getIdx());
1125f4a2713aSLionel Sambuc QualType IdxTy = E->getIdx()->getType();
1126f4a2713aSLionel Sambuc
1127*0a6a1f1dSLionel Sambuc if (CGF.SanOpts.has(SanitizerKind::ArrayBounds))
1128f4a2713aSLionel Sambuc CGF.EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, /*Accessed*/true);
1129f4a2713aSLionel Sambuc
1130f4a2713aSLionel Sambuc return Builder.CreateExtractElement(Base, Idx, "vecext");
1131f4a2713aSLionel Sambuc }
1132f4a2713aSLionel Sambuc
getMaskElt(llvm::ShuffleVectorInst * SVI,unsigned Idx,unsigned Off,llvm::Type * I32Ty)1133f4a2713aSLionel Sambuc static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
1134f4a2713aSLionel Sambuc unsigned Off, llvm::Type *I32Ty) {
1135f4a2713aSLionel Sambuc int MV = SVI->getMaskValue(Idx);
1136f4a2713aSLionel Sambuc if (MV == -1)
1137f4a2713aSLionel Sambuc return llvm::UndefValue::get(I32Ty);
1138f4a2713aSLionel Sambuc return llvm::ConstantInt::get(I32Ty, Off+MV);
1139f4a2713aSLionel Sambuc }
1140f4a2713aSLionel Sambuc
VisitInitListExpr(InitListExpr * E)1141f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
1142f4a2713aSLionel Sambuc bool Ignore = TestAndClearIgnoreResultAssign();
1143f4a2713aSLionel Sambuc (void)Ignore;
1144f4a2713aSLionel Sambuc assert (Ignore == false && "init list ignored");
1145f4a2713aSLionel Sambuc unsigned NumInitElements = E->getNumInits();
1146f4a2713aSLionel Sambuc
1147f4a2713aSLionel Sambuc if (E->hadArrayRangeDesignator())
1148f4a2713aSLionel Sambuc CGF.ErrorUnsupported(E, "GNU array range designator extension");
1149f4a2713aSLionel Sambuc
1150f4a2713aSLionel Sambuc llvm::VectorType *VType =
1151f4a2713aSLionel Sambuc dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
1152f4a2713aSLionel Sambuc
1153f4a2713aSLionel Sambuc if (!VType) {
1154f4a2713aSLionel Sambuc if (NumInitElements == 0) {
1155f4a2713aSLionel Sambuc // C++11 value-initialization for the scalar.
1156f4a2713aSLionel Sambuc return EmitNullValue(E->getType());
1157f4a2713aSLionel Sambuc }
1158f4a2713aSLionel Sambuc // We have a scalar in braces. Just use the first element.
1159f4a2713aSLionel Sambuc return Visit(E->getInit(0));
1160f4a2713aSLionel Sambuc }
1161f4a2713aSLionel Sambuc
1162f4a2713aSLionel Sambuc unsigned ResElts = VType->getNumElements();
1163f4a2713aSLionel Sambuc
1164f4a2713aSLionel Sambuc // Loop over initializers collecting the Value for each, and remembering
1165f4a2713aSLionel Sambuc // whether the source was swizzle (ExtVectorElementExpr). This will allow
1166f4a2713aSLionel Sambuc // us to fold the shuffle for the swizzle into the shuffle for the vector
1167f4a2713aSLionel Sambuc // initializer, since LLVM optimizers generally do not want to touch
1168f4a2713aSLionel Sambuc // shuffles.
1169f4a2713aSLionel Sambuc unsigned CurIdx = 0;
1170f4a2713aSLionel Sambuc bool VIsUndefShuffle = false;
1171f4a2713aSLionel Sambuc llvm::Value *V = llvm::UndefValue::get(VType);
1172f4a2713aSLionel Sambuc for (unsigned i = 0; i != NumInitElements; ++i) {
1173f4a2713aSLionel Sambuc Expr *IE = E->getInit(i);
1174f4a2713aSLionel Sambuc Value *Init = Visit(IE);
1175f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 16> Args;
1176f4a2713aSLionel Sambuc
1177f4a2713aSLionel Sambuc llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType());
1178f4a2713aSLionel Sambuc
1179f4a2713aSLionel Sambuc // Handle scalar elements. If the scalar initializer is actually one
1180f4a2713aSLionel Sambuc // element of a different vector of the same width, use shuffle instead of
1181f4a2713aSLionel Sambuc // extract+insert.
1182f4a2713aSLionel Sambuc if (!VVT) {
1183f4a2713aSLionel Sambuc if (isa<ExtVectorElementExpr>(IE)) {
1184f4a2713aSLionel Sambuc llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init);
1185f4a2713aSLionel Sambuc
1186f4a2713aSLionel Sambuc if (EI->getVectorOperandType()->getNumElements() == ResElts) {
1187f4a2713aSLionel Sambuc llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand());
1188*0a6a1f1dSLionel Sambuc Value *LHS = nullptr, *RHS = nullptr;
1189f4a2713aSLionel Sambuc if (CurIdx == 0) {
1190f4a2713aSLionel Sambuc // insert into undef -> shuffle (src, undef)
1191f4a2713aSLionel Sambuc Args.push_back(C);
1192f4a2713aSLionel Sambuc Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1193f4a2713aSLionel Sambuc
1194f4a2713aSLionel Sambuc LHS = EI->getVectorOperand();
1195f4a2713aSLionel Sambuc RHS = V;
1196f4a2713aSLionel Sambuc VIsUndefShuffle = true;
1197f4a2713aSLionel Sambuc } else if (VIsUndefShuffle) {
1198f4a2713aSLionel Sambuc // insert into undefshuffle && size match -> shuffle (v, src)
1199f4a2713aSLionel Sambuc llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V);
1200f4a2713aSLionel Sambuc for (unsigned j = 0; j != CurIdx; ++j)
1201f4a2713aSLionel Sambuc Args.push_back(getMaskElt(SVV, j, 0, CGF.Int32Ty));
1202f4a2713aSLionel Sambuc Args.push_back(Builder.getInt32(ResElts + C->getZExtValue()));
1203f4a2713aSLionel Sambuc Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1204f4a2713aSLionel Sambuc
1205f4a2713aSLionel Sambuc LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
1206f4a2713aSLionel Sambuc RHS = EI->getVectorOperand();
1207f4a2713aSLionel Sambuc VIsUndefShuffle = false;
1208f4a2713aSLionel Sambuc }
1209f4a2713aSLionel Sambuc if (!Args.empty()) {
1210f4a2713aSLionel Sambuc llvm::Constant *Mask = llvm::ConstantVector::get(Args);
1211f4a2713aSLionel Sambuc V = Builder.CreateShuffleVector(LHS, RHS, Mask);
1212f4a2713aSLionel Sambuc ++CurIdx;
1213f4a2713aSLionel Sambuc continue;
1214f4a2713aSLionel Sambuc }
1215f4a2713aSLionel Sambuc }
1216f4a2713aSLionel Sambuc }
1217f4a2713aSLionel Sambuc V = Builder.CreateInsertElement(V, Init, Builder.getInt32(CurIdx),
1218f4a2713aSLionel Sambuc "vecinit");
1219f4a2713aSLionel Sambuc VIsUndefShuffle = false;
1220f4a2713aSLionel Sambuc ++CurIdx;
1221f4a2713aSLionel Sambuc continue;
1222f4a2713aSLionel Sambuc }
1223f4a2713aSLionel Sambuc
1224f4a2713aSLionel Sambuc unsigned InitElts = VVT->getNumElements();
1225f4a2713aSLionel Sambuc
1226f4a2713aSLionel Sambuc // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's
1227f4a2713aSLionel Sambuc // input is the same width as the vector being constructed, generate an
1228f4a2713aSLionel Sambuc // optimized shuffle of the swizzle input into the result.
1229f4a2713aSLionel Sambuc unsigned Offset = (CurIdx == 0) ? 0 : ResElts;
1230f4a2713aSLionel Sambuc if (isa<ExtVectorElementExpr>(IE)) {
1231f4a2713aSLionel Sambuc llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init);
1232f4a2713aSLionel Sambuc Value *SVOp = SVI->getOperand(0);
1233f4a2713aSLionel Sambuc llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType());
1234f4a2713aSLionel Sambuc
1235f4a2713aSLionel Sambuc if (OpTy->getNumElements() == ResElts) {
1236f4a2713aSLionel Sambuc for (unsigned j = 0; j != CurIdx; ++j) {
1237f4a2713aSLionel Sambuc // If the current vector initializer is a shuffle with undef, merge
1238f4a2713aSLionel Sambuc // this shuffle directly into it.
1239f4a2713aSLionel Sambuc if (VIsUndefShuffle) {
1240f4a2713aSLionel Sambuc Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0,
1241f4a2713aSLionel Sambuc CGF.Int32Ty));
1242f4a2713aSLionel Sambuc } else {
1243f4a2713aSLionel Sambuc Args.push_back(Builder.getInt32(j));
1244f4a2713aSLionel Sambuc }
1245f4a2713aSLionel Sambuc }
1246f4a2713aSLionel Sambuc for (unsigned j = 0, je = InitElts; j != je; ++j)
1247f4a2713aSLionel Sambuc Args.push_back(getMaskElt(SVI, j, Offset, CGF.Int32Ty));
1248f4a2713aSLionel Sambuc Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1249f4a2713aSLionel Sambuc
1250f4a2713aSLionel Sambuc if (VIsUndefShuffle)
1251f4a2713aSLionel Sambuc V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
1252f4a2713aSLionel Sambuc
1253f4a2713aSLionel Sambuc Init = SVOp;
1254f4a2713aSLionel Sambuc }
1255f4a2713aSLionel Sambuc }
1256f4a2713aSLionel Sambuc
1257f4a2713aSLionel Sambuc // Extend init to result vector length, and then shuffle its contribution
1258f4a2713aSLionel Sambuc // to the vector initializer into V.
1259f4a2713aSLionel Sambuc if (Args.empty()) {
1260f4a2713aSLionel Sambuc for (unsigned j = 0; j != InitElts; ++j)
1261f4a2713aSLionel Sambuc Args.push_back(Builder.getInt32(j));
1262f4a2713aSLionel Sambuc Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1263f4a2713aSLionel Sambuc llvm::Constant *Mask = llvm::ConstantVector::get(Args);
1264f4a2713aSLionel Sambuc Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT),
1265f4a2713aSLionel Sambuc Mask, "vext");
1266f4a2713aSLionel Sambuc
1267f4a2713aSLionel Sambuc Args.clear();
1268f4a2713aSLionel Sambuc for (unsigned j = 0; j != CurIdx; ++j)
1269f4a2713aSLionel Sambuc Args.push_back(Builder.getInt32(j));
1270f4a2713aSLionel Sambuc for (unsigned j = 0; j != InitElts; ++j)
1271f4a2713aSLionel Sambuc Args.push_back(Builder.getInt32(j+Offset));
1272f4a2713aSLionel Sambuc Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
1273f4a2713aSLionel Sambuc }
1274f4a2713aSLionel Sambuc
1275f4a2713aSLionel Sambuc // If V is undef, make sure it ends up on the RHS of the shuffle to aid
1276f4a2713aSLionel Sambuc // merging subsequent shuffles into this one.
1277f4a2713aSLionel Sambuc if (CurIdx == 0)
1278f4a2713aSLionel Sambuc std::swap(V, Init);
1279f4a2713aSLionel Sambuc llvm::Constant *Mask = llvm::ConstantVector::get(Args);
1280f4a2713aSLionel Sambuc V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit");
1281f4a2713aSLionel Sambuc VIsUndefShuffle = isa<llvm::UndefValue>(Init);
1282f4a2713aSLionel Sambuc CurIdx += InitElts;
1283f4a2713aSLionel Sambuc }
1284f4a2713aSLionel Sambuc
1285f4a2713aSLionel Sambuc // FIXME: evaluate codegen vs. shuffling against constant null vector.
1286f4a2713aSLionel Sambuc // Emit remaining default initializers.
1287f4a2713aSLionel Sambuc llvm::Type *EltTy = VType->getElementType();
1288f4a2713aSLionel Sambuc
1289f4a2713aSLionel Sambuc // Emit remaining default initializers
1290f4a2713aSLionel Sambuc for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) {
1291f4a2713aSLionel Sambuc Value *Idx = Builder.getInt32(CurIdx);
1292f4a2713aSLionel Sambuc llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
1293f4a2713aSLionel Sambuc V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
1294f4a2713aSLionel Sambuc }
1295f4a2713aSLionel Sambuc return V;
1296f4a2713aSLionel Sambuc }
1297f4a2713aSLionel Sambuc
ShouldNullCheckClassCastValue(const CastExpr * CE)1298f4a2713aSLionel Sambuc static bool ShouldNullCheckClassCastValue(const CastExpr *CE) {
1299f4a2713aSLionel Sambuc const Expr *E = CE->getSubExpr();
1300f4a2713aSLionel Sambuc
1301f4a2713aSLionel Sambuc if (CE->getCastKind() == CK_UncheckedDerivedToBase)
1302f4a2713aSLionel Sambuc return false;
1303f4a2713aSLionel Sambuc
1304f4a2713aSLionel Sambuc if (isa<CXXThisExpr>(E)) {
1305f4a2713aSLionel Sambuc // We always assume that 'this' is never null.
1306f4a2713aSLionel Sambuc return false;
1307f4a2713aSLionel Sambuc }
1308f4a2713aSLionel Sambuc
1309f4a2713aSLionel Sambuc if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) {
1310f4a2713aSLionel Sambuc // And that glvalue casts are never null.
1311f4a2713aSLionel Sambuc if (ICE->getValueKind() != VK_RValue)
1312f4a2713aSLionel Sambuc return false;
1313f4a2713aSLionel Sambuc }
1314f4a2713aSLionel Sambuc
1315f4a2713aSLionel Sambuc return true;
1316f4a2713aSLionel Sambuc }
1317f4a2713aSLionel Sambuc
1318f4a2713aSLionel Sambuc // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
1319f4a2713aSLionel Sambuc // have to handle a more broad range of conversions than explicit casts, as they
1320f4a2713aSLionel Sambuc // handle things like function to ptr-to-function decay etc.
VisitCastExpr(CastExpr * CE)1321f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
1322f4a2713aSLionel Sambuc Expr *E = CE->getSubExpr();
1323f4a2713aSLionel Sambuc QualType DestTy = CE->getType();
1324f4a2713aSLionel Sambuc CastKind Kind = CE->getCastKind();
1325f4a2713aSLionel Sambuc
1326f4a2713aSLionel Sambuc if (!DestTy->isVoidType())
1327f4a2713aSLionel Sambuc TestAndClearIgnoreResultAssign();
1328f4a2713aSLionel Sambuc
1329f4a2713aSLionel Sambuc // Since almost all cast kinds apply to scalars, this switch doesn't have
1330f4a2713aSLionel Sambuc // a default case, so the compiler will warn on a missing case. The cases
1331f4a2713aSLionel Sambuc // are in the same order as in the CastKind enum.
1332f4a2713aSLionel Sambuc switch (Kind) {
1333f4a2713aSLionel Sambuc case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
1334f4a2713aSLionel Sambuc case CK_BuiltinFnToFnPtr:
1335f4a2713aSLionel Sambuc llvm_unreachable("builtin functions are handled elsewhere");
1336f4a2713aSLionel Sambuc
1337f4a2713aSLionel Sambuc case CK_LValueBitCast:
1338f4a2713aSLionel Sambuc case CK_ObjCObjectLValueCast: {
1339f4a2713aSLionel Sambuc Value *V = EmitLValue(E).getAddress();
1340f4a2713aSLionel Sambuc V = Builder.CreateBitCast(V,
1341f4a2713aSLionel Sambuc ConvertType(CGF.getContext().getPointerType(DestTy)));
1342f4a2713aSLionel Sambuc return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(V, DestTy),
1343f4a2713aSLionel Sambuc CE->getExprLoc());
1344f4a2713aSLionel Sambuc }
1345f4a2713aSLionel Sambuc
1346f4a2713aSLionel Sambuc case CK_CPointerToObjCPointerCast:
1347f4a2713aSLionel Sambuc case CK_BlockPointerToObjCPointerCast:
1348f4a2713aSLionel Sambuc case CK_AnyPointerToBlockPointerCast:
1349f4a2713aSLionel Sambuc case CK_BitCast: {
1350f4a2713aSLionel Sambuc Value *Src = Visit(const_cast<Expr*>(E));
1351*0a6a1f1dSLionel Sambuc llvm::Type *SrcTy = Src->getType();
1352*0a6a1f1dSLionel Sambuc llvm::Type *DstTy = ConvertType(DestTy);
1353*0a6a1f1dSLionel Sambuc if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() &&
1354*0a6a1f1dSLionel Sambuc SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) {
1355*0a6a1f1dSLionel Sambuc llvm_unreachable("wrong cast for pointers in different address spaces"
1356*0a6a1f1dSLionel Sambuc "(must be an address space cast)!");
1357*0a6a1f1dSLionel Sambuc }
1358*0a6a1f1dSLionel Sambuc return Builder.CreateBitCast(Src, DstTy);
1359*0a6a1f1dSLionel Sambuc }
1360*0a6a1f1dSLionel Sambuc case CK_AddressSpaceConversion: {
1361*0a6a1f1dSLionel Sambuc Value *Src = Visit(const_cast<Expr*>(E));
1362*0a6a1f1dSLionel Sambuc return Builder.CreateAddrSpaceCast(Src, ConvertType(DestTy));
1363f4a2713aSLionel Sambuc }
1364f4a2713aSLionel Sambuc case CK_AtomicToNonAtomic:
1365f4a2713aSLionel Sambuc case CK_NonAtomicToAtomic:
1366f4a2713aSLionel Sambuc case CK_NoOp:
1367f4a2713aSLionel Sambuc case CK_UserDefinedConversion:
1368f4a2713aSLionel Sambuc return Visit(const_cast<Expr*>(E));
1369f4a2713aSLionel Sambuc
1370f4a2713aSLionel Sambuc case CK_BaseToDerived: {
1371f4a2713aSLionel Sambuc const CXXRecordDecl *DerivedClassDecl = DestTy->getPointeeCXXRecordDecl();
1372f4a2713aSLionel Sambuc assert(DerivedClassDecl && "BaseToDerived arg isn't a C++ object pointer!");
1373f4a2713aSLionel Sambuc
1374f4a2713aSLionel Sambuc llvm::Value *V = Visit(E);
1375f4a2713aSLionel Sambuc
1376f4a2713aSLionel Sambuc llvm::Value *Derived =
1377f4a2713aSLionel Sambuc CGF.GetAddressOfDerivedClass(V, DerivedClassDecl,
1378f4a2713aSLionel Sambuc CE->path_begin(), CE->path_end(),
1379f4a2713aSLionel Sambuc ShouldNullCheckClassCastValue(CE));
1380f4a2713aSLionel Sambuc
1381f4a2713aSLionel Sambuc // C++11 [expr.static.cast]p11: Behavior is undefined if a downcast is
1382f4a2713aSLionel Sambuc // performed and the object is not of the derived type.
1383*0a6a1f1dSLionel Sambuc if (CGF.sanitizePerformTypeCheck())
1384f4a2713aSLionel Sambuc CGF.EmitTypeCheck(CodeGenFunction::TCK_DowncastPointer, CE->getExprLoc(),
1385f4a2713aSLionel Sambuc Derived, DestTy->getPointeeType());
1386f4a2713aSLionel Sambuc
1387f4a2713aSLionel Sambuc return Derived;
1388f4a2713aSLionel Sambuc }
1389f4a2713aSLionel Sambuc case CK_UncheckedDerivedToBase:
1390f4a2713aSLionel Sambuc case CK_DerivedToBase: {
1391f4a2713aSLionel Sambuc const CXXRecordDecl *DerivedClassDecl =
1392f4a2713aSLionel Sambuc E->getType()->getPointeeCXXRecordDecl();
1393f4a2713aSLionel Sambuc assert(DerivedClassDecl && "DerivedToBase arg isn't a C++ object pointer!");
1394f4a2713aSLionel Sambuc
1395*0a6a1f1dSLionel Sambuc return CGF.GetAddressOfBaseClass(
1396*0a6a1f1dSLionel Sambuc Visit(E), DerivedClassDecl, CE->path_begin(), CE->path_end(),
1397*0a6a1f1dSLionel Sambuc ShouldNullCheckClassCastValue(CE), CE->getExprLoc());
1398f4a2713aSLionel Sambuc }
1399f4a2713aSLionel Sambuc case CK_Dynamic: {
1400f4a2713aSLionel Sambuc Value *V = Visit(const_cast<Expr*>(E));
1401f4a2713aSLionel Sambuc const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE);
1402f4a2713aSLionel Sambuc return CGF.EmitDynamicCast(V, DCE);
1403f4a2713aSLionel Sambuc }
1404f4a2713aSLionel Sambuc
1405f4a2713aSLionel Sambuc case CK_ArrayToPointerDecay: {
1406f4a2713aSLionel Sambuc assert(E->getType()->isArrayType() &&
1407f4a2713aSLionel Sambuc "Array to pointer decay must have array source type!");
1408f4a2713aSLionel Sambuc
1409f4a2713aSLionel Sambuc Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
1410f4a2713aSLionel Sambuc
1411f4a2713aSLionel Sambuc // Note that VLA pointers are always decayed, so we don't need to do
1412f4a2713aSLionel Sambuc // anything here.
1413f4a2713aSLionel Sambuc if (!E->getType()->isVariableArrayType()) {
1414f4a2713aSLionel Sambuc assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
1415*0a6a1f1dSLionel Sambuc V = CGF.Builder.CreatePointerCast(
1416*0a6a1f1dSLionel Sambuc V, ConvertType(E->getType())->getPointerTo(
1417*0a6a1f1dSLionel Sambuc V->getType()->getPointerAddressSpace()));
1418*0a6a1f1dSLionel Sambuc
1419*0a6a1f1dSLionel Sambuc assert(isa<llvm::ArrayType>(V->getType()->getPointerElementType()) &&
1420f4a2713aSLionel Sambuc "Expected pointer to array");
1421f4a2713aSLionel Sambuc V = Builder.CreateStructGEP(V, 0, "arraydecay");
1422f4a2713aSLionel Sambuc }
1423f4a2713aSLionel Sambuc
1424f4a2713aSLionel Sambuc // Make sure the array decay ends up being the right type. This matters if
1425f4a2713aSLionel Sambuc // the array type was of an incomplete type.
1426*0a6a1f1dSLionel Sambuc return CGF.Builder.CreatePointerCast(V, ConvertType(CE->getType()));
1427f4a2713aSLionel Sambuc }
1428f4a2713aSLionel Sambuc case CK_FunctionToPointerDecay:
1429f4a2713aSLionel Sambuc return EmitLValue(E).getAddress();
1430f4a2713aSLionel Sambuc
1431f4a2713aSLionel Sambuc case CK_NullToPointer:
1432f4a2713aSLionel Sambuc if (MustVisitNullValue(E))
1433f4a2713aSLionel Sambuc (void) Visit(E);
1434f4a2713aSLionel Sambuc
1435f4a2713aSLionel Sambuc return llvm::ConstantPointerNull::get(
1436f4a2713aSLionel Sambuc cast<llvm::PointerType>(ConvertType(DestTy)));
1437f4a2713aSLionel Sambuc
1438f4a2713aSLionel Sambuc case CK_NullToMemberPointer: {
1439f4a2713aSLionel Sambuc if (MustVisitNullValue(E))
1440f4a2713aSLionel Sambuc (void) Visit(E);
1441f4a2713aSLionel Sambuc
1442f4a2713aSLionel Sambuc const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>();
1443f4a2713aSLionel Sambuc return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT);
1444f4a2713aSLionel Sambuc }
1445f4a2713aSLionel Sambuc
1446f4a2713aSLionel Sambuc case CK_ReinterpretMemberPointer:
1447f4a2713aSLionel Sambuc case CK_BaseToDerivedMemberPointer:
1448f4a2713aSLionel Sambuc case CK_DerivedToBaseMemberPointer: {
1449f4a2713aSLionel Sambuc Value *Src = Visit(E);
1450f4a2713aSLionel Sambuc
1451f4a2713aSLionel Sambuc // Note that the AST doesn't distinguish between checked and
1452f4a2713aSLionel Sambuc // unchecked member pointer conversions, so we always have to
1453f4a2713aSLionel Sambuc // implement checked conversions here. This is inefficient when
1454f4a2713aSLionel Sambuc // actual control flow may be required in order to perform the
1455f4a2713aSLionel Sambuc // check, which it is for data member pointers (but not member
1456f4a2713aSLionel Sambuc // function pointers on Itanium and ARM).
1457f4a2713aSLionel Sambuc return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src);
1458f4a2713aSLionel Sambuc }
1459f4a2713aSLionel Sambuc
1460f4a2713aSLionel Sambuc case CK_ARCProduceObject:
1461f4a2713aSLionel Sambuc return CGF.EmitARCRetainScalarExpr(E);
1462f4a2713aSLionel Sambuc case CK_ARCConsumeObject:
1463f4a2713aSLionel Sambuc return CGF.EmitObjCConsumeObject(E->getType(), Visit(E));
1464f4a2713aSLionel Sambuc case CK_ARCReclaimReturnedObject: {
1465f4a2713aSLionel Sambuc llvm::Value *value = Visit(E);
1466f4a2713aSLionel Sambuc value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
1467f4a2713aSLionel Sambuc return CGF.EmitObjCConsumeObject(E->getType(), value);
1468f4a2713aSLionel Sambuc }
1469f4a2713aSLionel Sambuc case CK_ARCExtendBlockObject:
1470f4a2713aSLionel Sambuc return CGF.EmitARCExtendBlockObject(E);
1471f4a2713aSLionel Sambuc
1472f4a2713aSLionel Sambuc case CK_CopyAndAutoreleaseBlockObject:
1473f4a2713aSLionel Sambuc return CGF.EmitBlockCopyAndAutorelease(Visit(E), E->getType());
1474f4a2713aSLionel Sambuc
1475f4a2713aSLionel Sambuc case CK_FloatingRealToComplex:
1476f4a2713aSLionel Sambuc case CK_FloatingComplexCast:
1477f4a2713aSLionel Sambuc case CK_IntegralRealToComplex:
1478f4a2713aSLionel Sambuc case CK_IntegralComplexCast:
1479f4a2713aSLionel Sambuc case CK_IntegralComplexToFloatingComplex:
1480f4a2713aSLionel Sambuc case CK_FloatingComplexToIntegralComplex:
1481f4a2713aSLionel Sambuc case CK_ConstructorConversion:
1482f4a2713aSLionel Sambuc case CK_ToUnion:
1483f4a2713aSLionel Sambuc llvm_unreachable("scalar cast to non-scalar value");
1484f4a2713aSLionel Sambuc
1485f4a2713aSLionel Sambuc case CK_LValueToRValue:
1486f4a2713aSLionel Sambuc assert(CGF.getContext().hasSameUnqualifiedType(E->getType(), DestTy));
1487f4a2713aSLionel Sambuc assert(E->isGLValue() && "lvalue-to-rvalue applied to r-value!");
1488f4a2713aSLionel Sambuc return Visit(const_cast<Expr*>(E));
1489f4a2713aSLionel Sambuc
1490f4a2713aSLionel Sambuc case CK_IntegralToPointer: {
1491f4a2713aSLionel Sambuc Value *Src = Visit(const_cast<Expr*>(E));
1492f4a2713aSLionel Sambuc
1493f4a2713aSLionel Sambuc // First, convert to the correct width so that we control the kind of
1494f4a2713aSLionel Sambuc // extension.
1495f4a2713aSLionel Sambuc llvm::Type *MiddleTy = CGF.IntPtrTy;
1496f4a2713aSLionel Sambuc bool InputSigned = E->getType()->isSignedIntegerOrEnumerationType();
1497f4a2713aSLionel Sambuc llvm::Value* IntResult =
1498f4a2713aSLionel Sambuc Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
1499f4a2713aSLionel Sambuc
1500f4a2713aSLionel Sambuc return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy));
1501f4a2713aSLionel Sambuc }
1502f4a2713aSLionel Sambuc case CK_PointerToIntegral:
1503f4a2713aSLionel Sambuc assert(!DestTy->isBooleanType() && "bool should use PointerToBool");
1504f4a2713aSLionel Sambuc return Builder.CreatePtrToInt(Visit(E), ConvertType(DestTy));
1505f4a2713aSLionel Sambuc
1506f4a2713aSLionel Sambuc case CK_ToVoid: {
1507f4a2713aSLionel Sambuc CGF.EmitIgnoredExpr(E);
1508*0a6a1f1dSLionel Sambuc return nullptr;
1509f4a2713aSLionel Sambuc }
1510f4a2713aSLionel Sambuc case CK_VectorSplat: {
1511f4a2713aSLionel Sambuc llvm::Type *DstTy = ConvertType(DestTy);
1512f4a2713aSLionel Sambuc Value *Elt = Visit(const_cast<Expr*>(E));
1513f4a2713aSLionel Sambuc Elt = EmitScalarConversion(Elt, E->getType(),
1514f4a2713aSLionel Sambuc DestTy->getAs<VectorType>()->getElementType());
1515f4a2713aSLionel Sambuc
1516f4a2713aSLionel Sambuc // Splat the element across to all elements
1517f4a2713aSLionel Sambuc unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
1518*0a6a1f1dSLionel Sambuc return Builder.CreateVectorSplat(NumElements, Elt, "splat");
1519f4a2713aSLionel Sambuc }
1520f4a2713aSLionel Sambuc
1521f4a2713aSLionel Sambuc case CK_IntegralCast:
1522f4a2713aSLionel Sambuc case CK_IntegralToFloating:
1523f4a2713aSLionel Sambuc case CK_FloatingToIntegral:
1524f4a2713aSLionel Sambuc case CK_FloatingCast:
1525f4a2713aSLionel Sambuc return EmitScalarConversion(Visit(E), E->getType(), DestTy);
1526f4a2713aSLionel Sambuc case CK_IntegralToBoolean:
1527f4a2713aSLionel Sambuc return EmitIntToBoolConversion(Visit(E));
1528f4a2713aSLionel Sambuc case CK_PointerToBoolean:
1529f4a2713aSLionel Sambuc return EmitPointerToBoolConversion(Visit(E));
1530f4a2713aSLionel Sambuc case CK_FloatingToBoolean:
1531f4a2713aSLionel Sambuc return EmitFloatToBoolConversion(Visit(E));
1532f4a2713aSLionel Sambuc case CK_MemberPointerToBoolean: {
1533f4a2713aSLionel Sambuc llvm::Value *MemPtr = Visit(E);
1534f4a2713aSLionel Sambuc const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
1535f4a2713aSLionel Sambuc return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT);
1536f4a2713aSLionel Sambuc }
1537f4a2713aSLionel Sambuc
1538f4a2713aSLionel Sambuc case CK_FloatingComplexToReal:
1539f4a2713aSLionel Sambuc case CK_IntegralComplexToReal:
1540f4a2713aSLionel Sambuc return CGF.EmitComplexExpr(E, false, true).first;
1541f4a2713aSLionel Sambuc
1542f4a2713aSLionel Sambuc case CK_FloatingComplexToBoolean:
1543f4a2713aSLionel Sambuc case CK_IntegralComplexToBoolean: {
1544f4a2713aSLionel Sambuc CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E);
1545f4a2713aSLionel Sambuc
1546f4a2713aSLionel Sambuc // TODO: kill this function off, inline appropriate case here
1547f4a2713aSLionel Sambuc return EmitComplexToScalarConversion(V, E->getType(), DestTy);
1548f4a2713aSLionel Sambuc }
1549f4a2713aSLionel Sambuc
1550f4a2713aSLionel Sambuc case CK_ZeroToOCLEvent: {
1551*0a6a1f1dSLionel Sambuc assert(DestTy->isEventT() && "CK_ZeroToOCLEvent cast on non-event type");
1552f4a2713aSLionel Sambuc return llvm::Constant::getNullValue(ConvertType(DestTy));
1553f4a2713aSLionel Sambuc }
1554f4a2713aSLionel Sambuc
1555f4a2713aSLionel Sambuc }
1556f4a2713aSLionel Sambuc
1557f4a2713aSLionel Sambuc llvm_unreachable("unknown scalar cast");
1558f4a2713aSLionel Sambuc }
1559f4a2713aSLionel Sambuc
VisitStmtExpr(const StmtExpr * E)1560f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
1561f4a2713aSLionel Sambuc CodeGenFunction::StmtExprEvaluation eval(CGF);
1562f4a2713aSLionel Sambuc llvm::Value *RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(),
1563f4a2713aSLionel Sambuc !E->getType()->isVoidType());
1564f4a2713aSLionel Sambuc if (!RetAlloca)
1565*0a6a1f1dSLionel Sambuc return nullptr;
1566f4a2713aSLionel Sambuc return CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(RetAlloca, E->getType()),
1567f4a2713aSLionel Sambuc E->getExprLoc());
1568f4a2713aSLionel Sambuc }
1569f4a2713aSLionel Sambuc
1570f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1571f4a2713aSLionel Sambuc // Unary Operators
1572f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
1573f4a2713aSLionel Sambuc
1574f4a2713aSLionel Sambuc llvm::Value *ScalarExprEmitter::
EmitAddConsiderOverflowBehavior(const UnaryOperator * E,llvm::Value * InVal,llvm::Value * NextVal,bool IsInc)1575f4a2713aSLionel Sambuc EmitAddConsiderOverflowBehavior(const UnaryOperator *E,
1576f4a2713aSLionel Sambuc llvm::Value *InVal,
1577f4a2713aSLionel Sambuc llvm::Value *NextVal, bool IsInc) {
1578f4a2713aSLionel Sambuc switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
1579f4a2713aSLionel Sambuc case LangOptions::SOB_Defined:
1580f4a2713aSLionel Sambuc return Builder.CreateAdd(InVal, NextVal, IsInc ? "inc" : "dec");
1581f4a2713aSLionel Sambuc case LangOptions::SOB_Undefined:
1582*0a6a1f1dSLionel Sambuc if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow))
1583f4a2713aSLionel Sambuc return Builder.CreateNSWAdd(InVal, NextVal, IsInc ? "inc" : "dec");
1584f4a2713aSLionel Sambuc // Fall through.
1585f4a2713aSLionel Sambuc case LangOptions::SOB_Trapping:
1586f4a2713aSLionel Sambuc BinOpInfo BinOp;
1587f4a2713aSLionel Sambuc BinOp.LHS = InVal;
1588f4a2713aSLionel Sambuc BinOp.RHS = NextVal;
1589f4a2713aSLionel Sambuc BinOp.Ty = E->getType();
1590f4a2713aSLionel Sambuc BinOp.Opcode = BO_Add;
1591f4a2713aSLionel Sambuc BinOp.FPContractable = false;
1592f4a2713aSLionel Sambuc BinOp.E = E;
1593f4a2713aSLionel Sambuc return EmitOverflowCheckedBinOp(BinOp);
1594f4a2713aSLionel Sambuc }
1595f4a2713aSLionel Sambuc llvm_unreachable("Unknown SignedOverflowBehaviorTy");
1596f4a2713aSLionel Sambuc }
1597f4a2713aSLionel Sambuc
1598f4a2713aSLionel Sambuc llvm::Value *
EmitScalarPrePostIncDec(const UnaryOperator * E,LValue LV,bool isInc,bool isPre)1599f4a2713aSLionel Sambuc ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1600f4a2713aSLionel Sambuc bool isInc, bool isPre) {
1601f4a2713aSLionel Sambuc
1602f4a2713aSLionel Sambuc QualType type = E->getSubExpr()->getType();
1603*0a6a1f1dSLionel Sambuc llvm::PHINode *atomicPHI = nullptr;
1604f4a2713aSLionel Sambuc llvm::Value *value;
1605f4a2713aSLionel Sambuc llvm::Value *input;
1606f4a2713aSLionel Sambuc
1607f4a2713aSLionel Sambuc int amount = (isInc ? 1 : -1);
1608f4a2713aSLionel Sambuc
1609f4a2713aSLionel Sambuc if (const AtomicType *atomicTy = type->getAs<AtomicType>()) {
1610f4a2713aSLionel Sambuc type = atomicTy->getValueType();
1611f4a2713aSLionel Sambuc if (isInc && type->isBooleanType()) {
1612f4a2713aSLionel Sambuc llvm::Value *True = CGF.EmitToMemory(Builder.getTrue(), type);
1613f4a2713aSLionel Sambuc if (isPre) {
1614f4a2713aSLionel Sambuc Builder.Insert(new llvm::StoreInst(True,
1615f4a2713aSLionel Sambuc LV.getAddress(), LV.isVolatileQualified(),
1616f4a2713aSLionel Sambuc LV.getAlignment().getQuantity(),
1617f4a2713aSLionel Sambuc llvm::SequentiallyConsistent));
1618f4a2713aSLionel Sambuc return Builder.getTrue();
1619f4a2713aSLionel Sambuc }
1620f4a2713aSLionel Sambuc // For atomic bool increment, we just store true and return it for
1621f4a2713aSLionel Sambuc // preincrement, do an atomic swap with true for postincrement
1622f4a2713aSLionel Sambuc return Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
1623f4a2713aSLionel Sambuc LV.getAddress(), True, llvm::SequentiallyConsistent);
1624f4a2713aSLionel Sambuc }
1625f4a2713aSLionel Sambuc // Special case for atomic increment / decrement on integers, emit
1626f4a2713aSLionel Sambuc // atomicrmw instructions. We skip this if we want to be doing overflow
1627f4a2713aSLionel Sambuc // checking, and fall into the slow path with the atomic cmpxchg loop.
1628f4a2713aSLionel Sambuc if (!type->isBooleanType() && type->isIntegerType() &&
1629f4a2713aSLionel Sambuc !(type->isUnsignedIntegerType() &&
1630*0a6a1f1dSLionel Sambuc CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
1631f4a2713aSLionel Sambuc CGF.getLangOpts().getSignedOverflowBehavior() !=
1632f4a2713aSLionel Sambuc LangOptions::SOB_Trapping) {
1633f4a2713aSLionel Sambuc llvm::AtomicRMWInst::BinOp aop = isInc ? llvm::AtomicRMWInst::Add :
1634f4a2713aSLionel Sambuc llvm::AtomicRMWInst::Sub;
1635f4a2713aSLionel Sambuc llvm::Instruction::BinaryOps op = isInc ? llvm::Instruction::Add :
1636f4a2713aSLionel Sambuc llvm::Instruction::Sub;
1637f4a2713aSLionel Sambuc llvm::Value *amt = CGF.EmitToMemory(
1638f4a2713aSLionel Sambuc llvm::ConstantInt::get(ConvertType(type), 1, true), type);
1639f4a2713aSLionel Sambuc llvm::Value *old = Builder.CreateAtomicRMW(aop,
1640f4a2713aSLionel Sambuc LV.getAddress(), amt, llvm::SequentiallyConsistent);
1641f4a2713aSLionel Sambuc return isPre ? Builder.CreateBinOp(op, old, amt) : old;
1642f4a2713aSLionel Sambuc }
1643f4a2713aSLionel Sambuc value = EmitLoadOfLValue(LV, E->getExprLoc());
1644f4a2713aSLionel Sambuc input = value;
1645f4a2713aSLionel Sambuc // For every other atomic operation, we need to emit a load-op-cmpxchg loop
1646f4a2713aSLionel Sambuc llvm::BasicBlock *startBB = Builder.GetInsertBlock();
1647f4a2713aSLionel Sambuc llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn);
1648f4a2713aSLionel Sambuc value = CGF.EmitToMemory(value, type);
1649f4a2713aSLionel Sambuc Builder.CreateBr(opBB);
1650f4a2713aSLionel Sambuc Builder.SetInsertPoint(opBB);
1651f4a2713aSLionel Sambuc atomicPHI = Builder.CreatePHI(value->getType(), 2);
1652f4a2713aSLionel Sambuc atomicPHI->addIncoming(value, startBB);
1653f4a2713aSLionel Sambuc value = atomicPHI;
1654f4a2713aSLionel Sambuc } else {
1655f4a2713aSLionel Sambuc value = EmitLoadOfLValue(LV, E->getExprLoc());
1656f4a2713aSLionel Sambuc input = value;
1657f4a2713aSLionel Sambuc }
1658f4a2713aSLionel Sambuc
1659f4a2713aSLionel Sambuc // Special case of integer increment that we have to check first: bool++.
1660f4a2713aSLionel Sambuc // Due to promotion rules, we get:
1661f4a2713aSLionel Sambuc // bool++ -> bool = bool + 1
1662f4a2713aSLionel Sambuc // -> bool = (int)bool + 1
1663f4a2713aSLionel Sambuc // -> bool = ((int)bool + 1 != 0)
1664f4a2713aSLionel Sambuc // An interesting aspect of this is that increment is always true.
1665f4a2713aSLionel Sambuc // Decrement does not have this property.
1666f4a2713aSLionel Sambuc if (isInc && type->isBooleanType()) {
1667f4a2713aSLionel Sambuc value = Builder.getTrue();
1668f4a2713aSLionel Sambuc
1669f4a2713aSLionel Sambuc // Most common case by far: integer increment.
1670f4a2713aSLionel Sambuc } else if (type->isIntegerType()) {
1671f4a2713aSLionel Sambuc
1672f4a2713aSLionel Sambuc llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount, true);
1673f4a2713aSLionel Sambuc
1674f4a2713aSLionel Sambuc // Note that signed integer inc/dec with width less than int can't
1675f4a2713aSLionel Sambuc // overflow because of promotion rules; we're just eliding a few steps here.
1676*0a6a1f1dSLionel Sambuc bool CanOverflow = value->getType()->getIntegerBitWidth() >=
1677*0a6a1f1dSLionel Sambuc CGF.IntTy->getIntegerBitWidth();
1678*0a6a1f1dSLionel Sambuc if (CanOverflow && type->isSignedIntegerOrEnumerationType()) {
1679f4a2713aSLionel Sambuc value = EmitAddConsiderOverflowBehavior(E, value, amt, isInc);
1680*0a6a1f1dSLionel Sambuc } else if (CanOverflow && type->isUnsignedIntegerType() &&
1681*0a6a1f1dSLionel Sambuc CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) {
1682f4a2713aSLionel Sambuc BinOpInfo BinOp;
1683f4a2713aSLionel Sambuc BinOp.LHS = value;
1684f4a2713aSLionel Sambuc BinOp.RHS = llvm::ConstantInt::get(value->getType(), 1, false);
1685f4a2713aSLionel Sambuc BinOp.Ty = E->getType();
1686f4a2713aSLionel Sambuc BinOp.Opcode = isInc ? BO_Add : BO_Sub;
1687f4a2713aSLionel Sambuc BinOp.FPContractable = false;
1688f4a2713aSLionel Sambuc BinOp.E = E;
1689f4a2713aSLionel Sambuc value = EmitOverflowCheckedBinOp(BinOp);
1690f4a2713aSLionel Sambuc } else
1691f4a2713aSLionel Sambuc value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
1692f4a2713aSLionel Sambuc
1693f4a2713aSLionel Sambuc // Next most common: pointer increment.
1694f4a2713aSLionel Sambuc } else if (const PointerType *ptr = type->getAs<PointerType>()) {
1695f4a2713aSLionel Sambuc QualType type = ptr->getPointeeType();
1696f4a2713aSLionel Sambuc
1697f4a2713aSLionel Sambuc // VLA types don't have constant size.
1698f4a2713aSLionel Sambuc if (const VariableArrayType *vla
1699f4a2713aSLionel Sambuc = CGF.getContext().getAsVariableArrayType(type)) {
1700f4a2713aSLionel Sambuc llvm::Value *numElts = CGF.getVLASize(vla).first;
1701f4a2713aSLionel Sambuc if (!isInc) numElts = Builder.CreateNSWNeg(numElts, "vla.negsize");
1702f4a2713aSLionel Sambuc if (CGF.getLangOpts().isSignedOverflowDefined())
1703f4a2713aSLionel Sambuc value = Builder.CreateGEP(value, numElts, "vla.inc");
1704f4a2713aSLionel Sambuc else
1705f4a2713aSLionel Sambuc value = Builder.CreateInBoundsGEP(value, numElts, "vla.inc");
1706f4a2713aSLionel Sambuc
1707f4a2713aSLionel Sambuc // Arithmetic on function pointers (!) is just +-1.
1708f4a2713aSLionel Sambuc } else if (type->isFunctionType()) {
1709f4a2713aSLionel Sambuc llvm::Value *amt = Builder.getInt32(amount);
1710f4a2713aSLionel Sambuc
1711f4a2713aSLionel Sambuc value = CGF.EmitCastToVoidPtr(value);
1712f4a2713aSLionel Sambuc if (CGF.getLangOpts().isSignedOverflowDefined())
1713f4a2713aSLionel Sambuc value = Builder.CreateGEP(value, amt, "incdec.funcptr");
1714f4a2713aSLionel Sambuc else
1715f4a2713aSLionel Sambuc value = Builder.CreateInBoundsGEP(value, amt, "incdec.funcptr");
1716f4a2713aSLionel Sambuc value = Builder.CreateBitCast(value, input->getType());
1717f4a2713aSLionel Sambuc
1718f4a2713aSLionel Sambuc // For everything else, we can just do a simple increment.
1719f4a2713aSLionel Sambuc } else {
1720f4a2713aSLionel Sambuc llvm::Value *amt = Builder.getInt32(amount);
1721f4a2713aSLionel Sambuc if (CGF.getLangOpts().isSignedOverflowDefined())
1722f4a2713aSLionel Sambuc value = Builder.CreateGEP(value, amt, "incdec.ptr");
1723f4a2713aSLionel Sambuc else
1724f4a2713aSLionel Sambuc value = Builder.CreateInBoundsGEP(value, amt, "incdec.ptr");
1725f4a2713aSLionel Sambuc }
1726f4a2713aSLionel Sambuc
1727f4a2713aSLionel Sambuc // Vector increment/decrement.
1728f4a2713aSLionel Sambuc } else if (type->isVectorType()) {
1729f4a2713aSLionel Sambuc if (type->hasIntegerRepresentation()) {
1730f4a2713aSLionel Sambuc llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount);
1731f4a2713aSLionel Sambuc
1732f4a2713aSLionel Sambuc value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
1733f4a2713aSLionel Sambuc } else {
1734f4a2713aSLionel Sambuc value = Builder.CreateFAdd(
1735f4a2713aSLionel Sambuc value,
1736f4a2713aSLionel Sambuc llvm::ConstantFP::get(value->getType(), amount),
1737f4a2713aSLionel Sambuc isInc ? "inc" : "dec");
1738f4a2713aSLionel Sambuc }
1739f4a2713aSLionel Sambuc
1740f4a2713aSLionel Sambuc // Floating point.
1741f4a2713aSLionel Sambuc } else if (type->isRealFloatingType()) {
1742f4a2713aSLionel Sambuc // Add the inc/dec to the real part.
1743f4a2713aSLionel Sambuc llvm::Value *amt;
1744f4a2713aSLionel Sambuc
1745*0a6a1f1dSLionel Sambuc if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType &&
1746*0a6a1f1dSLionel Sambuc !CGF.getContext().getLangOpts().HalfArgsAndReturns) {
1747f4a2713aSLionel Sambuc // Another special case: half FP increment should be done via float
1748*0a6a1f1dSLionel Sambuc value = Builder.CreateCall(
1749*0a6a1f1dSLionel Sambuc CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16,
1750*0a6a1f1dSLionel Sambuc CGF.CGM.FloatTy),
1751f4a2713aSLionel Sambuc input);
1752f4a2713aSLionel Sambuc }
1753f4a2713aSLionel Sambuc
1754f4a2713aSLionel Sambuc if (value->getType()->isFloatTy())
1755f4a2713aSLionel Sambuc amt = llvm::ConstantFP::get(VMContext,
1756f4a2713aSLionel Sambuc llvm::APFloat(static_cast<float>(amount)));
1757f4a2713aSLionel Sambuc else if (value->getType()->isDoubleTy())
1758f4a2713aSLionel Sambuc amt = llvm::ConstantFP::get(VMContext,
1759f4a2713aSLionel Sambuc llvm::APFloat(static_cast<double>(amount)));
1760f4a2713aSLionel Sambuc else {
1761f4a2713aSLionel Sambuc llvm::APFloat F(static_cast<float>(amount));
1762f4a2713aSLionel Sambuc bool ignored;
1763f4a2713aSLionel Sambuc F.convert(CGF.getTarget().getLongDoubleFormat(),
1764f4a2713aSLionel Sambuc llvm::APFloat::rmTowardZero, &ignored);
1765f4a2713aSLionel Sambuc amt = llvm::ConstantFP::get(VMContext, F);
1766f4a2713aSLionel Sambuc }
1767f4a2713aSLionel Sambuc value = Builder.CreateFAdd(value, amt, isInc ? "inc" : "dec");
1768f4a2713aSLionel Sambuc
1769*0a6a1f1dSLionel Sambuc if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType &&
1770*0a6a1f1dSLionel Sambuc !CGF.getContext().getLangOpts().HalfArgsAndReturns)
1771*0a6a1f1dSLionel Sambuc value = Builder.CreateCall(
1772*0a6a1f1dSLionel Sambuc CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16,
1773*0a6a1f1dSLionel Sambuc CGF.CGM.FloatTy),
1774f4a2713aSLionel Sambuc value);
1775f4a2713aSLionel Sambuc
1776f4a2713aSLionel Sambuc // Objective-C pointer types.
1777f4a2713aSLionel Sambuc } else {
1778f4a2713aSLionel Sambuc const ObjCObjectPointerType *OPT = type->castAs<ObjCObjectPointerType>();
1779f4a2713aSLionel Sambuc value = CGF.EmitCastToVoidPtr(value);
1780f4a2713aSLionel Sambuc
1781f4a2713aSLionel Sambuc CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType());
1782f4a2713aSLionel Sambuc if (!isInc) size = -size;
1783f4a2713aSLionel Sambuc llvm::Value *sizeValue =
1784f4a2713aSLionel Sambuc llvm::ConstantInt::get(CGF.SizeTy, size.getQuantity());
1785f4a2713aSLionel Sambuc
1786f4a2713aSLionel Sambuc if (CGF.getLangOpts().isSignedOverflowDefined())
1787f4a2713aSLionel Sambuc value = Builder.CreateGEP(value, sizeValue, "incdec.objptr");
1788f4a2713aSLionel Sambuc else
1789f4a2713aSLionel Sambuc value = Builder.CreateInBoundsGEP(value, sizeValue, "incdec.objptr");
1790f4a2713aSLionel Sambuc value = Builder.CreateBitCast(value, input->getType());
1791f4a2713aSLionel Sambuc }
1792f4a2713aSLionel Sambuc
1793f4a2713aSLionel Sambuc if (atomicPHI) {
1794f4a2713aSLionel Sambuc llvm::BasicBlock *opBB = Builder.GetInsertBlock();
1795f4a2713aSLionel Sambuc llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
1796*0a6a1f1dSLionel Sambuc auto Pair = CGF.EmitAtomicCompareExchange(
1797*0a6a1f1dSLionel Sambuc LV, RValue::get(atomicPHI), RValue::get(CGF.EmitToMemory(value, type)),
1798*0a6a1f1dSLionel Sambuc E->getExprLoc());
1799*0a6a1f1dSLionel Sambuc llvm::Value *old = Pair.first.getScalarVal();
1800*0a6a1f1dSLionel Sambuc llvm::Value *success = Pair.second.getScalarVal();
1801f4a2713aSLionel Sambuc atomicPHI->addIncoming(old, opBB);
1802f4a2713aSLionel Sambuc Builder.CreateCondBr(success, contBB, opBB);
1803f4a2713aSLionel Sambuc Builder.SetInsertPoint(contBB);
1804f4a2713aSLionel Sambuc return isPre ? value : input;
1805f4a2713aSLionel Sambuc }
1806f4a2713aSLionel Sambuc
1807f4a2713aSLionel Sambuc // Store the updated result through the lvalue.
1808f4a2713aSLionel Sambuc if (LV.isBitField())
1809f4a2713aSLionel Sambuc CGF.EmitStoreThroughBitfieldLValue(RValue::get(value), LV, &value);
1810f4a2713aSLionel Sambuc else
1811f4a2713aSLionel Sambuc CGF.EmitStoreThroughLValue(RValue::get(value), LV);
1812f4a2713aSLionel Sambuc
1813f4a2713aSLionel Sambuc // If this is a postinc, return the value read from memory, otherwise use the
1814f4a2713aSLionel Sambuc // updated value.
1815f4a2713aSLionel Sambuc return isPre ? value : input;
1816f4a2713aSLionel Sambuc }
1817f4a2713aSLionel Sambuc
1818f4a2713aSLionel Sambuc
1819f4a2713aSLionel Sambuc
VisitUnaryMinus(const UnaryOperator * E)1820f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
1821f4a2713aSLionel Sambuc TestAndClearIgnoreResultAssign();
1822f4a2713aSLionel Sambuc // Emit unary minus with EmitSub so we handle overflow cases etc.
1823f4a2713aSLionel Sambuc BinOpInfo BinOp;
1824f4a2713aSLionel Sambuc BinOp.RHS = Visit(E->getSubExpr());
1825f4a2713aSLionel Sambuc
1826f4a2713aSLionel Sambuc if (BinOp.RHS->getType()->isFPOrFPVectorTy())
1827f4a2713aSLionel Sambuc BinOp.LHS = llvm::ConstantFP::getZeroValueForNegation(BinOp.RHS->getType());
1828f4a2713aSLionel Sambuc else
1829f4a2713aSLionel Sambuc BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType());
1830f4a2713aSLionel Sambuc BinOp.Ty = E->getType();
1831f4a2713aSLionel Sambuc BinOp.Opcode = BO_Sub;
1832f4a2713aSLionel Sambuc BinOp.FPContractable = false;
1833f4a2713aSLionel Sambuc BinOp.E = E;
1834f4a2713aSLionel Sambuc return EmitSub(BinOp);
1835f4a2713aSLionel Sambuc }
1836f4a2713aSLionel Sambuc
VisitUnaryNot(const UnaryOperator * E)1837f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
1838f4a2713aSLionel Sambuc TestAndClearIgnoreResultAssign();
1839f4a2713aSLionel Sambuc Value *Op = Visit(E->getSubExpr());
1840f4a2713aSLionel Sambuc return Builder.CreateNot(Op, "neg");
1841f4a2713aSLionel Sambuc }
1842f4a2713aSLionel Sambuc
VisitUnaryLNot(const UnaryOperator * E)1843f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
1844f4a2713aSLionel Sambuc // Perform vector logical not on comparison with zero vector.
1845f4a2713aSLionel Sambuc if (E->getType()->isExtVectorType()) {
1846f4a2713aSLionel Sambuc Value *Oper = Visit(E->getSubExpr());
1847f4a2713aSLionel Sambuc Value *Zero = llvm::Constant::getNullValue(Oper->getType());
1848f4a2713aSLionel Sambuc Value *Result;
1849f4a2713aSLionel Sambuc if (Oper->getType()->isFPOrFPVectorTy())
1850f4a2713aSLionel Sambuc Result = Builder.CreateFCmp(llvm::CmpInst::FCMP_OEQ, Oper, Zero, "cmp");
1851f4a2713aSLionel Sambuc else
1852f4a2713aSLionel Sambuc Result = Builder.CreateICmp(llvm::CmpInst::ICMP_EQ, Oper, Zero, "cmp");
1853f4a2713aSLionel Sambuc return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
1854f4a2713aSLionel Sambuc }
1855f4a2713aSLionel Sambuc
1856f4a2713aSLionel Sambuc // Compare operand to zero.
1857f4a2713aSLionel Sambuc Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
1858f4a2713aSLionel Sambuc
1859f4a2713aSLionel Sambuc // Invert value.
1860f4a2713aSLionel Sambuc // TODO: Could dynamically modify easy computations here. For example, if
1861f4a2713aSLionel Sambuc // the operand is an icmp ne, turn into icmp eq.
1862f4a2713aSLionel Sambuc BoolVal = Builder.CreateNot(BoolVal, "lnot");
1863f4a2713aSLionel Sambuc
1864f4a2713aSLionel Sambuc // ZExt result to the expr type.
1865f4a2713aSLionel Sambuc return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext");
1866f4a2713aSLionel Sambuc }
1867f4a2713aSLionel Sambuc
VisitOffsetOfExpr(OffsetOfExpr * E)1868f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) {
1869f4a2713aSLionel Sambuc // Try folding the offsetof to a constant.
1870f4a2713aSLionel Sambuc llvm::APSInt Value;
1871f4a2713aSLionel Sambuc if (E->EvaluateAsInt(Value, CGF.getContext()))
1872f4a2713aSLionel Sambuc return Builder.getInt(Value);
1873f4a2713aSLionel Sambuc
1874f4a2713aSLionel Sambuc // Loop over the components of the offsetof to compute the value.
1875f4a2713aSLionel Sambuc unsigned n = E->getNumComponents();
1876f4a2713aSLionel Sambuc llvm::Type* ResultType = ConvertType(E->getType());
1877f4a2713aSLionel Sambuc llvm::Value* Result = llvm::Constant::getNullValue(ResultType);
1878f4a2713aSLionel Sambuc QualType CurrentType = E->getTypeSourceInfo()->getType();
1879f4a2713aSLionel Sambuc for (unsigned i = 0; i != n; ++i) {
1880f4a2713aSLionel Sambuc OffsetOfExpr::OffsetOfNode ON = E->getComponent(i);
1881*0a6a1f1dSLionel Sambuc llvm::Value *Offset = nullptr;
1882f4a2713aSLionel Sambuc switch (ON.getKind()) {
1883f4a2713aSLionel Sambuc case OffsetOfExpr::OffsetOfNode::Array: {
1884f4a2713aSLionel Sambuc // Compute the index
1885f4a2713aSLionel Sambuc Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex());
1886f4a2713aSLionel Sambuc llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr);
1887f4a2713aSLionel Sambuc bool IdxSigned = IdxExpr->getType()->isSignedIntegerOrEnumerationType();
1888f4a2713aSLionel Sambuc Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv");
1889f4a2713aSLionel Sambuc
1890f4a2713aSLionel Sambuc // Save the element type
1891f4a2713aSLionel Sambuc CurrentType =
1892f4a2713aSLionel Sambuc CGF.getContext().getAsArrayType(CurrentType)->getElementType();
1893f4a2713aSLionel Sambuc
1894f4a2713aSLionel Sambuc // Compute the element size
1895f4a2713aSLionel Sambuc llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType,
1896f4a2713aSLionel Sambuc CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity());
1897f4a2713aSLionel Sambuc
1898f4a2713aSLionel Sambuc // Multiply out to compute the result
1899f4a2713aSLionel Sambuc Offset = Builder.CreateMul(Idx, ElemSize);
1900f4a2713aSLionel Sambuc break;
1901f4a2713aSLionel Sambuc }
1902f4a2713aSLionel Sambuc
1903f4a2713aSLionel Sambuc case OffsetOfExpr::OffsetOfNode::Field: {
1904f4a2713aSLionel Sambuc FieldDecl *MemberDecl = ON.getField();
1905f4a2713aSLionel Sambuc RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl();
1906f4a2713aSLionel Sambuc const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD);
1907f4a2713aSLionel Sambuc
1908f4a2713aSLionel Sambuc // Compute the index of the field in its parent.
1909f4a2713aSLionel Sambuc unsigned i = 0;
1910f4a2713aSLionel Sambuc // FIXME: It would be nice if we didn't have to loop here!
1911f4a2713aSLionel Sambuc for (RecordDecl::field_iterator Field = RD->field_begin(),
1912f4a2713aSLionel Sambuc FieldEnd = RD->field_end();
1913f4a2713aSLionel Sambuc Field != FieldEnd; ++Field, ++i) {
1914f4a2713aSLionel Sambuc if (*Field == MemberDecl)
1915f4a2713aSLionel Sambuc break;
1916f4a2713aSLionel Sambuc }
1917f4a2713aSLionel Sambuc assert(i < RL.getFieldCount() && "offsetof field in wrong type");
1918f4a2713aSLionel Sambuc
1919f4a2713aSLionel Sambuc // Compute the offset to the field
1920f4a2713aSLionel Sambuc int64_t OffsetInt = RL.getFieldOffset(i) /
1921f4a2713aSLionel Sambuc CGF.getContext().getCharWidth();
1922f4a2713aSLionel Sambuc Offset = llvm::ConstantInt::get(ResultType, OffsetInt);
1923f4a2713aSLionel Sambuc
1924f4a2713aSLionel Sambuc // Save the element type.
1925f4a2713aSLionel Sambuc CurrentType = MemberDecl->getType();
1926f4a2713aSLionel Sambuc break;
1927f4a2713aSLionel Sambuc }
1928f4a2713aSLionel Sambuc
1929f4a2713aSLionel Sambuc case OffsetOfExpr::OffsetOfNode::Identifier:
1930f4a2713aSLionel Sambuc llvm_unreachable("dependent __builtin_offsetof");
1931f4a2713aSLionel Sambuc
1932f4a2713aSLionel Sambuc case OffsetOfExpr::OffsetOfNode::Base: {
1933f4a2713aSLionel Sambuc if (ON.getBase()->isVirtual()) {
1934f4a2713aSLionel Sambuc CGF.ErrorUnsupported(E, "virtual base in offsetof");
1935f4a2713aSLionel Sambuc continue;
1936f4a2713aSLionel Sambuc }
1937f4a2713aSLionel Sambuc
1938f4a2713aSLionel Sambuc RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl();
1939f4a2713aSLionel Sambuc const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD);
1940f4a2713aSLionel Sambuc
1941f4a2713aSLionel Sambuc // Save the element type.
1942f4a2713aSLionel Sambuc CurrentType = ON.getBase()->getType();
1943f4a2713aSLionel Sambuc
1944f4a2713aSLionel Sambuc // Compute the offset to the base.
1945f4a2713aSLionel Sambuc const RecordType *BaseRT = CurrentType->getAs<RecordType>();
1946f4a2713aSLionel Sambuc CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl());
1947f4a2713aSLionel Sambuc CharUnits OffsetInt = RL.getBaseClassOffset(BaseRD);
1948f4a2713aSLionel Sambuc Offset = llvm::ConstantInt::get(ResultType, OffsetInt.getQuantity());
1949f4a2713aSLionel Sambuc break;
1950f4a2713aSLionel Sambuc }
1951f4a2713aSLionel Sambuc }
1952f4a2713aSLionel Sambuc Result = Builder.CreateAdd(Result, Offset);
1953f4a2713aSLionel Sambuc }
1954f4a2713aSLionel Sambuc return Result;
1955f4a2713aSLionel Sambuc }
1956f4a2713aSLionel Sambuc
1957f4a2713aSLionel Sambuc /// VisitUnaryExprOrTypeTraitExpr - Return the size or alignment of the type of
1958f4a2713aSLionel Sambuc /// argument of the sizeof expression as an integer.
1959f4a2713aSLionel Sambuc Value *
VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr * E)1960f4a2713aSLionel Sambuc ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
1961f4a2713aSLionel Sambuc const UnaryExprOrTypeTraitExpr *E) {
1962f4a2713aSLionel Sambuc QualType TypeToSize = E->getTypeOfArgument();
1963f4a2713aSLionel Sambuc if (E->getKind() == UETT_SizeOf) {
1964f4a2713aSLionel Sambuc if (const VariableArrayType *VAT =
1965f4a2713aSLionel Sambuc CGF.getContext().getAsVariableArrayType(TypeToSize)) {
1966f4a2713aSLionel Sambuc if (E->isArgumentType()) {
1967f4a2713aSLionel Sambuc // sizeof(type) - make sure to emit the VLA size.
1968f4a2713aSLionel Sambuc CGF.EmitVariablyModifiedType(TypeToSize);
1969f4a2713aSLionel Sambuc } else {
1970f4a2713aSLionel Sambuc // C99 6.5.3.4p2: If the argument is an expression of type
1971f4a2713aSLionel Sambuc // VLA, it is evaluated.
1972f4a2713aSLionel Sambuc CGF.EmitIgnoredExpr(E->getArgumentExpr());
1973f4a2713aSLionel Sambuc }
1974f4a2713aSLionel Sambuc
1975f4a2713aSLionel Sambuc QualType eltType;
1976f4a2713aSLionel Sambuc llvm::Value *numElts;
1977*0a6a1f1dSLionel Sambuc std::tie(numElts, eltType) = CGF.getVLASize(VAT);
1978f4a2713aSLionel Sambuc
1979f4a2713aSLionel Sambuc llvm::Value *size = numElts;
1980f4a2713aSLionel Sambuc
1981f4a2713aSLionel Sambuc // Scale the number of non-VLA elements by the non-VLA element size.
1982f4a2713aSLionel Sambuc CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType);
1983f4a2713aSLionel Sambuc if (!eltSize.isOne())
1984f4a2713aSLionel Sambuc size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), numElts);
1985f4a2713aSLionel Sambuc
1986f4a2713aSLionel Sambuc return size;
1987f4a2713aSLionel Sambuc }
1988f4a2713aSLionel Sambuc }
1989f4a2713aSLionel Sambuc
1990f4a2713aSLionel Sambuc // If this isn't sizeof(vla), the result must be constant; use the constant
1991f4a2713aSLionel Sambuc // folding logic so we don't have to duplicate it here.
1992f4a2713aSLionel Sambuc return Builder.getInt(E->EvaluateKnownConstInt(CGF.getContext()));
1993f4a2713aSLionel Sambuc }
1994f4a2713aSLionel Sambuc
VisitUnaryReal(const UnaryOperator * E)1995f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
1996f4a2713aSLionel Sambuc Expr *Op = E->getSubExpr();
1997f4a2713aSLionel Sambuc if (Op->getType()->isAnyComplexType()) {
1998f4a2713aSLionel Sambuc // If it's an l-value, load through the appropriate subobject l-value.
1999f4a2713aSLionel Sambuc // Note that we have to ask E because Op might be an l-value that
2000f4a2713aSLionel Sambuc // this won't work for, e.g. an Obj-C property.
2001f4a2713aSLionel Sambuc if (E->isGLValue())
2002f4a2713aSLionel Sambuc return CGF.EmitLoadOfLValue(CGF.EmitLValue(E),
2003f4a2713aSLionel Sambuc E->getExprLoc()).getScalarVal();
2004f4a2713aSLionel Sambuc
2005f4a2713aSLionel Sambuc // Otherwise, calculate and project.
2006f4a2713aSLionel Sambuc return CGF.EmitComplexExpr(Op, false, true).first;
2007f4a2713aSLionel Sambuc }
2008f4a2713aSLionel Sambuc
2009f4a2713aSLionel Sambuc return Visit(Op);
2010f4a2713aSLionel Sambuc }
2011f4a2713aSLionel Sambuc
VisitUnaryImag(const UnaryOperator * E)2012f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
2013f4a2713aSLionel Sambuc Expr *Op = E->getSubExpr();
2014f4a2713aSLionel Sambuc if (Op->getType()->isAnyComplexType()) {
2015f4a2713aSLionel Sambuc // If it's an l-value, load through the appropriate subobject l-value.
2016f4a2713aSLionel Sambuc // Note that we have to ask E because Op might be an l-value that
2017f4a2713aSLionel Sambuc // this won't work for, e.g. an Obj-C property.
2018f4a2713aSLionel Sambuc if (Op->isGLValue())
2019f4a2713aSLionel Sambuc return CGF.EmitLoadOfLValue(CGF.EmitLValue(E),
2020f4a2713aSLionel Sambuc E->getExprLoc()).getScalarVal();
2021f4a2713aSLionel Sambuc
2022f4a2713aSLionel Sambuc // Otherwise, calculate and project.
2023f4a2713aSLionel Sambuc return CGF.EmitComplexExpr(Op, true, false).second;
2024f4a2713aSLionel Sambuc }
2025f4a2713aSLionel Sambuc
2026f4a2713aSLionel Sambuc // __imag on a scalar returns zero. Emit the subexpr to ensure side
2027f4a2713aSLionel Sambuc // effects are evaluated, but not the actual value.
2028f4a2713aSLionel Sambuc if (Op->isGLValue())
2029f4a2713aSLionel Sambuc CGF.EmitLValue(Op);
2030f4a2713aSLionel Sambuc else
2031f4a2713aSLionel Sambuc CGF.EmitScalarExpr(Op, true);
2032f4a2713aSLionel Sambuc return llvm::Constant::getNullValue(ConvertType(E->getType()));
2033f4a2713aSLionel Sambuc }
2034f4a2713aSLionel Sambuc
2035f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2036f4a2713aSLionel Sambuc // Binary Operators
2037f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2038f4a2713aSLionel Sambuc
EmitBinOps(const BinaryOperator * E)2039f4a2713aSLionel Sambuc BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
2040f4a2713aSLionel Sambuc TestAndClearIgnoreResultAssign();
2041f4a2713aSLionel Sambuc BinOpInfo Result;
2042f4a2713aSLionel Sambuc Result.LHS = Visit(E->getLHS());
2043f4a2713aSLionel Sambuc Result.RHS = Visit(E->getRHS());
2044f4a2713aSLionel Sambuc Result.Ty = E->getType();
2045f4a2713aSLionel Sambuc Result.Opcode = E->getOpcode();
2046f4a2713aSLionel Sambuc Result.FPContractable = E->isFPContractable();
2047f4a2713aSLionel Sambuc Result.E = E;
2048f4a2713aSLionel Sambuc return Result;
2049f4a2713aSLionel Sambuc }
2050f4a2713aSLionel Sambuc
EmitCompoundAssignLValue(const CompoundAssignOperator * E,Value * (ScalarExprEmitter::* Func)(const BinOpInfo &),Value * & Result)2051f4a2713aSLionel Sambuc LValue ScalarExprEmitter::EmitCompoundAssignLValue(
2052f4a2713aSLionel Sambuc const CompoundAssignOperator *E,
2053f4a2713aSLionel Sambuc Value *(ScalarExprEmitter::*Func)(const BinOpInfo &),
2054f4a2713aSLionel Sambuc Value *&Result) {
2055f4a2713aSLionel Sambuc QualType LHSTy = E->getLHS()->getType();
2056f4a2713aSLionel Sambuc BinOpInfo OpInfo;
2057f4a2713aSLionel Sambuc
2058f4a2713aSLionel Sambuc if (E->getComputationResultType()->isAnyComplexType())
2059f4a2713aSLionel Sambuc return CGF.EmitScalarCompooundAssignWithComplex(E, Result);
2060f4a2713aSLionel Sambuc
2061f4a2713aSLionel Sambuc // Emit the RHS first. __block variables need to have the rhs evaluated
2062f4a2713aSLionel Sambuc // first, plus this should improve codegen a little.
2063f4a2713aSLionel Sambuc OpInfo.RHS = Visit(E->getRHS());
2064f4a2713aSLionel Sambuc OpInfo.Ty = E->getComputationResultType();
2065f4a2713aSLionel Sambuc OpInfo.Opcode = E->getOpcode();
2066f4a2713aSLionel Sambuc OpInfo.FPContractable = false;
2067f4a2713aSLionel Sambuc OpInfo.E = E;
2068f4a2713aSLionel Sambuc // Load/convert the LHS.
2069f4a2713aSLionel Sambuc LValue LHSLV = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
2070f4a2713aSLionel Sambuc
2071*0a6a1f1dSLionel Sambuc llvm::PHINode *atomicPHI = nullptr;
2072f4a2713aSLionel Sambuc if (const AtomicType *atomicTy = LHSTy->getAs<AtomicType>()) {
2073f4a2713aSLionel Sambuc QualType type = atomicTy->getValueType();
2074f4a2713aSLionel Sambuc if (!type->isBooleanType() && type->isIntegerType() &&
2075f4a2713aSLionel Sambuc !(type->isUnsignedIntegerType() &&
2076*0a6a1f1dSLionel Sambuc CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
2077f4a2713aSLionel Sambuc CGF.getLangOpts().getSignedOverflowBehavior() !=
2078f4a2713aSLionel Sambuc LangOptions::SOB_Trapping) {
2079f4a2713aSLionel Sambuc llvm::AtomicRMWInst::BinOp aop = llvm::AtomicRMWInst::BAD_BINOP;
2080f4a2713aSLionel Sambuc switch (OpInfo.Opcode) {
2081f4a2713aSLionel Sambuc // We don't have atomicrmw operands for *, %, /, <<, >>
2082f4a2713aSLionel Sambuc case BO_MulAssign: case BO_DivAssign:
2083f4a2713aSLionel Sambuc case BO_RemAssign:
2084f4a2713aSLionel Sambuc case BO_ShlAssign:
2085f4a2713aSLionel Sambuc case BO_ShrAssign:
2086f4a2713aSLionel Sambuc break;
2087f4a2713aSLionel Sambuc case BO_AddAssign:
2088f4a2713aSLionel Sambuc aop = llvm::AtomicRMWInst::Add;
2089f4a2713aSLionel Sambuc break;
2090f4a2713aSLionel Sambuc case BO_SubAssign:
2091f4a2713aSLionel Sambuc aop = llvm::AtomicRMWInst::Sub;
2092f4a2713aSLionel Sambuc break;
2093f4a2713aSLionel Sambuc case BO_AndAssign:
2094f4a2713aSLionel Sambuc aop = llvm::AtomicRMWInst::And;
2095f4a2713aSLionel Sambuc break;
2096f4a2713aSLionel Sambuc case BO_XorAssign:
2097f4a2713aSLionel Sambuc aop = llvm::AtomicRMWInst::Xor;
2098f4a2713aSLionel Sambuc break;
2099f4a2713aSLionel Sambuc case BO_OrAssign:
2100f4a2713aSLionel Sambuc aop = llvm::AtomicRMWInst::Or;
2101f4a2713aSLionel Sambuc break;
2102f4a2713aSLionel Sambuc default:
2103f4a2713aSLionel Sambuc llvm_unreachable("Invalid compound assignment type");
2104f4a2713aSLionel Sambuc }
2105f4a2713aSLionel Sambuc if (aop != llvm::AtomicRMWInst::BAD_BINOP) {
2106f4a2713aSLionel Sambuc llvm::Value *amt = CGF.EmitToMemory(EmitScalarConversion(OpInfo.RHS,
2107f4a2713aSLionel Sambuc E->getRHS()->getType(), LHSTy), LHSTy);
2108f4a2713aSLionel Sambuc Builder.CreateAtomicRMW(aop, LHSLV.getAddress(), amt,
2109f4a2713aSLionel Sambuc llvm::SequentiallyConsistent);
2110f4a2713aSLionel Sambuc return LHSLV;
2111f4a2713aSLionel Sambuc }
2112f4a2713aSLionel Sambuc }
2113f4a2713aSLionel Sambuc // FIXME: For floating point types, we should be saving and restoring the
2114f4a2713aSLionel Sambuc // floating point environment in the loop.
2115f4a2713aSLionel Sambuc llvm::BasicBlock *startBB = Builder.GetInsertBlock();
2116f4a2713aSLionel Sambuc llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn);
2117f4a2713aSLionel Sambuc OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc());
2118f4a2713aSLionel Sambuc OpInfo.LHS = CGF.EmitToMemory(OpInfo.LHS, type);
2119f4a2713aSLionel Sambuc Builder.CreateBr(opBB);
2120f4a2713aSLionel Sambuc Builder.SetInsertPoint(opBB);
2121f4a2713aSLionel Sambuc atomicPHI = Builder.CreatePHI(OpInfo.LHS->getType(), 2);
2122f4a2713aSLionel Sambuc atomicPHI->addIncoming(OpInfo.LHS, startBB);
2123f4a2713aSLionel Sambuc OpInfo.LHS = atomicPHI;
2124f4a2713aSLionel Sambuc }
2125f4a2713aSLionel Sambuc else
2126f4a2713aSLionel Sambuc OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc());
2127f4a2713aSLionel Sambuc
2128f4a2713aSLionel Sambuc OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
2129f4a2713aSLionel Sambuc E->getComputationLHSType());
2130f4a2713aSLionel Sambuc
2131f4a2713aSLionel Sambuc // Expand the binary operator.
2132f4a2713aSLionel Sambuc Result = (this->*Func)(OpInfo);
2133f4a2713aSLionel Sambuc
2134f4a2713aSLionel Sambuc // Convert the result back to the LHS type.
2135f4a2713aSLionel Sambuc Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
2136f4a2713aSLionel Sambuc
2137f4a2713aSLionel Sambuc if (atomicPHI) {
2138f4a2713aSLionel Sambuc llvm::BasicBlock *opBB = Builder.GetInsertBlock();
2139f4a2713aSLionel Sambuc llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
2140*0a6a1f1dSLionel Sambuc auto Pair = CGF.EmitAtomicCompareExchange(
2141*0a6a1f1dSLionel Sambuc LHSLV, RValue::get(atomicPHI),
2142*0a6a1f1dSLionel Sambuc RValue::get(CGF.EmitToMemory(Result, LHSTy)), E->getExprLoc());
2143*0a6a1f1dSLionel Sambuc llvm::Value *old = Pair.first.getScalarVal();
2144*0a6a1f1dSLionel Sambuc llvm::Value *success = Pair.second.getScalarVal();
2145f4a2713aSLionel Sambuc atomicPHI->addIncoming(old, opBB);
2146f4a2713aSLionel Sambuc Builder.CreateCondBr(success, contBB, opBB);
2147f4a2713aSLionel Sambuc Builder.SetInsertPoint(contBB);
2148f4a2713aSLionel Sambuc return LHSLV;
2149f4a2713aSLionel Sambuc }
2150f4a2713aSLionel Sambuc
2151f4a2713aSLionel Sambuc // Store the result value into the LHS lvalue. Bit-fields are handled
2152f4a2713aSLionel Sambuc // specially because the result is altered by the store, i.e., [C99 6.5.16p1]
2153f4a2713aSLionel Sambuc // 'An assignment expression has the value of the left operand after the
2154f4a2713aSLionel Sambuc // assignment...'.
2155f4a2713aSLionel Sambuc if (LHSLV.isBitField())
2156f4a2713aSLionel Sambuc CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, &Result);
2157f4a2713aSLionel Sambuc else
2158f4a2713aSLionel Sambuc CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV);
2159f4a2713aSLionel Sambuc
2160f4a2713aSLionel Sambuc return LHSLV;
2161f4a2713aSLionel Sambuc }
2162f4a2713aSLionel Sambuc
EmitCompoundAssign(const CompoundAssignOperator * E,Value * (ScalarExprEmitter::* Func)(const BinOpInfo &))2163f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
2164f4a2713aSLionel Sambuc Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
2165f4a2713aSLionel Sambuc bool Ignore = TestAndClearIgnoreResultAssign();
2166f4a2713aSLionel Sambuc Value *RHS;
2167f4a2713aSLionel Sambuc LValue LHS = EmitCompoundAssignLValue(E, Func, RHS);
2168f4a2713aSLionel Sambuc
2169f4a2713aSLionel Sambuc // If the result is clearly ignored, return now.
2170f4a2713aSLionel Sambuc if (Ignore)
2171*0a6a1f1dSLionel Sambuc return nullptr;
2172f4a2713aSLionel Sambuc
2173f4a2713aSLionel Sambuc // The result of an assignment in C is the assigned r-value.
2174f4a2713aSLionel Sambuc if (!CGF.getLangOpts().CPlusPlus)
2175f4a2713aSLionel Sambuc return RHS;
2176f4a2713aSLionel Sambuc
2177f4a2713aSLionel Sambuc // If the lvalue is non-volatile, return the computed value of the assignment.
2178f4a2713aSLionel Sambuc if (!LHS.isVolatileQualified())
2179f4a2713aSLionel Sambuc return RHS;
2180f4a2713aSLionel Sambuc
2181f4a2713aSLionel Sambuc // Otherwise, reload the value.
2182f4a2713aSLionel Sambuc return EmitLoadOfLValue(LHS, E->getExprLoc());
2183f4a2713aSLionel Sambuc }
2184f4a2713aSLionel Sambuc
EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo & Ops,llvm::Value * Zero,bool isDiv)2185f4a2713aSLionel Sambuc void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
2186f4a2713aSLionel Sambuc const BinOpInfo &Ops, llvm::Value *Zero, bool isDiv) {
2187*0a6a1f1dSLionel Sambuc SmallVector<std::pair<llvm::Value *, SanitizerKind>, 2> Checks;
2188f4a2713aSLionel Sambuc
2189*0a6a1f1dSLionel Sambuc if (CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero)) {
2190*0a6a1f1dSLionel Sambuc Checks.push_back(std::make_pair(Builder.CreateICmpNE(Ops.RHS, Zero),
2191*0a6a1f1dSLionel Sambuc SanitizerKind::IntegerDivideByZero));
2192*0a6a1f1dSLionel Sambuc }
2193f4a2713aSLionel Sambuc
2194*0a6a1f1dSLionel Sambuc if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow) &&
2195f4a2713aSLionel Sambuc Ops.Ty->hasSignedIntegerRepresentation()) {
2196f4a2713aSLionel Sambuc llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType());
2197f4a2713aSLionel Sambuc
2198f4a2713aSLionel Sambuc llvm::Value *IntMin =
2199f4a2713aSLionel Sambuc Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth()));
2200f4a2713aSLionel Sambuc llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL);
2201f4a2713aSLionel Sambuc
2202f4a2713aSLionel Sambuc llvm::Value *LHSCmp = Builder.CreateICmpNE(Ops.LHS, IntMin);
2203f4a2713aSLionel Sambuc llvm::Value *RHSCmp = Builder.CreateICmpNE(Ops.RHS, NegOne);
2204*0a6a1f1dSLionel Sambuc llvm::Value *NotOverflow = Builder.CreateOr(LHSCmp, RHSCmp, "or");
2205*0a6a1f1dSLionel Sambuc Checks.push_back(
2206*0a6a1f1dSLionel Sambuc std::make_pair(NotOverflow, SanitizerKind::SignedIntegerOverflow));
2207f4a2713aSLionel Sambuc }
2208f4a2713aSLionel Sambuc
2209*0a6a1f1dSLionel Sambuc if (Checks.size() > 0)
2210*0a6a1f1dSLionel Sambuc EmitBinOpCheck(Checks, Ops);
2211f4a2713aSLionel Sambuc }
2212f4a2713aSLionel Sambuc
EmitDiv(const BinOpInfo & Ops)2213f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
2214*0a6a1f1dSLionel Sambuc {
2215*0a6a1f1dSLionel Sambuc CodeGenFunction::SanitizerScope SanScope(&CGF);
2216*0a6a1f1dSLionel Sambuc if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) ||
2217*0a6a1f1dSLionel Sambuc CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) &&
2218f4a2713aSLionel Sambuc Ops.Ty->isIntegerType()) {
2219f4a2713aSLionel Sambuc llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
2220f4a2713aSLionel Sambuc EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true);
2221*0a6a1f1dSLionel Sambuc } else if (CGF.SanOpts.has(SanitizerKind::FloatDivideByZero) &&
2222f4a2713aSLionel Sambuc Ops.Ty->isRealFloatingType()) {
2223f4a2713aSLionel Sambuc llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
2224*0a6a1f1dSLionel Sambuc llvm::Value *NonZero = Builder.CreateFCmpUNE(Ops.RHS, Zero);
2225*0a6a1f1dSLionel Sambuc EmitBinOpCheck(std::make_pair(NonZero, SanitizerKind::FloatDivideByZero),
2226*0a6a1f1dSLionel Sambuc Ops);
2227*0a6a1f1dSLionel Sambuc }
2228f4a2713aSLionel Sambuc }
2229f4a2713aSLionel Sambuc
2230f4a2713aSLionel Sambuc if (Ops.LHS->getType()->isFPOrFPVectorTy()) {
2231f4a2713aSLionel Sambuc llvm::Value *Val = Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
2232f4a2713aSLionel Sambuc if (CGF.getLangOpts().OpenCL) {
2233f4a2713aSLionel Sambuc // OpenCL 1.1 7.4: minimum accuracy of single precision / is 2.5ulp
2234f4a2713aSLionel Sambuc llvm::Type *ValTy = Val->getType();
2235f4a2713aSLionel Sambuc if (ValTy->isFloatTy() ||
2236f4a2713aSLionel Sambuc (isa<llvm::VectorType>(ValTy) &&
2237f4a2713aSLionel Sambuc cast<llvm::VectorType>(ValTy)->getElementType()->isFloatTy()))
2238f4a2713aSLionel Sambuc CGF.SetFPAccuracy(Val, 2.5);
2239f4a2713aSLionel Sambuc }
2240f4a2713aSLionel Sambuc return Val;
2241f4a2713aSLionel Sambuc }
2242f4a2713aSLionel Sambuc else if (Ops.Ty->hasUnsignedIntegerRepresentation())
2243f4a2713aSLionel Sambuc return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
2244f4a2713aSLionel Sambuc else
2245f4a2713aSLionel Sambuc return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
2246f4a2713aSLionel Sambuc }
2247f4a2713aSLionel Sambuc
EmitRem(const BinOpInfo & Ops)2248f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
2249f4a2713aSLionel Sambuc // Rem in C can't be a floating point type: C99 6.5.5p2.
2250*0a6a1f1dSLionel Sambuc if (CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero)) {
2251*0a6a1f1dSLionel Sambuc CodeGenFunction::SanitizerScope SanScope(&CGF);
2252f4a2713aSLionel Sambuc llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
2253f4a2713aSLionel Sambuc
2254f4a2713aSLionel Sambuc if (Ops.Ty->isIntegerType())
2255f4a2713aSLionel Sambuc EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false);
2256f4a2713aSLionel Sambuc }
2257f4a2713aSLionel Sambuc
2258f4a2713aSLionel Sambuc if (Ops.Ty->hasUnsignedIntegerRepresentation())
2259f4a2713aSLionel Sambuc return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
2260f4a2713aSLionel Sambuc else
2261f4a2713aSLionel Sambuc return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
2262f4a2713aSLionel Sambuc }
2263f4a2713aSLionel Sambuc
EmitOverflowCheckedBinOp(const BinOpInfo & Ops)2264f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
2265f4a2713aSLionel Sambuc unsigned IID;
2266f4a2713aSLionel Sambuc unsigned OpID = 0;
2267f4a2713aSLionel Sambuc
2268f4a2713aSLionel Sambuc bool isSigned = Ops.Ty->isSignedIntegerOrEnumerationType();
2269f4a2713aSLionel Sambuc switch (Ops.Opcode) {
2270f4a2713aSLionel Sambuc case BO_Add:
2271f4a2713aSLionel Sambuc case BO_AddAssign:
2272f4a2713aSLionel Sambuc OpID = 1;
2273f4a2713aSLionel Sambuc IID = isSigned ? llvm::Intrinsic::sadd_with_overflow :
2274f4a2713aSLionel Sambuc llvm::Intrinsic::uadd_with_overflow;
2275f4a2713aSLionel Sambuc break;
2276f4a2713aSLionel Sambuc case BO_Sub:
2277f4a2713aSLionel Sambuc case BO_SubAssign:
2278f4a2713aSLionel Sambuc OpID = 2;
2279f4a2713aSLionel Sambuc IID = isSigned ? llvm::Intrinsic::ssub_with_overflow :
2280f4a2713aSLionel Sambuc llvm::Intrinsic::usub_with_overflow;
2281f4a2713aSLionel Sambuc break;
2282f4a2713aSLionel Sambuc case BO_Mul:
2283f4a2713aSLionel Sambuc case BO_MulAssign:
2284f4a2713aSLionel Sambuc OpID = 3;
2285f4a2713aSLionel Sambuc IID = isSigned ? llvm::Intrinsic::smul_with_overflow :
2286f4a2713aSLionel Sambuc llvm::Intrinsic::umul_with_overflow;
2287f4a2713aSLionel Sambuc break;
2288f4a2713aSLionel Sambuc default:
2289f4a2713aSLionel Sambuc llvm_unreachable("Unsupported operation for overflow detection");
2290f4a2713aSLionel Sambuc }
2291f4a2713aSLionel Sambuc OpID <<= 1;
2292f4a2713aSLionel Sambuc if (isSigned)
2293f4a2713aSLionel Sambuc OpID |= 1;
2294f4a2713aSLionel Sambuc
2295f4a2713aSLionel Sambuc llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
2296f4a2713aSLionel Sambuc
2297f4a2713aSLionel Sambuc llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, opTy);
2298f4a2713aSLionel Sambuc
2299f4a2713aSLionel Sambuc Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
2300f4a2713aSLionel Sambuc Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
2301f4a2713aSLionel Sambuc Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
2302f4a2713aSLionel Sambuc
2303f4a2713aSLionel Sambuc // Handle overflow with llvm.trap if no custom handler has been specified.
2304f4a2713aSLionel Sambuc const std::string *handlerName =
2305f4a2713aSLionel Sambuc &CGF.getLangOpts().OverflowHandler;
2306f4a2713aSLionel Sambuc if (handlerName->empty()) {
2307f4a2713aSLionel Sambuc // If the signed-integer-overflow sanitizer is enabled, emit a call to its
2308f4a2713aSLionel Sambuc // runtime. Otherwise, this is a -ftrapv check, so just emit a trap.
2309*0a6a1f1dSLionel Sambuc if (!isSigned || CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) {
2310*0a6a1f1dSLionel Sambuc CodeGenFunction::SanitizerScope SanScope(&CGF);
2311*0a6a1f1dSLionel Sambuc llvm::Value *NotOverflow = Builder.CreateNot(overflow);
2312*0a6a1f1dSLionel Sambuc SanitizerKind Kind = isSigned ? SanitizerKind::SignedIntegerOverflow
2313*0a6a1f1dSLionel Sambuc : SanitizerKind::UnsignedIntegerOverflow;
2314*0a6a1f1dSLionel Sambuc EmitBinOpCheck(std::make_pair(NotOverflow, Kind), Ops);
2315*0a6a1f1dSLionel Sambuc } else
2316f4a2713aSLionel Sambuc CGF.EmitTrapCheck(Builder.CreateNot(overflow));
2317f4a2713aSLionel Sambuc return result;
2318f4a2713aSLionel Sambuc }
2319f4a2713aSLionel Sambuc
2320f4a2713aSLionel Sambuc // Branch in case of overflow.
2321f4a2713aSLionel Sambuc llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
2322f4a2713aSLionel Sambuc llvm::Function::iterator insertPt = initialBB;
2323f4a2713aSLionel Sambuc llvm::BasicBlock *continueBB = CGF.createBasicBlock("nooverflow", CGF.CurFn,
2324*0a6a1f1dSLionel Sambuc std::next(insertPt));
2325f4a2713aSLionel Sambuc llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn);
2326f4a2713aSLionel Sambuc
2327f4a2713aSLionel Sambuc Builder.CreateCondBr(overflow, overflowBB, continueBB);
2328f4a2713aSLionel Sambuc
2329f4a2713aSLionel Sambuc // If an overflow handler is set, then we want to call it and then use its
2330f4a2713aSLionel Sambuc // result, if it returns.
2331f4a2713aSLionel Sambuc Builder.SetInsertPoint(overflowBB);
2332f4a2713aSLionel Sambuc
2333f4a2713aSLionel Sambuc // Get the overflow handler.
2334f4a2713aSLionel Sambuc llvm::Type *Int8Ty = CGF.Int8Ty;
2335f4a2713aSLionel Sambuc llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty };
2336f4a2713aSLionel Sambuc llvm::FunctionType *handlerTy =
2337f4a2713aSLionel Sambuc llvm::FunctionType::get(CGF.Int64Ty, argTypes, true);
2338f4a2713aSLionel Sambuc llvm::Value *handler = CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName);
2339f4a2713aSLionel Sambuc
2340f4a2713aSLionel Sambuc // Sign extend the args to 64-bit, so that we can use the same handler for
2341f4a2713aSLionel Sambuc // all types of overflow.
2342f4a2713aSLionel Sambuc llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty);
2343f4a2713aSLionel Sambuc llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty);
2344f4a2713aSLionel Sambuc
2345f4a2713aSLionel Sambuc // Call the handler with the two arguments, the operation, and the size of
2346f4a2713aSLionel Sambuc // the result.
2347f4a2713aSLionel Sambuc llvm::Value *handlerArgs[] = {
2348f4a2713aSLionel Sambuc lhs,
2349f4a2713aSLionel Sambuc rhs,
2350f4a2713aSLionel Sambuc Builder.getInt8(OpID),
2351f4a2713aSLionel Sambuc Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth())
2352f4a2713aSLionel Sambuc };
2353f4a2713aSLionel Sambuc llvm::Value *handlerResult =
2354f4a2713aSLionel Sambuc CGF.EmitNounwindRuntimeCall(handler, handlerArgs);
2355f4a2713aSLionel Sambuc
2356f4a2713aSLionel Sambuc // Truncate the result back to the desired size.
2357f4a2713aSLionel Sambuc handlerResult = Builder.CreateTrunc(handlerResult, opTy);
2358f4a2713aSLionel Sambuc Builder.CreateBr(continueBB);
2359f4a2713aSLionel Sambuc
2360f4a2713aSLionel Sambuc Builder.SetInsertPoint(continueBB);
2361f4a2713aSLionel Sambuc llvm::PHINode *phi = Builder.CreatePHI(opTy, 2);
2362f4a2713aSLionel Sambuc phi->addIncoming(result, initialBB);
2363f4a2713aSLionel Sambuc phi->addIncoming(handlerResult, overflowBB);
2364f4a2713aSLionel Sambuc
2365f4a2713aSLionel Sambuc return phi;
2366f4a2713aSLionel Sambuc }
2367f4a2713aSLionel Sambuc
2368f4a2713aSLionel Sambuc /// Emit pointer + index arithmetic.
emitPointerArithmetic(CodeGenFunction & CGF,const BinOpInfo & op,bool isSubtraction)2369f4a2713aSLionel Sambuc static Value *emitPointerArithmetic(CodeGenFunction &CGF,
2370f4a2713aSLionel Sambuc const BinOpInfo &op,
2371f4a2713aSLionel Sambuc bool isSubtraction) {
2372f4a2713aSLionel Sambuc // Must have binary (not unary) expr here. Unary pointer
2373f4a2713aSLionel Sambuc // increment/decrement doesn't use this path.
2374f4a2713aSLionel Sambuc const BinaryOperator *expr = cast<BinaryOperator>(op.E);
2375f4a2713aSLionel Sambuc
2376f4a2713aSLionel Sambuc Value *pointer = op.LHS;
2377f4a2713aSLionel Sambuc Expr *pointerOperand = expr->getLHS();
2378f4a2713aSLionel Sambuc Value *index = op.RHS;
2379f4a2713aSLionel Sambuc Expr *indexOperand = expr->getRHS();
2380f4a2713aSLionel Sambuc
2381f4a2713aSLionel Sambuc // In a subtraction, the LHS is always the pointer.
2382f4a2713aSLionel Sambuc if (!isSubtraction && !pointer->getType()->isPointerTy()) {
2383f4a2713aSLionel Sambuc std::swap(pointer, index);
2384f4a2713aSLionel Sambuc std::swap(pointerOperand, indexOperand);
2385f4a2713aSLionel Sambuc }
2386f4a2713aSLionel Sambuc
2387f4a2713aSLionel Sambuc unsigned width = cast<llvm::IntegerType>(index->getType())->getBitWidth();
2388f4a2713aSLionel Sambuc if (width != CGF.PointerWidthInBits) {
2389f4a2713aSLionel Sambuc // Zero-extend or sign-extend the pointer value according to
2390f4a2713aSLionel Sambuc // whether the index is signed or not.
2391f4a2713aSLionel Sambuc bool isSigned = indexOperand->getType()->isSignedIntegerOrEnumerationType();
2392f4a2713aSLionel Sambuc index = CGF.Builder.CreateIntCast(index, CGF.PtrDiffTy, isSigned,
2393f4a2713aSLionel Sambuc "idx.ext");
2394f4a2713aSLionel Sambuc }
2395f4a2713aSLionel Sambuc
2396f4a2713aSLionel Sambuc // If this is subtraction, negate the index.
2397f4a2713aSLionel Sambuc if (isSubtraction)
2398f4a2713aSLionel Sambuc index = CGF.Builder.CreateNeg(index, "idx.neg");
2399f4a2713aSLionel Sambuc
2400*0a6a1f1dSLionel Sambuc if (CGF.SanOpts.has(SanitizerKind::ArrayBounds))
2401f4a2713aSLionel Sambuc CGF.EmitBoundsCheck(op.E, pointerOperand, index, indexOperand->getType(),
2402f4a2713aSLionel Sambuc /*Accessed*/ false);
2403f4a2713aSLionel Sambuc
2404f4a2713aSLionel Sambuc const PointerType *pointerType
2405f4a2713aSLionel Sambuc = pointerOperand->getType()->getAs<PointerType>();
2406f4a2713aSLionel Sambuc if (!pointerType) {
2407f4a2713aSLionel Sambuc QualType objectType = pointerOperand->getType()
2408f4a2713aSLionel Sambuc ->castAs<ObjCObjectPointerType>()
2409f4a2713aSLionel Sambuc ->getPointeeType();
2410f4a2713aSLionel Sambuc llvm::Value *objectSize
2411f4a2713aSLionel Sambuc = CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(objectType));
2412f4a2713aSLionel Sambuc
2413f4a2713aSLionel Sambuc index = CGF.Builder.CreateMul(index, objectSize);
2414f4a2713aSLionel Sambuc
2415f4a2713aSLionel Sambuc Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy);
2416f4a2713aSLionel Sambuc result = CGF.Builder.CreateGEP(result, index, "add.ptr");
2417f4a2713aSLionel Sambuc return CGF.Builder.CreateBitCast(result, pointer->getType());
2418f4a2713aSLionel Sambuc }
2419f4a2713aSLionel Sambuc
2420f4a2713aSLionel Sambuc QualType elementType = pointerType->getPointeeType();
2421f4a2713aSLionel Sambuc if (const VariableArrayType *vla
2422f4a2713aSLionel Sambuc = CGF.getContext().getAsVariableArrayType(elementType)) {
2423f4a2713aSLionel Sambuc // The element count here is the total number of non-VLA elements.
2424f4a2713aSLionel Sambuc llvm::Value *numElements = CGF.getVLASize(vla).first;
2425f4a2713aSLionel Sambuc
2426f4a2713aSLionel Sambuc // Effectively, the multiply by the VLA size is part of the GEP.
2427f4a2713aSLionel Sambuc // GEP indexes are signed, and scaling an index isn't permitted to
2428f4a2713aSLionel Sambuc // signed-overflow, so we use the same semantics for our explicit
2429f4a2713aSLionel Sambuc // multiply. We suppress this if overflow is not undefined behavior.
2430f4a2713aSLionel Sambuc if (CGF.getLangOpts().isSignedOverflowDefined()) {
2431f4a2713aSLionel Sambuc index = CGF.Builder.CreateMul(index, numElements, "vla.index");
2432f4a2713aSLionel Sambuc pointer = CGF.Builder.CreateGEP(pointer, index, "add.ptr");
2433f4a2713aSLionel Sambuc } else {
2434f4a2713aSLionel Sambuc index = CGF.Builder.CreateNSWMul(index, numElements, "vla.index");
2435f4a2713aSLionel Sambuc pointer = CGF.Builder.CreateInBoundsGEP(pointer, index, "add.ptr");
2436f4a2713aSLionel Sambuc }
2437f4a2713aSLionel Sambuc return pointer;
2438f4a2713aSLionel Sambuc }
2439f4a2713aSLionel Sambuc
2440f4a2713aSLionel Sambuc // Explicitly handle GNU void* and function pointer arithmetic extensions. The
2441f4a2713aSLionel Sambuc // GNU void* casts amount to no-ops since our void* type is i8*, but this is
2442f4a2713aSLionel Sambuc // future proof.
2443f4a2713aSLionel Sambuc if (elementType->isVoidType() || elementType->isFunctionType()) {
2444f4a2713aSLionel Sambuc Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy);
2445f4a2713aSLionel Sambuc result = CGF.Builder.CreateGEP(result, index, "add.ptr");
2446f4a2713aSLionel Sambuc return CGF.Builder.CreateBitCast(result, pointer->getType());
2447f4a2713aSLionel Sambuc }
2448f4a2713aSLionel Sambuc
2449f4a2713aSLionel Sambuc if (CGF.getLangOpts().isSignedOverflowDefined())
2450f4a2713aSLionel Sambuc return CGF.Builder.CreateGEP(pointer, index, "add.ptr");
2451f4a2713aSLionel Sambuc
2452f4a2713aSLionel Sambuc return CGF.Builder.CreateInBoundsGEP(pointer, index, "add.ptr");
2453f4a2713aSLionel Sambuc }
2454f4a2713aSLionel Sambuc
2455f4a2713aSLionel Sambuc // Construct an fmuladd intrinsic to represent a fused mul-add of MulOp and
2456f4a2713aSLionel Sambuc // Addend. Use negMul and negAdd to negate the first operand of the Mul or
2457f4a2713aSLionel Sambuc // the add operand respectively. This allows fmuladd to represent a*b-c, or
2458f4a2713aSLionel Sambuc // c-a*b. Patterns in LLVM should catch the negated forms and translate them to
2459f4a2713aSLionel Sambuc // efficient operations.
buildFMulAdd(llvm::BinaryOperator * MulOp,Value * Addend,const CodeGenFunction & CGF,CGBuilderTy & Builder,bool negMul,bool negAdd)2460f4a2713aSLionel Sambuc static Value* buildFMulAdd(llvm::BinaryOperator *MulOp, Value *Addend,
2461f4a2713aSLionel Sambuc const CodeGenFunction &CGF, CGBuilderTy &Builder,
2462f4a2713aSLionel Sambuc bool negMul, bool negAdd) {
2463f4a2713aSLionel Sambuc assert(!(negMul && negAdd) && "Only one of negMul and negAdd should be set.");
2464f4a2713aSLionel Sambuc
2465f4a2713aSLionel Sambuc Value *MulOp0 = MulOp->getOperand(0);
2466f4a2713aSLionel Sambuc Value *MulOp1 = MulOp->getOperand(1);
2467f4a2713aSLionel Sambuc if (negMul) {
2468f4a2713aSLionel Sambuc MulOp0 =
2469f4a2713aSLionel Sambuc Builder.CreateFSub(
2470f4a2713aSLionel Sambuc llvm::ConstantFP::getZeroValueForNegation(MulOp0->getType()), MulOp0,
2471f4a2713aSLionel Sambuc "neg");
2472f4a2713aSLionel Sambuc } else if (negAdd) {
2473f4a2713aSLionel Sambuc Addend =
2474f4a2713aSLionel Sambuc Builder.CreateFSub(
2475f4a2713aSLionel Sambuc llvm::ConstantFP::getZeroValueForNegation(Addend->getType()), Addend,
2476f4a2713aSLionel Sambuc "neg");
2477f4a2713aSLionel Sambuc }
2478f4a2713aSLionel Sambuc
2479f4a2713aSLionel Sambuc Value *FMulAdd =
2480f4a2713aSLionel Sambuc Builder.CreateCall3(
2481f4a2713aSLionel Sambuc CGF.CGM.getIntrinsic(llvm::Intrinsic::fmuladd, Addend->getType()),
2482f4a2713aSLionel Sambuc MulOp0, MulOp1, Addend);
2483f4a2713aSLionel Sambuc MulOp->eraseFromParent();
2484f4a2713aSLionel Sambuc
2485f4a2713aSLionel Sambuc return FMulAdd;
2486f4a2713aSLionel Sambuc }
2487f4a2713aSLionel Sambuc
2488f4a2713aSLionel Sambuc // Check whether it would be legal to emit an fmuladd intrinsic call to
2489f4a2713aSLionel Sambuc // represent op and if so, build the fmuladd.
2490f4a2713aSLionel Sambuc //
2491f4a2713aSLionel Sambuc // Checks that (a) the operation is fusable, and (b) -ffp-contract=on.
2492f4a2713aSLionel Sambuc // Does NOT check the type of the operation - it's assumed that this function
2493f4a2713aSLionel Sambuc // will be called from contexts where it's known that the type is contractable.
tryEmitFMulAdd(const BinOpInfo & op,const CodeGenFunction & CGF,CGBuilderTy & Builder,bool isSub=false)2494f4a2713aSLionel Sambuc static Value* tryEmitFMulAdd(const BinOpInfo &op,
2495f4a2713aSLionel Sambuc const CodeGenFunction &CGF, CGBuilderTy &Builder,
2496f4a2713aSLionel Sambuc bool isSub=false) {
2497f4a2713aSLionel Sambuc
2498f4a2713aSLionel Sambuc assert((op.Opcode == BO_Add || op.Opcode == BO_AddAssign ||
2499f4a2713aSLionel Sambuc op.Opcode == BO_Sub || op.Opcode == BO_SubAssign) &&
2500f4a2713aSLionel Sambuc "Only fadd/fsub can be the root of an fmuladd.");
2501f4a2713aSLionel Sambuc
2502f4a2713aSLionel Sambuc // Check whether this op is marked as fusable.
2503f4a2713aSLionel Sambuc if (!op.FPContractable)
2504*0a6a1f1dSLionel Sambuc return nullptr;
2505f4a2713aSLionel Sambuc
2506f4a2713aSLionel Sambuc // Check whether -ffp-contract=on. (If -ffp-contract=off/fast, fusing is
2507f4a2713aSLionel Sambuc // either disabled, or handled entirely by the LLVM backend).
2508f4a2713aSLionel Sambuc if (CGF.CGM.getCodeGenOpts().getFPContractMode() != CodeGenOptions::FPC_On)
2509*0a6a1f1dSLionel Sambuc return nullptr;
2510f4a2713aSLionel Sambuc
2511f4a2713aSLionel Sambuc // We have a potentially fusable op. Look for a mul on one of the operands.
2512f4a2713aSLionel Sambuc if (llvm::BinaryOperator* LHSBinOp = dyn_cast<llvm::BinaryOperator>(op.LHS)) {
2513f4a2713aSLionel Sambuc if (LHSBinOp->getOpcode() == llvm::Instruction::FMul) {
2514f4a2713aSLionel Sambuc assert(LHSBinOp->getNumUses() == 0 &&
2515f4a2713aSLionel Sambuc "Operations with multiple uses shouldn't be contracted.");
2516f4a2713aSLionel Sambuc return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder, false, isSub);
2517f4a2713aSLionel Sambuc }
2518f4a2713aSLionel Sambuc } else if (llvm::BinaryOperator* RHSBinOp =
2519f4a2713aSLionel Sambuc dyn_cast<llvm::BinaryOperator>(op.RHS)) {
2520f4a2713aSLionel Sambuc if (RHSBinOp->getOpcode() == llvm::Instruction::FMul) {
2521f4a2713aSLionel Sambuc assert(RHSBinOp->getNumUses() == 0 &&
2522f4a2713aSLionel Sambuc "Operations with multiple uses shouldn't be contracted.");
2523f4a2713aSLionel Sambuc return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub, false);
2524f4a2713aSLionel Sambuc }
2525f4a2713aSLionel Sambuc }
2526f4a2713aSLionel Sambuc
2527*0a6a1f1dSLionel Sambuc return nullptr;
2528f4a2713aSLionel Sambuc }
2529f4a2713aSLionel Sambuc
EmitAdd(const BinOpInfo & op)2530f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &op) {
2531f4a2713aSLionel Sambuc if (op.LHS->getType()->isPointerTy() ||
2532f4a2713aSLionel Sambuc op.RHS->getType()->isPointerTy())
2533f4a2713aSLionel Sambuc return emitPointerArithmetic(CGF, op, /*subtraction*/ false);
2534f4a2713aSLionel Sambuc
2535f4a2713aSLionel Sambuc if (op.Ty->isSignedIntegerOrEnumerationType()) {
2536f4a2713aSLionel Sambuc switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
2537f4a2713aSLionel Sambuc case LangOptions::SOB_Defined:
2538f4a2713aSLionel Sambuc return Builder.CreateAdd(op.LHS, op.RHS, "add");
2539f4a2713aSLionel Sambuc case LangOptions::SOB_Undefined:
2540*0a6a1f1dSLionel Sambuc if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow))
2541f4a2713aSLionel Sambuc return Builder.CreateNSWAdd(op.LHS, op.RHS, "add");
2542f4a2713aSLionel Sambuc // Fall through.
2543f4a2713aSLionel Sambuc case LangOptions::SOB_Trapping:
2544f4a2713aSLionel Sambuc return EmitOverflowCheckedBinOp(op);
2545f4a2713aSLionel Sambuc }
2546f4a2713aSLionel Sambuc }
2547f4a2713aSLionel Sambuc
2548*0a6a1f1dSLionel Sambuc if (op.Ty->isUnsignedIntegerType() &&
2549*0a6a1f1dSLionel Sambuc CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow))
2550f4a2713aSLionel Sambuc return EmitOverflowCheckedBinOp(op);
2551f4a2713aSLionel Sambuc
2552f4a2713aSLionel Sambuc if (op.LHS->getType()->isFPOrFPVectorTy()) {
2553f4a2713aSLionel Sambuc // Try to form an fmuladd.
2554f4a2713aSLionel Sambuc if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder))
2555f4a2713aSLionel Sambuc return FMulAdd;
2556f4a2713aSLionel Sambuc
2557f4a2713aSLionel Sambuc return Builder.CreateFAdd(op.LHS, op.RHS, "add");
2558f4a2713aSLionel Sambuc }
2559f4a2713aSLionel Sambuc
2560f4a2713aSLionel Sambuc return Builder.CreateAdd(op.LHS, op.RHS, "add");
2561f4a2713aSLionel Sambuc }
2562f4a2713aSLionel Sambuc
EmitSub(const BinOpInfo & op)2563f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) {
2564f4a2713aSLionel Sambuc // The LHS is always a pointer if either side is.
2565f4a2713aSLionel Sambuc if (!op.LHS->getType()->isPointerTy()) {
2566f4a2713aSLionel Sambuc if (op.Ty->isSignedIntegerOrEnumerationType()) {
2567f4a2713aSLionel Sambuc switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
2568f4a2713aSLionel Sambuc case LangOptions::SOB_Defined:
2569f4a2713aSLionel Sambuc return Builder.CreateSub(op.LHS, op.RHS, "sub");
2570f4a2713aSLionel Sambuc case LangOptions::SOB_Undefined:
2571*0a6a1f1dSLionel Sambuc if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow))
2572f4a2713aSLionel Sambuc return Builder.CreateNSWSub(op.LHS, op.RHS, "sub");
2573f4a2713aSLionel Sambuc // Fall through.
2574f4a2713aSLionel Sambuc case LangOptions::SOB_Trapping:
2575f4a2713aSLionel Sambuc return EmitOverflowCheckedBinOp(op);
2576f4a2713aSLionel Sambuc }
2577f4a2713aSLionel Sambuc }
2578f4a2713aSLionel Sambuc
2579*0a6a1f1dSLionel Sambuc if (op.Ty->isUnsignedIntegerType() &&
2580*0a6a1f1dSLionel Sambuc CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow))
2581f4a2713aSLionel Sambuc return EmitOverflowCheckedBinOp(op);
2582f4a2713aSLionel Sambuc
2583f4a2713aSLionel Sambuc if (op.LHS->getType()->isFPOrFPVectorTy()) {
2584f4a2713aSLionel Sambuc // Try to form an fmuladd.
2585f4a2713aSLionel Sambuc if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder, true))
2586f4a2713aSLionel Sambuc return FMulAdd;
2587f4a2713aSLionel Sambuc return Builder.CreateFSub(op.LHS, op.RHS, "sub");
2588f4a2713aSLionel Sambuc }
2589f4a2713aSLionel Sambuc
2590f4a2713aSLionel Sambuc return Builder.CreateSub(op.LHS, op.RHS, "sub");
2591f4a2713aSLionel Sambuc }
2592f4a2713aSLionel Sambuc
2593f4a2713aSLionel Sambuc // If the RHS is not a pointer, then we have normal pointer
2594f4a2713aSLionel Sambuc // arithmetic.
2595f4a2713aSLionel Sambuc if (!op.RHS->getType()->isPointerTy())
2596f4a2713aSLionel Sambuc return emitPointerArithmetic(CGF, op, /*subtraction*/ true);
2597f4a2713aSLionel Sambuc
2598f4a2713aSLionel Sambuc // Otherwise, this is a pointer subtraction.
2599f4a2713aSLionel Sambuc
2600f4a2713aSLionel Sambuc // Do the raw subtraction part.
2601f4a2713aSLionel Sambuc llvm::Value *LHS
2602f4a2713aSLionel Sambuc = Builder.CreatePtrToInt(op.LHS, CGF.PtrDiffTy, "sub.ptr.lhs.cast");
2603f4a2713aSLionel Sambuc llvm::Value *RHS
2604f4a2713aSLionel Sambuc = Builder.CreatePtrToInt(op.RHS, CGF.PtrDiffTy, "sub.ptr.rhs.cast");
2605f4a2713aSLionel Sambuc Value *diffInChars = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
2606f4a2713aSLionel Sambuc
2607f4a2713aSLionel Sambuc // Okay, figure out the element size.
2608f4a2713aSLionel Sambuc const BinaryOperator *expr = cast<BinaryOperator>(op.E);
2609f4a2713aSLionel Sambuc QualType elementType = expr->getLHS()->getType()->getPointeeType();
2610f4a2713aSLionel Sambuc
2611*0a6a1f1dSLionel Sambuc llvm::Value *divisor = nullptr;
2612f4a2713aSLionel Sambuc
2613f4a2713aSLionel Sambuc // For a variable-length array, this is going to be non-constant.
2614f4a2713aSLionel Sambuc if (const VariableArrayType *vla
2615f4a2713aSLionel Sambuc = CGF.getContext().getAsVariableArrayType(elementType)) {
2616f4a2713aSLionel Sambuc llvm::Value *numElements;
2617*0a6a1f1dSLionel Sambuc std::tie(numElements, elementType) = CGF.getVLASize(vla);
2618f4a2713aSLionel Sambuc
2619f4a2713aSLionel Sambuc divisor = numElements;
2620f4a2713aSLionel Sambuc
2621f4a2713aSLionel Sambuc // Scale the number of non-VLA elements by the non-VLA element size.
2622f4a2713aSLionel Sambuc CharUnits eltSize = CGF.getContext().getTypeSizeInChars(elementType);
2623f4a2713aSLionel Sambuc if (!eltSize.isOne())
2624f4a2713aSLionel Sambuc divisor = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), divisor);
2625f4a2713aSLionel Sambuc
2626f4a2713aSLionel Sambuc // For everything elese, we can just compute it, safe in the
2627f4a2713aSLionel Sambuc // assumption that Sema won't let anything through that we can't
2628f4a2713aSLionel Sambuc // safely compute the size of.
2629f4a2713aSLionel Sambuc } else {
2630f4a2713aSLionel Sambuc CharUnits elementSize;
2631f4a2713aSLionel Sambuc // Handle GCC extension for pointer arithmetic on void* and
2632f4a2713aSLionel Sambuc // function pointer types.
2633f4a2713aSLionel Sambuc if (elementType->isVoidType() || elementType->isFunctionType())
2634f4a2713aSLionel Sambuc elementSize = CharUnits::One();
2635f4a2713aSLionel Sambuc else
2636f4a2713aSLionel Sambuc elementSize = CGF.getContext().getTypeSizeInChars(elementType);
2637f4a2713aSLionel Sambuc
2638f4a2713aSLionel Sambuc // Don't even emit the divide for element size of 1.
2639f4a2713aSLionel Sambuc if (elementSize.isOne())
2640f4a2713aSLionel Sambuc return diffInChars;
2641f4a2713aSLionel Sambuc
2642f4a2713aSLionel Sambuc divisor = CGF.CGM.getSize(elementSize);
2643f4a2713aSLionel Sambuc }
2644f4a2713aSLionel Sambuc
2645f4a2713aSLionel Sambuc // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
2646f4a2713aSLionel Sambuc // pointer difference in C is only defined in the case where both operands
2647f4a2713aSLionel Sambuc // are pointing to elements of an array.
2648f4a2713aSLionel Sambuc return Builder.CreateExactSDiv(diffInChars, divisor, "sub.ptr.div");
2649f4a2713aSLionel Sambuc }
2650f4a2713aSLionel Sambuc
GetWidthMinusOneValue(Value * LHS,Value * RHS)2651f4a2713aSLionel Sambuc Value *ScalarExprEmitter::GetWidthMinusOneValue(Value* LHS,Value* RHS) {
2652f4a2713aSLionel Sambuc llvm::IntegerType *Ty;
2653f4a2713aSLionel Sambuc if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(LHS->getType()))
2654f4a2713aSLionel Sambuc Ty = cast<llvm::IntegerType>(VT->getElementType());
2655f4a2713aSLionel Sambuc else
2656f4a2713aSLionel Sambuc Ty = cast<llvm::IntegerType>(LHS->getType());
2657f4a2713aSLionel Sambuc return llvm::ConstantInt::get(RHS->getType(), Ty->getBitWidth() - 1);
2658f4a2713aSLionel Sambuc }
2659f4a2713aSLionel Sambuc
EmitShl(const BinOpInfo & Ops)2660f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
2661f4a2713aSLionel Sambuc // LLVM requires the LHS and RHS to be the same type: promote or truncate the
2662f4a2713aSLionel Sambuc // RHS to the same size as the LHS.
2663f4a2713aSLionel Sambuc Value *RHS = Ops.RHS;
2664f4a2713aSLionel Sambuc if (Ops.LHS->getType() != RHS->getType())
2665f4a2713aSLionel Sambuc RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
2666f4a2713aSLionel Sambuc
2667*0a6a1f1dSLionel Sambuc if (CGF.SanOpts.has(SanitizerKind::Shift) && !CGF.getLangOpts().OpenCL &&
2668f4a2713aSLionel Sambuc isa<llvm::IntegerType>(Ops.LHS->getType())) {
2669*0a6a1f1dSLionel Sambuc CodeGenFunction::SanitizerScope SanScope(&CGF);
2670f4a2713aSLionel Sambuc llvm::Value *WidthMinusOne = GetWidthMinusOneValue(Ops.LHS, RHS);
2671f4a2713aSLionel Sambuc llvm::Value *Valid = Builder.CreateICmpULE(RHS, WidthMinusOne);
2672f4a2713aSLionel Sambuc
2673f4a2713aSLionel Sambuc if (Ops.Ty->hasSignedIntegerRepresentation()) {
2674f4a2713aSLionel Sambuc llvm::BasicBlock *Orig = Builder.GetInsertBlock();
2675f4a2713aSLionel Sambuc llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
2676f4a2713aSLionel Sambuc llvm::BasicBlock *CheckBitsShifted = CGF.createBasicBlock("check");
2677f4a2713aSLionel Sambuc Builder.CreateCondBr(Valid, CheckBitsShifted, Cont);
2678f4a2713aSLionel Sambuc
2679f4a2713aSLionel Sambuc // Check whether we are shifting any non-zero bits off the top of the
2680f4a2713aSLionel Sambuc // integer.
2681f4a2713aSLionel Sambuc CGF.EmitBlock(CheckBitsShifted);
2682f4a2713aSLionel Sambuc llvm::Value *BitsShiftedOff =
2683f4a2713aSLionel Sambuc Builder.CreateLShr(Ops.LHS,
2684f4a2713aSLionel Sambuc Builder.CreateSub(WidthMinusOne, RHS, "shl.zeros",
2685f4a2713aSLionel Sambuc /*NUW*/true, /*NSW*/true),
2686f4a2713aSLionel Sambuc "shl.check");
2687f4a2713aSLionel Sambuc if (CGF.getLangOpts().CPlusPlus) {
2688f4a2713aSLionel Sambuc // In C99, we are not permitted to shift a 1 bit into the sign bit.
2689f4a2713aSLionel Sambuc // Under C++11's rules, shifting a 1 bit into the sign bit is
2690f4a2713aSLionel Sambuc // OK, but shifting a 1 bit out of it is not. (C89 and C++03 don't
2691f4a2713aSLionel Sambuc // define signed left shifts, so we use the C99 and C++11 rules there).
2692f4a2713aSLionel Sambuc llvm::Value *One = llvm::ConstantInt::get(BitsShiftedOff->getType(), 1);
2693f4a2713aSLionel Sambuc BitsShiftedOff = Builder.CreateLShr(BitsShiftedOff, One);
2694f4a2713aSLionel Sambuc }
2695f4a2713aSLionel Sambuc llvm::Value *Zero = llvm::ConstantInt::get(BitsShiftedOff->getType(), 0);
2696f4a2713aSLionel Sambuc llvm::Value *SecondCheck = Builder.CreateICmpEQ(BitsShiftedOff, Zero);
2697f4a2713aSLionel Sambuc CGF.EmitBlock(Cont);
2698f4a2713aSLionel Sambuc llvm::PHINode *P = Builder.CreatePHI(Valid->getType(), 2);
2699f4a2713aSLionel Sambuc P->addIncoming(Valid, Orig);
2700f4a2713aSLionel Sambuc P->addIncoming(SecondCheck, CheckBitsShifted);
2701f4a2713aSLionel Sambuc Valid = P;
2702f4a2713aSLionel Sambuc }
2703f4a2713aSLionel Sambuc
2704*0a6a1f1dSLionel Sambuc EmitBinOpCheck(std::make_pair(Valid, SanitizerKind::Shift), Ops);
2705f4a2713aSLionel Sambuc }
2706f4a2713aSLionel Sambuc // OpenCL 6.3j: shift values are effectively % word size of LHS.
2707f4a2713aSLionel Sambuc if (CGF.getLangOpts().OpenCL)
2708f4a2713aSLionel Sambuc RHS = Builder.CreateAnd(RHS, GetWidthMinusOneValue(Ops.LHS, RHS), "shl.mask");
2709f4a2713aSLionel Sambuc
2710f4a2713aSLionel Sambuc return Builder.CreateShl(Ops.LHS, RHS, "shl");
2711f4a2713aSLionel Sambuc }
2712f4a2713aSLionel Sambuc
EmitShr(const BinOpInfo & Ops)2713f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
2714f4a2713aSLionel Sambuc // LLVM requires the LHS and RHS to be the same type: promote or truncate the
2715f4a2713aSLionel Sambuc // RHS to the same size as the LHS.
2716f4a2713aSLionel Sambuc Value *RHS = Ops.RHS;
2717f4a2713aSLionel Sambuc if (Ops.LHS->getType() != RHS->getType())
2718f4a2713aSLionel Sambuc RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
2719f4a2713aSLionel Sambuc
2720*0a6a1f1dSLionel Sambuc if (CGF.SanOpts.has(SanitizerKind::Shift) && !CGF.getLangOpts().OpenCL &&
2721*0a6a1f1dSLionel Sambuc isa<llvm::IntegerType>(Ops.LHS->getType())) {
2722*0a6a1f1dSLionel Sambuc CodeGenFunction::SanitizerScope SanScope(&CGF);
2723*0a6a1f1dSLionel Sambuc llvm::Value *Valid =
2724*0a6a1f1dSLionel Sambuc Builder.CreateICmpULE(RHS, GetWidthMinusOneValue(Ops.LHS, RHS));
2725*0a6a1f1dSLionel Sambuc EmitBinOpCheck(std::make_pair(Valid, SanitizerKind::Shift), Ops);
2726*0a6a1f1dSLionel Sambuc }
2727f4a2713aSLionel Sambuc
2728f4a2713aSLionel Sambuc // OpenCL 6.3j: shift values are effectively % word size of LHS.
2729f4a2713aSLionel Sambuc if (CGF.getLangOpts().OpenCL)
2730f4a2713aSLionel Sambuc RHS = Builder.CreateAnd(RHS, GetWidthMinusOneValue(Ops.LHS, RHS), "shr.mask");
2731f4a2713aSLionel Sambuc
2732f4a2713aSLionel Sambuc if (Ops.Ty->hasUnsignedIntegerRepresentation())
2733f4a2713aSLionel Sambuc return Builder.CreateLShr(Ops.LHS, RHS, "shr");
2734f4a2713aSLionel Sambuc return Builder.CreateAShr(Ops.LHS, RHS, "shr");
2735f4a2713aSLionel Sambuc }
2736f4a2713aSLionel Sambuc
2737f4a2713aSLionel Sambuc enum IntrinsicType { VCMPEQ, VCMPGT };
2738f4a2713aSLionel Sambuc // return corresponding comparison intrinsic for given vector type
GetIntrinsic(IntrinsicType IT,BuiltinType::Kind ElemKind)2739f4a2713aSLionel Sambuc static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT,
2740f4a2713aSLionel Sambuc BuiltinType::Kind ElemKind) {
2741f4a2713aSLionel Sambuc switch (ElemKind) {
2742f4a2713aSLionel Sambuc default: llvm_unreachable("unexpected element type");
2743f4a2713aSLionel Sambuc case BuiltinType::Char_U:
2744f4a2713aSLionel Sambuc case BuiltinType::UChar:
2745f4a2713aSLionel Sambuc return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
2746f4a2713aSLionel Sambuc llvm::Intrinsic::ppc_altivec_vcmpgtub_p;
2747f4a2713aSLionel Sambuc case BuiltinType::Char_S:
2748f4a2713aSLionel Sambuc case BuiltinType::SChar:
2749f4a2713aSLionel Sambuc return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
2750f4a2713aSLionel Sambuc llvm::Intrinsic::ppc_altivec_vcmpgtsb_p;
2751f4a2713aSLionel Sambuc case BuiltinType::UShort:
2752f4a2713aSLionel Sambuc return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
2753f4a2713aSLionel Sambuc llvm::Intrinsic::ppc_altivec_vcmpgtuh_p;
2754f4a2713aSLionel Sambuc case BuiltinType::Short:
2755f4a2713aSLionel Sambuc return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
2756f4a2713aSLionel Sambuc llvm::Intrinsic::ppc_altivec_vcmpgtsh_p;
2757f4a2713aSLionel Sambuc case BuiltinType::UInt:
2758f4a2713aSLionel Sambuc case BuiltinType::ULong:
2759f4a2713aSLionel Sambuc return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
2760f4a2713aSLionel Sambuc llvm::Intrinsic::ppc_altivec_vcmpgtuw_p;
2761f4a2713aSLionel Sambuc case BuiltinType::Int:
2762f4a2713aSLionel Sambuc case BuiltinType::Long:
2763f4a2713aSLionel Sambuc return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
2764f4a2713aSLionel Sambuc llvm::Intrinsic::ppc_altivec_vcmpgtsw_p;
2765f4a2713aSLionel Sambuc case BuiltinType::Float:
2766f4a2713aSLionel Sambuc return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p :
2767f4a2713aSLionel Sambuc llvm::Intrinsic::ppc_altivec_vcmpgtfp_p;
2768f4a2713aSLionel Sambuc }
2769f4a2713aSLionel Sambuc }
2770f4a2713aSLionel Sambuc
EmitCompare(const BinaryOperator * E,unsigned UICmpOpc,unsigned SICmpOpc,unsigned FCmpOpc)2771f4a2713aSLionel Sambuc Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
2772f4a2713aSLionel Sambuc unsigned SICmpOpc, unsigned FCmpOpc) {
2773f4a2713aSLionel Sambuc TestAndClearIgnoreResultAssign();
2774f4a2713aSLionel Sambuc Value *Result;
2775f4a2713aSLionel Sambuc QualType LHSTy = E->getLHS()->getType();
2776*0a6a1f1dSLionel Sambuc QualType RHSTy = E->getRHS()->getType();
2777f4a2713aSLionel Sambuc if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) {
2778f4a2713aSLionel Sambuc assert(E->getOpcode() == BO_EQ ||
2779f4a2713aSLionel Sambuc E->getOpcode() == BO_NE);
2780f4a2713aSLionel Sambuc Value *LHS = CGF.EmitScalarExpr(E->getLHS());
2781f4a2713aSLionel Sambuc Value *RHS = CGF.EmitScalarExpr(E->getRHS());
2782f4a2713aSLionel Sambuc Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison(
2783f4a2713aSLionel Sambuc CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE);
2784*0a6a1f1dSLionel Sambuc } else if (!LHSTy->isAnyComplexType() && !RHSTy->isAnyComplexType()) {
2785f4a2713aSLionel Sambuc Value *LHS = Visit(E->getLHS());
2786f4a2713aSLionel Sambuc Value *RHS = Visit(E->getRHS());
2787f4a2713aSLionel Sambuc
2788f4a2713aSLionel Sambuc // If AltiVec, the comparison results in a numeric type, so we use
2789f4a2713aSLionel Sambuc // intrinsics comparing vectors and giving 0 or 1 as a result
2790f4a2713aSLionel Sambuc if (LHSTy->isVectorType() && !E->getType()->isVectorType()) {
2791f4a2713aSLionel Sambuc // constants for mapping CR6 register bits to predicate result
2792f4a2713aSLionel Sambuc enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6;
2793f4a2713aSLionel Sambuc
2794f4a2713aSLionel Sambuc llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic;
2795f4a2713aSLionel Sambuc
2796f4a2713aSLionel Sambuc // in several cases vector arguments order will be reversed
2797f4a2713aSLionel Sambuc Value *FirstVecArg = LHS,
2798f4a2713aSLionel Sambuc *SecondVecArg = RHS;
2799f4a2713aSLionel Sambuc
2800f4a2713aSLionel Sambuc QualType ElTy = LHSTy->getAs<VectorType>()->getElementType();
2801f4a2713aSLionel Sambuc const BuiltinType *BTy = ElTy->getAs<BuiltinType>();
2802f4a2713aSLionel Sambuc BuiltinType::Kind ElementKind = BTy->getKind();
2803f4a2713aSLionel Sambuc
2804f4a2713aSLionel Sambuc switch(E->getOpcode()) {
2805f4a2713aSLionel Sambuc default: llvm_unreachable("is not a comparison operation");
2806f4a2713aSLionel Sambuc case BO_EQ:
2807f4a2713aSLionel Sambuc CR6 = CR6_LT;
2808f4a2713aSLionel Sambuc ID = GetIntrinsic(VCMPEQ, ElementKind);
2809f4a2713aSLionel Sambuc break;
2810f4a2713aSLionel Sambuc case BO_NE:
2811f4a2713aSLionel Sambuc CR6 = CR6_EQ;
2812f4a2713aSLionel Sambuc ID = GetIntrinsic(VCMPEQ, ElementKind);
2813f4a2713aSLionel Sambuc break;
2814f4a2713aSLionel Sambuc case BO_LT:
2815f4a2713aSLionel Sambuc CR6 = CR6_LT;
2816f4a2713aSLionel Sambuc ID = GetIntrinsic(VCMPGT, ElementKind);
2817f4a2713aSLionel Sambuc std::swap(FirstVecArg, SecondVecArg);
2818f4a2713aSLionel Sambuc break;
2819f4a2713aSLionel Sambuc case BO_GT:
2820f4a2713aSLionel Sambuc CR6 = CR6_LT;
2821f4a2713aSLionel Sambuc ID = GetIntrinsic(VCMPGT, ElementKind);
2822f4a2713aSLionel Sambuc break;
2823f4a2713aSLionel Sambuc case BO_LE:
2824f4a2713aSLionel Sambuc if (ElementKind == BuiltinType::Float) {
2825f4a2713aSLionel Sambuc CR6 = CR6_LT;
2826f4a2713aSLionel Sambuc ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
2827f4a2713aSLionel Sambuc std::swap(FirstVecArg, SecondVecArg);
2828f4a2713aSLionel Sambuc }
2829f4a2713aSLionel Sambuc else {
2830f4a2713aSLionel Sambuc CR6 = CR6_EQ;
2831f4a2713aSLionel Sambuc ID = GetIntrinsic(VCMPGT, ElementKind);
2832f4a2713aSLionel Sambuc }
2833f4a2713aSLionel Sambuc break;
2834f4a2713aSLionel Sambuc case BO_GE:
2835f4a2713aSLionel Sambuc if (ElementKind == BuiltinType::Float) {
2836f4a2713aSLionel Sambuc CR6 = CR6_LT;
2837f4a2713aSLionel Sambuc ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
2838f4a2713aSLionel Sambuc }
2839f4a2713aSLionel Sambuc else {
2840f4a2713aSLionel Sambuc CR6 = CR6_EQ;
2841f4a2713aSLionel Sambuc ID = GetIntrinsic(VCMPGT, ElementKind);
2842f4a2713aSLionel Sambuc std::swap(FirstVecArg, SecondVecArg);
2843f4a2713aSLionel Sambuc }
2844f4a2713aSLionel Sambuc break;
2845f4a2713aSLionel Sambuc }
2846f4a2713aSLionel Sambuc
2847f4a2713aSLionel Sambuc Value *CR6Param = Builder.getInt32(CR6);
2848f4a2713aSLionel Sambuc llvm::Function *F = CGF.CGM.getIntrinsic(ID);
2849f4a2713aSLionel Sambuc Result = Builder.CreateCall3(F, CR6Param, FirstVecArg, SecondVecArg, "");
2850f4a2713aSLionel Sambuc return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
2851f4a2713aSLionel Sambuc }
2852f4a2713aSLionel Sambuc
2853f4a2713aSLionel Sambuc if (LHS->getType()->isFPOrFPVectorTy()) {
2854f4a2713aSLionel Sambuc Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
2855f4a2713aSLionel Sambuc LHS, RHS, "cmp");
2856f4a2713aSLionel Sambuc } else if (LHSTy->hasSignedIntegerRepresentation()) {
2857f4a2713aSLionel Sambuc Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
2858f4a2713aSLionel Sambuc LHS, RHS, "cmp");
2859f4a2713aSLionel Sambuc } else {
2860f4a2713aSLionel Sambuc // Unsigned integers and pointers.
2861f4a2713aSLionel Sambuc Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
2862f4a2713aSLionel Sambuc LHS, RHS, "cmp");
2863f4a2713aSLionel Sambuc }
2864f4a2713aSLionel Sambuc
2865f4a2713aSLionel Sambuc // If this is a vector comparison, sign extend the result to the appropriate
2866f4a2713aSLionel Sambuc // vector integer type and return it (don't convert to bool).
2867f4a2713aSLionel Sambuc if (LHSTy->isVectorType())
2868f4a2713aSLionel Sambuc return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
2869f4a2713aSLionel Sambuc
2870f4a2713aSLionel Sambuc } else {
2871f4a2713aSLionel Sambuc // Complex Comparison: can only be an equality comparison.
2872*0a6a1f1dSLionel Sambuc CodeGenFunction::ComplexPairTy LHS, RHS;
2873*0a6a1f1dSLionel Sambuc QualType CETy;
2874*0a6a1f1dSLionel Sambuc if (auto *CTy = LHSTy->getAs<ComplexType>()) {
2875*0a6a1f1dSLionel Sambuc LHS = CGF.EmitComplexExpr(E->getLHS());
2876*0a6a1f1dSLionel Sambuc CETy = CTy->getElementType();
2877*0a6a1f1dSLionel Sambuc } else {
2878*0a6a1f1dSLionel Sambuc LHS.first = Visit(E->getLHS());
2879*0a6a1f1dSLionel Sambuc LHS.second = llvm::Constant::getNullValue(LHS.first->getType());
2880*0a6a1f1dSLionel Sambuc CETy = LHSTy;
2881*0a6a1f1dSLionel Sambuc }
2882*0a6a1f1dSLionel Sambuc if (auto *CTy = RHSTy->getAs<ComplexType>()) {
2883*0a6a1f1dSLionel Sambuc RHS = CGF.EmitComplexExpr(E->getRHS());
2884*0a6a1f1dSLionel Sambuc assert(CGF.getContext().hasSameUnqualifiedType(CETy,
2885*0a6a1f1dSLionel Sambuc CTy->getElementType()) &&
2886*0a6a1f1dSLionel Sambuc "The element types must always match.");
2887*0a6a1f1dSLionel Sambuc (void)CTy;
2888*0a6a1f1dSLionel Sambuc } else {
2889*0a6a1f1dSLionel Sambuc RHS.first = Visit(E->getRHS());
2890*0a6a1f1dSLionel Sambuc RHS.second = llvm::Constant::getNullValue(RHS.first->getType());
2891*0a6a1f1dSLionel Sambuc assert(CGF.getContext().hasSameUnqualifiedType(CETy, RHSTy) &&
2892*0a6a1f1dSLionel Sambuc "The element types must always match.");
2893*0a6a1f1dSLionel Sambuc }
2894f4a2713aSLionel Sambuc
2895f4a2713aSLionel Sambuc Value *ResultR, *ResultI;
2896f4a2713aSLionel Sambuc if (CETy->isRealFloatingType()) {
2897f4a2713aSLionel Sambuc ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
2898f4a2713aSLionel Sambuc LHS.first, RHS.first, "cmp.r");
2899f4a2713aSLionel Sambuc ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
2900f4a2713aSLionel Sambuc LHS.second, RHS.second, "cmp.i");
2901f4a2713aSLionel Sambuc } else {
2902f4a2713aSLionel Sambuc // Complex comparisons can only be equality comparisons. As such, signed
2903f4a2713aSLionel Sambuc // and unsigned opcodes are the same.
2904f4a2713aSLionel Sambuc ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
2905f4a2713aSLionel Sambuc LHS.first, RHS.first, "cmp.r");
2906f4a2713aSLionel Sambuc ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
2907f4a2713aSLionel Sambuc LHS.second, RHS.second, "cmp.i");
2908f4a2713aSLionel Sambuc }
2909f4a2713aSLionel Sambuc
2910f4a2713aSLionel Sambuc if (E->getOpcode() == BO_EQ) {
2911f4a2713aSLionel Sambuc Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
2912f4a2713aSLionel Sambuc } else {
2913f4a2713aSLionel Sambuc assert(E->getOpcode() == BO_NE &&
2914f4a2713aSLionel Sambuc "Complex comparison other than == or != ?");
2915f4a2713aSLionel Sambuc Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
2916f4a2713aSLionel Sambuc }
2917f4a2713aSLionel Sambuc }
2918f4a2713aSLionel Sambuc
2919f4a2713aSLionel Sambuc return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
2920f4a2713aSLionel Sambuc }
2921f4a2713aSLionel Sambuc
VisitBinAssign(const BinaryOperator * E)2922f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
2923f4a2713aSLionel Sambuc bool Ignore = TestAndClearIgnoreResultAssign();
2924f4a2713aSLionel Sambuc
2925f4a2713aSLionel Sambuc Value *RHS;
2926f4a2713aSLionel Sambuc LValue LHS;
2927f4a2713aSLionel Sambuc
2928f4a2713aSLionel Sambuc switch (E->getLHS()->getType().getObjCLifetime()) {
2929f4a2713aSLionel Sambuc case Qualifiers::OCL_Strong:
2930*0a6a1f1dSLionel Sambuc std::tie(LHS, RHS) = CGF.EmitARCStoreStrong(E, Ignore);
2931f4a2713aSLionel Sambuc break;
2932f4a2713aSLionel Sambuc
2933f4a2713aSLionel Sambuc case Qualifiers::OCL_Autoreleasing:
2934*0a6a1f1dSLionel Sambuc std::tie(LHS, RHS) = CGF.EmitARCStoreAutoreleasing(E);
2935f4a2713aSLionel Sambuc break;
2936f4a2713aSLionel Sambuc
2937f4a2713aSLionel Sambuc case Qualifiers::OCL_Weak:
2938f4a2713aSLionel Sambuc RHS = Visit(E->getRHS());
2939f4a2713aSLionel Sambuc LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
2940f4a2713aSLionel Sambuc RHS = CGF.EmitARCStoreWeak(LHS.getAddress(), RHS, Ignore);
2941f4a2713aSLionel Sambuc break;
2942f4a2713aSLionel Sambuc
2943f4a2713aSLionel Sambuc // No reason to do any of these differently.
2944f4a2713aSLionel Sambuc case Qualifiers::OCL_None:
2945f4a2713aSLionel Sambuc case Qualifiers::OCL_ExplicitNone:
2946f4a2713aSLionel Sambuc // __block variables need to have the rhs evaluated first, plus
2947f4a2713aSLionel Sambuc // this should improve codegen just a little.
2948f4a2713aSLionel Sambuc RHS = Visit(E->getRHS());
2949f4a2713aSLionel Sambuc LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
2950f4a2713aSLionel Sambuc
2951f4a2713aSLionel Sambuc // Store the value into the LHS. Bit-fields are handled specially
2952f4a2713aSLionel Sambuc // because the result is altered by the store, i.e., [C99 6.5.16p1]
2953f4a2713aSLionel Sambuc // 'An assignment expression has the value of the left operand after
2954f4a2713aSLionel Sambuc // the assignment...'.
2955f4a2713aSLionel Sambuc if (LHS.isBitField())
2956f4a2713aSLionel Sambuc CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, &RHS);
2957f4a2713aSLionel Sambuc else
2958f4a2713aSLionel Sambuc CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS);
2959f4a2713aSLionel Sambuc }
2960f4a2713aSLionel Sambuc
2961f4a2713aSLionel Sambuc // If the result is clearly ignored, return now.
2962f4a2713aSLionel Sambuc if (Ignore)
2963*0a6a1f1dSLionel Sambuc return nullptr;
2964f4a2713aSLionel Sambuc
2965f4a2713aSLionel Sambuc // The result of an assignment in C is the assigned r-value.
2966f4a2713aSLionel Sambuc if (!CGF.getLangOpts().CPlusPlus)
2967f4a2713aSLionel Sambuc return RHS;
2968f4a2713aSLionel Sambuc
2969f4a2713aSLionel Sambuc // If the lvalue is non-volatile, return the computed value of the assignment.
2970f4a2713aSLionel Sambuc if (!LHS.isVolatileQualified())
2971f4a2713aSLionel Sambuc return RHS;
2972f4a2713aSLionel Sambuc
2973f4a2713aSLionel Sambuc // Otherwise, reload the value.
2974f4a2713aSLionel Sambuc return EmitLoadOfLValue(LHS, E->getExprLoc());
2975f4a2713aSLionel Sambuc }
2976f4a2713aSLionel Sambuc
VisitBinLAnd(const BinaryOperator * E)2977f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
2978*0a6a1f1dSLionel Sambuc RegionCounter Cnt = CGF.getPGORegionCounter(E);
2979*0a6a1f1dSLionel Sambuc
2980f4a2713aSLionel Sambuc // Perform vector logical and on comparisons with zero vectors.
2981f4a2713aSLionel Sambuc if (E->getType()->isVectorType()) {
2982*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
2983*0a6a1f1dSLionel Sambuc
2984f4a2713aSLionel Sambuc Value *LHS = Visit(E->getLHS());
2985f4a2713aSLionel Sambuc Value *RHS = Visit(E->getRHS());
2986f4a2713aSLionel Sambuc Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
2987f4a2713aSLionel Sambuc if (LHS->getType()->isFPOrFPVectorTy()) {
2988f4a2713aSLionel Sambuc LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp");
2989f4a2713aSLionel Sambuc RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp");
2990f4a2713aSLionel Sambuc } else {
2991f4a2713aSLionel Sambuc LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
2992f4a2713aSLionel Sambuc RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
2993f4a2713aSLionel Sambuc }
2994f4a2713aSLionel Sambuc Value *And = Builder.CreateAnd(LHS, RHS);
2995f4a2713aSLionel Sambuc return Builder.CreateSExt(And, ConvertType(E->getType()), "sext");
2996f4a2713aSLionel Sambuc }
2997f4a2713aSLionel Sambuc
2998f4a2713aSLionel Sambuc llvm::Type *ResTy = ConvertType(E->getType());
2999f4a2713aSLionel Sambuc
3000f4a2713aSLionel Sambuc // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
3001f4a2713aSLionel Sambuc // If we have 1 && X, just emit X without inserting the control flow.
3002f4a2713aSLionel Sambuc bool LHSCondVal;
3003f4a2713aSLionel Sambuc if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
3004f4a2713aSLionel Sambuc if (LHSCondVal) { // If we have 1 && X, just emit X.
3005*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3006*0a6a1f1dSLionel Sambuc
3007f4a2713aSLionel Sambuc Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
3008f4a2713aSLionel Sambuc // ZExt result to int or bool.
3009f4a2713aSLionel Sambuc return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext");
3010f4a2713aSLionel Sambuc }
3011f4a2713aSLionel Sambuc
3012f4a2713aSLionel Sambuc // 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
3013f4a2713aSLionel Sambuc if (!CGF.ContainsLabel(E->getRHS()))
3014f4a2713aSLionel Sambuc return llvm::Constant::getNullValue(ResTy);
3015f4a2713aSLionel Sambuc }
3016f4a2713aSLionel Sambuc
3017f4a2713aSLionel Sambuc llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
3018f4a2713aSLionel Sambuc llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
3019f4a2713aSLionel Sambuc
3020f4a2713aSLionel Sambuc CodeGenFunction::ConditionalEvaluation eval(CGF);
3021f4a2713aSLionel Sambuc
3022f4a2713aSLionel Sambuc // Branch on the LHS first. If it is false, go to the failure (cont) block.
3023*0a6a1f1dSLionel Sambuc CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock, Cnt.getCount());
3024f4a2713aSLionel Sambuc
3025f4a2713aSLionel Sambuc // Any edges into the ContBlock are now from an (indeterminate number of)
3026f4a2713aSLionel Sambuc // edges from this first condition. All of these values will be false. Start
3027f4a2713aSLionel Sambuc // setting up the PHI node in the Cont Block for this.
3028f4a2713aSLionel Sambuc llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2,
3029f4a2713aSLionel Sambuc "", ContBlock);
3030f4a2713aSLionel Sambuc for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
3031f4a2713aSLionel Sambuc PI != PE; ++PI)
3032f4a2713aSLionel Sambuc PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
3033f4a2713aSLionel Sambuc
3034f4a2713aSLionel Sambuc eval.begin(CGF);
3035f4a2713aSLionel Sambuc CGF.EmitBlock(RHSBlock);
3036*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3037f4a2713aSLionel Sambuc Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
3038f4a2713aSLionel Sambuc eval.end(CGF);
3039f4a2713aSLionel Sambuc
3040f4a2713aSLionel Sambuc // Reaquire the RHS block, as there may be subblocks inserted.
3041f4a2713aSLionel Sambuc RHSBlock = Builder.GetInsertBlock();
3042f4a2713aSLionel Sambuc
3043*0a6a1f1dSLionel Sambuc // Emit an unconditional branch from this block to ContBlock.
3044*0a6a1f1dSLionel Sambuc {
3045f4a2713aSLionel Sambuc // There is no need to emit line number for unconditional branch.
3046*0a6a1f1dSLionel Sambuc ApplyDebugLocation DL(CGF);
3047f4a2713aSLionel Sambuc CGF.EmitBlock(ContBlock);
3048*0a6a1f1dSLionel Sambuc }
3049*0a6a1f1dSLionel Sambuc // Insert an entry into the phi node for the edge with the value of RHSCond.
3050f4a2713aSLionel Sambuc PN->addIncoming(RHSCond, RHSBlock);
3051f4a2713aSLionel Sambuc
3052f4a2713aSLionel Sambuc // ZExt result to int.
3053f4a2713aSLionel Sambuc return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext");
3054f4a2713aSLionel Sambuc }
3055f4a2713aSLionel Sambuc
VisitBinLOr(const BinaryOperator * E)3056f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
3057*0a6a1f1dSLionel Sambuc RegionCounter Cnt = CGF.getPGORegionCounter(E);
3058*0a6a1f1dSLionel Sambuc
3059f4a2713aSLionel Sambuc // Perform vector logical or on comparisons with zero vectors.
3060f4a2713aSLionel Sambuc if (E->getType()->isVectorType()) {
3061*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3062*0a6a1f1dSLionel Sambuc
3063f4a2713aSLionel Sambuc Value *LHS = Visit(E->getLHS());
3064f4a2713aSLionel Sambuc Value *RHS = Visit(E->getRHS());
3065f4a2713aSLionel Sambuc Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
3066f4a2713aSLionel Sambuc if (LHS->getType()->isFPOrFPVectorTy()) {
3067f4a2713aSLionel Sambuc LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp");
3068f4a2713aSLionel Sambuc RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp");
3069f4a2713aSLionel Sambuc } else {
3070f4a2713aSLionel Sambuc LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
3071f4a2713aSLionel Sambuc RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
3072f4a2713aSLionel Sambuc }
3073f4a2713aSLionel Sambuc Value *Or = Builder.CreateOr(LHS, RHS);
3074f4a2713aSLionel Sambuc return Builder.CreateSExt(Or, ConvertType(E->getType()), "sext");
3075f4a2713aSLionel Sambuc }
3076f4a2713aSLionel Sambuc
3077f4a2713aSLionel Sambuc llvm::Type *ResTy = ConvertType(E->getType());
3078f4a2713aSLionel Sambuc
3079f4a2713aSLionel Sambuc // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
3080f4a2713aSLionel Sambuc // If we have 0 || X, just emit X without inserting the control flow.
3081f4a2713aSLionel Sambuc bool LHSCondVal;
3082f4a2713aSLionel Sambuc if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
3083f4a2713aSLionel Sambuc if (!LHSCondVal) { // If we have 0 || X, just emit X.
3084*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3085*0a6a1f1dSLionel Sambuc
3086f4a2713aSLionel Sambuc Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
3087f4a2713aSLionel Sambuc // ZExt result to int or bool.
3088f4a2713aSLionel Sambuc return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext");
3089f4a2713aSLionel Sambuc }
3090f4a2713aSLionel Sambuc
3091f4a2713aSLionel Sambuc // 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
3092f4a2713aSLionel Sambuc if (!CGF.ContainsLabel(E->getRHS()))
3093f4a2713aSLionel Sambuc return llvm::ConstantInt::get(ResTy, 1);
3094f4a2713aSLionel Sambuc }
3095f4a2713aSLionel Sambuc
3096f4a2713aSLionel Sambuc llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
3097f4a2713aSLionel Sambuc llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
3098f4a2713aSLionel Sambuc
3099f4a2713aSLionel Sambuc CodeGenFunction::ConditionalEvaluation eval(CGF);
3100f4a2713aSLionel Sambuc
3101f4a2713aSLionel Sambuc // Branch on the LHS first. If it is true, go to the success (cont) block.
3102*0a6a1f1dSLionel Sambuc CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock,
3103*0a6a1f1dSLionel Sambuc Cnt.getParentCount() - Cnt.getCount());
3104f4a2713aSLionel Sambuc
3105f4a2713aSLionel Sambuc // Any edges into the ContBlock are now from an (indeterminate number of)
3106f4a2713aSLionel Sambuc // edges from this first condition. All of these values will be true. Start
3107f4a2713aSLionel Sambuc // setting up the PHI node in the Cont Block for this.
3108f4a2713aSLionel Sambuc llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2,
3109f4a2713aSLionel Sambuc "", ContBlock);
3110f4a2713aSLionel Sambuc for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
3111f4a2713aSLionel Sambuc PI != PE; ++PI)
3112f4a2713aSLionel Sambuc PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
3113f4a2713aSLionel Sambuc
3114f4a2713aSLionel Sambuc eval.begin(CGF);
3115f4a2713aSLionel Sambuc
3116f4a2713aSLionel Sambuc // Emit the RHS condition as a bool value.
3117f4a2713aSLionel Sambuc CGF.EmitBlock(RHSBlock);
3118*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3119f4a2713aSLionel Sambuc Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
3120f4a2713aSLionel Sambuc
3121f4a2713aSLionel Sambuc eval.end(CGF);
3122f4a2713aSLionel Sambuc
3123f4a2713aSLionel Sambuc // Reaquire the RHS block, as there may be subblocks inserted.
3124f4a2713aSLionel Sambuc RHSBlock = Builder.GetInsertBlock();
3125f4a2713aSLionel Sambuc
3126f4a2713aSLionel Sambuc // Emit an unconditional branch from this block to ContBlock. Insert an entry
3127f4a2713aSLionel Sambuc // into the phi node for the edge with the value of RHSCond.
3128f4a2713aSLionel Sambuc CGF.EmitBlock(ContBlock);
3129f4a2713aSLionel Sambuc PN->addIncoming(RHSCond, RHSBlock);
3130f4a2713aSLionel Sambuc
3131f4a2713aSLionel Sambuc // ZExt result to int.
3132f4a2713aSLionel Sambuc return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext");
3133f4a2713aSLionel Sambuc }
3134f4a2713aSLionel Sambuc
VisitBinComma(const BinaryOperator * E)3135f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
3136f4a2713aSLionel Sambuc CGF.EmitIgnoredExpr(E->getLHS());
3137f4a2713aSLionel Sambuc CGF.EnsureInsertPoint();
3138f4a2713aSLionel Sambuc return Visit(E->getRHS());
3139f4a2713aSLionel Sambuc }
3140f4a2713aSLionel Sambuc
3141f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3142f4a2713aSLionel Sambuc // Other Operators
3143f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3144f4a2713aSLionel Sambuc
3145f4a2713aSLionel Sambuc /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
3146f4a2713aSLionel Sambuc /// expression is cheap enough and side-effect-free enough to evaluate
3147f4a2713aSLionel Sambuc /// unconditionally instead of conditionally. This is used to convert control
3148f4a2713aSLionel Sambuc /// flow into selects in some cases.
isCheapEnoughToEvaluateUnconditionally(const Expr * E,CodeGenFunction & CGF)3149f4a2713aSLionel Sambuc static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E,
3150f4a2713aSLionel Sambuc CodeGenFunction &CGF) {
3151f4a2713aSLionel Sambuc // Anything that is an integer or floating point constant is fine.
3152f4a2713aSLionel Sambuc return E->IgnoreParens()->isEvaluatable(CGF.getContext());
3153f4a2713aSLionel Sambuc
3154f4a2713aSLionel Sambuc // Even non-volatile automatic variables can't be evaluated unconditionally.
3155f4a2713aSLionel Sambuc // Referencing a thread_local may cause non-trivial initialization work to
3156f4a2713aSLionel Sambuc // occur. If we're inside a lambda and one of the variables is from the scope
3157f4a2713aSLionel Sambuc // outside the lambda, that function may have returned already. Reading its
3158f4a2713aSLionel Sambuc // locals is a bad idea. Also, these reads may introduce races there didn't
3159f4a2713aSLionel Sambuc // exist in the source-level program.
3160f4a2713aSLionel Sambuc }
3161f4a2713aSLionel Sambuc
3162f4a2713aSLionel Sambuc
3163f4a2713aSLionel Sambuc Value *ScalarExprEmitter::
VisitAbstractConditionalOperator(const AbstractConditionalOperator * E)3164f4a2713aSLionel Sambuc VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
3165f4a2713aSLionel Sambuc TestAndClearIgnoreResultAssign();
3166f4a2713aSLionel Sambuc
3167f4a2713aSLionel Sambuc // Bind the common expression if necessary.
3168f4a2713aSLionel Sambuc CodeGenFunction::OpaqueValueMapping binding(CGF, E);
3169*0a6a1f1dSLionel Sambuc RegionCounter Cnt = CGF.getPGORegionCounter(E);
3170f4a2713aSLionel Sambuc
3171f4a2713aSLionel Sambuc Expr *condExpr = E->getCond();
3172f4a2713aSLionel Sambuc Expr *lhsExpr = E->getTrueExpr();
3173f4a2713aSLionel Sambuc Expr *rhsExpr = E->getFalseExpr();
3174f4a2713aSLionel Sambuc
3175f4a2713aSLionel Sambuc // If the condition constant folds and can be elided, try to avoid emitting
3176f4a2713aSLionel Sambuc // the condition and the dead arm.
3177f4a2713aSLionel Sambuc bool CondExprBool;
3178f4a2713aSLionel Sambuc if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
3179f4a2713aSLionel Sambuc Expr *live = lhsExpr, *dead = rhsExpr;
3180f4a2713aSLionel Sambuc if (!CondExprBool) std::swap(live, dead);
3181f4a2713aSLionel Sambuc
3182f4a2713aSLionel Sambuc // If the dead side doesn't have labels we need, just emit the Live part.
3183f4a2713aSLionel Sambuc if (!CGF.ContainsLabel(dead)) {
3184*0a6a1f1dSLionel Sambuc if (CondExprBool)
3185*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3186f4a2713aSLionel Sambuc Value *Result = Visit(live);
3187f4a2713aSLionel Sambuc
3188f4a2713aSLionel Sambuc // If the live part is a throw expression, it acts like it has a void
3189f4a2713aSLionel Sambuc // type, so evaluating it returns a null Value*. However, a conditional
3190f4a2713aSLionel Sambuc // with non-void type must return a non-null Value*.
3191f4a2713aSLionel Sambuc if (!Result && !E->getType()->isVoidType())
3192f4a2713aSLionel Sambuc Result = llvm::UndefValue::get(CGF.ConvertType(E->getType()));
3193f4a2713aSLionel Sambuc
3194f4a2713aSLionel Sambuc return Result;
3195f4a2713aSLionel Sambuc }
3196f4a2713aSLionel Sambuc }
3197f4a2713aSLionel Sambuc
3198f4a2713aSLionel Sambuc // OpenCL: If the condition is a vector, we can treat this condition like
3199f4a2713aSLionel Sambuc // the select function.
3200f4a2713aSLionel Sambuc if (CGF.getLangOpts().OpenCL
3201f4a2713aSLionel Sambuc && condExpr->getType()->isVectorType()) {
3202*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3203*0a6a1f1dSLionel Sambuc
3204f4a2713aSLionel Sambuc llvm::Value *CondV = CGF.EmitScalarExpr(condExpr);
3205f4a2713aSLionel Sambuc llvm::Value *LHS = Visit(lhsExpr);
3206f4a2713aSLionel Sambuc llvm::Value *RHS = Visit(rhsExpr);
3207f4a2713aSLionel Sambuc
3208f4a2713aSLionel Sambuc llvm::Type *condType = ConvertType(condExpr->getType());
3209f4a2713aSLionel Sambuc llvm::VectorType *vecTy = cast<llvm::VectorType>(condType);
3210f4a2713aSLionel Sambuc
3211f4a2713aSLionel Sambuc unsigned numElem = vecTy->getNumElements();
3212f4a2713aSLionel Sambuc llvm::Type *elemType = vecTy->getElementType();
3213f4a2713aSLionel Sambuc
3214f4a2713aSLionel Sambuc llvm::Value *zeroVec = llvm::Constant::getNullValue(vecTy);
3215f4a2713aSLionel Sambuc llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec);
3216f4a2713aSLionel Sambuc llvm::Value *tmp = Builder.CreateSExt(TestMSB,
3217f4a2713aSLionel Sambuc llvm::VectorType::get(elemType,
3218f4a2713aSLionel Sambuc numElem),
3219f4a2713aSLionel Sambuc "sext");
3220f4a2713aSLionel Sambuc llvm::Value *tmp2 = Builder.CreateNot(tmp);
3221f4a2713aSLionel Sambuc
3222f4a2713aSLionel Sambuc // Cast float to int to perform ANDs if necessary.
3223f4a2713aSLionel Sambuc llvm::Value *RHSTmp = RHS;
3224f4a2713aSLionel Sambuc llvm::Value *LHSTmp = LHS;
3225f4a2713aSLionel Sambuc bool wasCast = false;
3226f4a2713aSLionel Sambuc llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType());
3227f4a2713aSLionel Sambuc if (rhsVTy->getElementType()->isFloatingPointTy()) {
3228f4a2713aSLionel Sambuc RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType());
3229f4a2713aSLionel Sambuc LHSTmp = Builder.CreateBitCast(LHS, tmp->getType());
3230f4a2713aSLionel Sambuc wasCast = true;
3231f4a2713aSLionel Sambuc }
3232f4a2713aSLionel Sambuc
3233f4a2713aSLionel Sambuc llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2);
3234f4a2713aSLionel Sambuc llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp);
3235f4a2713aSLionel Sambuc llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond");
3236f4a2713aSLionel Sambuc if (wasCast)
3237f4a2713aSLionel Sambuc tmp5 = Builder.CreateBitCast(tmp5, RHS->getType());
3238f4a2713aSLionel Sambuc
3239f4a2713aSLionel Sambuc return tmp5;
3240f4a2713aSLionel Sambuc }
3241f4a2713aSLionel Sambuc
3242f4a2713aSLionel Sambuc // If this is a really simple expression (like x ? 4 : 5), emit this as a
3243f4a2713aSLionel Sambuc // select instead of as control flow. We can only do this if it is cheap and
3244f4a2713aSLionel Sambuc // safe to evaluate the LHS and RHS unconditionally.
3245f4a2713aSLionel Sambuc if (isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF) &&
3246f4a2713aSLionel Sambuc isCheapEnoughToEvaluateUnconditionally(rhsExpr, CGF)) {
3247*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3248*0a6a1f1dSLionel Sambuc
3249f4a2713aSLionel Sambuc llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr);
3250f4a2713aSLionel Sambuc llvm::Value *LHS = Visit(lhsExpr);
3251f4a2713aSLionel Sambuc llvm::Value *RHS = Visit(rhsExpr);
3252f4a2713aSLionel Sambuc if (!LHS) {
3253f4a2713aSLionel Sambuc // If the conditional has void type, make sure we return a null Value*.
3254f4a2713aSLionel Sambuc assert(!RHS && "LHS and RHS types must match");
3255*0a6a1f1dSLionel Sambuc return nullptr;
3256f4a2713aSLionel Sambuc }
3257f4a2713aSLionel Sambuc return Builder.CreateSelect(CondV, LHS, RHS, "cond");
3258f4a2713aSLionel Sambuc }
3259f4a2713aSLionel Sambuc
3260f4a2713aSLionel Sambuc llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
3261f4a2713aSLionel Sambuc llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
3262f4a2713aSLionel Sambuc llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
3263f4a2713aSLionel Sambuc
3264f4a2713aSLionel Sambuc CodeGenFunction::ConditionalEvaluation eval(CGF);
3265*0a6a1f1dSLionel Sambuc CGF.EmitBranchOnBoolExpr(condExpr, LHSBlock, RHSBlock, Cnt.getCount());
3266f4a2713aSLionel Sambuc
3267f4a2713aSLionel Sambuc CGF.EmitBlock(LHSBlock);
3268*0a6a1f1dSLionel Sambuc Cnt.beginRegion(Builder);
3269f4a2713aSLionel Sambuc eval.begin(CGF);
3270f4a2713aSLionel Sambuc Value *LHS = Visit(lhsExpr);
3271f4a2713aSLionel Sambuc eval.end(CGF);
3272f4a2713aSLionel Sambuc
3273f4a2713aSLionel Sambuc LHSBlock = Builder.GetInsertBlock();
3274f4a2713aSLionel Sambuc Builder.CreateBr(ContBlock);
3275f4a2713aSLionel Sambuc
3276f4a2713aSLionel Sambuc CGF.EmitBlock(RHSBlock);
3277f4a2713aSLionel Sambuc eval.begin(CGF);
3278f4a2713aSLionel Sambuc Value *RHS = Visit(rhsExpr);
3279f4a2713aSLionel Sambuc eval.end(CGF);
3280f4a2713aSLionel Sambuc
3281f4a2713aSLionel Sambuc RHSBlock = Builder.GetInsertBlock();
3282f4a2713aSLionel Sambuc CGF.EmitBlock(ContBlock);
3283f4a2713aSLionel Sambuc
3284f4a2713aSLionel Sambuc // If the LHS or RHS is a throw expression, it will be legitimately null.
3285f4a2713aSLionel Sambuc if (!LHS)
3286f4a2713aSLionel Sambuc return RHS;
3287f4a2713aSLionel Sambuc if (!RHS)
3288f4a2713aSLionel Sambuc return LHS;
3289f4a2713aSLionel Sambuc
3290f4a2713aSLionel Sambuc // Create a PHI node for the real part.
3291f4a2713aSLionel Sambuc llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), 2, "cond");
3292f4a2713aSLionel Sambuc PN->addIncoming(LHS, LHSBlock);
3293f4a2713aSLionel Sambuc PN->addIncoming(RHS, RHSBlock);
3294f4a2713aSLionel Sambuc return PN;
3295f4a2713aSLionel Sambuc }
3296f4a2713aSLionel Sambuc
VisitChooseExpr(ChooseExpr * E)3297f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
3298f4a2713aSLionel Sambuc return Visit(E->getChosenSubExpr());
3299f4a2713aSLionel Sambuc }
3300f4a2713aSLionel Sambuc
VisitVAArgExpr(VAArgExpr * VE)3301f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
3302*0a6a1f1dSLionel Sambuc QualType Ty = VE->getType();
3303*0a6a1f1dSLionel Sambuc
3304*0a6a1f1dSLionel Sambuc if (Ty->isVariablyModifiedType())
3305*0a6a1f1dSLionel Sambuc CGF.EmitVariablyModifiedType(Ty);
3306*0a6a1f1dSLionel Sambuc
3307f4a2713aSLionel Sambuc llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
3308f4a2713aSLionel Sambuc llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
3309*0a6a1f1dSLionel Sambuc llvm::Type *ArgTy = ConvertType(VE->getType());
3310f4a2713aSLionel Sambuc
3311f4a2713aSLionel Sambuc // If EmitVAArg fails, we fall back to the LLVM instruction.
3312f4a2713aSLionel Sambuc if (!ArgPtr)
3313*0a6a1f1dSLionel Sambuc return Builder.CreateVAArg(ArgValue, ArgTy);
3314f4a2713aSLionel Sambuc
3315f4a2713aSLionel Sambuc // FIXME Volatility.
3316*0a6a1f1dSLionel Sambuc llvm::Value *Val = Builder.CreateLoad(ArgPtr);
3317*0a6a1f1dSLionel Sambuc
3318*0a6a1f1dSLionel Sambuc // If EmitVAArg promoted the type, we must truncate it.
3319*0a6a1f1dSLionel Sambuc if (ArgTy != Val->getType()) {
3320*0a6a1f1dSLionel Sambuc if (ArgTy->isPointerTy() && !Val->getType()->isPointerTy())
3321*0a6a1f1dSLionel Sambuc Val = Builder.CreateIntToPtr(Val, ArgTy);
3322*0a6a1f1dSLionel Sambuc else
3323*0a6a1f1dSLionel Sambuc Val = Builder.CreateTrunc(Val, ArgTy);
3324*0a6a1f1dSLionel Sambuc }
3325*0a6a1f1dSLionel Sambuc
3326*0a6a1f1dSLionel Sambuc return Val;
3327f4a2713aSLionel Sambuc }
3328f4a2713aSLionel Sambuc
VisitBlockExpr(const BlockExpr * block)3329f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *block) {
3330f4a2713aSLionel Sambuc return CGF.EmitBlockLiteral(block);
3331f4a2713aSLionel Sambuc }
3332f4a2713aSLionel Sambuc
VisitAsTypeExpr(AsTypeExpr * E)3333f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitAsTypeExpr(AsTypeExpr *E) {
3334f4a2713aSLionel Sambuc Value *Src = CGF.EmitScalarExpr(E->getSrcExpr());
3335f4a2713aSLionel Sambuc llvm::Type *DstTy = ConvertType(E->getType());
3336f4a2713aSLionel Sambuc
3337f4a2713aSLionel Sambuc // Going from vec4->vec3 or vec3->vec4 is a special case and requires
3338f4a2713aSLionel Sambuc // a shuffle vector instead of a bitcast.
3339f4a2713aSLionel Sambuc llvm::Type *SrcTy = Src->getType();
3340f4a2713aSLionel Sambuc if (isa<llvm::VectorType>(DstTy) && isa<llvm::VectorType>(SrcTy)) {
3341f4a2713aSLionel Sambuc unsigned numElementsDst = cast<llvm::VectorType>(DstTy)->getNumElements();
3342f4a2713aSLionel Sambuc unsigned numElementsSrc = cast<llvm::VectorType>(SrcTy)->getNumElements();
3343f4a2713aSLionel Sambuc if ((numElementsDst == 3 && numElementsSrc == 4)
3344f4a2713aSLionel Sambuc || (numElementsDst == 4 && numElementsSrc == 3)) {
3345f4a2713aSLionel Sambuc
3346f4a2713aSLionel Sambuc
3347f4a2713aSLionel Sambuc // In the case of going from int4->float3, a bitcast is needed before
3348f4a2713aSLionel Sambuc // doing a shuffle.
3349f4a2713aSLionel Sambuc llvm::Type *srcElemTy =
3350f4a2713aSLionel Sambuc cast<llvm::VectorType>(SrcTy)->getElementType();
3351f4a2713aSLionel Sambuc llvm::Type *dstElemTy =
3352f4a2713aSLionel Sambuc cast<llvm::VectorType>(DstTy)->getElementType();
3353f4a2713aSLionel Sambuc
3354f4a2713aSLionel Sambuc if ((srcElemTy->isIntegerTy() && dstElemTy->isFloatTy())
3355f4a2713aSLionel Sambuc || (srcElemTy->isFloatTy() && dstElemTy->isIntegerTy())) {
3356f4a2713aSLionel Sambuc // Create a float type of the same size as the source or destination.
3357f4a2713aSLionel Sambuc llvm::VectorType *newSrcTy = llvm::VectorType::get(dstElemTy,
3358f4a2713aSLionel Sambuc numElementsSrc);
3359f4a2713aSLionel Sambuc
3360f4a2713aSLionel Sambuc Src = Builder.CreateBitCast(Src, newSrcTy, "astypeCast");
3361f4a2713aSLionel Sambuc }
3362f4a2713aSLionel Sambuc
3363f4a2713aSLionel Sambuc llvm::Value *UnV = llvm::UndefValue::get(Src->getType());
3364f4a2713aSLionel Sambuc
3365f4a2713aSLionel Sambuc SmallVector<llvm::Constant*, 3> Args;
3366f4a2713aSLionel Sambuc Args.push_back(Builder.getInt32(0));
3367f4a2713aSLionel Sambuc Args.push_back(Builder.getInt32(1));
3368f4a2713aSLionel Sambuc Args.push_back(Builder.getInt32(2));
3369f4a2713aSLionel Sambuc
3370f4a2713aSLionel Sambuc if (numElementsDst == 4)
3371f4a2713aSLionel Sambuc Args.push_back(llvm::UndefValue::get(CGF.Int32Ty));
3372f4a2713aSLionel Sambuc
3373f4a2713aSLionel Sambuc llvm::Constant *Mask = llvm::ConstantVector::get(Args);
3374f4a2713aSLionel Sambuc
3375f4a2713aSLionel Sambuc return Builder.CreateShuffleVector(Src, UnV, Mask, "astype");
3376f4a2713aSLionel Sambuc }
3377f4a2713aSLionel Sambuc }
3378f4a2713aSLionel Sambuc
3379f4a2713aSLionel Sambuc return Builder.CreateBitCast(Src, DstTy, "astype");
3380f4a2713aSLionel Sambuc }
3381f4a2713aSLionel Sambuc
VisitAtomicExpr(AtomicExpr * E)3382f4a2713aSLionel Sambuc Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) {
3383f4a2713aSLionel Sambuc return CGF.EmitAtomicExpr(E).getScalarVal();
3384f4a2713aSLionel Sambuc }
3385f4a2713aSLionel Sambuc
3386f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3387f4a2713aSLionel Sambuc // Entry Point into this File
3388f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3389f4a2713aSLionel Sambuc
3390f4a2713aSLionel Sambuc /// EmitScalarExpr - Emit the computation of the specified expression of scalar
3391f4a2713aSLionel Sambuc /// type, ignoring the result.
EmitScalarExpr(const Expr * E,bool IgnoreResultAssign)3392f4a2713aSLionel Sambuc Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
3393f4a2713aSLionel Sambuc assert(E && hasScalarEvaluationKind(E->getType()) &&
3394f4a2713aSLionel Sambuc "Invalid scalar expression to emit");
3395f4a2713aSLionel Sambuc
3396*0a6a1f1dSLionel Sambuc bool hasDebugInfo = getDebugInfo();
3397f4a2713aSLionel Sambuc if (isa<CXXDefaultArgExpr>(E))
3398f4a2713aSLionel Sambuc disableDebugInfo();
3399f4a2713aSLionel Sambuc Value *V = ScalarExprEmitter(*this, IgnoreResultAssign)
3400f4a2713aSLionel Sambuc .Visit(const_cast<Expr*>(E));
3401*0a6a1f1dSLionel Sambuc if (isa<CXXDefaultArgExpr>(E) && hasDebugInfo)
3402f4a2713aSLionel Sambuc enableDebugInfo();
3403f4a2713aSLionel Sambuc return V;
3404f4a2713aSLionel Sambuc }
3405f4a2713aSLionel Sambuc
3406f4a2713aSLionel Sambuc /// EmitScalarConversion - Emit a conversion from the specified type to the
3407f4a2713aSLionel Sambuc /// specified destination type, both of which are LLVM scalar types.
EmitScalarConversion(Value * Src,QualType SrcTy,QualType DstTy)3408f4a2713aSLionel Sambuc Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
3409f4a2713aSLionel Sambuc QualType DstTy) {
3410f4a2713aSLionel Sambuc assert(hasScalarEvaluationKind(SrcTy) && hasScalarEvaluationKind(DstTy) &&
3411f4a2713aSLionel Sambuc "Invalid scalar expression to emit");
3412f4a2713aSLionel Sambuc return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
3413f4a2713aSLionel Sambuc }
3414f4a2713aSLionel Sambuc
3415f4a2713aSLionel Sambuc /// EmitComplexToScalarConversion - Emit a conversion from the specified complex
3416f4a2713aSLionel Sambuc /// type to the specified destination type, where the destination type is an
3417f4a2713aSLionel Sambuc /// LLVM scalar type.
EmitComplexToScalarConversion(ComplexPairTy Src,QualType SrcTy,QualType DstTy)3418f4a2713aSLionel Sambuc Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
3419f4a2713aSLionel Sambuc QualType SrcTy,
3420f4a2713aSLionel Sambuc QualType DstTy) {
3421f4a2713aSLionel Sambuc assert(SrcTy->isAnyComplexType() && hasScalarEvaluationKind(DstTy) &&
3422f4a2713aSLionel Sambuc "Invalid complex -> scalar conversion");
3423f4a2713aSLionel Sambuc return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
3424f4a2713aSLionel Sambuc DstTy);
3425f4a2713aSLionel Sambuc }
3426f4a2713aSLionel Sambuc
3427f4a2713aSLionel Sambuc
3428f4a2713aSLionel Sambuc llvm::Value *CodeGenFunction::
EmitScalarPrePostIncDec(const UnaryOperator * E,LValue LV,bool isInc,bool isPre)3429f4a2713aSLionel Sambuc EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
3430f4a2713aSLionel Sambuc bool isInc, bool isPre) {
3431f4a2713aSLionel Sambuc return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre);
3432f4a2713aSLionel Sambuc }
3433f4a2713aSLionel Sambuc
EmitObjCIsaExpr(const ObjCIsaExpr * E)3434f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) {
3435f4a2713aSLionel Sambuc llvm::Value *V;
3436f4a2713aSLionel Sambuc // object->isa or (*object).isa
3437f4a2713aSLionel Sambuc // Generate code as for: *(Class*)object
3438f4a2713aSLionel Sambuc // build Class* type
3439f4a2713aSLionel Sambuc llvm::Type *ClassPtrTy = ConvertType(E->getType());
3440f4a2713aSLionel Sambuc
3441f4a2713aSLionel Sambuc Expr *BaseExpr = E->getBase();
3442f4a2713aSLionel Sambuc if (BaseExpr->isRValue()) {
3443f4a2713aSLionel Sambuc V = CreateMemTemp(E->getType(), "resval");
3444f4a2713aSLionel Sambuc llvm::Value *Src = EmitScalarExpr(BaseExpr);
3445f4a2713aSLionel Sambuc Builder.CreateStore(Src, V);
3446f4a2713aSLionel Sambuc V = ScalarExprEmitter(*this).EmitLoadOfLValue(
3447f4a2713aSLionel Sambuc MakeNaturalAlignAddrLValue(V, E->getType()), E->getExprLoc());
3448f4a2713aSLionel Sambuc } else {
3449f4a2713aSLionel Sambuc if (E->isArrow())
3450f4a2713aSLionel Sambuc V = ScalarExprEmitter(*this).EmitLoadOfLValue(BaseExpr);
3451f4a2713aSLionel Sambuc else
3452f4a2713aSLionel Sambuc V = EmitLValue(BaseExpr).getAddress();
3453f4a2713aSLionel Sambuc }
3454f4a2713aSLionel Sambuc
3455f4a2713aSLionel Sambuc // build Class* type
3456f4a2713aSLionel Sambuc ClassPtrTy = ClassPtrTy->getPointerTo();
3457f4a2713aSLionel Sambuc V = Builder.CreateBitCast(V, ClassPtrTy);
3458f4a2713aSLionel Sambuc return MakeNaturalAlignAddrLValue(V, E->getType());
3459f4a2713aSLionel Sambuc }
3460f4a2713aSLionel Sambuc
3461f4a2713aSLionel Sambuc
EmitCompoundAssignmentLValue(const CompoundAssignOperator * E)3462f4a2713aSLionel Sambuc LValue CodeGenFunction::EmitCompoundAssignmentLValue(
3463f4a2713aSLionel Sambuc const CompoundAssignOperator *E) {
3464f4a2713aSLionel Sambuc ScalarExprEmitter Scalar(*this);
3465*0a6a1f1dSLionel Sambuc Value *Result = nullptr;
3466f4a2713aSLionel Sambuc switch (E->getOpcode()) {
3467f4a2713aSLionel Sambuc #define COMPOUND_OP(Op) \
3468f4a2713aSLionel Sambuc case BO_##Op##Assign: \
3469f4a2713aSLionel Sambuc return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \
3470f4a2713aSLionel Sambuc Result)
3471f4a2713aSLionel Sambuc COMPOUND_OP(Mul);
3472f4a2713aSLionel Sambuc COMPOUND_OP(Div);
3473f4a2713aSLionel Sambuc COMPOUND_OP(Rem);
3474f4a2713aSLionel Sambuc COMPOUND_OP(Add);
3475f4a2713aSLionel Sambuc COMPOUND_OP(Sub);
3476f4a2713aSLionel Sambuc COMPOUND_OP(Shl);
3477f4a2713aSLionel Sambuc COMPOUND_OP(Shr);
3478f4a2713aSLionel Sambuc COMPOUND_OP(And);
3479f4a2713aSLionel Sambuc COMPOUND_OP(Xor);
3480f4a2713aSLionel Sambuc COMPOUND_OP(Or);
3481f4a2713aSLionel Sambuc #undef COMPOUND_OP
3482f4a2713aSLionel Sambuc
3483f4a2713aSLionel Sambuc case BO_PtrMemD:
3484f4a2713aSLionel Sambuc case BO_PtrMemI:
3485f4a2713aSLionel Sambuc case BO_Mul:
3486f4a2713aSLionel Sambuc case BO_Div:
3487f4a2713aSLionel Sambuc case BO_Rem:
3488f4a2713aSLionel Sambuc case BO_Add:
3489f4a2713aSLionel Sambuc case BO_Sub:
3490f4a2713aSLionel Sambuc case BO_Shl:
3491f4a2713aSLionel Sambuc case BO_Shr:
3492f4a2713aSLionel Sambuc case BO_LT:
3493f4a2713aSLionel Sambuc case BO_GT:
3494f4a2713aSLionel Sambuc case BO_LE:
3495f4a2713aSLionel Sambuc case BO_GE:
3496f4a2713aSLionel Sambuc case BO_EQ:
3497f4a2713aSLionel Sambuc case BO_NE:
3498f4a2713aSLionel Sambuc case BO_And:
3499f4a2713aSLionel Sambuc case BO_Xor:
3500f4a2713aSLionel Sambuc case BO_Or:
3501f4a2713aSLionel Sambuc case BO_LAnd:
3502f4a2713aSLionel Sambuc case BO_LOr:
3503f4a2713aSLionel Sambuc case BO_Assign:
3504f4a2713aSLionel Sambuc case BO_Comma:
3505f4a2713aSLionel Sambuc llvm_unreachable("Not valid compound assignment operators");
3506f4a2713aSLionel Sambuc }
3507f4a2713aSLionel Sambuc
3508f4a2713aSLionel Sambuc llvm_unreachable("Unhandled compound assignment operator");
3509f4a2713aSLionel Sambuc }
3510