e98ec3e35e378f8042e8ba12269e776bc409b486
[lldb.git] / clang / lib / CodeGen / CGBuiltin.cpp
1 //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code to emit Builtin calls as LLVM code.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "CGCXXABI.h"
14 #include "CGObjCRuntime.h"
15 #include "CGOpenCLRuntime.h"
16 #include "CGRecordLayout.h"
17 #include "CodeGenFunction.h"
18 #include "CodeGenModule.h"
19 #include "ConstantEmitter.h"
20 #include "PatternInit.h"
21 #include "TargetInfo.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Attr.h"
24 #include "clang/AST/Decl.h"
25 #include "clang/AST/OSLog.h"
26 #include "clang/Basic/TargetBuiltins.h"
27 #include "clang/Basic/TargetInfo.h"
28 #include "clang/CodeGen/CGFunctionInfo.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Analysis/ValueTracking.h"
32 #include "llvm/IR/DataLayout.h"
33 #include "llvm/IR/InlineAsm.h"
34 #include "llvm/IR/Intrinsics.h"
35 #include "llvm/IR/IntrinsicsAArch64.h"
36 #include "llvm/IR/IntrinsicsAMDGPU.h"
37 #include "llvm/IR/IntrinsicsARM.h"
38 #include "llvm/IR/IntrinsicsBPF.h"
39 #include "llvm/IR/IntrinsicsHexagon.h"
40 #include "llvm/IR/IntrinsicsNVPTX.h"
41 #include "llvm/IR/IntrinsicsPowerPC.h"
42 #include "llvm/IR/IntrinsicsR600.h"
43 #include "llvm/IR/IntrinsicsS390.h"
44 #include "llvm/IR/IntrinsicsWebAssembly.h"
45 #include "llvm/IR/IntrinsicsX86.h"
46 #include "llvm/IR/MDBuilder.h"
47 #include "llvm/IR/MatrixBuilder.h"
48 #include "llvm/Support/ConvertUTF.h"
49 #include "llvm/Support/ScopedPrinter.h"
50 #include "llvm/Support/X86TargetParser.h"
51 #include <sstream>
52
53 using namespace clang;
54 using namespace CodeGen;
55 using namespace llvm;
56
57 static
58 int64_t clamp(int64_t Value, int64_t Low, int64_t High) {
59   return std::min(High, std::max(Low, Value));
60 }
61
62 static void initializeAlloca(CodeGenFunction &CGF, AllocaInst *AI, Value *Size,
63                              Align AlignmentInBytes) {
64   ConstantInt *Byte;
65   switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
66   case LangOptions::TrivialAutoVarInitKind::Uninitialized:
67     // Nothing to initialize.
68     return;
69   case LangOptions::TrivialAutoVarInitKind::Zero:
70     Byte = CGF.Builder.getInt8(0x00);
71     break;
72   case LangOptions::TrivialAutoVarInitKind::Pattern: {
73     llvm::Type *Int8 = llvm::IntegerType::getInt8Ty(CGF.CGM.getLLVMContext());
74     Byte = llvm::dyn_cast<llvm::ConstantInt>(
75         initializationPatternFor(CGF.CGM, Int8));
76     break;
77   }
78   }
79   if (CGF.CGM.stopAutoInit())
80     return;
81   auto *I = CGF.Builder.CreateMemSet(AI, Byte, Size, AlignmentInBytes);
82   I->addAnnotationMetadata("auto-init");
83 }
84
85 /// getBuiltinLibFunction - Given a builtin id for a function like
86 /// "__builtin_fabsf", return a Function* for "fabsf".
87 llvm::Constant *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
88                                                      unsigned BuiltinID) {
89   assert(Context.BuiltinInfo.isLibFunction(BuiltinID));
90
91   // Get the name, skip over the __builtin_ prefix (if necessary).
92   StringRef Name;
93   GlobalDecl D(FD);
94
95   // If the builtin has been declared explicitly with an assembler label,
96   // use the mangled name. This differs from the plain label on platforms
97   // that prefix labels.
98   if (FD->hasAttr<AsmLabelAttr>())
99     Name = getMangledName(D);
100   else
101     Name = Context.BuiltinInfo.getName(BuiltinID) + 10;
102
103   llvm::FunctionType *Ty =
104     cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType()));
105
106   return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false);
107 }
108
109 /// Emit the conversions required to turn the given value into an
110 /// integer of the given size.
111 static Value *EmitToInt(CodeGenFunction &CGF, llvm::Value *V,
112                         QualType T, llvm::IntegerType *IntType) {
113   V = CGF.EmitToMemory(V, T);
114
115   if (V->getType()->isPointerTy())
116     return CGF.Builder.CreatePtrToInt(V, IntType);
117
118   assert(V->getType() == IntType);
119   return V;
120 }
121
122 static Value *EmitFromInt(CodeGenFunction &CGF, llvm::Value *V,
123                           QualType T, llvm::Type *ResultType) {
124   V = CGF.EmitFromMemory(V, T);
125
126   if (ResultType->isPointerTy())
127     return CGF.Builder.CreateIntToPtr(V, ResultType);
128
129   assert(V->getType() == ResultType);
130   return V;
131 }
132
133 /// Utility to insert an atomic instruction based on Intrinsic::ID
134 /// and the expression node.
135 static Value *MakeBinaryAtomicValue(
136     CodeGenFunction &CGF, llvm::AtomicRMWInst::BinOp Kind, const CallExpr *E,
137     AtomicOrdering Ordering = AtomicOrdering::SequentiallyConsistent) {
138   QualType T = E->getType();
139   assert(E->getArg(0)->getType()->isPointerType());
140   assert(CGF.getContext().hasSameUnqualifiedType(T,
141                                   E->getArg(0)->getType()->getPointeeType()));
142   assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
143
144   llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
145   unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
146
147   llvm::IntegerType *IntType =
148     llvm::IntegerType::get(CGF.getLLVMContext(),
149                            CGF.getContext().getTypeSize(T));
150   llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
151
152   llvm::Value *Args[2];
153   Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
154   Args[1] = CGF.EmitScalarExpr(E->getArg(1));
155   llvm::Type *ValueType = Args[1]->getType();
156   Args[1] = EmitToInt(CGF, Args[1], T, IntType);
157
158   llvm::Value *Result = CGF.Builder.CreateAtomicRMW(
159       Kind, Args[0], Args[1], Ordering);
160   return EmitFromInt(CGF, Result, T, ValueType);
161 }
162
163 static Value *EmitNontemporalStore(CodeGenFunction &CGF, const CallExpr *E) {
164   Value *Val = CGF.EmitScalarExpr(E->getArg(0));
165   Value *Address = CGF.EmitScalarExpr(E->getArg(1));
166
167   // Convert the type of the pointer to a pointer to the stored type.
168   Val = CGF.EmitToMemory(Val, E->getArg(0)->getType());
169   Value *BC = CGF.Builder.CreateBitCast(
170       Address, llvm::PointerType::getUnqual(Val->getType()), "cast");
171   LValue LV = CGF.MakeNaturalAlignAddrLValue(BC, E->getArg(0)->getType());
172   LV.setNontemporal(true);
173   CGF.EmitStoreOfScalar(Val, LV, false);
174   return nullptr;
175 }
176
177 static Value *EmitNontemporalLoad(CodeGenFunction &CGF, const CallExpr *E) {
178   Value *Address = CGF.EmitScalarExpr(E->getArg(0));
179
180   LValue LV = CGF.MakeNaturalAlignAddrLValue(Address, E->getType());
181   LV.setNontemporal(true);
182   return CGF.EmitLoadOfScalar(LV, E->getExprLoc());
183 }
184
185 static RValue EmitBinaryAtomic(CodeGenFunction &CGF,
186                                llvm::AtomicRMWInst::BinOp Kind,
187                                const CallExpr *E) {
188   return RValue::get(MakeBinaryAtomicValue(CGF, Kind, E));
189 }
190
191 /// Utility to insert an atomic instruction based Intrinsic::ID and
192 /// the expression node, where the return value is the result of the
193 /// operation.
194 static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF,
195                                    llvm::AtomicRMWInst::BinOp Kind,
196                                    const CallExpr *E,
197                                    Instruction::BinaryOps Op,
198                                    bool Invert = false) {
199   QualType T = E->getType();
200   assert(E->getArg(0)->getType()->isPointerType());
201   assert(CGF.getContext().hasSameUnqualifiedType(T,
202                                   E->getArg(0)->getType()->getPointeeType()));
203   assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
204
205   llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
206   unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
207
208   llvm::IntegerType *IntType =
209     llvm::IntegerType::get(CGF.getLLVMContext(),
210                            CGF.getContext().getTypeSize(T));
211   llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
212
213   llvm::Value *Args[2];
214   Args[1] = CGF.EmitScalarExpr(E->getArg(1));
215   llvm::Type *ValueType = Args[1]->getType();
216   Args[1] = EmitToInt(CGF, Args[1], T, IntType);
217   Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
218
219   llvm::Value *Result = CGF.Builder.CreateAtomicRMW(
220       Kind, Args[0], Args[1], llvm::AtomicOrdering::SequentiallyConsistent);
221   Result = CGF.Builder.CreateBinOp(Op, Result, Args[1]);
222   if (Invert)
223     Result =
224         CGF.Builder.CreateBinOp(llvm::Instruction::Xor, Result,
225                                 llvm::ConstantInt::getAllOnesValue(IntType));
226   Result = EmitFromInt(CGF, Result, T, ValueType);
227   return RValue::get(Result);
228 }
229
230 /// Utility to insert an atomic cmpxchg instruction.
231 ///
232 /// @param CGF The current codegen function.
233 /// @param E   Builtin call expression to convert to cmpxchg.
234 ///            arg0 - address to operate on
235 ///            arg1 - value to compare with
236 ///            arg2 - new value
237 /// @param ReturnBool Specifies whether to return success flag of
238 ///                   cmpxchg result or the old value.
239 ///
240 /// @returns result of cmpxchg, according to ReturnBool
241 ///
242 /// Note: In order to lower Microsoft's _InterlockedCompareExchange* intrinsics
243 /// invoke the function EmitAtomicCmpXchgForMSIntrin.
244 static Value *MakeAtomicCmpXchgValue(CodeGenFunction &CGF, const CallExpr *E,
245                                      bool ReturnBool) {
246   QualType T = ReturnBool ? E->getArg(1)->getType() : E->getType();
247   llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
248   unsigned AddrSpace = DestPtr->getType()->getPointerAddressSpace();
249
250   llvm::IntegerType *IntType = llvm::IntegerType::get(
251       CGF.getLLVMContext(), CGF.getContext().getTypeSize(T));
252   llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
253
254   Value *Args[3];
255   Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
256   Args[1] = CGF.EmitScalarExpr(E->getArg(1));
257   llvm::Type *ValueType = Args[1]->getType();
258   Args[1] = EmitToInt(CGF, Args[1], T, IntType);
259   Args[2] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(2)), T, IntType);
260
261   Value *Pair = CGF.Builder.CreateAtomicCmpXchg(
262       Args[0], Args[1], Args[2], llvm::AtomicOrdering::SequentiallyConsistent,
263       llvm::AtomicOrdering::SequentiallyConsistent);
264   if (ReturnBool)
265     // Extract boolean success flag and zext it to int.
266     return CGF.Builder.CreateZExt(CGF.Builder.CreateExtractValue(Pair, 1),
267                                   CGF.ConvertType(E->getType()));
268   else
269     // Extract old value and emit it using the same type as compare value.
270     return EmitFromInt(CGF, CGF.Builder.CreateExtractValue(Pair, 0), T,
271                        ValueType);
272 }
273
274 /// This function should be invoked to emit atomic cmpxchg for Microsoft's
275 /// _InterlockedCompareExchange* intrinsics which have the following signature:
276 /// T _InterlockedCompareExchange(T volatile *Destination,
277 ///                               T Exchange,
278 ///                               T Comparand);
279 ///
280 /// Whereas the llvm 'cmpxchg' instruction has the following syntax:
281 /// cmpxchg *Destination, Comparand, Exchange.
282 /// So we need to swap Comparand and Exchange when invoking
283 /// CreateAtomicCmpXchg. That is the reason we could not use the above utility
284 /// function MakeAtomicCmpXchgValue since it expects the arguments to be
285 /// already swapped.
286
287 static
288 Value *EmitAtomicCmpXchgForMSIntrin(CodeGenFunction &CGF, const CallExpr *E,
289     AtomicOrdering SuccessOrdering = AtomicOrdering::SequentiallyConsistent) {
290   assert(E->getArg(0)->getType()->isPointerType());
291   assert(CGF.getContext().hasSameUnqualifiedType(
292       E->getType(), E->getArg(0)->getType()->getPointeeType()));
293   assert(CGF.getContext().hasSameUnqualifiedType(E->getType(),
294                                                  E->getArg(1)->getType()));
295   assert(CGF.getContext().hasSameUnqualifiedType(E->getType(),
296                                                  E->getArg(2)->getType()));
297
298   auto *Destination = CGF.EmitScalarExpr(E->getArg(0));
299   auto *Comparand = CGF.EmitScalarExpr(E->getArg(2));
300   auto *Exchange = CGF.EmitScalarExpr(E->getArg(1));
301
302   // For Release ordering, the failure ordering should be Monotonic.
303   auto FailureOrdering = SuccessOrdering == AtomicOrdering::Release ?
304                          AtomicOrdering::Monotonic :
305                          SuccessOrdering;
306
307   auto *Result = CGF.Builder.CreateAtomicCmpXchg(
308                    Destination, Comparand, Exchange,
309                    SuccessOrdering, FailureOrdering);
310   Result->setVolatile(true);
311   return CGF.Builder.CreateExtractValue(Result, 0);
312 }
313
314 static Value *EmitAtomicIncrementValue(CodeGenFunction &CGF, const CallExpr *E,
315     AtomicOrdering Ordering = AtomicOrdering::SequentiallyConsistent) {
316   assert(E->getArg(0)->getType()->isPointerType());
317
318   auto *IntTy = CGF.ConvertType(E->getType());
319   auto *Result = CGF.Builder.CreateAtomicRMW(
320                    AtomicRMWInst::Add,
321                    CGF.EmitScalarExpr(E->getArg(0)),
322                    ConstantInt::get(IntTy, 1),
323                    Ordering);
324   return CGF.Builder.CreateAdd(Result, ConstantInt::get(IntTy, 1));
325 }
326
327 static Value *EmitAtomicDecrementValue(CodeGenFunction &CGF, const CallExpr *E,
328     AtomicOrdering Ordering = AtomicOrdering::SequentiallyConsistent) {
329   assert(E->getArg(0)->getType()->isPointerType());
330
331   auto *IntTy = CGF.ConvertType(E->getType());
332   auto *Result = CGF.Builder.CreateAtomicRMW(
333                    AtomicRMWInst::Sub,
334                    CGF.EmitScalarExpr(E->getArg(0)),
335                    ConstantInt::get(IntTy, 1),
336                    Ordering);
337   return CGF.Builder.CreateSub(Result, ConstantInt::get(IntTy, 1));
338 }
339
340 // Build a plain volatile load.
341 static Value *EmitISOVolatileLoad(CodeGenFunction &CGF, const CallExpr *E) {
342   Value *Ptr = CGF.EmitScalarExpr(E->getArg(0));
343   QualType ElTy = E->getArg(0)->getType()->getPointeeType();
344   CharUnits LoadSize = CGF.getContext().getTypeSizeInChars(ElTy);
345   llvm::Type *ITy =
346       llvm::IntegerType::get(CGF.getLLVMContext(), LoadSize.getQuantity() * 8);
347   Ptr = CGF.Builder.CreateBitCast(Ptr, ITy->getPointerTo());
348   llvm::LoadInst *Load = CGF.Builder.CreateAlignedLoad(Ptr, LoadSize);
349   Load->setVolatile(true);
350   return Load;
351 }
352
353 // Build a plain volatile store.
354 static Value *EmitISOVolatileStore(CodeGenFunction &CGF, const CallExpr *E) {
355   Value *Ptr = CGF.EmitScalarExpr(E->getArg(0));
356   Value *Value = CGF.EmitScalarExpr(E->getArg(1));
357   QualType ElTy = E->getArg(0)->getType()->getPointeeType();
358   CharUnits StoreSize = CGF.getContext().getTypeSizeInChars(ElTy);
359   llvm::Type *ITy =
360       llvm::IntegerType::get(CGF.getLLVMContext(), StoreSize.getQuantity() * 8);
361   Ptr = CGF.Builder.CreateBitCast(Ptr, ITy->getPointerTo());
362   llvm::StoreInst *Store =
363       CGF.Builder.CreateAlignedStore(Value, Ptr, StoreSize);
364   Store->setVolatile(true);
365   return Store;
366 }
367
368 // Emit a simple mangled intrinsic that has 1 argument and a return type
369 // matching the argument type. Depending on mode, this may be a constrained
370 // floating-point intrinsic.
371 static Value *emitUnaryMaybeConstrainedFPBuiltin(CodeGenFunction &CGF,
372                                 const CallExpr *E, unsigned IntrinsicID,
373                                 unsigned ConstrainedIntrinsicID) {
374   llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
375
376   if (CGF.Builder.getIsFPConstrained()) {
377     Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID, Src0->getType());
378     return CGF.Builder.CreateConstrainedFPCall(F, { Src0 });
379   } else {
380     Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
381     return CGF.Builder.CreateCall(F, Src0);
382   }
383 }
384
385 // Emit an intrinsic that has 2 operands of the same type as its result.
386 // Depending on mode, this may be a constrained floating-point intrinsic.
387 static Value *emitBinaryMaybeConstrainedFPBuiltin(CodeGenFunction &CGF,
388                                 const CallExpr *E, unsigned IntrinsicID,
389                                 unsigned ConstrainedIntrinsicID) {
390   llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
391   llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
392
393   if (CGF.Builder.getIsFPConstrained()) {
394     Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID, Src0->getType());
395     return CGF.Builder.CreateConstrainedFPCall(F, { Src0, Src1 });
396   } else {
397     Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
398     return CGF.Builder.CreateCall(F, { Src0, Src1 });
399   }
400 }
401
402 // Emit an intrinsic that has 3 operands of the same type as its result.
403 // Depending on mode, this may be a constrained floating-point intrinsic.
404 static Value *emitTernaryMaybeConstrainedFPBuiltin(CodeGenFunction &CGF,
405                                  const CallExpr *E, unsigned IntrinsicID,
406                                  unsigned ConstrainedIntrinsicID) {
407   llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
408   llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
409   llvm::Value *Src2 = CGF.EmitScalarExpr(E->getArg(2));
410
411   if (CGF.Builder.getIsFPConstrained()) {
412     Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID, Src0->getType());
413     return CGF.Builder.CreateConstrainedFPCall(F, { Src0, Src1, Src2 });
414   } else {
415     Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
416     return CGF.Builder.CreateCall(F, { Src0, Src1, Src2 });
417   }
418 }
419
420 // Emit an intrinsic where all operands are of the same type as the result.
421 // Depending on mode, this may be a constrained floating-point intrinsic.
422 static Value *emitCallMaybeConstrainedFPBuiltin(CodeGenFunction &CGF,
423                                                 unsigned IntrinsicID,
424                                                 unsigned ConstrainedIntrinsicID,
425                                                 llvm::Type *Ty,
426                                                 ArrayRef<Value *> Args) {
427   Function *F;
428   if (CGF.Builder.getIsFPConstrained())
429     F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID, Ty);
430   else
431     F = CGF.CGM.getIntrinsic(IntrinsicID, Ty);
432
433   if (CGF.Builder.getIsFPConstrained())
434     return CGF.Builder.CreateConstrainedFPCall(F, Args);
435   else
436     return CGF.Builder.CreateCall(F, Args);
437 }
438
439 // Emit a simple mangled intrinsic that has 1 argument and a return type
440 // matching the argument type.
441 static Value *emitUnaryBuiltin(CodeGenFunction &CGF,
442                                const CallExpr *E,
443                                unsigned IntrinsicID) {
444   llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
445
446   Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
447   return CGF.Builder.CreateCall(F, Src0);
448 }
449
450 // Emit an intrinsic that has 2 operands of the same type as its result.
451 static Value *emitBinaryBuiltin(CodeGenFunction &CGF,
452                                 const CallExpr *E,
453                                 unsigned IntrinsicID) {
454   llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
455   llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
456
457   Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
458   return CGF.Builder.CreateCall(F, { Src0, Src1 });
459 }
460
461 // Emit an intrinsic that has 3 operands of the same type as its result.
462 static Value *emitTernaryBuiltin(CodeGenFunction &CGF,
463                                  const CallExpr *E,
464                                  unsigned IntrinsicID) {
465   llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
466   llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
467   llvm::Value *Src2 = CGF.EmitScalarExpr(E->getArg(2));
468
469   Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
470   return CGF.Builder.CreateCall(F, { Src0, Src1, Src2 });
471 }
472
473 // Emit an intrinsic that has 1 float or double operand, and 1 integer.
474 static Value *emitFPIntBuiltin(CodeGenFunction &CGF,
475                                const CallExpr *E,
476                                unsigned IntrinsicID) {
477   llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
478   llvm::Value *Src1 = CGF.EmitScalarExpr(E->getArg(1));
479
480   Function *F = CGF.CGM.getIntrinsic(IntrinsicID, Src0->getType());
481   return CGF.Builder.CreateCall(F, {Src0, Src1});
482 }
483
484 // Emit an intrinsic that has overloaded integer result and fp operand.
485 static Value *
486 emitMaybeConstrainedFPToIntRoundBuiltin(CodeGenFunction &CGF, const CallExpr *E,
487                                         unsigned IntrinsicID,
488                                         unsigned ConstrainedIntrinsicID) {
489   llvm::Type *ResultType = CGF.ConvertType(E->getType());
490   llvm::Value *Src0 = CGF.EmitScalarExpr(E->getArg(0));
491
492   if (CGF.Builder.getIsFPConstrained()) {
493     Function *F = CGF.CGM.getIntrinsic(ConstrainedIntrinsicID,
494                                        {ResultType, Src0->getType()});
495     return CGF.Builder.CreateConstrainedFPCall(F, {Src0});
496   } else {
497     Function *F =
498         CGF.CGM.getIntrinsic(IntrinsicID, {ResultType, Src0->getType()});
499     return CGF.Builder.CreateCall(F, Src0);
500   }
501 }
502
503 /// EmitFAbs - Emit a call to @llvm.fabs().
504 static Value *EmitFAbs(CodeGenFunction &CGF, Value *V) {
505   Function *F = CGF.CGM.getIntrinsic(Intrinsic::fabs, V->getType());
506   llvm::CallInst *Call = CGF.Builder.CreateCall(F, V);
507   Call->setDoesNotAccessMemory();
508   return Call;
509 }
510
511 /// Emit the computation of the sign bit for a floating point value. Returns
512 /// the i1 sign bit value.
513 static Value *EmitSignBit(CodeGenFunction &CGF, Value *V) {
514   LLVMContext &C = CGF.CGM.getLLVMContext();
515
516   llvm::Type *Ty = V->getType();
517   int Width = Ty->getPrimitiveSizeInBits();
518   llvm::Type *IntTy = llvm::IntegerType::get(C, Width);
519   V = CGF.Builder.CreateBitCast(V, IntTy);
520   if (Ty->isPPC_FP128Ty()) {
521     // We want the sign bit of the higher-order double. The bitcast we just
522     // did works as if the double-double was stored to memory and then
523     // read as an i128. The "store" will put the higher-order double in the
524     // lower address in both little- and big-Endian modes, but the "load"
525     // will treat those bits as a different part of the i128: the low bits in
526     // little-Endian, the high bits in big-Endian. Therefore, on big-Endian
527     // we need to shift the high bits down to the low before truncating.
528     Width >>= 1;
529     if (CGF.getTarget().isBigEndian()) {
530       Value *ShiftCst = llvm::ConstantInt::get(IntTy, Width);
531       V = CGF.Builder.CreateLShr(V, ShiftCst);
532     }
533     // We are truncating value in order to extract the higher-order
534     // double, which we will be using to extract the sign from.
535     IntTy = llvm::IntegerType::get(C, Width);
536     V = CGF.Builder.CreateTrunc(V, IntTy);
537   }
538   Value *Zero = llvm::Constant::getNullValue(IntTy);
539   return CGF.Builder.CreateICmpSLT(V, Zero);
540 }
541
542 static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
543                               const CallExpr *E, llvm::Constant *calleeValue) {
544   CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
545   return CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
546 }
547
548 /// Emit a call to llvm.{sadd,uadd,ssub,usub,smul,umul}.with.overflow.*
549 /// depending on IntrinsicID.
550 ///
551 /// \arg CGF The current codegen function.
552 /// \arg IntrinsicID The ID for the Intrinsic we wish to generate.
553 /// \arg X The first argument to the llvm.*.with.overflow.*.
554 /// \arg Y The second argument to the llvm.*.with.overflow.*.
555 /// \arg Carry The carry returned by the llvm.*.with.overflow.*.
556 /// \returns The result (i.e. sum/product) returned by the intrinsic.
557 static llvm::Value *EmitOverflowIntrinsic(CodeGenFunction &CGF,
558                                           const llvm::Intrinsic::ID IntrinsicID,
559                                           llvm::Value *X, llvm::Value *Y,
560                                           llvm::Value *&Carry) {
561   // Make sure we have integers of the same width.
562   assert(X->getType() == Y->getType() &&
563          "Arguments must be the same type. (Did you forget to make sure both "
564          "arguments have the same integer width?)");
565
566   Function *Callee = CGF.CGM.getIntrinsic(IntrinsicID, X->getType());
567   llvm::Value *Tmp = CGF.Builder.CreateCall(Callee, {X, Y});
568   Carry = CGF.Builder.CreateExtractValue(Tmp, 1);
569   return CGF.Builder.CreateExtractValue(Tmp, 0);
570 }
571
572 static Value *emitRangedBuiltin(CodeGenFunction &CGF,
573                                 unsigned IntrinsicID,
574                                 int low, int high) {
575     llvm::MDBuilder MDHelper(CGF.getLLVMContext());
576     llvm::MDNode *RNode = MDHelper.createRange(APInt(32, low), APInt(32, high));
577     Function *F = CGF.CGM.getIntrinsic(IntrinsicID, {});
578     llvm::Instruction *Call = CGF.Builder.CreateCall(F);
579     Call->setMetadata(llvm::LLVMContext::MD_range, RNode);
580     return Call;
581 }
582
583 namespace {
584   struct WidthAndSignedness {
585     unsigned Width;
586     bool Signed;
587   };
588 }
589
590 static WidthAndSignedness
591 getIntegerWidthAndSignedness(const clang::ASTContext &context,
592                              const clang::QualType Type) {
593   assert(Type->isIntegerType() && "Given type is not an integer.");
594   unsigned Width = Type->isBooleanType()  ? 1
595                    : Type->isExtIntType() ? context.getIntWidth(Type)
596                                           : context.getTypeInfo(Type).Width;
597   bool Signed = Type->isSignedIntegerType();
598   return {Width, Signed};
599 }
600
601 // Given one or more integer types, this function produces an integer type that
602 // encompasses them: any value in one of the given types could be expressed in
603 // the encompassing type.
604 static struct WidthAndSignedness
605 EncompassingIntegerType(ArrayRef<struct WidthAndSignedness> Types) {
606   assert(Types.size() > 0 && "Empty list of types.");
607
608   // If any of the given types is signed, we must return a signed type.
609   bool Signed = false;
610   for (const auto &Type : Types) {
611     Signed |= Type.Signed;
612   }
613
614   // The encompassing type must have a width greater than or equal to the width
615   // of the specified types.  Additionally, if the encompassing type is signed,
616   // its width must be strictly greater than the width of any unsigned types
617   // given.
618   unsigned Width = 0;
619   for (const auto &Type : Types) {
620     unsigned MinWidth = Type.Width + (Signed && !Type.Signed);
621     if (Width < MinWidth) {
622       Width = MinWidth;
623     }
624   }
625
626   return {Width, Signed};
627 }
628
629 Value *CodeGenFunction::EmitVAStartEnd(Value *ArgValue, bool IsStart) {
630   llvm::Type *DestType = Int8PtrTy;
631   if (ArgValue->getType() != DestType)
632     ArgValue =
633         Builder.CreateBitCast(ArgValue, DestType, ArgValue->getName().data());
634
635   Intrinsic::ID inst = IsStart ? Intrinsic::vastart : Intrinsic::vaend;
636   return Builder.CreateCall(CGM.getIntrinsic(inst), ArgValue);
637 }
638
639 /// Checks if using the result of __builtin_object_size(p, @p From) in place of
640 /// __builtin_object_size(p, @p To) is correct
641 static bool areBOSTypesCompatible(int From, int To) {
642   // Note: Our __builtin_object_size implementation currently treats Type=0 and
643   // Type=2 identically. Encoding this implementation detail here may make
644   // improving __builtin_object_size difficult in the future, so it's omitted.
645   return From == To || (From == 0 && To == 1) || (From == 3 && To == 2);
646 }
647
648 static llvm::Value *
649 getDefaultBuiltinObjectSizeResult(unsigned Type, llvm::IntegerType *ResType) {
650   return ConstantInt::get(ResType, (Type & 2) ? 0 : -1, /*isSigned=*/true);
651 }
652
653 llvm::Value *
654 CodeGenFunction::evaluateOrEmitBuiltinObjectSize(const Expr *E, unsigned Type,
655                                                  llvm::IntegerType *ResType,
656                                                  llvm::Value *EmittedE,
657                                                  bool IsDynamic) {
658   uint64_t ObjectSize;
659   if (!E->tryEvaluateObjectSize(ObjectSize, getContext(), Type))
660     return emitBuiltinObjectSize(E, Type, ResType, EmittedE, IsDynamic);
661   return ConstantInt::get(ResType, ObjectSize, /*isSigned=*/true);
662 }
663
664 /// Returns a Value corresponding to the size of the given expression.
665 /// This Value may be either of the following:
666 ///   - A llvm::Argument (if E is a param with the pass_object_size attribute on
667 ///     it)
668 ///   - A call to the @llvm.objectsize intrinsic
669 ///
670 /// EmittedE is the result of emitting `E` as a scalar expr. If it's non-null
671 /// and we wouldn't otherwise try to reference a pass_object_size parameter,
672 /// we'll call @llvm.objectsize on EmittedE, rather than emitting E.
673 llvm::Value *
674 CodeGenFunction::emitBuiltinObjectSize(const Expr *E, unsigned Type,
675                                        llvm::IntegerType *ResType,
676                                        llvm::Value *EmittedE, bool IsDynamic) {
677   // We need to reference an argument if the pointer is a parameter with the
678   // pass_object_size attribute.
679   if (auto *D = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) {
680     auto *Param = dyn_cast<ParmVarDecl>(D->getDecl());
681     auto *PS = D->getDecl()->getAttr<PassObjectSizeAttr>();
682     if (Param != nullptr && PS != nullptr &&
683         areBOSTypesCompatible(PS->getType(), Type)) {
684       auto Iter = SizeArguments.find(Param);
685       assert(Iter != SizeArguments.end());
686
687       const ImplicitParamDecl *D = Iter->second;
688       auto DIter = LocalDeclMap.find(D);
689       assert(DIter != LocalDeclMap.end());
690
691       return EmitLoadOfScalar(DIter->second, /*Volatile=*/false,
692                               getContext().getSizeType(), E->getBeginLoc());
693     }
694   }
695
696   // LLVM can't handle Type=3 appropriately, and __builtin_object_size shouldn't
697   // evaluate E for side-effects. In either case, we shouldn't lower to
698   // @llvm.objectsize.
699   if (Type == 3 || (!EmittedE && E->HasSideEffects(getContext())))
700     return getDefaultBuiltinObjectSizeResult(Type, ResType);
701
702   Value *Ptr = EmittedE ? EmittedE : EmitScalarExpr(E);
703   assert(Ptr->getType()->isPointerTy() &&
704          "Non-pointer passed to __builtin_object_size?");
705
706   Function *F =
707       CGM.getIntrinsic(Intrinsic::objectsize, {ResType, Ptr->getType()});
708
709   // LLVM only supports 0 and 2, make sure that we pass along that as a boolean.
710   Value *Min = Builder.getInt1((Type & 2) != 0);
711   // For GCC compatibility, __builtin_object_size treat NULL as unknown size.
712   Value *NullIsUnknown = Builder.getTrue();
713   Value *Dynamic = Builder.getInt1(IsDynamic);
714   return Builder.CreateCall(F, {Ptr, Min, NullIsUnknown, Dynamic});
715 }
716
717 namespace {
718 /// A struct to generically describe a bit test intrinsic.
719 struct BitTest {
720   enum ActionKind : uint8_t { TestOnly, Complement, Reset, Set };
721   enum InterlockingKind : uint8_t {
722     Unlocked,
723     Sequential,
724     Acquire,
725     Release,
726     NoFence
727   };
728
729   ActionKind Action;
730   InterlockingKind Interlocking;
731   bool Is64Bit;
732
733   static BitTest decodeBitTestBuiltin(unsigned BuiltinID);
734 };
735 } // namespace
736
737 BitTest BitTest::decodeBitTestBuiltin(unsigned BuiltinID) {
738   switch (BuiltinID) {
739     // Main portable variants.
740   case Builtin::BI_bittest:
741     return {TestOnly, Unlocked, false};
742   case Builtin::BI_bittestandcomplement:
743     return {Complement, Unlocked, false};
744   case Builtin::BI_bittestandreset:
745     return {Reset, Unlocked, false};
746   case Builtin::BI_bittestandset:
747     return {Set, Unlocked, false};
748   case Builtin::BI_interlockedbittestandreset:
749     return {Reset, Sequential, false};
750   case Builtin::BI_interlockedbittestandset:
751     return {Set, Sequential, false};
752
753     // X86-specific 64-bit variants.
754   case Builtin::BI_bittest64:
755     return {TestOnly, Unlocked, true};
756   case Builtin::BI_bittestandcomplement64:
757     return {Complement, Unlocked, true};
758   case Builtin::BI_bittestandreset64:
759     return {Reset, Unlocked, true};
760   case Builtin::BI_bittestandset64:
761     return {Set, Unlocked, true};
762   case Builtin::BI_interlockedbittestandreset64:
763     return {Reset, Sequential, true};
764   case Builtin::BI_interlockedbittestandset64:
765     return {Set, Sequential, true};
766
767     // ARM/AArch64-specific ordering variants.
768   case Builtin::BI_interlockedbittestandset_acq:
769     return {Set, Acquire, false};
770   case Builtin::BI_interlockedbittestandset_rel:
771     return {Set, Release, false};
772   case Builtin::BI_interlockedbittestandset_nf:
773     return {Set, NoFence, false};
774   case Builtin::BI_interlockedbittestandreset_acq:
775     return {Reset, Acquire, false};
776   case Builtin::BI_interlockedbittestandreset_rel:
777     return {Reset, Release, false};
778   case Builtin::BI_interlockedbittestandreset_nf:
779     return {Reset, NoFence, false};
780   }
781   llvm_unreachable("expected only bittest intrinsics");
782 }
783
784 static char bitActionToX86BTCode(BitTest::ActionKind A) {
785   switch (A) {
786   case BitTest::TestOnly:   return '\0';
787   case BitTest::Complement: return 'c';
788   case BitTest::Reset:      return 'r';
789   case BitTest::Set:        return 's';
790   }
791   llvm_unreachable("invalid action");
792 }
793
794 static llvm::Value *EmitX86BitTestIntrinsic(CodeGenFunction &CGF,
795                                             BitTest BT,
796                                             const CallExpr *E, Value *BitBase,
797                                             Value *BitPos) {
798   char Action = bitActionToX86BTCode(BT.Action);
799   char SizeSuffix = BT.Is64Bit ? 'q' : 'l';
800
801   // Build the assembly.
802   SmallString<64> Asm;
803   raw_svector_ostream AsmOS(Asm);
804   if (BT.Interlocking != BitTest::Unlocked)
805     AsmOS << "lock ";
806   AsmOS << "bt";
807   if (Action)
808     AsmOS << Action;
809   AsmOS << SizeSuffix << " $2, ($1)";
810
811   // Build the constraints. FIXME: We should support immediates when possible.
812   std::string Constraints = "={@ccc},r,r,~{cc},~{memory}";
813   std::string MachineClobbers = CGF.getTarget().getClobbers();
814   if (!MachineClobbers.empty()) {
815     Constraints += ',';
816     Constraints += MachineClobbers;
817   }
818   llvm::IntegerType *IntType = llvm::IntegerType::get(
819       CGF.getLLVMContext(),
820       CGF.getContext().getTypeSize(E->getArg(1)->getType()));
821   llvm::Type *IntPtrType = IntType->getPointerTo();
822   llvm::FunctionType *FTy =
823       llvm::FunctionType::get(CGF.Int8Ty, {IntPtrType, IntType}, false);
824
825   llvm::InlineAsm *IA =
826       llvm::InlineAsm::get(FTy, Asm, Constraints, /*hasSideEffects=*/true);
827   return CGF.Builder.CreateCall(IA, {BitBase, BitPos});
828 }
829
830 static llvm::AtomicOrdering
831 getBitTestAtomicOrdering(BitTest::InterlockingKind I) {
832   switch (I) {
833   case BitTest::Unlocked:   return llvm::AtomicOrdering::NotAtomic;
834   case BitTest::Sequential: return llvm::AtomicOrdering::SequentiallyConsistent;
835   case BitTest::Acquire:    return llvm::AtomicOrdering::Acquire;
836   case BitTest::Release:    return llvm::AtomicOrdering::Release;
837   case BitTest::NoFence:    return llvm::AtomicOrdering::Monotonic;
838   }
839   llvm_unreachable("invalid interlocking");
840 }
841
842 /// Emit a _bittest* intrinsic. These intrinsics take a pointer to an array of
843 /// bits and a bit position and read and optionally modify the bit at that
844 /// position. The position index can be arbitrarily large, i.e. it can be larger
845 /// than 31 or 63, so we need an indexed load in the general case.
846 static llvm::Value *EmitBitTestIntrinsic(CodeGenFunction &CGF,
847                                          unsigned BuiltinID,
848                                          const CallExpr *E) {
849   Value *BitBase = CGF.EmitScalarExpr(E->getArg(0));
850   Value *BitPos = CGF.EmitScalarExpr(E->getArg(1));
851
852   BitTest BT = BitTest::decodeBitTestBuiltin(BuiltinID);
853
854   // X86 has special BT, BTC, BTR, and BTS instructions that handle the array
855   // indexing operation internally. Use them if possible.
856   if (CGF.getTarget().getTriple().isX86())
857     return EmitX86BitTestIntrinsic(CGF, BT, E, BitBase, BitPos);
858
859   // Otherwise, use generic code to load one byte and test the bit. Use all but
860   // the bottom three bits as the array index, and the bottom three bits to form
861   // a mask.
862   // Bit = BitBaseI8[BitPos >> 3] & (1 << (BitPos & 0x7)) != 0;
863   Value *ByteIndex = CGF.Builder.CreateAShr(
864       BitPos, llvm::ConstantInt::get(BitPos->getType(), 3), "bittest.byteidx");
865   Value *BitBaseI8 = CGF.Builder.CreatePointerCast(BitBase, CGF.Int8PtrTy);
866   Address ByteAddr(CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, BitBaseI8,
867                                                  ByteIndex, "bittest.byteaddr"),
868                    CharUnits::One());
869   Value *PosLow =
870       CGF.Builder.CreateAnd(CGF.Builder.CreateTrunc(BitPos, CGF.Int8Ty),
871                             llvm::ConstantInt::get(CGF.Int8Ty, 0x7));
872
873   // The updating instructions will need a mask.
874   Value *Mask = nullptr;
875   if (BT.Action != BitTest::TestOnly) {
876     Mask = CGF.Builder.CreateShl(llvm::ConstantInt::get(CGF.Int8Ty, 1), PosLow,
877                                  "bittest.mask");
878   }
879
880   // Check the action and ordering of the interlocked intrinsics.
881   llvm::AtomicOrdering Ordering = getBitTestAtomicOrdering(BT.Interlocking);
882
883   Value *OldByte = nullptr;
884   if (Ordering != llvm::AtomicOrdering::NotAtomic) {
885     // Emit a combined atomicrmw load/store operation for the interlocked
886     // intrinsics.
887     llvm::AtomicRMWInst::BinOp RMWOp = llvm::AtomicRMWInst::Or;
888     if (BT.Action == BitTest::Reset) {
889       Mask = CGF.Builder.CreateNot(Mask);
890       RMWOp = llvm::AtomicRMWInst::And;
891     }
892     OldByte = CGF.Builder.CreateAtomicRMW(RMWOp, ByteAddr.getPointer(), Mask,
893                                           Ordering);
894   } else {
895     // Emit a plain load for the non-interlocked intrinsics.
896     OldByte = CGF.Builder.CreateLoad(ByteAddr, "bittest.byte");
897     Value *NewByte = nullptr;
898     switch (BT.Action) {
899     case BitTest::TestOnly:
900       // Don't store anything.
901       break;
902     case BitTest::Complement:
903       NewByte = CGF.Builder.CreateXor(OldByte, Mask);
904       break;
905     case BitTest::Reset:
906       NewByte = CGF.Builder.CreateAnd(OldByte, CGF.Builder.CreateNot(Mask));
907       break;
908     case BitTest::Set:
909       NewByte = CGF.Builder.CreateOr(OldByte, Mask);
910       break;
911     }
912     if (NewByte)
913       CGF.Builder.CreateStore(NewByte, ByteAddr);
914   }
915
916   // However we loaded the old byte, either by plain load or atomicrmw, shift
917   // the bit into the low position and mask it to 0 or 1.
918   Value *ShiftedByte = CGF.Builder.CreateLShr(OldByte, PosLow, "bittest.shr");
919   return CGF.Builder.CreateAnd(
920       ShiftedByte, llvm::ConstantInt::get(CGF.Int8Ty, 1), "bittest.res");
921 }
922
923 namespace {
924 enum class MSVCSetJmpKind {
925   _setjmpex,
926   _setjmp3,
927   _setjmp
928 };
929 }
930
931 /// MSVC handles setjmp a bit differently on different platforms. On every
932 /// architecture except 32-bit x86, the frame address is passed. On x86, extra
933 /// parameters can be passed as variadic arguments, but we always pass none.
934 static RValue EmitMSVCRTSetJmp(CodeGenFunction &CGF, MSVCSetJmpKind SJKind,
935                                const CallExpr *E) {
936   llvm::Value *Arg1 = nullptr;
937   llvm::Type *Arg1Ty = nullptr;
938   StringRef Name;
939   bool IsVarArg = false;
940   if (SJKind == MSVCSetJmpKind::_setjmp3) {
941     Name = "_setjmp3";
942     Arg1Ty = CGF.Int32Ty;
943     Arg1 = llvm::ConstantInt::get(CGF.IntTy, 0);
944     IsVarArg = true;
945   } else {
946     Name = SJKind == MSVCSetJmpKind::_setjmp ? "_setjmp" : "_setjmpex";
947     Arg1Ty = CGF.Int8PtrTy;
948     if (CGF.getTarget().getTriple().getArch() == llvm::Triple::aarch64) {
949       Arg1 = CGF.Builder.CreateCall(
950           CGF.CGM.getIntrinsic(Intrinsic::sponentry, CGF.AllocaInt8PtrTy));
951     } else
952       Arg1 = CGF.Builder.CreateCall(
953           CGF.CGM.getIntrinsic(Intrinsic::frameaddress, CGF.AllocaInt8PtrTy),
954           llvm::ConstantInt::get(CGF.Int32Ty, 0));
955   }
956
957   // Mark the call site and declaration with ReturnsTwice.
958   llvm::Type *ArgTypes[2] = {CGF.Int8PtrTy, Arg1Ty};
959   llvm::AttributeList ReturnsTwiceAttr = llvm::AttributeList::get(
960       CGF.getLLVMContext(), llvm::AttributeList::FunctionIndex,
961       llvm::Attribute::ReturnsTwice);
962   llvm::FunctionCallee SetJmpFn = CGF.CGM.CreateRuntimeFunction(
963       llvm::FunctionType::get(CGF.IntTy, ArgTypes, IsVarArg), Name,
964       ReturnsTwiceAttr, /*Local=*/true);
965
966   llvm::Value *Buf = CGF.Builder.CreateBitOrPointerCast(
967       CGF.EmitScalarExpr(E->getArg(0)), CGF.Int8PtrTy);
968   llvm::Value *Args[] = {Buf, Arg1};
969   llvm::CallBase *CB = CGF.EmitRuntimeCallOrInvoke(SetJmpFn, Args);
970   CB->setAttributes(ReturnsTwiceAttr);
971   return RValue::get(CB);
972 }
973
974 // Many of MSVC builtins are on x64, ARM and AArch64; to avoid repeating code,
975 // we handle them here.
976 enum class CodeGenFunction::MSVCIntrin {
977   _BitScanForward,
978   _BitScanReverse,
979   _InterlockedAnd,
980   _InterlockedDecrement,
981   _InterlockedExchange,
982   _InterlockedExchangeAdd,
983   _InterlockedExchangeSub,
984   _InterlockedIncrement,
985   _InterlockedOr,
986   _InterlockedXor,
987   _InterlockedExchangeAdd_acq,
988   _InterlockedExchangeAdd_rel,
989   _InterlockedExchangeAdd_nf,
990   _InterlockedExchange_acq,
991   _InterlockedExchange_rel,
992   _InterlockedExchange_nf,
993   _InterlockedCompareExchange_acq,
994   _InterlockedCompareExchange_rel,
995   _InterlockedCompareExchange_nf,
996   _InterlockedOr_acq,
997   _InterlockedOr_rel,
998   _InterlockedOr_nf,
999   _InterlockedXor_acq,
1000   _InterlockedXor_rel,
1001   _InterlockedXor_nf,
1002   _InterlockedAnd_acq,
1003   _InterlockedAnd_rel,
1004   _InterlockedAnd_nf,
1005   _InterlockedIncrement_acq,
1006   _InterlockedIncrement_rel,
1007   _InterlockedIncrement_nf,
1008   _InterlockedDecrement_acq,
1009   _InterlockedDecrement_rel,
1010   _InterlockedDecrement_nf,
1011   __fastfail,
1012 };
1013
1014 Value *CodeGenFunction::EmitMSVCBuiltinExpr(MSVCIntrin BuiltinID,
1015                                             const CallExpr *E) {
1016   switch (BuiltinID) {
1017   case MSVCIntrin::_BitScanForward:
1018   case MSVCIntrin::_BitScanReverse: {
1019     Value *ArgValue = EmitScalarExpr(E->getArg(1));
1020
1021     llvm::Type *ArgType = ArgValue->getType();
1022     llvm::Type *IndexType =
1023       EmitScalarExpr(E->getArg(0))->getType()->getPointerElementType();
1024     llvm::Type *ResultType = ConvertType(E->getType());
1025
1026     Value *ArgZero = llvm::Constant::getNullValue(ArgType);
1027     Value *ResZero = llvm::Constant::getNullValue(ResultType);
1028     Value *ResOne = llvm::ConstantInt::get(ResultType, 1);
1029
1030     BasicBlock *Begin = Builder.GetInsertBlock();
1031     BasicBlock *End = createBasicBlock("bitscan_end", this->CurFn);
1032     Builder.SetInsertPoint(End);
1033     PHINode *Result = Builder.CreatePHI(ResultType, 2, "bitscan_result");
1034
1035     Builder.SetInsertPoint(Begin);
1036     Value *IsZero = Builder.CreateICmpEQ(ArgValue, ArgZero);
1037     BasicBlock *NotZero = createBasicBlock("bitscan_not_zero", this->CurFn);
1038     Builder.CreateCondBr(IsZero, End, NotZero);
1039     Result->addIncoming(ResZero, Begin);
1040
1041     Builder.SetInsertPoint(NotZero);
1042     Address IndexAddress = EmitPointerWithAlignment(E->getArg(0));
1043
1044     if (BuiltinID == MSVCIntrin::_BitScanForward) {
1045       Function *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
1046       Value *ZeroCount = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
1047       ZeroCount = Builder.CreateIntCast(ZeroCount, IndexType, false);
1048       Builder.CreateStore(ZeroCount, IndexAddress, false);
1049     } else {
1050       unsigned ArgWidth = cast<llvm::IntegerType>(ArgType)->getBitWidth();
1051       Value *ArgTypeLastIndex = llvm::ConstantInt::get(IndexType, ArgWidth - 1);
1052
1053       Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
1054       Value *ZeroCount = Builder.CreateCall(F, {ArgValue, Builder.getTrue()});
1055       ZeroCount = Builder.CreateIntCast(ZeroCount, IndexType, false);
1056       Value *Index = Builder.CreateNSWSub(ArgTypeLastIndex, ZeroCount);
1057       Builder.CreateStore(Index, IndexAddress, false);
1058     }
1059     Builder.CreateBr(End);
1060     Result->addIncoming(ResOne, NotZero);
1061
1062     Builder.SetInsertPoint(End);
1063     return Result;
1064   }
1065   case MSVCIntrin::_InterlockedAnd:
1066     return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E);
1067   case MSVCIntrin::_InterlockedExchange:
1068     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E);
1069   case MSVCIntrin::_InterlockedExchangeAdd:
1070     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E);
1071   case MSVCIntrin::_InterlockedExchangeSub:
1072     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Sub, E);
1073   case MSVCIntrin::_InterlockedOr:
1074     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E);
1075   case MSVCIntrin::_InterlockedXor:
1076     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E);
1077   case MSVCIntrin::_InterlockedExchangeAdd_acq:
1078     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E,
1079                                  AtomicOrdering::Acquire);
1080   case MSVCIntrin::_InterlockedExchangeAdd_rel:
1081     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E,
1082                                  AtomicOrdering::Release);
1083   case MSVCIntrin::_InterlockedExchangeAdd_nf:
1084     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Add, E,
1085                                  AtomicOrdering::Monotonic);
1086   case MSVCIntrin::_InterlockedExchange_acq:
1087     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E,
1088                                  AtomicOrdering::Acquire);
1089   case MSVCIntrin::_InterlockedExchange_rel:
1090     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E,
1091                                  AtomicOrdering::Release);
1092   case MSVCIntrin::_InterlockedExchange_nf:
1093     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xchg, E,
1094                                  AtomicOrdering::Monotonic);
1095   case MSVCIntrin::_InterlockedCompareExchange_acq:
1096     return EmitAtomicCmpXchgForMSIntrin(*this, E, AtomicOrdering::Acquire);
1097   case MSVCIntrin::_InterlockedCompareExchange_rel:
1098     return EmitAtomicCmpXchgForMSIntrin(*this, E, AtomicOrdering::Release);
1099   case MSVCIntrin::_InterlockedCompareExchange_nf:
1100     return EmitAtomicCmpXchgForMSIntrin(*this, E, AtomicOrdering::Monotonic);
1101   case MSVCIntrin::_InterlockedOr_acq:
1102     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E,
1103                                  AtomicOrdering::Acquire);
1104   case MSVCIntrin::_InterlockedOr_rel:
1105     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E,
1106                                  AtomicOrdering::Release);
1107   case MSVCIntrin::_InterlockedOr_nf:
1108     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Or, E,
1109                                  AtomicOrdering::Monotonic);
1110   case MSVCIntrin::_InterlockedXor_acq:
1111     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E,
1112                                  AtomicOrdering::Acquire);
1113   case MSVCIntrin::_InterlockedXor_rel:
1114     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E,
1115                                  AtomicOrdering::Release);
1116   case MSVCIntrin::_InterlockedXor_nf:
1117     return MakeBinaryAtomicValue(*this, AtomicRMWInst::Xor, E,
1118                                  AtomicOrdering::Monotonic);
1119   case MSVCIntrin::_InterlockedAnd_acq:
1120     return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E,
1121                                  AtomicOrdering::Acquire);
1122   case MSVCIntrin::_InterlockedAnd_rel:
1123     return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E,
1124                                  AtomicOrdering::Release);
1125   case MSVCIntrin::_InterlockedAnd_nf:
1126     return MakeBinaryAtomicValue(*this, AtomicRMWInst::And, E,
1127                                  AtomicOrdering::Monotonic);
1128   case MSVCIntrin::_InterlockedIncrement_acq:
1129     return EmitAtomicIncrementValue(*this, E, AtomicOrdering::Acquire);
1130   case MSVCIntrin::_InterlockedIncrement_rel:
1131     return EmitAtomicIncrementValue(*this, E, AtomicOrdering::Release);
1132   case MSVCIntrin::_InterlockedIncrement_nf:
1133     return EmitAtomicIncrementValue(*this, E, AtomicOrdering::Monotonic);
1134   case MSVCIntrin::_InterlockedDecrement_acq:
1135     return EmitAtomicDecrementValue(*this, E, AtomicOrdering::Acquire);
1136   case MSVCIntrin::_InterlockedDecrement_rel:
1137     return EmitAtomicDecrementValue(*this, E, AtomicOrdering::Release);
1138   case MSVCIntrin::_InterlockedDecrement_nf:
1139     return EmitAtomicDecrementValue(*this, E, AtomicOrdering::Monotonic);
1140
1141   case MSVCIntrin::_InterlockedDecrement:
1142     return EmitAtomicDecrementValue(*this, E);
1143   case MSVCIntrin::_InterlockedIncrement:
1144     return EmitAtomicIncrementValue(*this, E);
1145
1146   case MSVCIntrin::__fastfail: {
1147     // Request immediate process termination from the kernel. The instruction
1148     // sequences to do this are documented on MSDN:
1149     // https://msdn.microsoft.com/en-us/library/dn774154.aspx
1150     llvm::Triple::ArchType ISA = getTarget().getTriple().getArch();
1151     StringRef Asm, Constraints;
1152     switch (ISA) {
1153     default:
1154       ErrorUnsupported(E, "__fastfail call for this architecture");
1155       break;
1156     case llvm::Triple::x86:
1157     case llvm::Triple::x86_64:
1158       Asm = "int $$0x29";
1159       Constraints = "{cx}";
1160       break;
1161     case llvm::Triple::thumb:
1162       Asm = "udf #251";
1163       Constraints = "{r0}";
1164       break;
1165     case llvm::Triple::aarch64:
1166       Asm = "brk #0xF003";
1167       Constraints = "{w0}";
1168     }
1169     llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, {Int32Ty}, false);
1170     llvm::InlineAsm *IA =
1171         llvm::InlineAsm::get(FTy, Asm, Constraints, /*hasSideEffects=*/true);
1172     llvm::AttributeList NoReturnAttr = llvm::AttributeList::get(
1173         getLLVMContext(), llvm::AttributeList::FunctionIndex,
1174         llvm::Attribute::NoReturn);
1175     llvm::CallInst *CI = Builder.CreateCall(IA, EmitScalarExpr(E->getArg(0)));
1176     CI->setAttributes(NoReturnAttr);
1177     return CI;
1178   }
1179   }
1180   llvm_unreachable("Incorrect MSVC intrinsic!");
1181 }
1182
1183 namespace {
1184 // ARC cleanup for __builtin_os_log_format
1185 struct CallObjCArcUse final : EHScopeStack::Cleanup {
1186   CallObjCArcUse(llvm::Value *object) : object(object) {}
1187   llvm::Value *object;
1188
1189   void Emit(CodeGenFunction &CGF, Flags flags) override {
1190     CGF.EmitARCIntrinsicUse(object);
1191   }
1192 };
1193 }
1194
1195 Value *CodeGenFunction::EmitCheckedArgForBuiltin(const Expr *E,
1196                                                  BuiltinCheckKind Kind) {
1197   assert((Kind == BCK_CLZPassedZero || Kind == BCK_CTZPassedZero)
1198           && "Unsupported builtin check kind");
1199
1200   Value *ArgValue = EmitScalarExpr(E);
1201   if (!SanOpts.has(SanitizerKind::Builtin) || !getTarget().isCLZForZeroUndef())
1202     return ArgValue;
1203
1204   SanitizerScope SanScope(this);
1205   Value *Cond = Builder.CreateICmpNE(
1206       ArgValue, llvm::Constant::getNullValue(ArgValue->getType()));
1207   EmitCheck(std::make_pair(Cond, SanitizerKind::Builtin),
1208             SanitizerHandler::InvalidBuiltin,
1209             {EmitCheckSourceLocation(E->getExprLoc()),
1210              llvm::ConstantInt::get(Builder.getInt8Ty(), Kind)},
1211             None);
1212   return ArgValue;
1213 }
1214
1215 /// Get the argument type for arguments to os_log_helper.
1216 static CanQualType getOSLogArgType(ASTContext &C, int Size) {
1217   QualType UnsignedTy = C.getIntTypeForBitwidth(Size * 8, /*Signed=*/false);
1218   return C.getCanonicalType(UnsignedTy);
1219 }
1220
1221 llvm::Function *CodeGenFunction::generateBuiltinOSLogHelperFunction(
1222     const analyze_os_log::OSLogBufferLayout &Layout,
1223     CharUnits BufferAlignment) {
1224   ASTContext &Ctx = getContext();
1225
1226   llvm::SmallString<64> Name;
1227   {
1228     raw_svector_ostream OS(Name);
1229     OS << "__os_log_helper";
1230     OS << "_" << BufferAlignment.getQuantity();
1231     OS << "_" << int(Layout.getSummaryByte());
1232     OS << "_" << int(Layout.getNumArgsByte());
1233     for (const auto &Item : Layout.Items)
1234       OS << "_" << int(Item.getSizeByte()) << "_"
1235          << int(Item.getDescriptorByte());
1236   }
1237
1238   if (llvm::Function *F = CGM.getModule().getFunction(Name))
1239     return F;
1240
1241   llvm::SmallVector<QualType, 4> ArgTys;
1242   FunctionArgList Args;
1243   Args.push_back(ImplicitParamDecl::Create(
1244       Ctx, nullptr, SourceLocation(), &Ctx.Idents.get("buffer"), Ctx.VoidPtrTy,
1245       ImplicitParamDecl::Other));
1246   ArgTys.emplace_back(Ctx.VoidPtrTy);
1247
1248   for (unsigned int I = 0, E = Layout.Items.size(); I < E; ++I) {
1249     char Size = Layout.Items[I].getSizeByte();
1250     if (!Size)
1251       continue;
1252
1253     QualType ArgTy = getOSLogArgType(Ctx, Size);
1254     Args.push_back(ImplicitParamDecl::Create(
1255         Ctx, nullptr, SourceLocation(),
1256         &Ctx.Idents.get(std::string("arg") + llvm::to_string(I)), ArgTy,
1257         ImplicitParamDecl::Other));
1258     ArgTys.emplace_back(ArgTy);
1259   }
1260
1261   QualType ReturnTy = Ctx.VoidTy;
1262   QualType FuncionTy = Ctx.getFunctionType(ReturnTy, ArgTys, {});
1263
1264   // The helper function has linkonce_odr linkage to enable the linker to merge
1265   // identical functions. To ensure the merging always happens, 'noinline' is
1266   // attached to the function when compiling with -Oz.
1267   const CGFunctionInfo &FI =
1268       CGM.getTypes().arrangeBuiltinFunctionDeclaration(ReturnTy, Args);
1269   llvm::FunctionType *FuncTy = CGM.getTypes().GetFunctionType(FI);
1270   llvm::Function *Fn = llvm::Function::Create(
1271       FuncTy, llvm::GlobalValue::LinkOnceODRLinkage, Name, &CGM.getModule());
1272   Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
1273   CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, Fn);
1274   CGM.SetLLVMFunctionAttributesForDefinition(nullptr, Fn);
1275   Fn->setDoesNotThrow();
1276
1277   // Attach 'noinline' at -Oz.
1278   if (CGM.getCodeGenOpts().OptimizeSize == 2)
1279     Fn->addFnAttr(llvm::Attribute::NoInline);
1280
1281   auto NL = ApplyDebugLocation::CreateEmpty(*this);
1282   IdentifierInfo *II = &Ctx.Idents.get(Name);
1283   FunctionDecl *FD = FunctionDecl::Create(
1284       Ctx, Ctx.getTranslationUnitDecl(), SourceLocation(), SourceLocation(), II,
1285       FuncionTy, nullptr, SC_PrivateExtern, false, false);
1286   // Avoid generating debug location info for the function.
1287   FD->setImplicit();
1288
1289   StartFunction(FD, ReturnTy, Fn, FI, Args);
1290
1291   // Create a scope with an artificial location for the body of this function.
1292   auto AL = ApplyDebugLocation::CreateArtificial(*this);
1293
1294   CharUnits Offset;
1295   Address BufAddr(Builder.CreateLoad(GetAddrOfLocalVar(Args[0]), "buf"),
1296                   BufferAlignment);
1297   Builder.CreateStore(Builder.getInt8(Layout.getSummaryByte()),
1298                       Builder.CreateConstByteGEP(BufAddr, Offset++, "summary"));
1299   Builder.CreateStore(Builder.getInt8(Layout.getNumArgsByte()),
1300                       Builder.CreateConstByteGEP(BufAddr, Offset++, "numArgs"));
1301
1302   unsigned I = 1;
1303   for (const auto &Item : Layout.Items) {
1304     Builder.CreateStore(
1305         Builder.getInt8(Item.getDescriptorByte()),
1306         Builder.CreateConstByteGEP(BufAddr, Offset++, "argDescriptor"));
1307     Builder.CreateStore(
1308         Builder.getInt8(Item.getSizeByte()),
1309         Builder.CreateConstByteGEP(BufAddr, Offset++, "argSize"));
1310
1311     CharUnits Size = Item.size();
1312     if (!Size.getQuantity())
1313       continue;
1314
1315     Address Arg = GetAddrOfLocalVar(Args[I]);
1316     Address Addr = Builder.CreateConstByteGEP(BufAddr, Offset, "argData");
1317     Addr = Builder.CreateBitCast(Addr, Arg.getPointer()->getType(),
1318                                  "argDataCast");
1319     Builder.CreateStore(Builder.CreateLoad(Arg), Addr);
1320     Offset += Size;
1321     ++I;
1322   }
1323
1324   FinishFunction();
1325
1326   return Fn;
1327 }
1328
1329 RValue CodeGenFunction::emitBuiltinOSLogFormat(const CallExpr &E) {
1330   assert(E.getNumArgs() >= 2 &&
1331          "__builtin_os_log_format takes at least 2 arguments");
1332   ASTContext &Ctx = getContext();
1333   analyze_os_log::OSLogBufferLayout Layout;
1334   analyze_os_log::computeOSLogBufferLayout(Ctx, &E, Layout);
1335   Address BufAddr = EmitPointerWithAlignment(E.getArg(0));
1336   llvm::SmallVector<llvm::Value *, 4> RetainableOperands;
1337
1338   // Ignore argument 1, the format string. It is not currently used.
1339   CallArgList Args;
1340   Args.add(RValue::get(BufAddr.getPointer()), Ctx.VoidPtrTy);
1341
1342   for (const auto &Item : Layout.Items) {
1343     int Size = Item.getSizeByte();
1344     if (!Size)
1345       continue;
1346
1347     llvm::Value *ArgVal;
1348
1349     if (Item.getKind() == analyze_os_log::OSLogBufferItem::MaskKind) {
1350       uint64_t Val = 0;
1351       for (unsigned I = 0, E = Item.getMaskType().size(); I < E; ++I)
1352         Val |= ((uint64_t)Item.getMaskType()[I]) << I * 8;
1353       ArgVal = llvm::Constant::getIntegerValue(Int64Ty, llvm::APInt(64, Val));
1354     } else if (const Expr *TheExpr = Item.getExpr()) {
1355       ArgVal = EmitScalarExpr(TheExpr, /*Ignore*/ false);
1356
1357       // If a temporary object that requires destruction after the full
1358       // expression is passed, push a lifetime-extended cleanup to extend its
1359       // lifetime to the end of the enclosing block scope.
1360       auto LifetimeExtendObject = [&](const Expr *E) {
1361         E = E->IgnoreParenCasts();
1362         // Extend lifetimes of objects returned by function calls and message
1363         // sends.
1364
1365         // FIXME: We should do this in other cases in which temporaries are
1366         //        created including arguments of non-ARC types (e.g., C++
1367         //        temporaries).
1368         if (isa<CallExpr>(E) || isa<ObjCMessageExpr>(E))
1369           return true;
1370         return false;
1371       };
1372
1373       if (TheExpr->getType()->isObjCRetainableType() &&
1374           getLangOpts().ObjCAutoRefCount && LifetimeExtendObject(TheExpr)) {
1375         assert(getEvaluationKind(TheExpr->getType()) == TEK_Scalar &&
1376                "Only scalar can be a ObjC retainable type");
1377         if (!isa<Constant>(ArgVal)) {
1378           CleanupKind Cleanup = getARCCleanupKind();
1379           QualType Ty = TheExpr->getType();
1380           Address Alloca = Address::invalid();
1381           Address Addr = CreateMemTemp(Ty, "os.log.arg", &Alloca);
1382           ArgVal = EmitARCRetain(Ty, ArgVal);
1383           Builder.CreateStore(ArgVal, Addr);
1384           pushLifetimeExtendedDestroy(Cleanup, Alloca, Ty,
1385                                       CodeGenFunction::destroyARCStrongPrecise,
1386                                       Cleanup & EHCleanup);
1387
1388           // Push a clang.arc.use call to ensure ARC optimizer knows that the
1389           // argument has to be alive.
1390           if (CGM.getCodeGenOpts().OptimizationLevel != 0)
1391             pushCleanupAfterFullExpr<CallObjCArcUse>(Cleanup, ArgVal);
1392         }
1393       }
1394     } else {
1395       ArgVal = Builder.getInt32(Item.getConstValue().getQuantity());
1396     }
1397
1398     unsigned ArgValSize =
1399         CGM.getDataLayout().getTypeSizeInBits(ArgVal->getType());
1400     llvm::IntegerType *IntTy = llvm::Type::getIntNTy(getLLVMContext(),
1401                                                      ArgValSize);
1402     ArgVal = Builder.CreateBitOrPointerCast(ArgVal, IntTy);
1403     CanQualType ArgTy = getOSLogArgType(Ctx, Size);
1404     // If ArgVal has type x86_fp80, zero-extend ArgVal.
1405     ArgVal = Builder.CreateZExtOrBitCast(ArgVal, ConvertType(ArgTy));
1406     Args.add(RValue::get(ArgVal), ArgTy);
1407   }
1408
1409   const CGFunctionInfo &FI =
1410       CGM.getTypes().arrangeBuiltinFunctionCall(Ctx.VoidTy, Args);
1411   llvm::Function *F = CodeGenFunction(CGM).generateBuiltinOSLogHelperFunction(
1412       Layout, BufAddr.getAlignment());
1413   EmitCall(FI, CGCallee::forDirect(F), ReturnValueSlot(), Args);
1414   return RValue::get(BufAddr.getPointer());
1415 }
1416
1417 /// Determine if a binop is a checked mixed-sign multiply we can specialize.
1418 static bool isSpecialMixedSignMultiply(unsigned BuiltinID,
1419                                        WidthAndSignedness Op1Info,
1420                                        WidthAndSignedness Op2Info,
1421                                        WidthAndSignedness ResultInfo) {
1422   return BuiltinID == Builtin::BI__builtin_mul_overflow &&
1423          std::max(Op1Info.Width, Op2Info.Width) >= ResultInfo.Width &&
1424          Op1Info.Signed != Op2Info.Signed;
1425 }
1426
1427 /// Emit a checked mixed-sign multiply. This is a cheaper specialization of
1428 /// the generic checked-binop irgen.
1429 static RValue
1430 EmitCheckedMixedSignMultiply(CodeGenFunction &CGF, const clang::Expr *Op1,
1431                              WidthAndSignedness Op1Info, const clang::Expr *Op2,
1432                              WidthAndSignedness Op2Info,
1433                              const clang::Expr *ResultArg, QualType ResultQTy,
1434                              WidthAndSignedness ResultInfo) {
1435   assert(isSpecialMixedSignMultiply(Builtin::BI__builtin_mul_overflow, Op1Info,
1436                                     Op2Info, ResultInfo) &&
1437          "Not a mixed-sign multipliction we can specialize");
1438
1439   // Emit the signed and unsigned operands.
1440   const clang::Expr *SignedOp = Op1Info.Signed ? Op1 : Op2;
1441   const clang::Expr *UnsignedOp = Op1Info.Signed ? Op2 : Op1;
1442   llvm::Value *Signed = CGF.EmitScalarExpr(SignedOp);
1443   llvm::Value *Unsigned = CGF.EmitScalarExpr(UnsignedOp);
1444   unsigned SignedOpWidth = Op1Info.Signed ? Op1Info.Width : Op2Info.Width;
1445   unsigned UnsignedOpWidth = Op1Info.Signed ? Op2Info.Width : Op1Info.Width;
1446
1447   // One of the operands may be smaller than the other. If so, [s|z]ext it.
1448   if (SignedOpWidth < UnsignedOpWidth)
1449     Signed = CGF.Builder.CreateSExt(Signed, Unsigned->getType(), "op.sext");
1450   if (UnsignedOpWidth < SignedOpWidth)
1451     Unsigned = CGF.Builder.CreateZExt(Unsigned, Signed->getType(), "op.zext");
1452
1453   llvm::Type *OpTy = Signed->getType();
1454   llvm::Value *Zero = llvm::Constant::getNullValue(OpTy);
1455   Address ResultPtr = CGF.EmitPointerWithAlignment(ResultArg);
1456   llvm::Type *ResTy = ResultPtr.getElementType();
1457   unsigned OpWidth = std::max(Op1Info.Width, Op2Info.Width);
1458
1459   // Take the absolute value of the signed operand.
1460   llvm::Value *IsNegative = CGF.Builder.CreateICmpSLT(Signed, Zero);
1461   llvm::Value *AbsOfNegative = CGF.Builder.CreateSub(Zero, Signed);
1462   llvm::Value *AbsSigned =
1463       CGF.Builder.CreateSelect(IsNegative, AbsOfNegative, Signed);
1464
1465   // Perform a checked unsigned multiplication.
1466   llvm::Value *UnsignedOverflow;
1467   llvm::Value *UnsignedResult =
1468       EmitOverflowIntrinsic(CGF, llvm::Intrinsic::umul_with_overflow, AbsSigned,
1469                             Unsigned, UnsignedOverflow);
1470
1471   llvm::Value *Overflow, *Result;
1472   if (ResultInfo.Signed) {
1473     // Signed overflow occurs if the result is greater than INT_MAX or lesser
1474     // than INT_MIN, i.e when |Result| > (INT_MAX + IsNegative).
1475     auto IntMax =
1476         llvm::APInt::getSignedMaxValue(ResultInfo.Width).zextOrSelf(OpWidth);
1477     llvm::Value *MaxResult =
1478         CGF.Builder.CreateAdd(llvm::ConstantInt::get(OpTy, IntMax),
1479                               CGF.Builder.CreateZExt(IsNegative, OpTy));
1480     llvm::Value *SignedOverflow =
1481         CGF.Builder.CreateICmpUGT(UnsignedResult, MaxResult);
1482     Overflow = CGF.Builder.CreateOr(UnsignedOverflow, SignedOverflow);
1483
1484     // Prepare the signed result (possibly by negating it).
1485     llvm::Value *NegativeResult = CGF.Builder.CreateNeg(UnsignedResult);
1486     llvm::Value *SignedResult =
1487         CGF.Builder.CreateSelect(IsNegative, NegativeResult, UnsignedResult);
1488     Result = CGF.Builder.CreateTrunc(SignedResult, ResTy);
1489   } else {
1490     // Unsigned overflow occurs if the result is < 0 or greater than UINT_MAX.
1491     llvm::Value *Underflow = CGF.Builder.CreateAnd(
1492         IsNegative, CGF.Builder.CreateIsNotNull(UnsignedResult));
1493     Overflow = CGF.Builder.CreateOr(UnsignedOverflow, Underflow);
1494     if (ResultInfo.Width < OpWidth) {
1495       auto IntMax =
1496           llvm::APInt::getMaxValue(ResultInfo.Width).zext(OpWidth);
1497       llvm::Value *TruncOverflow = CGF.Builder.CreateICmpUGT(
1498           UnsignedResult, llvm::ConstantInt::get(OpTy, IntMax));
1499       Overflow = CGF.Builder.CreateOr(Overflow, TruncOverflow);
1500     }
1501
1502     // Negate the product if it would be negative in infinite precision.
1503     Result = CGF.Builder.CreateSelect(
1504         IsNegative, CGF.Builder.CreateNeg(UnsignedResult), UnsignedResult);
1505
1506     Result = CGF.Builder.CreateTrunc(Result, ResTy);
1507   }
1508   assert(Overflow && Result && "Missing overflow or result");
1509
1510   bool isVolatile =
1511       ResultArg->getType()->getPointeeType().isVolatileQualified();
1512   CGF.Builder.CreateStore(CGF.EmitToMemory(Result, ResultQTy), ResultPtr,
1513                           isVolatile);
1514   return RValue::get(Overflow);
1515 }
1516
1517 static llvm::Value *dumpRecord(CodeGenFunction &CGF, QualType RType,
1518                                Value *&RecordPtr, CharUnits Align,
1519                                llvm::FunctionCallee Func, int Lvl) {
1520   ASTContext &Context = CGF.getContext();
1521   RecordDecl *RD = RType->castAs<RecordType>()->getDecl()->getDefinition();
1522   std::string Pad = std::string(Lvl * 4, ' ');
1523
1524   Value *GString =
1525       CGF.Builder.CreateGlobalStringPtr(RType.getAsString() + " {\n");
1526   Value *Res = CGF.Builder.CreateCall(Func, {GString});
1527
1528   static llvm::DenseMap<QualType, const char *> Types;
1529   if (Types.empty()) {
1530     Types[Context.CharTy] = "%c";
1531     Types[Context.BoolTy] = "%d";
1532     Types[Context.SignedCharTy] = "%hhd";
1533     Types[Context.UnsignedCharTy] = "%hhu";
1534     Types[Context.IntTy] = "%d";
1535     Types[Context.UnsignedIntTy] = "%u";
1536     Types[Context.LongTy] = "%ld";
1537     Types[Context.UnsignedLongTy] = "%lu";
1538     Types[Context.LongLongTy] = "%lld";
1539     Types[Context.UnsignedLongLongTy] = "%llu";
1540     Types[Context.ShortTy] = "%hd";
1541     Types[Context.UnsignedShortTy] = "%hu";
1542     Types[Context.VoidPtrTy] = "%p";
1543     Types[Context.FloatTy] = "%f";
1544     Types[Context.DoubleTy] = "%f";
1545     Types[Context.LongDoubleTy] = "%Lf";
1546     Types[Context.getPointerType(Context.CharTy)] = "%s";
1547     Types[Context.getPointerType(Context.getConstType(Context.CharTy))] = "%s";
1548   }
1549
1550   for (const auto *FD : RD->fields()) {
1551     Value *FieldPtr = RecordPtr;
1552     if (RD->isUnion())
1553       FieldPtr = CGF.Builder.CreatePointerCast(
1554           FieldPtr, CGF.ConvertType(Context.getPointerType(FD->getType())));
1555     else
1556       FieldPtr = CGF.Builder.CreateStructGEP(CGF.ConvertType(RType), FieldPtr,
1557                                              FD->getFieldIndex());
1558
1559     GString = CGF.Builder.CreateGlobalStringPtr(
1560         llvm::Twine(Pad)
1561             .concat(FD->getType().getAsString())
1562             .concat(llvm::Twine(' '))
1563             .concat(FD->getNameAsString())
1564             .concat(" : ")
1565             .str());
1566     Value *TmpRes = CGF.Builder.CreateCall(Func, {GString});
1567     Res = CGF.Builder.CreateAdd(Res, TmpRes);
1568
1569     QualType CanonicalType =
1570         FD->getType().getUnqualifiedType().getCanonicalType();
1571
1572     // We check whether we are in a recursive type
1573     if (CanonicalType->isRecordType()) {
1574       TmpRes = dumpRecord(CGF, CanonicalType, FieldPtr, Align, Func, Lvl + 1);
1575       Res = CGF.Builder.CreateAdd(TmpRes, Res);
1576       continue;
1577     }
1578
1579     // We try to determine the best format to print the current field
1580     llvm::Twine Format = Types.find(CanonicalType) == Types.end()
1581                              ? Types[Context.VoidPtrTy]
1582                              : Types[CanonicalType];
1583
1584     Address FieldAddress = Address(FieldPtr, Align);
1585     FieldPtr = CGF.Builder.CreateLoad(FieldAddress);
1586
1587     // FIXME Need to handle bitfield here
1588     GString = CGF.Builder.CreateGlobalStringPtr(
1589         Format.concat(llvm::Twine('\n')).str());
1590     TmpRes = CGF.Builder.CreateCall(Func, {GString, FieldPtr});
1591     Res = CGF.Builder.CreateAdd(Res, TmpRes);
1592   }
1593
1594   GString = CGF.Builder.CreateGlobalStringPtr(Pad + "}\n");
1595   Value *TmpRes = CGF.Builder.CreateCall(Func, {GString});
1596   Res = CGF.Builder.CreateAdd(Res, TmpRes);
1597   return Res;
1598 }
1599
1600 static bool
1601 TypeRequiresBuiltinLaunderImp(const ASTContext &Ctx, QualType Ty,
1602                               llvm::SmallPtrSetImpl<const Decl *> &Seen) {
1603   if (const auto *Arr = Ctx.getAsArrayType(Ty))
1604     Ty = Ctx.getBaseElementType(Arr);
1605
1606   const auto *Record = Ty->getAsCXXRecordDecl();
1607   if (!Record)
1608     return false;
1609
1610   // We've already checked this type, or are in the process of checking it.
1611   if (!Seen.insert(Record).second)
1612     return false;
1613
1614   assert(Record->hasDefinition() &&
1615          "Incomplete types should already be diagnosed");
1616
1617   if (Record->isDynamicClass())
1618     return true;
1619
1620   for (FieldDecl *F : Record->fields()) {
1621     if (TypeRequiresBuiltinLaunderImp(Ctx, F->getType(), Seen))
1622       return true;
1623   }
1624   return false;
1625 }
1626
1627 /// Determine if the specified type requires laundering by checking if it is a
1628 /// dynamic class type or contains a subobject which is a dynamic class type.
1629 static bool TypeRequiresBuiltinLaunder(CodeGenModule &CGM, QualType Ty) {
1630   if (!CGM.getCodeGenOpts().StrictVTablePointers)
1631     return false;
1632   llvm::SmallPtrSet<const Decl *, 16> Seen;
1633   return TypeRequiresBuiltinLaunderImp(CGM.getContext(), Ty, Seen);
1634 }
1635
1636 RValue CodeGenFunction::emitRotate(const CallExpr *E, bool IsRotateRight) {
1637   llvm::Value *Src = EmitScalarExpr(E->getArg(0));
1638   llvm::Value *ShiftAmt = EmitScalarExpr(E->getArg(1));
1639
1640   // The builtin's shift arg may have a different type than the source arg and
1641   // result, but the LLVM intrinsic uses the same type for all values.
1642   llvm::Type *Ty = Src->getType();
1643   ShiftAmt = Builder.CreateIntCast(ShiftAmt, Ty, false);
1644
1645   // Rotate is a special case of LLVM funnel shift - 1st 2 args are the same.
1646   unsigned IID = IsRotateRight ? Intrinsic::fshr : Intrinsic::fshl;
1647   Function *F = CGM.getIntrinsic(IID, Ty);
1648   return RValue::get(Builder.CreateCall(F, { Src, Src, ShiftAmt }));
1649 }
1650
1651 RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
1652                                         const CallExpr *E,
1653                                         ReturnValueSlot ReturnValue) {
1654   const FunctionDecl *FD = GD.getDecl()->getAsFunction();
1655   // See if we can constant fold this builtin.  If so, don't emit it at all.
1656   Expr::EvalResult Result;
1657   if (E->EvaluateAsRValue(Result, CGM.getContext()) &&
1658       !Result.hasSideEffects()) {
1659     if (Result.Val.isInt())
1660       return RValue::get(llvm::ConstantInt::get(getLLVMContext(),
1661                                                 Result.Val.getInt()));
1662     if (Result.Val.isFloat())
1663       return RValue::get(llvm::ConstantFP::get(getLLVMContext(),
1664                                                Result.Val.getFloat()));
1665   }
1666
1667   // If the builtin has been declared explicitly with an assembler label,
1668   // disable the specialized emitting below. Ideally we should communicate the
1669   // rename in IR, or at least avoid generating the intrinsic calls that are
1670   // likely to get lowered to the renamed library functions.
1671   const unsigned BuiltinIDIfNoAsmLabel =
1672       FD->hasAttr<AsmLabelAttr>() ? 0 : BuiltinID;
1673
1674   // There are LLVM math intrinsics/instructions corresponding to math library
1675   // functions except the LLVM op will never set errno while the math library
1676   // might. Also, math builtins have the same semantics as their math library
1677   // twins. Thus, we can transform math library and builtin calls to their
1678   // LLVM counterparts if the call is marked 'const' (known to never set errno).
1679   if (FD->hasAttr<ConstAttr>()) {
1680     switch (BuiltinIDIfNoAsmLabel) {
1681     case Builtin::BIceil:
1682     case Builtin::BIceilf:
1683     case Builtin::BIceill:
1684     case Builtin::BI__builtin_ceil:
1685     case Builtin::BI__builtin_ceilf:
1686     case Builtin::BI__builtin_ceilf16:
1687     case Builtin::BI__builtin_ceill:
1688     case Builtin::BI__builtin_ceilf128:
1689       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1690                                    Intrinsic::ceil,
1691                                    Intrinsic::experimental_constrained_ceil));
1692
1693     case Builtin::BIcopysign:
1694     case Builtin::BIcopysignf:
1695     case Builtin::BIcopysignl:
1696     case Builtin::BI__builtin_copysign:
1697     case Builtin::BI__builtin_copysignf:
1698     case Builtin::BI__builtin_copysignf16:
1699     case Builtin::BI__builtin_copysignl:
1700     case Builtin::BI__builtin_copysignf128:
1701       return RValue::get(emitBinaryBuiltin(*this, E, Intrinsic::copysign));
1702
1703     case Builtin::BIcos:
1704     case Builtin::BIcosf:
1705     case Builtin::BIcosl:
1706     case Builtin::BI__builtin_cos:
1707     case Builtin::BI__builtin_cosf:
1708     case Builtin::BI__builtin_cosf16:
1709     case Builtin::BI__builtin_cosl:
1710     case Builtin::BI__builtin_cosf128:
1711       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1712                                    Intrinsic::cos,
1713                                    Intrinsic::experimental_constrained_cos));
1714
1715     case Builtin::BIexp:
1716     case Builtin::BIexpf:
1717     case Builtin::BIexpl:
1718     case Builtin::BI__builtin_exp:
1719     case Builtin::BI__builtin_expf:
1720     case Builtin::BI__builtin_expf16:
1721     case Builtin::BI__builtin_expl:
1722     case Builtin::BI__builtin_expf128:
1723       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1724                                    Intrinsic::exp,
1725                                    Intrinsic::experimental_constrained_exp));
1726
1727     case Builtin::BIexp2:
1728     case Builtin::BIexp2f:
1729     case Builtin::BIexp2l:
1730     case Builtin::BI__builtin_exp2:
1731     case Builtin::BI__builtin_exp2f:
1732     case Builtin::BI__builtin_exp2f16:
1733     case Builtin::BI__builtin_exp2l:
1734     case Builtin::BI__builtin_exp2f128:
1735       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1736                                    Intrinsic::exp2,
1737                                    Intrinsic::experimental_constrained_exp2));
1738
1739     case Builtin::BIfabs:
1740     case Builtin::BIfabsf:
1741     case Builtin::BIfabsl:
1742     case Builtin::BI__builtin_fabs:
1743     case Builtin::BI__builtin_fabsf:
1744     case Builtin::BI__builtin_fabsf16:
1745     case Builtin::BI__builtin_fabsl:
1746     case Builtin::BI__builtin_fabsf128:
1747       return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::fabs));
1748
1749     case Builtin::BIfloor:
1750     case Builtin::BIfloorf:
1751     case Builtin::BIfloorl:
1752     case Builtin::BI__builtin_floor:
1753     case Builtin::BI__builtin_floorf:
1754     case Builtin::BI__builtin_floorf16:
1755     case Builtin::BI__builtin_floorl:
1756     case Builtin::BI__builtin_floorf128:
1757       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1758                                    Intrinsic::floor,
1759                                    Intrinsic::experimental_constrained_floor));
1760
1761     case Builtin::BIfma:
1762     case Builtin::BIfmaf:
1763     case Builtin::BIfmal:
1764     case Builtin::BI__builtin_fma:
1765     case Builtin::BI__builtin_fmaf:
1766     case Builtin::BI__builtin_fmaf16:
1767     case Builtin::BI__builtin_fmal:
1768     case Builtin::BI__builtin_fmaf128:
1769       return RValue::get(emitTernaryMaybeConstrainedFPBuiltin(*this, E,
1770                                    Intrinsic::fma,
1771                                    Intrinsic::experimental_constrained_fma));
1772
1773     case Builtin::BIfmax:
1774     case Builtin::BIfmaxf:
1775     case Builtin::BIfmaxl:
1776     case Builtin::BI__builtin_fmax:
1777     case Builtin::BI__builtin_fmaxf:
1778     case Builtin::BI__builtin_fmaxf16:
1779     case Builtin::BI__builtin_fmaxl:
1780     case Builtin::BI__builtin_fmaxf128:
1781       return RValue::get(emitBinaryMaybeConstrainedFPBuiltin(*this, E,
1782                                    Intrinsic::maxnum,
1783                                    Intrinsic::experimental_constrained_maxnum));
1784
1785     case Builtin::BIfmin:
1786     case Builtin::BIfminf:
1787     case Builtin::BIfminl:
1788     case Builtin::BI__builtin_fmin:
1789     case Builtin::BI__builtin_fminf:
1790     case Builtin::BI__builtin_fminf16:
1791     case Builtin::BI__builtin_fminl:
1792     case Builtin::BI__builtin_fminf128:
1793       return RValue::get(emitBinaryMaybeConstrainedFPBuiltin(*this, E,
1794                                    Intrinsic::minnum,
1795                                    Intrinsic::experimental_constrained_minnum));
1796
1797     // fmod() is a special-case. It maps to the frem instruction rather than an
1798     // LLVM intrinsic.
1799     case Builtin::BIfmod:
1800     case Builtin::BIfmodf:
1801     case Builtin::BIfmodl:
1802     case Builtin::BI__builtin_fmod:
1803     case Builtin::BI__builtin_fmodf:
1804     case Builtin::BI__builtin_fmodf16:
1805     case Builtin::BI__builtin_fmodl:
1806     case Builtin::BI__builtin_fmodf128: {
1807       Value *Arg1 = EmitScalarExpr(E->getArg(0));
1808       Value *Arg2 = EmitScalarExpr(E->getArg(1));
1809       return RValue::get(Builder.CreateFRem(Arg1, Arg2, "fmod"));
1810     }
1811
1812     case Builtin::BIlog:
1813     case Builtin::BIlogf:
1814     case Builtin::BIlogl:
1815     case Builtin::BI__builtin_log:
1816     case Builtin::BI__builtin_logf:
1817     case Builtin::BI__builtin_logf16:
1818     case Builtin::BI__builtin_logl:
1819     case Builtin::BI__builtin_logf128:
1820       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1821                                    Intrinsic::log,
1822                                    Intrinsic::experimental_constrained_log));
1823
1824     case Builtin::BIlog10:
1825     case Builtin::BIlog10f:
1826     case Builtin::BIlog10l:
1827     case Builtin::BI__builtin_log10:
1828     case Builtin::BI__builtin_log10f:
1829     case Builtin::BI__builtin_log10f16:
1830     case Builtin::BI__builtin_log10l:
1831     case Builtin::BI__builtin_log10f128:
1832       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1833                                    Intrinsic::log10,
1834                                    Intrinsic::experimental_constrained_log10));
1835
1836     case Builtin::BIlog2:
1837     case Builtin::BIlog2f:
1838     case Builtin::BIlog2l:
1839     case Builtin::BI__builtin_log2:
1840     case Builtin::BI__builtin_log2f:
1841     case Builtin::BI__builtin_log2f16:
1842     case Builtin::BI__builtin_log2l:
1843     case Builtin::BI__builtin_log2f128:
1844       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1845                                    Intrinsic::log2,
1846                                    Intrinsic::experimental_constrained_log2));
1847
1848     case Builtin::BInearbyint:
1849     case Builtin::BInearbyintf:
1850     case Builtin::BInearbyintl:
1851     case Builtin::BI__builtin_nearbyint:
1852     case Builtin::BI__builtin_nearbyintf:
1853     case Builtin::BI__builtin_nearbyintl:
1854     case Builtin::BI__builtin_nearbyintf128:
1855       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1856                                 Intrinsic::nearbyint,
1857                                 Intrinsic::experimental_constrained_nearbyint));
1858
1859     case Builtin::BIpow:
1860     case Builtin::BIpowf:
1861     case Builtin::BIpowl:
1862     case Builtin::BI__builtin_pow:
1863     case Builtin::BI__builtin_powf:
1864     case Builtin::BI__builtin_powf16:
1865     case Builtin::BI__builtin_powl:
1866     case Builtin::BI__builtin_powf128:
1867       return RValue::get(emitBinaryMaybeConstrainedFPBuiltin(*this, E,
1868                                    Intrinsic::pow,
1869                                    Intrinsic::experimental_constrained_pow));
1870
1871     case Builtin::BIrint:
1872     case Builtin::BIrintf:
1873     case Builtin::BIrintl:
1874     case Builtin::BI__builtin_rint:
1875     case Builtin::BI__builtin_rintf:
1876     case Builtin::BI__builtin_rintf16:
1877     case Builtin::BI__builtin_rintl:
1878     case Builtin::BI__builtin_rintf128:
1879       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1880                                    Intrinsic::rint,
1881                                    Intrinsic::experimental_constrained_rint));
1882
1883     case Builtin::BIround:
1884     case Builtin::BIroundf:
1885     case Builtin::BIroundl:
1886     case Builtin::BI__builtin_round:
1887     case Builtin::BI__builtin_roundf:
1888     case Builtin::BI__builtin_roundf16:
1889     case Builtin::BI__builtin_roundl:
1890     case Builtin::BI__builtin_roundf128:
1891       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1892                                    Intrinsic::round,
1893                                    Intrinsic::experimental_constrained_round));
1894
1895     case Builtin::BIsin:
1896     case Builtin::BIsinf:
1897     case Builtin::BIsinl:
1898     case Builtin::BI__builtin_sin:
1899     case Builtin::BI__builtin_sinf:
1900     case Builtin::BI__builtin_sinf16:
1901     case Builtin::BI__builtin_sinl:
1902     case Builtin::BI__builtin_sinf128:
1903       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1904                                    Intrinsic::sin,
1905                                    Intrinsic::experimental_constrained_sin));
1906
1907     case Builtin::BIsqrt:
1908     case Builtin::BIsqrtf:
1909     case Builtin::BIsqrtl:
1910     case Builtin::BI__builtin_sqrt:
1911     case Builtin::BI__builtin_sqrtf:
1912     case Builtin::BI__builtin_sqrtf16:
1913     case Builtin::BI__builtin_sqrtl:
1914     case Builtin::BI__builtin_sqrtf128:
1915       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1916                                    Intrinsic::sqrt,
1917                                    Intrinsic::experimental_constrained_sqrt));
1918
1919     case Builtin::BItrunc:
1920     case Builtin::BItruncf:
1921     case Builtin::BItruncl:
1922     case Builtin::BI__builtin_trunc:
1923     case Builtin::BI__builtin_truncf:
1924     case Builtin::BI__builtin_truncf16:
1925     case Builtin::BI__builtin_truncl:
1926     case Builtin::BI__builtin_truncf128:
1927       return RValue::get(emitUnaryMaybeConstrainedFPBuiltin(*this, E,
1928                                    Intrinsic::trunc,
1929                                    Intrinsic::experimental_constrained_trunc));
1930
1931     case Builtin::BIlround:
1932     case Builtin::BIlroundf:
1933     case Builtin::BIlroundl:
1934     case Builtin::BI__builtin_lround:
1935     case Builtin::BI__builtin_lroundf:
1936     case Builtin::BI__builtin_lroundl:
1937     case Builtin::BI__builtin_lroundf128:
1938       return RValue::get(emitMaybeConstrainedFPToIntRoundBuiltin(
1939           *this, E, Intrinsic::lround,
1940           Intrinsic::experimental_constrained_lround));
1941
1942     case Builtin::BIllround:
1943     case Builtin::BIllroundf:
1944     case Builtin::BIllroundl:
1945     case Builtin::BI__builtin_llround:
1946     case Builtin::BI__builtin_llroundf:
1947     case Builtin::BI__builtin_llroundl:
1948     case Builtin::BI__builtin_llroundf128:
1949       return RValue::get(emitMaybeConstrainedFPToIntRoundBuiltin(
1950           *this, E, Intrinsic::llround,
1951           Intrinsic::experimental_constrained_llround));
1952
1953     case Builtin::BIlrint:
1954     case Builtin::BIlrintf:
1955     case Builtin::BIlrintl:
1956     case Builtin::BI__builtin_lrint:
1957     case Builtin::BI__builtin_lrintf:
1958     case Builtin::BI__builtin_lrintl:
1959     case Builtin::BI__builtin_lrintf128:
1960       return RValue::get(emitMaybeConstrainedFPToIntRoundBuiltin(
1961           *this, E, Intrinsic::lrint,
1962           Intrinsic::experimental_constrained_lrint));
1963
1964     case Builtin::BIllrint:
1965     case Builtin::BIllrintf:
1966     case Builtin::BIllrintl:
1967     case Builtin::BI__builtin_llrint:
1968     case Builtin::BI__builtin_llrintf:
1969     case Builtin::BI__builtin_llrintl:
1970     case Builtin::BI__builtin_llrintf128:
1971       return RValue::get(emitMaybeConstrainedFPToIntRoundBuiltin(
1972           *this, E, Intrinsic::llrint,
1973           Intrinsic::experimental_constrained_llrint));
1974
1975     default:
1976       break;
1977     }
1978   }
1979
1980   switch (BuiltinIDIfNoAsmLabel) {
1981   default: break;
1982   case Builtin::BI__builtin___CFStringMakeConstantString:
1983   case Builtin::BI__builtin___NSStringMakeConstantString:
1984     return RValue::get(ConstantEmitter(*this).emitAbstract(E, E->getType()));
1985   case Builtin::BI__builtin_stdarg_start:
1986   case Builtin::BI__builtin_va_start:
1987   case Builtin::BI__va_start:
1988   case Builtin::BI__builtin_va_end:
1989     return RValue::get(
1990         EmitVAStartEnd(BuiltinID == Builtin::BI__va_start
1991                            ? EmitScalarExpr(E->getArg(0))
1992                            : EmitVAListRef(E->getArg(0)).getPointer(),
1993                        BuiltinID != Builtin::BI__builtin_va_end));
1994   case Builtin::BI__builtin_va_copy: {
1995     Value *DstPtr = EmitVAListRef(E->getArg(0)).getPointer();
1996     Value *SrcPtr = EmitVAListRef(E->getArg(1)).getPointer();
1997
1998     llvm::Type *Type = Int8PtrTy;
1999
2000     DstPtr = Builder.CreateBitCast(DstPtr, Type);
2001     SrcPtr = Builder.CreateBitCast(SrcPtr, Type);
2002     return RValue::get(Builder.CreateCall(CGM.getIntrinsic(Intrinsic::vacopy),
2003                                           {DstPtr, SrcPtr}));
2004   }
2005   case Builtin::BI__builtin_abs:
2006   case Builtin::BI__builtin_labs:
2007   case Builtin::BI__builtin_llabs: {
2008     // X < 0 ? -X : X
2009     // The negation has 'nsw' because abs of INT_MIN is undefined.
2010     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2011     Value *NegOp = Builder.CreateNSWNeg(ArgValue, "neg");
2012     Constant *Zero = llvm::Constant::getNullValue(ArgValue->getType());
2013     Value *CmpResult = Builder.CreateICmpSLT(ArgValue, Zero, "abscond");
2014     Value *Result = Builder.CreateSelect(CmpResult, NegOp, ArgValue, "abs");
2015     return RValue::get(Result);
2016   }
2017   case Builtin::BI__builtin_complex: {
2018     Value *Real = EmitScalarExpr(E->getArg(0));
2019     Value *Imag = EmitScalarExpr(E->getArg(1));
2020     return RValue::getComplex({Real, Imag});
2021   }
2022   case Builtin::BI__builtin_conj:
2023   case Builtin::BI__builtin_conjf:
2024   case Builtin::BI__builtin_conjl:
2025   case Builtin::BIconj:
2026   case Builtin::BIconjf:
2027   case Builtin::BIconjl: {
2028     ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
2029     Value *Real = ComplexVal.first;
2030     Value *Imag = ComplexVal.second;
2031     Imag = Builder.CreateFNeg(Imag, "neg");
2032     return RValue::getComplex(std::make_pair(Real, Imag));
2033   }
2034   case Builtin::BI__builtin_creal:
2035   case Builtin::BI__builtin_crealf:
2036   case Builtin::BI__builtin_creall:
2037   case Builtin::BIcreal:
2038   case Builtin::BIcrealf:
2039   case Builtin::BIcreall: {
2040     ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
2041     return RValue::get(ComplexVal.first);
2042   }
2043
2044   case Builtin::BI__builtin_dump_struct: {
2045     llvm::Type *LLVMIntTy = getTypes().ConvertType(getContext().IntTy);
2046     llvm::FunctionType *LLVMFuncType = llvm::FunctionType::get(
2047         LLVMIntTy, {llvm::Type::getInt8PtrTy(getLLVMContext())}, true);
2048
2049     Value *Func = EmitScalarExpr(E->getArg(1)->IgnoreImpCasts());
2050     CharUnits Arg0Align = EmitPointerWithAlignment(E->getArg(0)).getAlignment();
2051
2052     const Expr *Arg0 = E->getArg(0)->IgnoreImpCasts();
2053     QualType Arg0Type = Arg0->getType()->getPointeeType();
2054
2055     Value *RecordPtr = EmitScalarExpr(Arg0);
2056     Value *Res = dumpRecord(*this, Arg0Type, RecordPtr, Arg0Align,
2057                             {LLVMFuncType, Func}, 0);
2058     return RValue::get(Res);
2059   }
2060
2061   case Builtin::BI__builtin_preserve_access_index: {
2062     // Only enabled preserved access index region when debuginfo
2063     // is available as debuginfo is needed to preserve user-level
2064     // access pattern.
2065     if (!getDebugInfo()) {
2066       CGM.Error(E->getExprLoc(), "using builtin_preserve_access_index() without -g");
2067       return RValue::get(EmitScalarExpr(E->getArg(0)));
2068     }
2069
2070     // Nested builtin_preserve_access_index() not supported
2071     if (IsInPreservedAIRegion) {
2072       CGM.Error(E->getExprLoc(), "nested builtin_preserve_access_index() not supported");
2073       return RValue::get(EmitScalarExpr(E->getArg(0)));
2074     }
2075
2076     IsInPreservedAIRegion = true;
2077     Value *Res = EmitScalarExpr(E->getArg(0));
2078     IsInPreservedAIRegion = false;
2079     return RValue::get(Res);
2080   }
2081
2082   case Builtin::BI__builtin_cimag:
2083   case Builtin::BI__builtin_cimagf:
2084   case Builtin::BI__builtin_cimagl:
2085   case Builtin::BIcimag:
2086   case Builtin::BIcimagf:
2087   case Builtin::BIcimagl: {
2088     ComplexPairTy ComplexVal = EmitComplexExpr(E->getArg(0));
2089     return RValue::get(ComplexVal.second);
2090   }
2091
2092   case Builtin::BI__builtin_clrsb:
2093   case Builtin::BI__builtin_clrsbl:
2094   case Builtin::BI__builtin_clrsbll: {
2095     // clrsb(x) -> clz(x < 0 ? ~x : x) - 1 or
2096     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2097
2098     llvm::Type *ArgType = ArgValue->getType();
2099     Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
2100
2101     llvm::Type *ResultType = ConvertType(E->getType());
2102     Value *Zero = llvm::Constant::getNullValue(ArgType);
2103     Value *IsNeg = Builder.CreateICmpSLT(ArgValue, Zero, "isneg");
2104     Value *Inverse = Builder.CreateNot(ArgValue, "not");
2105     Value *Tmp = Builder.CreateSelect(IsNeg, Inverse, ArgValue);
2106     Value *Ctlz = Builder.CreateCall(F, {Tmp, Builder.getFalse()});
2107     Value *Result = Builder.CreateSub(Ctlz, llvm::ConstantInt::get(ArgType, 1));
2108     Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
2109                                    "cast");
2110     return RValue::get(Result);
2111   }
2112   case Builtin::BI__builtin_ctzs:
2113   case Builtin::BI__builtin_ctz:
2114   case Builtin::BI__builtin_ctzl:
2115   case Builtin::BI__builtin_ctzll: {
2116     Value *ArgValue = EmitCheckedArgForBuiltin(E->getArg(0), BCK_CTZPassedZero);
2117
2118     llvm::Type *ArgType = ArgValue->getType();
2119     Function *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
2120
2121     llvm::Type *ResultType = ConvertType(E->getType());
2122     Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef());
2123     Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef});
2124     if (Result->getType() != ResultType)
2125       Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
2126                                      "cast");
2127     return RValue::get(Result);
2128   }
2129   case Builtin::BI__builtin_clzs:
2130   case Builtin::BI__builtin_clz:
2131   case Builtin::BI__builtin_clzl:
2132   case Builtin::BI__builtin_clzll: {
2133     Value *ArgValue = EmitCheckedArgForBuiltin(E->getArg(0), BCK_CLZPassedZero);
2134
2135     llvm::Type *ArgType = ArgValue->getType();
2136     Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
2137
2138     llvm::Type *ResultType = ConvertType(E->getType());
2139     Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef());
2140     Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef});
2141     if (Result->getType() != ResultType)
2142       Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
2143                                      "cast");
2144     return RValue::get(Result);
2145   }
2146   case Builtin::BI__builtin_ffs:
2147   case Builtin::BI__builtin_ffsl:
2148   case Builtin::BI__builtin_ffsll: {
2149     // ffs(x) -> x ? cttz(x) + 1 : 0
2150     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2151
2152     llvm::Type *ArgType = ArgValue->getType();
2153     Function *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
2154
2155     llvm::Type *ResultType = ConvertType(E->getType());
2156     Value *Tmp =
2157         Builder.CreateAdd(Builder.CreateCall(F, {ArgValue, Builder.getTrue()}),
2158                           llvm::ConstantInt::get(ArgType, 1));
2159     Value *Zero = llvm::Constant::getNullValue(ArgType);
2160     Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero");
2161     Value *Result = Builder.CreateSelect(IsZero, Zero, Tmp, "ffs");
2162     if (Result->getType() != ResultType)
2163       Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
2164                                      "cast");
2165     return RValue::get(Result);
2166   }
2167   case Builtin::BI__builtin_parity:
2168   case Builtin::BI__builtin_parityl:
2169   case Builtin::BI__builtin_parityll: {
2170     // parity(x) -> ctpop(x) & 1
2171     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2172
2173     llvm::Type *ArgType = ArgValue->getType();
2174     Function *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
2175
2176     llvm::Type *ResultType = ConvertType(E->getType());
2177     Value *Tmp = Builder.CreateCall(F, ArgValue);
2178     Value *Result = Builder.CreateAnd(Tmp, llvm::ConstantInt::get(ArgType, 1));
2179     if (Result->getType() != ResultType)
2180       Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
2181                                      "cast");
2182     return RValue::get(Result);
2183   }
2184   case Builtin::BI__lzcnt16:
2185   case Builtin::BI__lzcnt:
2186   case Builtin::BI__lzcnt64: {
2187     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2188
2189     llvm::Type *ArgType = ArgValue->getType();
2190     Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
2191
2192     llvm::Type *ResultType = ConvertType(E->getType());
2193     Value *Result = Builder.CreateCall(F, {ArgValue, Builder.getFalse()});
2194     if (Result->getType() != ResultType)
2195       Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
2196                                      "cast");
2197     return RValue::get(Result);
2198   }
2199   case Builtin::BI__popcnt16:
2200   case Builtin::BI__popcnt:
2201   case Builtin::BI__popcnt64:
2202   case Builtin::BI__builtin_popcount:
2203   case Builtin::BI__builtin_popcountl:
2204   case Builtin::BI__builtin_popcountll: {
2205     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2206
2207     llvm::Type *ArgType = ArgValue->getType();
2208     Function *F = CGM.getIntrinsic(Intrinsic::ctpop, ArgType);
2209
2210     llvm::Type *ResultType = ConvertType(E->getType());
2211     Value *Result = Builder.CreateCall(F, ArgValue);
2212     if (Result->getType() != ResultType)
2213       Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
2214                                      "cast");
2215     return RValue::get(Result);
2216   }
2217   case Builtin::BI__builtin_unpredictable: {
2218     // Always return the argument of __builtin_unpredictable. LLVM does not
2219     // handle this builtin. Metadata for this builtin should be added directly
2220     // to instructions such as branches or switches that use it.
2221     return RValue::get(EmitScalarExpr(E->getArg(0)));
2222   }
2223   case Builtin::BI__builtin_expect: {
2224     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2225     llvm::Type *ArgType = ArgValue->getType();
2226
2227     Value *ExpectedValue = EmitScalarExpr(E->getArg(1));
2228     // Don't generate llvm.expect on -O0 as the backend won't use it for
2229     // anything.
2230     // Note, we still IRGen ExpectedValue because it could have side-effects.
2231     if (CGM.getCodeGenOpts().OptimizationLevel == 0)
2232       return RValue::get(ArgValue);
2233
2234     Function *FnExpect = CGM.getIntrinsic(Intrinsic::expect, ArgType);
2235     Value *Result =
2236         Builder.CreateCall(FnExpect, {ArgValue, ExpectedValue}, "expval");
2237     return RValue::get(Result);
2238   }
2239   case Builtin::BI__builtin_expect_with_probability: {
2240     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2241     llvm::Type *ArgType = ArgValue->getType();
2242
2243     Value *ExpectedValue = EmitScalarExpr(E->getArg(1));
2244     llvm::APFloat Probability(0.0);
2245     const Expr *ProbArg = E->getArg(2);
2246     bool EvalSucceed = ProbArg->EvaluateAsFloat(Probability, CGM.getContext());
2247     assert(EvalSucceed && "probability should be able to evaluate as float");
2248     (void)EvalSucceed;
2249     bool LoseInfo = false;
2250     Probability.convert(llvm::APFloat::IEEEdouble(),
2251                         llvm::RoundingMode::Dynamic, &LoseInfo);
2252     llvm::Type *Ty = ConvertType(ProbArg->getType());
2253     Constant *Confidence = ConstantFP::get(Ty, Probability);
2254     // Don't generate llvm.expect.with.probability on -O0 as the backend
2255     // won't use it for anything.
2256     // Note, we still IRGen ExpectedValue because it could have side-effects.
2257     if (CGM.getCodeGenOpts().OptimizationLevel == 0)
2258       return RValue::get(ArgValue);
2259
2260     Function *FnExpect =
2261         CGM.getIntrinsic(Intrinsic::expect_with_probability, ArgType);
2262     Value *Result = Builder.CreateCall(
2263         FnExpect, {ArgValue, ExpectedValue, Confidence}, "expval");
2264     return RValue::get(Result);
2265   }
2266   case Builtin::BI__builtin_assume_aligned: {
2267     const Expr *Ptr = E->getArg(0);
2268     Value *PtrValue = EmitScalarExpr(Ptr);
2269     Value *OffsetValue =
2270       (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : nullptr;
2271
2272     Value *AlignmentValue = EmitScalarExpr(E->getArg(1));
2273     ConstantInt *AlignmentCI = cast<ConstantInt>(AlignmentValue);
2274     if (AlignmentCI->getValue().ugt(llvm::Value::MaximumAlignment))
2275       AlignmentCI = ConstantInt::get(AlignmentCI->getType(),
2276                                      llvm::Value::MaximumAlignment);
2277
2278     emitAlignmentAssumption(PtrValue, Ptr,
2279                             /*The expr loc is sufficient.*/ SourceLocation(),
2280                             AlignmentCI, OffsetValue);
2281     return RValue::get(PtrValue);
2282   }
2283   case Builtin::BI__assume:
2284   case Builtin::BI__builtin_assume: {
2285     if (E->getArg(0)->HasSideEffects(getContext()))
2286       return RValue::get(nullptr);
2287
2288     Value *ArgValue = EmitScalarExpr(E->getArg(0));
2289     Function *FnAssume = CGM.getIntrinsic(Intrinsic::assume);
2290     return RValue::get(Builder.CreateCall(FnAssume, ArgValue));
2291   }
2292   case Builtin::BI__builtin_bswap16:
2293   case Builtin::BI__builtin_bswap32:
2294   case Builtin::BI__builtin_bswap64: {
2295     return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::bswap));
2296   }
2297   case Builtin::BI__builtin_bitreverse8:
2298   case Builtin::BI__builtin_bitreverse16:
2299   case Builtin::BI__builtin_bitreverse32:
2300   case Builtin::BI__builtin_bitreverse64: {
2301     return RValue::get(emitUnaryBuiltin(*this, E, Intrinsic::bitreverse));
2302   }
2303   case Builtin::BI__builtin_rotateleft8:
2304   case Builtin::BI__builtin_rotateleft16:
2305   case Builtin::BI__builtin_rotateleft32:
2306   case Builtin::BI__builtin_rotateleft64:
2307   case Builtin::BI_rotl8: // Microsoft variants of rotate left
2308   case Builtin::BI_rotl16:
2309   case Builtin::BI_rotl:
2310   case Builtin::BI_lrotl:
2311   case Builtin::BI_rotl64:
2312     return emitRotate(E, false);
2313
2314   case Builtin::BI__builtin_rotateright8:
2315   case Builtin::BI__builtin_rotateright16:
2316   case Builtin::BI__builtin_rotateright32:
2317   case Builtin::BI__builtin_rotateright64:
2318   case Builtin::BI_rotr8: // Microsoft variants of rotate right
2319   case Builtin::BI_rotr16:
2320   case Builtin::BI_rotr:
2321   case Builtin::BI_lrotr:
2322   case Builtin::BI_rotr64:
2323     return emitRotate(E, true);
2324
2325   case Builtin::BI__builtin_constant_p: {
2326     llvm::Type *ResultType = ConvertType(E->getType());
2327
2328     const Expr *Arg = E->getArg(0);
2329     QualType ArgType = Arg->getType();
2330     // FIXME: The allowance for Obj-C pointers and block pointers is historical
2331     // and likely a mistake.
2332     if (!ArgType->isIntegralOrEnumerationType() && !ArgType->isFloatingType() &&
2333         !ArgType->isObjCObjectPointerType() && !ArgType->isBlockPointerType())
2334       // Per the GCC documentation, only numeric constants are recognized after
2335       // inlining.
2336       return RValue::get(ConstantInt::get(ResultType, 0));
2337
2338     if (Arg->HasSideEffects(getContext()))
2339       // The argument is unevaluated, so be conservative if it might have
2340       // side-effects.
2341       return RValue::get(ConstantInt::get(ResultType, 0));
2342
2343     Value *ArgValue = EmitScalarExpr(Arg);
2344     if (ArgType->isObjCObjectPointerType()) {
2345       // Convert Objective-C objects to id because we cannot distinguish between
2346       // LLVM types for Obj-C classes as they are opaque.
2347       ArgType = CGM.getContext().getObjCIdType();
2348       ArgValue = Builder.CreateBitCast(ArgValue, ConvertType(ArgType));
2349     }
2350     Function *F =
2351         CGM.getIntrinsic(Intrinsic::is_constant, ConvertType(ArgType));
2352     Value *Result = Builder.CreateCall(F, ArgValue);
2353     if (Result->getType() != ResultType)
2354       Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/false);
2355     return RValue::get(Result);
2356   }
2357   case Builtin::BI__builtin_dynamic_object_size:
2358   case Builtin::BI__builtin_object_size: {
2359     unsigned Type =
2360         E->getArg(1)->EvaluateKnownConstInt(getContext()).getZExtValue();
2361     auto *ResType = cast<llvm::IntegerType>(ConvertType(E->getType()));
2362
2363     // We pass this builtin onto the optimizer so that it can figure out the
2364     // object size in more complex cases.
2365     bool IsDynamic = BuiltinID == Builtin::BI__builtin_dynamic_object_size;
2366     return RValue::get(emitBuiltinObjectSize(E->getArg(0), Type, ResType,
2367                                              /*EmittedE=*/nullptr, IsDynamic));
2368   }
2369   case Builtin::BI__builtin_prefetch: {
2370     Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
2371     // FIXME: Technically these constants should of type 'int', yes?
2372     RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
2373       llvm::ConstantInt::get(Int32Ty, 0);
2374     Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
2375       llvm::ConstantInt::get(Int32Ty, 3);
2376     Value *Data = llvm::ConstantInt::get(Int32Ty, 1);
2377     Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());
2378     return RValue::get(Builder.CreateCall(F, {Address, RW, Locality, Data}));
2379   }
2380   case Builtin::BI__builtin_readcyclecounter: {
2381     Function *F = CGM.getIntrinsic(Intrinsic::readcyclecounter);
2382     return RValue::get(Builder.CreateCall(F));
2383   }
2384   case Builtin::BI__builtin___clear_cache: {
2385     Value *Begin = EmitScalarExpr(E->getArg(0));
2386     Value *End = EmitScalarExpr(E->getArg(1));
2387     Function *F = CGM.getIntrinsic(Intrinsic::clear_cache);
2388     return RValue::get(Builder.CreateCall(F, {Begin, End}));
2389   }
2390   case Builtin::BI__builtin_trap:
2391     return RValue::get(EmitTrapCall(Intrinsic::trap));
2392   case Builtin::BI__debugbreak:
2393     return RValue::get(EmitTrapCall(Intrinsic::debugtrap));
2394   case Builtin::BI__builtin_unreachable: {
2395     EmitUnreachable(E->getExprLoc());
2396
2397     // We do need to preserve an insertion point.
2398     EmitBlock(createBasicBlock("unreachable.cont"));
2399
2400     return RValue::get(nullptr);
2401   }
2402
2403   case Builtin::BI__builtin_powi:
2404   case Builtin::BI__builtin_powif:
2405   case Builtin::BI__builtin_powil:
2406     return RValue::get(emitBinaryMaybeConstrainedFPBuiltin(
2407         *this, E, Intrinsic::powi, Intrinsic::experimental_constrained_powi));
2408
2409   case Builtin::BI__builtin_isgreater:
2410   case Builtin::BI__builtin_isgreaterequal:
2411   case Builtin::BI__builtin_isless:
2412   case Builtin::BI__builtin_islessequal:
2413   case Builtin::BI__builtin_islessgreater:
2414   case Builtin::BI__builtin_isunordered: {
2415     // Ordered comparisons: we know the arguments to these are matching scalar
2416     // floating point values.
2417     Value *LHS = EmitScalarExpr(E->getArg(0));
2418     Value *RHS = EmitScalarExpr(E->getArg(1));
2419
2420     switch (BuiltinID) {
2421     default: llvm_unreachable("Unknown ordered comparison");
2422     case Builtin::BI__builtin_isgreater:
2423       LHS = Builder.CreateFCmpOGT(LHS, RHS, "cmp");
2424       break;
2425     case Builtin::BI__builtin_isgreaterequal:
2426       LHS = Builder.CreateFCmpOGE(LHS, RHS, "cmp");
2427       break;
2428     case Builtin::BI__builtin_isless:
2429       LHS = Builder.CreateFCmpOLT(LHS, RHS, "cmp");
2430       break;
2431     case Builtin::BI__builtin_islessequal:
2432       LHS = Builder.CreateFCmpOLE(LHS, RHS, "cmp");
2433       break;
2434     case Builtin::BI__builtin_islessgreater:
2435       LHS = Builder.CreateFCmpONE(LHS, RHS, "cmp");
2436       break;
2437     case Builtin::BI__builtin_isunordered:
2438       LHS = Builder.CreateFCmpUNO(LHS, RHS, "cmp");
2439       break;
2440     }
2441     // ZExt bool to int type.
2442     return RValue::get(Builder.CreateZExt(LHS, ConvertType(E->getType())));
2443   }
2444   case Builtin::BI__builtin_isnan: {
2445     Value *V = EmitScalarExpr(E->getArg(0));
2446     V = Builder.CreateFCmpUNO(V, V, "cmp");
2447     return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
2448   }
2449
2450   case Builtin::BI__builtin_matrix_transpose: {
2451     const auto *MatrixTy = E->getArg(0)->getType()->getAs<ConstantMatrixType>();
2452     Value *MatValue = EmitScalarExpr(E->getArg(0));
2453     MatrixBuilder<CGBuilderTy> MB(Builder);
2454     Value *Result = MB.CreateMatrixTranspose(MatValue, MatrixTy->getNumRows(),
2455                                              MatrixTy->getNumColumns());
2456     return RValue::get(Result);
2457   }
2458
2459   case Builtin::BI__builtin_matrix_column_major_load: {
2460     MatrixBuilder<CGBuilderTy> MB(Builder);
2461     // Emit everything that isn't dependent on the first parameter type
2462     Value *Stride = EmitScalarExpr(E->getArg(3));
2463     const auto *ResultTy = E->getType()->getAs<ConstantMatrixType>();
2464     auto *PtrTy = E->getArg(0)->getType()->getAs<PointerType>();
2465     assert(PtrTy && "arg0 must be of pointer type");
2466     bool IsVolatile = PtrTy->getPointeeType().isVolatileQualified();
2467
2468     Address Src = EmitPointerWithAlignment(E->getArg(0));
2469     EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(0)->getType(),
2470                         E->getArg(0)->getExprLoc(), FD, 0);
2471     Value *Result = MB.CreateColumnMajorLoad(
2472         Src.getPointer(), Align(Src.getAlignment().getQuantity()), Stride,
2473         IsVolatile, ResultTy->getNumRows(), ResultTy->getNumColumns(),
2474         "matrix");
2475     return RValue::get(Result);
2476   }
2477
2478   case Builtin::BI__builtin_matrix_column_major_store: {
2479     MatrixBuilder<CGBuilderTy> MB(Builder);
2480     Value *Matrix = EmitScalarExpr(E->getArg(0));
2481     Address Dst = EmitPointerWithAlignment(E->getArg(1));
2482     Value *Stride = EmitScalarExpr(E->getArg(2));
2483
2484     const auto *MatrixTy = E->getArg(0)->getType()->getAs<ConstantMatrixType>();
2485     auto *PtrTy = E->getArg(1)->getType()->getAs<PointerType>();
2486     assert(PtrTy && "arg1 must be of pointer type");
2487     bool IsVolatile = PtrTy->getPointeeType().isVolatileQualified();
2488
2489     EmitNonNullArgCheck(RValue::get(Dst.getPointer()), E->getArg(1)->getType(),
2490                         E->getArg(1)->getExprLoc(), FD, 0);
2491     Value *Result = MB.CreateColumnMajorStore(
2492         Matrix, Dst.getPointer(), Align(Dst.getAlignment().getQuantity()),
2493         Stride, IsVolatile, MatrixTy->getNumRows(), MatrixTy->getNumColumns());
2494     return RValue::get(Result);
2495   }
2496
2497   case Builtin::BIfinite:
2498   case Builtin::BI__finite:
2499   case Builtin::BIfinitef:
2500   case Builtin::BI__finitef:
2501   case Builtin::BIfinitel:
2502   case Builtin::BI__finitel:
2503   case Builtin::BI__builtin_isinf:
2504   case Builtin::BI__builtin_isfinite: {
2505     // isinf(x)    --> fabs(x) == infinity
2506     // isfinite(x) --> fabs(x) != infinity
2507     // x != NaN via the ordered compare in either case.
2508     Value *V = EmitScalarExpr(E->getArg(0));
2509     Value *Fabs = EmitFAbs(*this, V);
2510     Constant *Infinity = ConstantFP::getInfinity(V->getType());
2511     CmpInst::Predicate Pred = (BuiltinID == Builtin::BI__builtin_isinf)
2512                                   ? CmpInst::FCMP_OEQ
2513                                   : CmpInst::FCMP_ONE;
2514     Value *FCmp = Builder.CreateFCmp(Pred, Fabs, Infinity, "cmpinf");
2515     return RValue::get(Builder.CreateZExt(FCmp, ConvertType(E->getType())));
2516   }
2517
2518   case Builtin::BI__builtin_isinf_sign: {
2519     // isinf_sign(x) -> fabs(x) == infinity ? (signbit(x) ? -1 : 1) : 0
2520     Value *Arg = EmitScalarExpr(E->getArg(0));
2521     Value *AbsArg = EmitFAbs(*this, Arg);
2522     Value *IsInf = Builder.CreateFCmpOEQ(
2523         AbsArg, ConstantFP::getInfinity(Arg->getType()), "isinf");
2524     Value *IsNeg = EmitSignBit(*this, Arg);
2525
2526     llvm::Type *IntTy = ConvertType(E->getType());
2527     Value *Zero = Constant::getNullValue(IntTy);
2528     Value *One = ConstantInt::get(IntTy, 1);
2529     Value *NegativeOne = ConstantInt::get(IntTy, -1);
2530     Value *SignResult = Builder.CreateSelect(IsNeg, NegativeOne, One);
2531     Value *Result = Builder.CreateSelect(IsInf, SignResult, Zero);
2532     return RValue::get(Result);
2533   }
2534
2535   case Builtin::BI__builtin_isnormal: {
2536     // isnormal(x) --> x == x && fabsf(x) < infinity && fabsf(x) >= float_min
2537     Value *V = EmitScalarExpr(E->getArg(0));
2538     Value *Eq = Builder.CreateFCmpOEQ(V, V, "iseq");
2539
2540     Value *Abs = EmitFAbs(*this, V);
2541     Value *IsLessThanInf =
2542       Builder.CreateFCmpULT(Abs, ConstantFP::getInfinity(V->getType()),"isinf");
2543     APFloat Smallest = APFloat::getSmallestNormalized(
2544                    getContext().getFloatTypeSemantics(E->getArg(0)->getType()));
2545     Value *IsNormal =
2546       Builder.CreateFCmpUGE(Abs, ConstantFP::get(V->getContext(), Smallest),
2547                             "isnormal");
2548     V = Builder.CreateAnd(Eq, IsLessThanInf, "and");
2549     V = Builder.CreateAnd(V, IsNormal, "and");
2550     return RValue::get(Builder.CreateZExt(V, ConvertType(E->getType())));
2551   }
2552
2553   case Builtin::BI__builtin_flt_rounds: {
2554     Function *F = CGM.getIntrinsic(Intrinsic::flt_rounds);
2555
2556     llvm::Type *ResultType = ConvertType(E->getType());
2557     Value *Result = Builder.CreateCall(F);
2558     if (Result->getType() != ResultType)
2559       Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
2560                                      "cast");
2561     return RValue::get(Result);
2562   }
2563
2564   case Builtin::BI__builtin_fpclassify: {
2565     Value *V = EmitScalarExpr(E->getArg(5));
2566     llvm::Type *Ty = ConvertType(E->getArg(5)->getType());
2567
2568     // Create Result
2569     BasicBlock *Begin = Builder.GetInsertBlock();
2570     BasicBlock *End = createBasicBlock("fpclassify_end", this->CurFn);
2571     Builder.SetInsertPoint(End);
2572     PHINode *Result =
2573       Builder.CreatePHI(ConvertType(E->getArg(0)->getType()), 4,
2574                         "fpclassify_result");
2575
2576     // if (V==0) return FP_ZERO
2577     Builder.SetInsertPoint(Begin);
2578     Value *IsZero = Builder.CreateFCmpOEQ(V, Constant::getNullValue(Ty),
2579                                           "iszero");
2580     Value *ZeroLiteral = EmitScalarExpr(E->getArg(4));
2581     BasicBlock *NotZero = createBasicBlock("fpclassify_not_zero", this->CurFn);
2582     Builder.CreateCondBr(IsZero, End, NotZero);
2583     Result->addIncoming(ZeroLiteral, Begin);
2584
2585     // if (V != V) return FP_NAN
2586     Builder.SetInsertPoint(NotZero);
2587     Value *IsNan = Builder.CreateFCmpUNO(V, V, "cmp");
2588     Value *NanLiteral = EmitScalarExpr(E->getArg(0));
2589     BasicBlock *NotNan = createBasicBlock("fpclassify_not_nan", this->CurFn);
2590     Builder.CreateCondBr(IsNan, End, NotNan);
2591     Result->addIncoming(NanLiteral, NotZero);
2592
2593     // if (fabs(V) == infinity) return FP_INFINITY
2594     Builder.SetInsertPoint(NotNan);
2595     Value *VAbs = EmitFAbs(*this, V);
2596     Value *IsInf =
2597       Builder.CreateFCmpOEQ(VAbs, ConstantFP::getInfinity(V->getType()),
2598                             "isinf");
2599     Value *InfLiteral = EmitScalarExpr(E->getArg(1));
2600     BasicBlock *NotInf = createBasicBlock("fpclassify_not_inf", this->CurFn);
2601     Builder.CreateCondBr(IsInf, End, NotInf);
2602     Result->addIncoming(InfLiteral, NotNan);
2603
2604     // if (fabs(V) >= MIN_NORMAL) return FP_NORMAL else FP_SUBNORMAL
2605     Builder.SetInsertPoint(NotInf);
2606     APFloat Smallest = APFloat::getSmallestNormalized(
2607         getContext().getFloatTypeSemantics(E->getArg(5)->getType()));
2608     Value *IsNormal =
2609       Builder.CreateFCmpUGE(VAbs, ConstantFP::get(V->getContext(), Smallest),
2610                             "isnormal");
2611     Value *NormalResult =
2612       Builder.CreateSelect(IsNormal, EmitScalarExpr(E->getArg(2)),
2613                            EmitScalarExpr(E->getArg(3)));
2614     Builder.CreateBr(End);
2615     Result->addIncoming(NormalResult, NotInf);
2616
2617     // return Result
2618     Builder.SetInsertPoint(End);
2619     return RValue::get(Result);
2620   }
2621
2622   case Builtin::BIalloca:
2623   case Builtin::BI_alloca:
2624   case Builtin::BI__builtin_alloca: {
2625     Value *Size = EmitScalarExpr(E->getArg(0));
2626     const TargetInfo &TI = getContext().getTargetInfo();
2627     // The alignment of the alloca should correspond to __BIGGEST_ALIGNMENT__.
2628     const Align SuitableAlignmentInBytes =
2629         CGM.getContext()
2630             .toCharUnitsFromBits(TI.getSuitableAlign())
2631             .getAsAlign();
2632     AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
2633     AI->setAlignment(SuitableAlignmentInBytes);
2634     initializeAlloca(*this, AI, Size, SuitableAlignmentInBytes);
2635     return RValue::get(AI);
2636   }
2637
2638   case Builtin::BI__builtin_alloca_with_align: {
2639     Value *Size = EmitScalarExpr(E->getArg(0));
2640     Value *AlignmentInBitsValue = EmitScalarExpr(E->getArg(1));
2641     auto *AlignmentInBitsCI = cast<ConstantInt>(AlignmentInBitsValue);
2642     unsigned AlignmentInBits = AlignmentInBitsCI->getZExtValue();
2643     const Align AlignmentInBytes =
2644         CGM.getContext().toCharUnitsFromBits(AlignmentInBits).getAsAlign();
2645     AllocaInst *AI = Builder.CreateAlloca(Builder.getInt8Ty(), Size);
2646     AI->setAlignment(AlignmentInBytes);
2647     initializeAlloca(*this, AI, Size, AlignmentInBytes);
2648     return RValue::get(AI);
2649   }
2650
2651   case Builtin::BIbzero:
2652   case Builtin::BI__builtin_bzero: {
2653     Address Dest = EmitPointerWithAlignment(E->getArg(0));
2654     Value *SizeVal = EmitScalarExpr(E->getArg(1));
2655     EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2656                         E->getArg(0)->getExprLoc(), FD, 0);
2657     Builder.CreateMemSet(Dest, Builder.getInt8(0), SizeVal, false);
2658     return RValue::get(nullptr);
2659   }
2660   case Builtin::BImemcpy:
2661   case Builtin::BI__builtin_memcpy:
2662   case Builtin::BImempcpy:
2663   case Builtin::BI__builtin_mempcpy: {
2664     Address Dest = EmitPointerWithAlignment(E->getArg(0));
2665     Address Src = EmitPointerWithAlignment(E->getArg(1));
2666     Value *SizeVal = EmitScalarExpr(E->getArg(2));
2667     EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2668                         E->getArg(0)->getExprLoc(), FD, 0);
2669     EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(1)->getType(),
2670                         E->getArg(1)->getExprLoc(), FD, 1);
2671     Builder.CreateMemCpy(Dest, Src, SizeVal, false);
2672     if (BuiltinID == Builtin::BImempcpy ||
2673         BuiltinID == Builtin::BI__builtin_mempcpy)
2674       return RValue::get(Builder.CreateInBoundsGEP(Dest.getPointer(), SizeVal));
2675     else
2676       return RValue::get(Dest.getPointer());
2677   }
2678
2679   case Builtin::BI__builtin_memcpy_inline: {
2680     Address Dest = EmitPointerWithAlignment(E->getArg(0));
2681     Address Src = EmitPointerWithAlignment(E->getArg(1));
2682     uint64_t Size =
2683         E->getArg(2)->EvaluateKnownConstInt(getContext()).getZExtValue();
2684     EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2685                         E->getArg(0)->getExprLoc(), FD, 0);
2686     EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(1)->getType(),
2687                         E->getArg(1)->getExprLoc(), FD, 1);
2688     Builder.CreateMemCpyInline(Dest, Src, Size);
2689     return RValue::get(nullptr);
2690   }
2691
2692   case Builtin::BI__builtin_char_memchr:
2693     BuiltinID = Builtin::BI__builtin_memchr;
2694     break;
2695
2696   case Builtin::BI__builtin___memcpy_chk: {
2697     // fold __builtin_memcpy_chk(x, y, cst1, cst2) to memcpy iff cst1<=cst2.
2698     Expr::EvalResult SizeResult, DstSizeResult;
2699     if (!E->getArg(2)->EvaluateAsInt(SizeResult, CGM.getContext()) ||
2700         !E->getArg(3)->EvaluateAsInt(DstSizeResult, CGM.getContext()))
2701       break;
2702     llvm::APSInt Size = SizeResult.Val.getInt();
2703     llvm::APSInt DstSize = DstSizeResult.Val.getInt();
2704     if (Size.ugt(DstSize))
2705       break;
2706     Address Dest = EmitPointerWithAlignment(E->getArg(0));
2707     Address Src = EmitPointerWithAlignment(E->getArg(1));
2708     Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2709     Builder.CreateMemCpy(Dest, Src, SizeVal, false);
2710     return RValue::get(Dest.getPointer());
2711   }
2712
2713   case Builtin::BI__builtin_objc_memmove_collectable: {
2714     Address DestAddr = EmitPointerWithAlignment(E->getArg(0));
2715     Address SrcAddr = EmitPointerWithAlignment(E->getArg(1));
2716     Value *SizeVal = EmitScalarExpr(E->getArg(2));
2717     CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this,
2718                                                   DestAddr, SrcAddr, SizeVal);
2719     return RValue::get(DestAddr.getPointer());
2720   }
2721
2722   case Builtin::BI__builtin___memmove_chk: {
2723     // fold __builtin_memmove_chk(x, y, cst1, cst2) to memmove iff cst1<=cst2.
2724     Expr::EvalResult SizeResult, DstSizeResult;
2725     if (!E->getArg(2)->EvaluateAsInt(SizeResult, CGM.getContext()) ||
2726         !E->getArg(3)->EvaluateAsInt(DstSizeResult, CGM.getContext()))
2727       break;
2728     llvm::APSInt Size = SizeResult.Val.getInt();
2729     llvm::APSInt DstSize = DstSizeResult.Val.getInt();
2730     if (Size.ugt(DstSize))
2731       break;
2732     Address Dest = EmitPointerWithAlignment(E->getArg(0));
2733     Address Src = EmitPointerWithAlignment(E->getArg(1));
2734     Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2735     Builder.CreateMemMove(Dest, Src, SizeVal, false);
2736     return RValue::get(Dest.getPointer());
2737   }
2738
2739   case Builtin::BImemmove:
2740   case Builtin::BI__builtin_memmove: {
2741     Address Dest = EmitPointerWithAlignment(E->getArg(0));
2742     Address Src = EmitPointerWithAlignment(E->getArg(1));
2743     Value *SizeVal = EmitScalarExpr(E->getArg(2));
2744     EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2745                         E->getArg(0)->getExprLoc(), FD, 0);
2746     EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(1)->getType(),
2747                         E->getArg(1)->getExprLoc(), FD, 1);
2748     Builder.CreateMemMove(Dest, Src, SizeVal, false);
2749     return RValue::get(Dest.getPointer());
2750   }
2751   case Builtin::BImemset:
2752   case Builtin::BI__builtin_memset: {
2753     Address Dest = EmitPointerWithAlignment(E->getArg(0));
2754     Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
2755                                          Builder.getInt8Ty());
2756     Value *SizeVal = EmitScalarExpr(E->getArg(2));
2757     EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(0)->getType(),
2758                         E->getArg(0)->getExprLoc(), FD, 0);
2759     Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
2760     return RValue::get(Dest.getPointer());
2761   }
2762   case Builtin::BI__builtin___memset_chk: {
2763     // fold __builtin_memset_chk(x, y, cst1, cst2) to memset iff cst1<=cst2.
2764     Expr::EvalResult SizeResult, DstSizeResult;
2765     if (!E->getArg(2)->EvaluateAsInt(SizeResult, CGM.getContext()) ||
2766         !E->getArg(3)->EvaluateAsInt(DstSizeResult, CGM.getContext()))
2767       break;
2768     llvm::APSInt Size = SizeResult.Val.getInt();
2769     llvm::APSInt DstSize = DstSizeResult.Val.getInt();
2770     if (Size.ugt(DstSize))
2771       break;
2772     Address Dest = EmitPointerWithAlignment(E->getArg(0));
2773     Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),
2774                                          Builder.getInt8Ty());
2775     Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);
2776     Builder.CreateMemSet(Dest, ByteVal, SizeVal, false);
2777     return RValue::get(Dest.getPointer());
2778   }
2779   case Builtin::BI__builtin_wmemcmp: {
2780     // The MSVC runtime library does not provide a definition of wmemcmp, so we
2781     // need an inline implementation.
2782     if (!getTarget().getTriple().isOSMSVCRT())
2783       break;
2784
2785     llvm::Type *WCharTy = ConvertType(getContext().WCharTy);
2786
2787     Value *Dst = EmitScalarExpr(E->getArg(0));
2788     Value *Src = EmitScalarExpr(E->getArg(1));
2789     Value *Size = EmitScalarExpr(E->getArg(2));
2790
2791     BasicBlock *Entry = Builder.GetInsertBlock();
2792     BasicBlock *CmpGT = createBasicBlock("wmemcmp.gt");
2793     BasicBlock *CmpLT = createBasicBlock("wmemcmp.lt");
2794     BasicBlock *Next = createBasicBlock("wmemcmp.next");
2795     BasicBlock *Exit = createBasicBlock("wmemcmp.exit");
2796     Value *SizeEq0 = Builder.CreateICmpEQ(Size, ConstantInt::get(SizeTy, 0));
2797     Builder.CreateCondBr(SizeEq0, Exit, CmpGT);
2798
2799     EmitBlock(CmpGT);
2800     PHINode *DstPhi = Builder.CreatePHI(Dst->getType(), 2);
2801     DstPhi->addIncoming(Dst, Entry);
2802     PHINode *SrcPhi = Builder.CreatePHI(Src->getType(), 2);
2803     SrcPhi->addIncoming(Src, Entry);
2804     PHINode *SizePhi = Builder.CreatePHI(SizeTy, 2);
2805     SizePhi->addIncoming(Size, Entry);
2806     CharUnits WCharAlign =
2807         getContext().getTypeAlignInChars(getContext().WCharTy);
2808     Value *DstCh = Builder.CreateAlignedLoad(WCharTy, DstPhi, WCharAlign);
2809     Value *SrcCh = Builder.CreateAlignedLoad(WCharTy, SrcPhi, WCharAlign);
2810     Value *DstGtSrc = Builder.CreateICmpUGT(DstCh, SrcCh);
2811     Builder.CreateCondBr(DstGtSrc, Exit, CmpLT);
2812
2813     EmitBlock(CmpLT);
2814     Value *DstLtSrc = Builder.CreateICmpULT(DstCh, SrcCh);
2815     Builder.CreateCondBr(DstLtSrc, Exit, Next);
2816
2817     EmitBlock(Next);
2818     Value *NextDst = Builder.CreateConstInBoundsGEP1_32(WCharTy, DstPhi, 1);
2819     Value *NextSrc = Builder.CreateConstInBoundsGEP1_32(WCharTy, SrcPhi, 1);
2820     Value *NextSize = Builder.CreateSub(SizePhi, ConstantInt::get(SizeTy, 1));
2821     Value *NextSizeEq0 =
2822         Builder.CreateICmpEQ(NextSize, ConstantInt::get(SizeTy, 0));
2823     Builder.CreateCondBr(NextSizeEq0, Exit, CmpGT);
2824     DstPhi->addIncoming(NextDst, Next);
2825     SrcPhi->addIncoming(NextSrc, Next);
2826     SizePhi->addIncoming(NextSize, Next);
2827
2828     EmitBlock(Exit);
2829     PHINode *Ret = Builder.CreatePHI(IntTy, 4);
2830     Ret->addIncoming(ConstantInt::get(IntTy, 0), Entry);
2831     Ret->addIncoming(ConstantInt::get(IntTy, 1), CmpGT);
2832     Ret->addIncoming(ConstantInt::get(IntTy, -1), CmpLT);
2833     Ret->addIncoming(ConstantInt::get(IntTy, 0), Next);
2834     return RValue::get(Ret);
2835   }
2836   case Builtin::BI__builtin_dwarf_cfa: {
2837     // The offset in bytes from the first argument to the CFA.
2838     //
2839     // Why on earth is this in the frontend?  Is there any reason at
2840     // all that the backend can't reasonably determine this while
2841     // lowering llvm.eh.dwarf.cfa()?
2842     //
2843     // TODO: If there's a satisfactory reason, add a target hook for
2844     // this instead of hard-coding 0, which is correct for most targets.
2845     int32_t Offset = 0;
2846
2847     Function *F = CGM.getIntrinsic(Intrinsic::eh_dwarf_cfa);
2848     return RValue::get(Builder.CreateCall(F,
2849                                       llvm::ConstantInt::get(Int32Ty, Offset)));
2850   }
2851   case Builtin::BI__builtin_return_address: {
2852     Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
2853                                                    getContext().UnsignedIntTy);
2854     Function *F = CGM.getIntrinsic(Intrinsic::returnaddress);
2855     return RValue::get(Builder.CreateCall(F, Depth));
2856   }
2857   case Builtin::BI_ReturnAddress: {
2858     Function *F = CGM.getIntrinsic(Intrinsic::returnaddress);
2859     return RValue::get(Builder.CreateCall(F, Builder.getInt32(0)));
2860   }
2861   case Builtin::BI__builtin_frame_address: {
2862     Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
2863                                                    getContext().UnsignedIntTy);
2864     Function *F = CGM.getIntrinsic(Intrinsic::frameaddress, AllocaInt8PtrTy);
2865     return RValue::get(Builder.CreateCall(F, Depth));
2866   }
2867   case Builtin::BI__builtin_extract_return_addr: {
2868     Value *Address = EmitScalarExpr(E->getArg(0));
2869     Value *Result = getTargetHooks().decodeReturnAddress(*this, Address);
2870     return RValue::get(Result);
2871   }
2872   case Builtin::BI__builtin_frob_return_addr: {
2873     Value *Address = EmitScalarExpr(E->getArg(0));
2874     Value *Result = getTargetHooks().encodeReturnAddress(*this, Address);
2875     return RValue::get(Result);
2876   }
2877   case Builtin::BI__builtin_dwarf_sp_column: {
2878     llvm::IntegerType *Ty
2879       = cast<llvm::IntegerType>(ConvertType(E->getType()));
2880     int Column = getTargetHooks().getDwarfEHStackPointer(CGM);
2881     if (Column == -1) {
2882       CGM.ErrorUnsupported(E, "__builtin_dwarf_sp_column");
2883       return RValue::get(llvm::UndefValue::get(Ty));
2884     }
2885     return RValue::get(llvm::ConstantInt::get(Ty, Column, true));
2886   }
2887   case Builtin::BI__builtin_init_dwarf_reg_size_table: {
2888     Value *Address = EmitScalarExpr(E->getArg(0));
2889     if (getTargetHooks().initDwarfEHRegSizeTable(*this, Address))
2890       CGM.ErrorUnsupported(E, "__builtin_init_dwarf_reg_size_table");
2891     return RValue::get(llvm::UndefValue::get(ConvertType(E->getType())));
2892   }
2893   case Builtin::BI__builtin_eh_return: {
2894     Value *Int = EmitScalarExpr(E->getArg(0));
2895     Value *Ptr = EmitScalarExpr(E->getArg(1));
2896
2897     llvm::IntegerType *IntTy = cast<llvm::IntegerType>(Int->getType());
2898     assert((IntTy->getBitWidth() == 32 || IntTy->getBitWidth() == 64) &&
2899            "LLVM's __builtin_eh_return only supports 32- and 64-bit variants");
2900     Function *F =
2901         CGM.getIntrinsic(IntTy->getBitWidth() == 32 ? Intrinsic::eh_return_i32
2902                                                     : Intrinsic::eh_return_i64);
2903     Builder.CreateCall(F, {Int, Ptr});
2904     Builder.CreateUnreachable();
2905
2906     // We do need to preserve an insertion point.
2907     EmitBlock(createBasicBlock("builtin_eh_return.cont"));
2908
2909     return RValue::get(nullptr);
2910   }
2911   case Builtin::BI__builtin_unwind_init: {
2912     Function *F = CGM.getIntrinsic(Intrinsic::eh_unwind_init);
2913     return RValue::get(Builder.CreateCall(F));
2914   }
2915   case Builtin::BI__builtin_extend_pointer: {
2916     // Extends a pointer to the size of an _Unwind_Word, which is
2917     // uint64_t on all platforms.  Generally this gets poked into a
2918     // register and eventually used as an address, so if the
2919     // addressing registers are wider than pointers and the platform
2920     // doesn't implicitly ignore high-order bits when doing
2921     // addressing, we need to make sure we zext / sext based on
2922     // the platform's expectations.
2923     //
2924     // See: http://gcc.gnu.org/ml/gcc-bugs/2002-02/msg00237.html
2925
2926     // Cast the pointer to intptr_t.
2927     Value *Ptr = EmitScalarExpr(E->getArg(0));
2928     Value *Result = Builder.CreatePtrToInt(Ptr, IntPtrTy, "extend.cast");
2929
2930     // If that's 64 bits, we're done.
2931     if (IntPtrTy->getBitWidth() == 64)
2932       return RValue::get(Result);
2933
2934     // Otherwise, ask the codegen data what to do.
2935     if (getTargetHooks().extendPointerWithSExt())
2936       return RValue::get(Builder.CreateSExt(Result, Int64Ty, "extend.sext"));
2937     else
2938       return RValue::get(Builder.CreateZExt(Result, Int64Ty, "extend.zext"));
2939   }
2940   case Builtin::BI__builtin_setjmp: {
2941     // Buffer is a void**.
2942     Address Buf = EmitPointerWithAlignment(E->getArg(0));
2943
2944     // Store the frame pointer to the setjmp buffer.
2945     Value *FrameAddr = Builder.CreateCall(
2946         CGM.getIntrinsic(Intrinsic::frameaddress, AllocaInt8PtrTy),
2947         ConstantInt::get(Int32Ty, 0));
2948     Builder.CreateStore(FrameAddr, Buf);
2949
2950     // Store the stack pointer to the setjmp buffer.
2951     Value *StackAddr =
2952         Builder.CreateCall(CGM.getIntrinsic(Intrinsic::stacksave));
2953     Address StackSaveSlot = Builder.CreateConstInBoundsGEP(Buf, 2);
2954     Builder.CreateStore(StackAddr, StackSaveSlot);
2955
2956     // Call LLVM's EH setjmp, which is lightweight.
2957     Function *F = CGM.getIntrinsic(Intrinsic::eh_sjlj_setjmp);
2958     Buf = Builder.CreateBitCast(Buf, Int8PtrTy);
2959     return RValue::get(Builder.CreateCall(F, Buf.getPointer()));
2960   }
2961   case Builtin::BI__builtin_longjmp: {
2962     Value *Buf = EmitScalarExpr(E->getArg(0));
2963     Buf = Builder.CreateBitCast(Buf, Int8PtrTy);
2964
2965     // Call LLVM's EH longjmp, which is lightweight.
2966     Builder.CreateCall(CGM.getIntrinsic(Intrinsic::eh_sjlj_longjmp), Buf);
2967
2968     // longjmp doesn't return; mark this as unreachable.
2969     Builder.CreateUnreachable();
2970
2971     // We do need to preserve an insertion point.
2972     EmitBlock(createBasicBlock("longjmp.cont"));
2973
2974     return RValue::get(nullptr);
2975   }
2976   case Builtin::BI__builtin_launder: {
2977     const Expr *Arg = E->getArg(0);
2978     QualType ArgTy = Arg->getType()->getPointeeType();
2979     Value *Ptr = EmitScalarExpr(Arg);
2980     if (TypeRequiresBuiltinLaunder(CGM, ArgTy))
2981       Ptr = Builder.CreateLaunderInvariantGroup(Ptr);
2982
2983     return RValue::get(Ptr);
2984   }
2985   case Builtin::BI__sync_fetch_and_add:
2986   case Builtin::BI__sync_fetch_and_sub:
2987   case Builtin::BI__sync_fetch_and_or:
2988   case Builtin::BI__sync_fetch_and_and:
2989   case Builtin::BI__sync_fetch_and_xor:
2990   case Builtin::BI__sync_fetch_and_nand:
2991   case Builtin::BI__sync_add_and_fetch:
2992   case Builtin::BI__sync_sub_and_fetch:
2993   case Builtin::BI__sync_and_and_fetch:
2994   case Builtin::BI__sync_or_and_fetch:
2995   case Builtin::BI__sync_xor_and_fetch:
2996   case Builtin::BI__sync_nand_and_fetch:
2997   case Builtin::BI__sync_val_compare_and_swap:
2998   case Builtin::BI__sync_bool_compare_and_swap:
2999   case Builtin::BI__sync_lock_test_and_set:
3000   case Builtin::BI__sync_lock_release:
3001   case Builtin::BI__sync_swap:
3002     llvm_unreachable("Shouldn't make it through sema");
3003   case Builtin::BI__sync_fetch_and_add_1:
3004   case Builtin::BI__sync_fetch_and_add_2:
3005   case Builtin::BI__sync_fetch_and_add_4:
3006   case Builtin::BI__sync_fetch_and_add_8:
3007   case Builtin::BI__sync_fetch_and_add_16:
3008     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Add, E);
3009   case Builtin::BI__sync_fetch_and_sub_1:
3010   case Builtin::BI__sync_fetch_and_sub_2:
3011   case Builtin::BI__sync_fetch_and_sub_4:
3012   case Builtin::BI__sync_fetch_and_sub_8:
3013   case Builtin::BI__sync_fetch_and_sub_16:
3014     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Sub, E);
3015   case Builtin::BI__sync_fetch_and_or_1:
3016   case Builtin::BI__sync_fetch_and_or_2:
3017   case Builtin::BI__sync_fetch_and_or_4:
3018   case Builtin::BI__sync_fetch_and_or_8:
3019   case Builtin::BI__sync_fetch_and_or_16:
3020     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Or, E);
3021   case Builtin::BI__sync_fetch_and_and_1:
3022   case Builtin::BI__sync_fetch_and_and_2:
3023   case Builtin::BI__sync_fetch_and_and_4:
3024   case Builtin::BI__sync_fetch_and_and_8:
3025   case Builtin::BI__sync_fetch_and_and_16:
3026     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::And, E);
3027   case Builtin::BI__sync_fetch_and_xor_1:
3028   case Builtin::BI__sync_fetch_and_xor_2:
3029   case Builtin::BI__sync_fetch_and_xor_4:
3030   case Builtin::BI__sync_fetch_and_xor_8:
3031   case Builtin::BI__sync_fetch_and_xor_16:
3032     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xor, E);
3033   case Builtin::BI__sync_fetch_and_nand_1:
3034   case Builtin::BI__sync_fetch_and_nand_2:
3035   case Builtin::BI__sync_fetch_and_nand_4:
3036   case Builtin::BI__sync_fetch_and_nand_8:
3037   case Builtin::BI__sync_fetch_and_nand_16:
3038     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Nand, E);
3039
3040   // Clang extensions: not overloaded yet.
3041   case Builtin::BI__sync_fetch_and_min:
3042     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Min, E);
3043   case Builtin::BI__sync_fetch_and_max:
3044     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Max, E);
3045   case Builtin::BI__sync_fetch_and_umin:
3046     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMin, E);
3047   case Builtin::BI__sync_fetch_and_umax:
3048     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::UMax, E);
3049
3050   case Builtin::BI__sync_add_and_fetch_1:
3051   case Builtin::BI__sync_add_and_fetch_2:
3052   case Builtin::BI__sync_add_and_fetch_4:
3053   case Builtin::BI__sync_add_and_fetch_8:
3054   case Builtin::BI__sync_add_and_fetch_16:
3055     return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Add, E,
3056                                 llvm::Instruction::Add);
3057   case Builtin::BI__sync_sub_and_fetch_1:
3058   case Builtin::BI__sync_sub_and_fetch_2:
3059   case Builtin::BI__sync_sub_and_fetch_4:
3060   case Builtin::BI__sync_sub_and_fetch_8:
3061   case Builtin::BI__sync_sub_and_fetch_16:
3062     return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Sub, E,
3063                                 llvm::Instruction::Sub);
3064   case Builtin::BI__sync_and_and_fetch_1:
3065   case Builtin::BI__sync_and_and_fetch_2:
3066   case Builtin::BI__sync_and_and_fetch_4:
3067   case Builtin::BI__sync_and_and_fetch_8:
3068   case Builtin::BI__sync_and_and_fetch_16:
3069     return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::And, E,
3070                                 llvm::Instruction::And);
3071   case Builtin::BI__sync_or_and_fetch_1:
3072   case Builtin::BI__sync_or_and_fetch_2:
3073   case Builtin::BI__sync_or_and_fetch_4:
3074   case Builtin::BI__sync_or_and_fetch_8:
3075   case Builtin::BI__sync_or_and_fetch_16:
3076     return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Or, E,
3077                                 llvm::Instruction::Or);
3078   case Builtin::BI__sync_xor_and_fetch_1:
3079   case Builtin::BI__sync_xor_and_fetch_2:
3080   case Builtin::BI__sync_xor_and_fetch_4:
3081   case Builtin::BI__sync_xor_and_fetch_8:
3082   case Builtin::BI__sync_xor_and_fetch_16:
3083     return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Xor, E,
3084                                 llvm::Instruction::Xor);
3085   case Builtin::BI__sync_nand_and_fetch_1:
3086   case Builtin::BI__sync_nand_and_fetch_2:
3087   case Builtin::BI__sync_nand_and_fetch_4:
3088   case Builtin::BI__sync_nand_and_fetch_8:
3089   case Builtin::BI__sync_nand_and_fetch_16:
3090     return EmitBinaryAtomicPost(*this, llvm::AtomicRMWInst::Nand, E,
3091                                 llvm::Instruction::And, true);
3092
3093   case Builtin::BI__sync_val_compare_and_swap_1:
3094   case Builtin::BI__sync_val_compare_and_swap_2:
3095   case Builtin::BI__sync_val_compare_and_swap_4:
3096   case Builtin::BI__sync_val_compare_and_swap_8:
3097   case Builtin::BI__sync_val_compare_and_swap_16:
3098     return RValue::get(MakeAtomicCmpXchgValue(*this, E, false));
3099
3100   case Builtin::BI__sync_bool_compare_and_swap_1:
3101   case Builtin::BI__sync_bool_compare_and_swap_2:
3102   case Builtin::BI__sync_bool_compare_and_swap_4:
3103   case Builtin::BI__sync_bool_compare_and_swap_8:
3104   case Builtin::BI__sync_bool_compare_and_swap_16:
3105     return RValue::get(MakeAtomicCmpXchgValue(*this, E, true));
3106
3107   case Builtin::BI__sync_swap_1:
3108   case Builtin::BI__sync_swap_2:
3109   case Builtin::BI__sync_swap_4:
3110   case Builtin::BI__sync_swap_8:
3111   case Builtin::BI__sync_swap_16:
3112     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
3113
3114   case Builtin::BI__sync_lock_test_and_set_1:
3115   case Builtin::BI__sync_lock_test_and_set_2:
3116   case Builtin::BI__sync_lock_test_and_set_4:
3117   case Builtin::BI__sync_lock_test_and_set_8:
3118   case Builtin::BI__sync_lock_test_and_set_16:
3119     return EmitBinaryAtomic(*this, llvm::AtomicRMWInst::Xchg, E);
3120
3121   case Builtin::BI__sync_lock_release_1:
3122   case Builtin::BI__sync_lock_release_2:
3123   case Builtin::BI__sync_lock_release_4:
3124   case Builtin::BI__sync_lock_release_8:
3125   case Builtin::BI__sync_lock_release_16: {
3126     Value *Ptr = EmitScalarExpr(E->getArg(0));
3127     QualType ElTy = E->getArg(0)->getType()->getPointeeType();
3128     CharUnits StoreSize = getContext().getTypeSizeInChars(ElTy);
3129     llvm::Type *ITy = llvm::IntegerType::get(getLLVMContext(),
3130                                              StoreSize.getQuantity() * 8);
3131     Ptr = Builder.CreateBitCast(Ptr, ITy->getPointerTo());
3132     llvm::StoreInst *Store =
3133       Builder.CreateAlignedStore(llvm::Constant::getNullValue(ITy), Ptr,
3134                                  StoreSize);
3135     Store->setAtomic(llvm::AtomicOrdering::Release);
3136     return RValue::get(nullptr);
3137   }
3138
3139   case Builtin::BI__sync_synchronize: {
3140     // We assume this is supposed to correspond to a C++0x-style
3141     // sequentially-consistent fence (i.e. this is only usable for
3142     // synchronization, not device I/O or anything like that). This intrinsic
3143     // is really badly designed in the sense that in theory, there isn't
3144     // any way to safely use it... but in practice, it mostly works
3145     // to use it with non-atomic loads and stores to get acquire/release
3146     // semantics.
3147     Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent);
3148     return RValue::get(nullptr);
3149   }
3150
3151   case Builtin::BI__builtin_nontemporal_load:
3152     return RValue::get(EmitNontemporalLoad(*this, E));
3153   case Builtin::BI__builtin_nontemporal_store:
3154     return RValue::get(EmitNontemporalStore(*this, E));
3155   case Builtin::BI__c11_atomic_is_lock_free:
3156   case Builtin::BI__atomic_is_lock_free: {
3157     // Call "bool __atomic_is_lock_free(size_t size, void *ptr)". For the
3158     // __c11 builtin, ptr is 0 (indicating a properly-aligned object), since
3159     // _Atomic(T) is always properly-aligned.
3160     const char *LibCallName = "__atomic_is_lock_free";
3161     CallArgList Args;
3162     Args.add(RValue::get(EmitScalarExpr(E->getArg(0))),
3163              getContext().getSizeType());
3164     if (BuiltinID == Builtin::BI__atomic_is_lock_free)
3165       Args.add(RValue::get(EmitScalarExpr(E->getArg(1))),
3166                getContext().VoidPtrTy);
3167     else
3168       Args.add(RValue::get(llvm::Constant::getNullValue(VoidPtrTy)),
3169                getContext().VoidPtrTy);
3170     const CGFunctionInfo &FuncInfo =
3171         CGM.getTypes().arrangeBuiltinFunctionCall(E->getType(), Args);
3172     llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FuncInfo);
3173     llvm::FunctionCallee Func = CGM.CreateRuntimeFunction(FTy, LibCallName);
3174     return EmitCall(FuncInfo, CGCallee::forDirect(Func),
3175                     ReturnValueSlot(), Args);
3176   }
3177
3178   case Builtin::BI__atomic_test_and_set: {
3179     // Look at the argument type to determine whether this is a volatile
3180     // operation. The parameter type is always volatile.
3181     QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
3182     bool Volatile =
3183         PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
3184
3185     Value *Ptr = EmitScalarExpr(E->getArg(0));
3186     unsigned AddrSpace = Ptr->getType()->getPointerAddressSpace();
3187     Ptr = Builder.CreateBitCast(Ptr, Int8Ty->getPointerTo(AddrSpace));
3188     Value *NewVal = Builder.getInt8(1);
3189     Value *Order = EmitScalarExpr(E->getArg(1));
3190     if (isa<llvm::ConstantInt>(Order)) {
3191       int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
3192       AtomicRMWInst *Result = nullptr;
3193       switch (ord) {
3194       case 0:  // memory_order_relaxed
3195       default: // invalid order
3196         Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
3197                                          llvm::AtomicOrdering::Monotonic);
3198         break;
3199       case 1: // memory_order_consume
3200       case 2: // memory_order_acquire
3201         Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
3202                                          llvm::AtomicOrdering::Acquire);
3203         break;
3204       case 3: // memory_order_release
3205         Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
3206                                          llvm::AtomicOrdering::Release);
3207         break;
3208       case 4: // memory_order_acq_rel
3209
3210         Result = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
3211                                          llvm::AtomicOrdering::AcquireRelease);
3212         break;
3213       case 5: // memory_order_seq_cst
3214         Result = Builder.CreateAtomicRMW(
3215             llvm::AtomicRMWInst::Xchg, Ptr, NewVal,
3216             llvm::AtomicOrdering::SequentiallyConsistent);
3217         break;
3218       }
3219       Result->setVolatile(Volatile);
3220       return RValue::get(Builder.CreateIsNotNull(Result, "tobool"));
3221     }
3222
3223     llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
3224
3225     llvm::BasicBlock *BBs[5] = {
3226       createBasicBlock("monotonic", CurFn),
3227       createBasicBlock("acquire", CurFn),
3228       createBasicBlock("release", CurFn),
3229       createBasicBlock("acqrel", CurFn),
3230       createBasicBlock("seqcst", CurFn)
3231     };
3232     llvm::AtomicOrdering Orders[5] = {
3233         llvm::AtomicOrdering::Monotonic, llvm::AtomicOrdering::Acquire,
3234         llvm::AtomicOrdering::Release, llvm::AtomicOrdering::AcquireRelease,
3235         llvm::AtomicOrdering::SequentiallyConsistent};
3236
3237     Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
3238     llvm::SwitchInst *SI = Builder.CreateSwitch(Order, BBs[0]);
3239
3240     Builder.SetInsertPoint(ContBB);
3241     PHINode *Result = Builder.CreatePHI(Int8Ty, 5, "was_set");
3242
3243     for (unsigned i = 0; i < 5; ++i) {
3244       Builder.SetInsertPoint(BBs[i]);
3245       AtomicRMWInst *RMW = Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
3246                                                    Ptr, NewVal, Orders[i]);
3247       RMW->setVolatile(Volatile);
3248       Result->addIncoming(RMW, BBs[i]);
3249       Builder.CreateBr(ContBB);
3250     }
3251
3252     SI->addCase(Builder.getInt32(0), BBs[0]);
3253     SI->addCase(Builder.getInt32(1), BBs[1]);
3254     SI->addCase(Builder.getInt32(2), BBs[1]);
3255     SI->addCase(Builder.getInt32(3), BBs[2]);
3256     SI->addCase(Builder.getInt32(4), BBs[3]);
3257     SI->addCase(Builder.getInt32(5), BBs[4]);
3258
3259     Builder.SetInsertPoint(ContBB);
3260     return RValue::get(Builder.CreateIsNotNull(Result, "tobool"));
3261   }
3262
3263   case Builtin::BI__atomic_clear: {
3264     QualType PtrTy = E->getArg(0)->IgnoreImpCasts()->getType();
3265     bool Volatile =
3266         PtrTy->castAs<PointerType>()->getPointeeType().isVolatileQualified();
3267
3268     Address Ptr = EmitPointerWithAlignment(E->getArg(0));
3269     unsigned AddrSpace = Ptr.getPointer()->getType()->getPointerAddressSpace();
3270     Ptr = Builder.CreateBitCast(Ptr, Int8Ty->getPointerTo(AddrSpace));
3271     Value *NewVal = Builder.getInt8(0);
3272     Value *Order = EmitScalarExpr(E->getArg(1));
3273     if (isa<llvm::ConstantInt>(Order)) {
3274       int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
3275       StoreInst *Store = Builder.CreateStore(NewVal, Ptr, Volatile);
3276       switch (ord) {
3277       case 0:  // memory_order_relaxed
3278       default: // invalid order
3279         Store->setOrdering(llvm::AtomicOrdering::Monotonic);
3280         break;
3281       case 3:  // memory_order_release
3282         Store->setOrdering(llvm::AtomicOrdering::Release);
3283         break;
3284       case 5:  // memory_order_seq_cst
3285         Store->setOrdering(llvm::AtomicOrdering::SequentiallyConsistent);
3286         break;
3287       }
3288       return RValue::get(nullptr);
3289     }
3290
3291     llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
3292
3293     llvm::BasicBlock *BBs[3] = {
3294       createBasicBlock("monotonic", CurFn),
3295       createBasicBlock("release", CurFn),
3296       createBasicBlock("seqcst", CurFn)
3297     };
3298     llvm::AtomicOrdering Orders[3] = {
3299         llvm::AtomicOrdering::Monotonic, llvm::AtomicOrdering::Release,
3300         llvm::AtomicOrdering::SequentiallyConsistent};
3301
3302     Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
3303     llvm::SwitchInst *SI = Builder.CreateSwitch(Order, BBs[0]);
3304
3305     for (unsigned i = 0; i < 3; ++i) {
3306       Builder.SetInsertPoint(BBs[i]);
3307       StoreInst *Store = Builder.CreateStore(NewVal, Ptr, Volatile);
3308       Store->setOrdering(Orders[i]);
3309       Builder.CreateBr(ContBB);
3310     }
3311
3312     SI->addCase(Builder.getInt32(0), BBs[0]);
3313     SI->addCase(Builder.getInt32(3), BBs[1]);
3314     SI->addCase(Builder.getInt32(5), BBs[2]);
3315
3316     Builder.SetInsertPoint(ContBB);
3317     return RValue::get(nullptr);
3318   }
3319
3320   case Builtin::BI__atomic_thread_fence:
3321   case Builtin::BI__atomic_signal_fence:
3322   case Builtin::BI__c11_atomic_thread_fence:
3323   case Builtin::BI__c11_atomic_signal_fence: {
3324     llvm::SyncScope::ID SSID;
3325     if (BuiltinID == Builtin::BI__atomic_signal_fence ||
3326         BuiltinID == Builtin::BI__c11_atomic_signal_fence)
3327       SSID = llvm::SyncScope::SingleThread;
3328     else
3329       SSID = llvm::SyncScope::System;
3330     Value *Order = EmitScalarExpr(E->getArg(0));
3331     if (isa<llvm::ConstantInt>(Order)) {
3332       int ord = cast<llvm::ConstantInt>(Order)->getZExtValue();
3333       switch (ord) {
3334       case 0:  // memory_order_relaxed
3335       default: // invalid order
3336         break;
3337       case 1:  // memory_order_consume
3338       case 2:  // memory_order_acquire
3339         Builder.CreateFence(llvm::AtomicOrdering::Acquire, SSID);
3340         break;
3341       case 3:  // memory_order_release
3342         Builder.CreateFence(llvm::AtomicOrdering::Release, SSID);
3343         break;
3344       case 4:  // memory_order_acq_rel
3345         Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease, SSID);
3346         break;
3347       case 5:  // memory_order_seq_cst
3348         Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent, SSID);
3349         break;
3350       }
3351       return RValue::get(nullptr);
3352     }
3353
3354     llvm::BasicBlock *AcquireBB, *ReleaseBB, *AcqRelBB, *SeqCstBB;
3355     AcquireBB = createBasicBlock("acquire", CurFn);
3356     ReleaseBB = createBasicBlock("release", CurFn);
3357     AcqRelBB = createBasicBlock("acqrel", CurFn);
3358     SeqCstBB = createBasicBlock("seqcst", CurFn);
3359     llvm::BasicBlock *ContBB = createBasicBlock("atomic.continue", CurFn);
3360
3361     Order = Builder.CreateIntCast(Order, Builder.getInt32Ty(), false);
3362     llvm::SwitchInst *SI = Builder.CreateSwitch(Order, ContBB);
3363
3364     Builder.SetInsertPoint(AcquireBB);
3365     Builder.CreateFence(llvm::AtomicOrdering::Acquire, SSID);
3366     Builder.CreateBr(ContBB);
3367     SI->addCase(Builder.getInt32(1), AcquireBB);
3368     SI->addCase(Builder.getInt32(2), AcquireBB);
3369
3370     Builder.SetInsertPoint(ReleaseBB);
3371     Builder.CreateFence(llvm::AtomicOrdering::Release, SSID);
3372     Builder.CreateBr(ContBB);
3373     SI->addCase(Builder.getInt32(3), ReleaseBB);
3374
3375     Builder.SetInsertPoint(AcqRelBB);
3376     Builder.CreateFence(llvm::AtomicOrdering::AcquireRelease, SSID);
3377     Builder.CreateBr(ContBB);
3378     SI->addCase(Builder.getInt32(4), AcqRelBB);
3379
3380     Builder.SetInsertPoint(SeqCstBB);
3381     Builder.CreateFence(llvm::AtomicOrdering::SequentiallyConsistent, SSID);
3382     Builder.CreateBr(ContBB);
3383     SI->addCase(Builder.getInt32(5), SeqCstBB);
3384
3385     Builder.SetInsertPoint(ContBB);
3386     return RValue::get(nullptr);
3387   }
3388
3389   case Builtin::BI__builtin_signbit:
3390   case Builtin::BI__builtin_signbitf:
3391   case Builtin::BI__builtin_signbitl: {
3392     return RValue::get(
3393         Builder.CreateZExt(EmitSignBit(*this, EmitScalarExpr(E->getArg(0))),
3394                            ConvertType(E->getType())));
3395   }
3396   case Builtin::BI__warn_memset_zero_len:
3397     return RValue::getIgnored();
3398   case Builtin::BI__annotation: {
3399     // Re-encode each wide string to UTF8 and make an MDString.
3400     SmallVector<Metadata *, 1> Strings;
3401     for (const Expr *Arg : E->arguments()) {
3402       const auto *Str = cast<StringLiteral>(Arg->IgnoreParenCasts());
3403       assert(Str->getCharByteWidth() == 2);
3404       StringRef WideBytes = Str->getBytes();
3405       std::string StrUtf8;
3406       if (!convertUTF16ToUTF8String(
3407               makeArrayRef(WideBytes.data(), WideBytes.size()), StrUtf8)) {
3408         CGM.ErrorUnsupported(E, "non-UTF16 __annotation argument");
3409         continue;
3410       }
3411       Strings.push_back(llvm::MDString::get(getLLVMContext(), StrUtf8));
3412     }
3413
3414     // Build and MDTuple of MDStrings and emit the intrinsic call.
3415     llvm::Function *F =
3416         CGM.getIntrinsic(llvm::Intrinsic::codeview_annotation, {});
3417     MDTuple *StrTuple = MDTuple::get(getLLVMContext(), Strings);
3418     Builder.CreateCall(F, MetadataAsValue::get(getLLVMContext(), StrTuple));
3419     return RValue::getIgnored();
3420   }
3421   case Builtin::BI__builtin_annotation: {
3422     llvm::Value *AnnVal = EmitScalarExpr(E->getArg(0));
3423     llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::annotation,
3424                                       AnnVal->getType());
3425
3426     // Get the annotation string, go through casts. Sema requires this to be a
3427     // non-wide string literal, potentially casted, so the cast<> is safe.
3428     const Expr *AnnotationStrExpr = E->getArg(1)->IgnoreParenCasts();
3429     StringRef Str = cast<StringLiteral>(AnnotationStrExpr)->getString();
3430     return RValue::get(
3431         EmitAnnotationCall(F, AnnVal, Str, E->getExprLoc(), nullptr));
3432   }
3433   case Builtin::BI__builtin_addcb:
3434   case Builtin::BI__builtin_addcs:
3435   case Builtin::BI__builtin_addc:
3436   case Builtin::BI__builtin_addcl:
3437   case Builtin::BI__builtin_addcll:
3438   case Builtin::BI__builtin_subcb:
3439   case Builtin::BI__builtin_subcs:
3440   case Builtin::BI__builtin_subc:
3441   case Builtin::BI__builtin_subcl:
3442   case Builtin::BI__builtin_subcll: {
3443
3444     // We translate all of these builtins from expressions of the form:
3445     //   int x = ..., y = ..., carryin = ..., carryout, result;
3446     //   result = __builtin_addc(x, y, carryin, &carryout);
3447     //
3448     // to LLVM IR of the form:
3449     //
3450     //   %tmp1 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %x, i32 %y)
3451     //   %tmpsum1 = extractvalue {i32, i1} %tmp1, 0
3452     //   %carry1 = extractvalue {i32, i1} %tmp1, 1
3453     //   %tmp2 = call {i32, i1} @llvm.uadd.with.overflow.i32(i32 %tmpsum1,
3454     //                                                       i32 %carryin)
3455     //   %result = extractvalue {i32, i1} %tmp2, 0
3456     //   %carry2 = extractvalue {i32, i1} %tmp2, 1
3457     //   %tmp3 = or i1 %carry1, %carry2
3458     //   %tmp4 = zext i1 %tmp3 to i32
3459     //   store i32 %tmp4, i32* %carryout
3460
3461     // Scalarize our inputs.
3462     llvm::Value *X = EmitScalarExpr(E->getArg(0));
3463     llvm::Value *Y = EmitScalarExpr(E->getArg(1));
3464     llvm::Value *Carryin = EmitScalarExpr(E->getArg(2));
3465     Address CarryOutPtr = EmitPointerWithAlignment(E->getArg(3));
3466
3467     // Decide if we are lowering to a uadd.with.overflow or usub.with.overflow.
3468     llvm::Intrinsic::ID IntrinsicId;
3469     switch (BuiltinID) {
3470     default: llvm_unreachable("Unknown multiprecision builtin id.");
3471     case Builtin::BI__builtin_addcb:
3472     case Builtin::BI__builtin_addcs:
3473     case Builtin::BI__builtin_addc:
3474     case Builtin::BI__builtin_addcl:
3475     case Builtin::BI__builtin_addcll:
3476       IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
3477       break;
3478     case Builtin::BI__builtin_subcb:
3479     case Builtin::BI__builtin_subcs:
3480     case Builtin::BI__builtin_subc:
3481     case Builtin::BI__builtin_subcl:
3482     case Builtin::BI__builtin_subcll:
3483       IntrinsicId = llvm::Intrinsic::usub_with_overflow;
3484       break;
3485     }
3486
3487     // Construct our resulting LLVM IR expression.
3488     llvm::Value *Carry1;
3489     llvm::Value *Sum1 = EmitOverflowIntrinsic(*this, IntrinsicId,
3490                                               X, Y, Carry1);
3491     llvm::Value *Carry2;
3492     llvm::Value *Sum2 = EmitOverflowIntrinsic(*this, IntrinsicId,
3493                                               Sum1, Carryin, Carry2);
3494     llvm::Value *CarryOut = Builder.CreateZExt(Builder.CreateOr(Carry1, Carry2),
3495                                                X->getType());
3496     Builder.CreateStore(CarryOut, CarryOutPtr);
3497     return RValue::get(Sum2);
3498   }
3499
3500   case Builtin::BI__builtin_add_overflow:
3501   case Builtin::BI__builtin_sub_overflow:
3502   case Builtin::BI__builtin_mul_overflow: {
3503     const clang::Expr *LeftArg = E->getArg(0);
3504     const clang::Expr *RightArg = E->getArg(1);
3505     const clang::Expr *ResultArg = E->getArg(2);
3506
3507     clang::QualType ResultQTy =
3508         ResultArg->getType()->castAs<PointerType>()->getPointeeType();
3509
3510     WidthAndSignedness LeftInfo =
3511         getIntegerWidthAndSignedness(CGM.getContext(), LeftArg->getType());
3512     WidthAndSignedness RightInfo =
3513         getIntegerWidthAndSignedness(CGM.getContext(), RightArg->getType());
3514     WidthAndSignedness ResultInfo =
3515         getIntegerWidthAndSignedness(CGM.getContext(), ResultQTy);
3516
3517     // Handle mixed-sign multiplication as a special case, because adding
3518     // runtime or backend support for our generic irgen would be too expensive.
3519     if (isSpecialMixedSignMultiply(BuiltinID, LeftInfo, RightInfo, ResultInfo))
3520       return EmitCheckedMixedSignMultiply(*this, LeftArg, LeftInfo, RightArg,
3521                                           RightInfo, ResultArg, ResultQTy,
3522                                           ResultInfo);
3523
3524     WidthAndSignedness EncompassingInfo =
3525         EncompassingIntegerType({LeftInfo, RightInfo, ResultInfo});
3526
3527     llvm::Type *EncompassingLLVMTy =
3528         llvm::IntegerType::get(CGM.getLLVMContext(), EncompassingInfo.Width);
3529
3530     llvm::Type *ResultLLVMTy = CGM.getTypes().ConvertType(ResultQTy);
3531
3532     llvm::Intrinsic::ID IntrinsicId;
3533     switch (BuiltinID) {
3534     default:
3535       llvm_unreachable("Unknown overflow builtin id.");
3536     case Builtin::BI__builtin_add_overflow:
3537       IntrinsicId = EncompassingInfo.Signed
3538                         ? llvm::Intrinsic::sadd_with_overflow
3539                         : llvm::Intrinsic::uadd_with_overflow;
3540       break;
3541     case Builtin::BI__builtin_sub_overflow:
3542       IntrinsicId = EncompassingInfo.Signed
3543                         ? llvm::Intrinsic::ssub_with_overflow
3544                         : llvm::Intrinsic::usub_with_overflow;
3545       break;
3546     case Builtin::BI__builtin_mul_overflow:
3547       IntrinsicId = EncompassingInfo.Signed
3548                         ? llvm::Intrinsic::smul_with_overflow
3549                         : llvm::Intrinsic::umul_with_overflow;
3550       break;
3551     }
3552
3553     llvm::Value *Left = EmitScalarExpr(LeftArg);
3554     llvm::Value *Right = EmitScalarExpr(RightArg);
3555     Address ResultPtr = EmitPointerWithAlignment(ResultArg);
3556
3557     // Extend each operand to the encompassing type.
3558     Left = Builder.CreateIntCast(Left, EncompassingLLVMTy, LeftInfo.Signed);
3559     Right = Builder.CreateIntCast(Right, EncompassingLLVMTy, RightInfo.Signed);
3560
3561     // Perform the operation on the extended values.
3562     llvm::Value *Overflow, *Result;
3563     Result = EmitOverflowIntrinsic(*this, IntrinsicId, Left, Right, Overflow);
3564
3565     if (EncompassingInfo.Width > ResultInfo.Width) {
3566       // The encompassing type is wider than the result type, so we need to
3567       // truncate it.
3568       llvm::Value *ResultTrunc = Builder.CreateTrunc(Result, ResultLLVMTy);
3569
3570       // To see if the truncation caused an overflow, we will extend
3571       // the result and then compare it to the original result.
3572       llvm::Value *ResultTruncExt = Builder.CreateIntCast(
3573           ResultTrunc, EncompassingLLVMTy, ResultInfo.Signed);
3574       llvm::Value *TruncationOverflow =
3575           Builder.CreateICmpNE(Result, ResultTruncExt);
3576
3577       Overflow = Builder.CreateOr(Overflow, TruncationOverflow);
3578       Result = ResultTrunc;
3579     }
3580
3581     // Finally, store the result using the pointer.
3582     bool isVolatile =
3583       ResultArg->getType()->getPointeeType().isVolatileQualified();
3584     Builder.CreateStore(EmitToMemory(Result, ResultQTy), ResultPtr, isVolatile);
3585
3586     return RValue::get(Overflow);
3587   }
3588
3589   case Builtin::BI__builtin_uadd_overflow:
3590   case Builtin::BI__builtin_uaddl_overflow:
3591   case Builtin::BI__builtin_uaddll_overflow:
3592   case Builtin::BI__builtin_usub_overflow:
3593   case Builtin::BI__builtin_usubl_overflow:
3594   case Builtin::BI__builtin_usubll_overflow:
3595   case Builtin::BI__builtin_umul_overflow:
3596   case Builtin::BI__builtin_umull_overflow:
3597   case Builtin::BI__builtin_umulll_overflow:
3598   case Builtin::BI__builtin_sadd_overflow:
3599   case Builtin::BI__builtin_saddl_overflow:
3600   case Builtin::BI__builtin_saddll_overflow:
3601   case Builtin::BI__builtin_ssub_overflow:
3602   case Builtin::BI__builtin_ssubl_overflow:
3603   case Builtin::BI__builtin_ssubll_overflow:
3604   case Builtin::BI__builtin_smul_overflow:
3605   case Builtin::BI__builtin_smull_overflow:
3606   case Builtin::BI__builtin_smulll_overflow: {
3607
3608     // We translate all of these builtins directly to the relevant llvm IR node.
3609
3610     // Scalarize our inputs.
3611     llvm::Value *X = EmitScalarExpr(E->getArg(0));
3612     llvm::Value *Y = EmitScalarExpr(E->getArg(1));
3613     Address SumOutPtr = EmitPointerWithAlignment(E->getArg(2));
3614
3615     // Decide which of the overflow intrinsics we are lowering to:
3616     llvm::Intrinsic::ID IntrinsicId;
3617     switch (BuiltinID) {
3618     default: llvm_unreachable("Unknown overflow builtin id.");
3619     case Builtin::BI__builtin_uadd_overflow:
3620     case Builtin::BI__builtin_uaddl_overflow:
3621     case Builtin::BI__builtin_uaddll_overflow:
3622       IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
3623       break;
3624     case Builtin::BI__builtin_usub_overflow:
3625     case Builtin::BI__builtin_usubl_overflow:
3626     case Builtin::BI__builtin_usubll_overflow:
3627       IntrinsicId = llvm::Intrinsic::usub_with_overflow;
3628       break;
3629     case Builtin::BI__builtin_umul_overflow:
3630     case Builtin::BI__builtin_umull_overflow:
3631     case Builtin::BI__builtin_umulll_overflow:
3632       IntrinsicId = llvm::Intrinsic::umul_with_overflow;
3633       break;
3634     case Builtin::BI__builtin_sadd_overflow:
3635     case Builtin::BI__builtin_saddl_overflow:
3636     case Builtin::BI__builtin_saddll_overflow:
3637       IntrinsicId = llvm::Intrinsic::sadd_with_overflow;
3638       break;
3639     case Builtin::BI__builtin_ssub_overflow:
3640     case Builtin::BI__builtin_ssubl_overflow:
3641     case Builtin::BI__builtin_ssubll_overflow:
3642       IntrinsicId = llvm::Intrinsic::ssub_with_overflow;
3643       break;
3644     case Builtin::BI__builtin_smul_overflow:
3645     case Builtin::BI__builtin_smull_overflow:
3646     case Builtin::BI__builtin_smulll_overflow:
3647       IntrinsicId = llvm::Intrinsic::smul_with_overflow;
3648       break;
3649     }
3650
3651
3652     llvm::Value *Carry;
3653     llvm::Value *Sum = EmitOverflowIntrinsic(*this, IntrinsicId, X, Y, Carry);
3654     Builder.CreateStore(Sum, SumOutPtr);
3655
3656     return RValue::get(Carry);
3657   }
3658   case Builtin::BI__builtin_addressof:
3659     return RValue::get(EmitLValue(E->getArg(0)).getPointer(*this));
3660   case Builtin::BI__builtin_operator_new:
3661     return EmitBuiltinNewDeleteCall(
3662         E->getCallee()->getType()->castAs<FunctionProtoType>(), E, false);
3663   case Builtin::BI__builtin_operator_delete:
3664     return EmitBuiltinNewDeleteCall(
3665         E->getCallee()->getType()->castAs<FunctionProtoType>(), E, true);
3666
3667   case Builtin::BI__builtin_is_aligned:
3668     return EmitBuiltinIsAligned(E);
3669   case Builtin::BI__builtin_align_up:
3670     return EmitBuiltinAlignTo(E, true);
3671   case Builtin::BI__builtin_align_down:
3672     return EmitBuiltinAlignTo(E, false);
3673
3674   case Builtin::BI__noop:
3675     // __noop always evaluates to an integer literal zero.
3676     return RValue::get(ConstantInt::get(IntTy, 0));
3677   case Builtin::BI__builtin_call_with_static_chain: {
3678     const CallExpr *Call = cast<CallExpr>(E->getArg(0));
3679     const Expr *Chain = E->getArg(1);
3680     return EmitCall(Call->getCallee()->getType(),
3681                     EmitCallee(Call->getCallee()), Call, ReturnValue,
3682                     EmitScalarExpr(Chain));
3683   }
3684   case Builtin::BI_InterlockedExchange8:
3685   case Builtin::BI_InterlockedExchange16:
3686   case Builtin::BI_InterlockedExchange:
3687   case Builtin::BI_InterlockedExchangePointer:
3688     return RValue::get(
3689         EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchange, E));
3690   case Builtin::BI_InterlockedCompareExchangePointer:
3691   case Builtin::BI_InterlockedCompareExchangePointer_nf: {
3692     llvm::Type *RTy;
3693     llvm::IntegerType *IntType =
3694       IntegerType::get(getLLVMContext(),
3695                        getContext().getTypeSize(E->getType()));
3696     llvm::Type *IntPtrType = IntType->getPointerTo();
3697
3698     llvm::Value *Destination =
3699       Builder.CreateBitCast(EmitScalarExpr(E->getArg(0)), IntPtrType);
3700
3701     llvm::Value *Exchange = EmitScalarExpr(E->getArg(1));
3702     RTy = Exchange->getType();
3703     Exchange = Builder.CreatePtrToInt(Exchange, IntType);
3704
3705     llvm::Value *Comparand =
3706       Builder.CreatePtrToInt(EmitScalarExpr(E->getArg(2)), IntType);
3707
3708     auto Ordering =
3709       BuiltinID == Builtin::BI_InterlockedCompareExchangePointer_nf ?
3710       AtomicOrdering::Monotonic : AtomicOrdering::SequentiallyConsistent;
3711
3712     auto Result = Builder.CreateAtomicCmpXchg(Destination, Comparand, Exchange,
3713                                               Ordering, Ordering);
3714     Result->setVolatile(true);
3715
3716     return RValue::get(Builder.CreateIntToPtr(Builder.CreateExtractValue(Result,
3717                                                                          0),
3718                                               RTy));
3719   }
3720   case Builtin::BI_InterlockedCompareExchange8:
3721   case Builtin::BI_InterlockedCompareExchange16:
3722   case Builtin::BI_InterlockedCompareExchange:
3723   case Builtin::BI_InterlockedCompareExchange64:
3724     return RValue::get(EmitAtomicCmpXchgForMSIntrin(*this, E));
3725   case Builtin::BI_InterlockedIncrement16:
3726   case Builtin::BI_InterlockedIncrement:
3727     return RValue::get(
3728         EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedIncrement, E));
3729   case Builtin::BI_InterlockedDecrement16:
3730   case Builtin::BI_InterlockedDecrement:
3731     return RValue::get(
3732         EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedDecrement, E));
3733   case Builtin::BI_InterlockedAnd8:
3734   case Builtin::BI_InterlockedAnd16:
3735   case Builtin::BI_InterlockedAnd:
3736     return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedAnd, E));
3737   case Builtin::BI_InterlockedExchangeAdd8:
3738   case Builtin::BI_InterlockedExchangeAdd16:
3739   case Builtin::BI_InterlockedExchangeAdd:
3740     return RValue::get(
3741         EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchangeAdd, E));
3742   case Builtin::BI_InterlockedExchangeSub8:
3743   case Builtin::BI_InterlockedExchangeSub16:
3744   case Builtin::BI_InterlockedExchangeSub:
3745     return RValue::get(
3746         EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedExchangeSub, E));
3747   case Builtin::BI_InterlockedOr8:
3748   case Builtin::BI_InterlockedOr16:
3749   case Builtin::BI_InterlockedOr:
3750     return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedOr, E));
3751   case Builtin::BI_InterlockedXor8:
3752   case Builtin::BI_InterlockedXor16:
3753   case Builtin::BI_InterlockedXor:
3754     return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::_InterlockedXor, E));
3755
3756   case Builtin::BI_bittest64:
3757   case Builtin::BI_bittest:
3758   case Builtin::BI_bittestandcomplement64:
3759   case Builtin::BI_bittestandcomplement:
3760   case Builtin::BI_bittestandreset64:
3761   case Builtin::BI_bittestandreset:
3762   case Builtin::BI_bittestandset64:
3763   case Builtin::BI_bittestandset:
3764   case Builtin::BI_interlockedbittestandreset:
3765   case Builtin::BI_interlockedbittestandreset64:
3766   case Builtin::BI_interlockedbittestandset64:
3767   case Builtin::BI_interlockedbittestandset:
3768   case Builtin::BI_interlockedbittestandset_acq:
3769   case Builtin::BI_interlockedbittestandset_rel:
3770   case Builtin::BI_interlockedbittestandset_nf:
3771   case Builtin::BI_interlockedbittestandreset_acq:
3772   case Builtin::BI_interlockedbittestandreset_rel:
3773   case Builtin::BI_interlockedbittestandreset_nf:
3774     return RValue::get(EmitBitTestIntrinsic(*this, BuiltinID, E));
3775
3776     // These builtins exist to emit regular volatile loads and stores not
3777     // affected by the -fms-volatile setting.
3778   case Builtin::BI__iso_volatile_load8:
3779   case Builtin::BI__iso_volatile_load16:
3780   case Builtin::BI__iso_volatile_load32:
3781   case Builtin::BI__iso_volatile_load64:
3782     return RValue::get(EmitISOVolatileLoad(*this, E));
3783   case Builtin::BI__iso_volatile_store8:
3784   case Builtin::BI__iso_volatile_store16:
3785   case Builtin::BI__iso_volatile_store32:
3786   case Builtin::BI__iso_volatile_store64:
3787     return RValue::get(EmitISOVolatileStore(*this, E));
3788
3789   case Builtin::BI__exception_code:
3790   case Builtin::BI_exception_code:
3791     return RValue::get(EmitSEHExceptionCode());
3792   case Builtin::BI__exception_info:
3793   case Builtin::BI_exception_info:
3794     return RValue::get(EmitSEHExceptionInfo());
3795   case Builtin::BI__abnormal_termination:
3796   case Builtin::BI_abnormal_termination:
3797     return RValue::get(EmitSEHAbnormalTermination());
3798   case Builtin::BI_setjmpex:
3799     if (getTarget().getTriple().isOSMSVCRT() && E->getNumArgs() == 1 &&
3800         E->getArg(0)->getType()->isPointerType())
3801       return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmpex, E);
3802     break;
3803   case Builtin::BI_setjmp:
3804     if (getTarget().getTriple().isOSMSVCRT() && E->getNumArgs() == 1 &&
3805         E->getArg(0)->getType()->isPointerType()) {
3806       if (getTarget().getTriple().getArch() == llvm::Triple::x86)
3807         return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmp3, E);
3808       else if (getTarget().getTriple().getArch() == llvm::Triple::aarch64)
3809         return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmpex, E);
3810       return EmitMSVCRTSetJmp(*this, MSVCSetJmpKind::_setjmp, E);
3811     }
3812     break;
3813
3814   case Builtin::BI__GetExceptionInfo: {
3815     if (llvm::GlobalVariable *GV =
3816             CGM.getCXXABI().getThrowInfo(FD->getParamDecl(0)->getType()))
3817       return RValue::get(llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy));
3818     break;
3819   }
3820
3821   case Builtin::BI__fastfail:
3822     return RValue::get(EmitMSVCBuiltinExpr(MSVCIntrin::__fastfail, E));
3823
3824   case Builtin::BI__builtin_coro_size: {
3825     auto & Context = getContext();
3826     auto SizeTy = Context.getSizeType();
3827     auto T = Builder.getIntNTy(Context.getTypeSize(SizeTy));
3828     Function *F = CGM.getIntrinsic(Intrinsic::coro_size, T);
3829     return RValue::get(Builder.CreateCall(F));
3830   }
3831
3832   case Builtin::BI__builtin_coro_id:
3833     return EmitCoroutineIntrinsic(E, Intrinsic::coro_id);
3834   case Builtin::BI__builtin_coro_promise:
3835     return EmitCoroutineIntrinsic(E, Intrinsic::coro_promise);
3836   case Builtin::BI__builtin_coro_resume:
3837     return EmitCoroutineIntrinsic(E, Intrinsic::coro_resume);
3838   case Builtin::BI__builtin_coro_frame:
3839     return EmitCoroutineIntrinsic(E, Intrinsic::coro_frame);
3840   case Builtin::BI__builtin_coro_noop:
3841     return EmitCoroutineIntrinsic(E, Intrinsic::coro_noop);
3842   case Builtin::BI__builtin_coro_free:
3843     return EmitCoroutineIntrinsic(E, Intrinsic::coro_free);
3844   case Builtin::BI__builtin_coro_destroy:
3845     return EmitCoroutineIntrinsic(E, Intrinsic::coro_destroy);
3846   case Builtin::BI__builtin_coro_done:
3847     return EmitCoroutineIntrinsic(E, Intrinsic::coro_done);
3848   case Builtin::BI__builtin_coro_alloc:
3849     return EmitCoroutineIntrinsic(E, Intrinsic::coro_alloc);
3850   case Builtin::BI__builtin_coro_begin:
3851     return EmitCoroutineIntrinsic(E, Intrinsic::coro_begin);
3852   case Builtin::BI__builtin_coro_end:
3853     return EmitCoroutineIntrinsic(E, Intrinsic::coro_end);
3854   case Builtin::BI__builtin_coro_suspend:
3855     return EmitCoroutineIntrinsic(E, Intrinsic::coro_suspend);
3856   case Builtin::BI__builtin_coro_param:
3857     return EmitCoroutineIntrinsic(E, Intrinsic::coro_param);
3858
3859   // OpenCL v2.0 s6.13.16.2, Built-in pipe read and write functions
3860   case Builtin::BIread_pipe:
3861   case Builtin::BIwrite_pipe: {
3862     Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3863           *Arg1 = EmitScalarExpr(E->getArg(1));
3864     CGOpenCLRuntime OpenCLRT(CGM);
3865     Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3866     Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3867
3868     // Type of the generic packet parameter.
3869     unsigned GenericAS =
3870         getContext().getTargetAddressSpace(LangAS::opencl_generic);
3871     llvm::Type *I8PTy = llvm::PointerType::get(
3872         llvm::Type::getInt8Ty(getLLVMContext()), GenericAS);
3873
3874     // Testing which overloaded version we should generate the call for.
3875     if (2U == E->getNumArgs()) {
3876       const char *Name = (BuiltinID == Builtin::BIread_pipe) ? "__read_pipe_2"
3877                                                              : "__write_pipe_2";
3878       // Creating a generic function type to be able to call with any builtin or
3879       // user defined type.
3880       llvm::Type *ArgTys[] = {Arg0->getType(), I8PTy, Int32Ty, Int32Ty};
3881       llvm::FunctionType *FTy = llvm::FunctionType::get(
3882           Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3883       Value *BCast = Builder.CreatePointerCast(Arg1, I8PTy);
3884       return RValue::get(
3885           Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3886                              {Arg0, BCast, PacketSize, PacketAlign}));
3887     } else {
3888       assert(4 == E->getNumArgs() &&
3889              "Illegal number of parameters to pipe function");
3890       const char *Name = (BuiltinID == Builtin::BIread_pipe) ? "__read_pipe_4"
3891                                                              : "__write_pipe_4";
3892
3893       llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, I8PTy,
3894                               Int32Ty, Int32Ty};
3895       Value *Arg2 = EmitScalarExpr(E->getArg(2)),
3896             *Arg3 = EmitScalarExpr(E->getArg(3));
3897       llvm::FunctionType *FTy = llvm::FunctionType::get(
3898           Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3899       Value *BCast = Builder.CreatePointerCast(Arg3, I8PTy);
3900       // We know the third argument is an integer type, but we may need to cast
3901       // it to i32.
3902       if (Arg2->getType() != Int32Ty)
3903         Arg2 = Builder.CreateZExtOrTrunc(Arg2, Int32Ty);
3904       return RValue::get(Builder.CreateCall(
3905           CGM.CreateRuntimeFunction(FTy, Name),
3906           {Arg0, Arg1, Arg2, BCast, PacketSize, PacketAlign}));
3907     }
3908   }
3909   // OpenCL v2.0 s6.13.16 ,s9.17.3.5 - Built-in pipe reserve read and write
3910   // functions
3911   case Builtin::BIreserve_read_pipe:
3912   case Builtin::BIreserve_write_pipe:
3913   case Builtin::BIwork_group_reserve_read_pipe:
3914   case Builtin::BIwork_group_reserve_write_pipe:
3915   case Builtin::BIsub_group_reserve_read_pipe:
3916   case Builtin::BIsub_group_reserve_write_pipe: {
3917     // Composing the mangled name for the function.
3918     const char *Name;
3919     if (BuiltinID == Builtin::BIreserve_read_pipe)
3920       Name = "__reserve_read_pipe";
3921     else if (BuiltinID == Builtin::BIreserve_write_pipe)
3922       Name = "__reserve_write_pipe";
3923     else if (BuiltinID == Builtin::BIwork_group_reserve_read_pipe)
3924       Name = "__work_group_reserve_read_pipe";
3925     else if (BuiltinID == Builtin::BIwork_group_reserve_write_pipe)
3926       Name = "__work_group_reserve_write_pipe";
3927     else if (BuiltinID == Builtin::BIsub_group_reserve_read_pipe)
3928       Name = "__sub_group_reserve_read_pipe";
3929     else
3930       Name = "__sub_group_reserve_write_pipe";
3931
3932     Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3933           *Arg1 = EmitScalarExpr(E->getArg(1));
3934     llvm::Type *ReservedIDTy = ConvertType(getContext().OCLReserveIDTy);
3935     CGOpenCLRuntime OpenCLRT(CGM);
3936     Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3937     Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3938
3939     // Building the generic function prototype.
3940     llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty, Int32Ty};
3941     llvm::FunctionType *FTy = llvm::FunctionType::get(
3942         ReservedIDTy, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3943     // We know the second argument is an integer type, but we may need to cast
3944     // it to i32.
3945     if (Arg1->getType() != Int32Ty)
3946       Arg1 = Builder.CreateZExtOrTrunc(Arg1, Int32Ty);
3947     return RValue::get(
3948         Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3949                            {Arg0, Arg1, PacketSize, PacketAlign}));
3950   }
3951   // OpenCL v2.0 s6.13.16, s9.17.3.5 - Built-in pipe commit read and write
3952   // functions
3953   case Builtin::BIcommit_read_pipe:
3954   case Builtin::BIcommit_write_pipe:
3955   case Builtin::BIwork_group_commit_read_pipe:
3956   case Builtin::BIwork_group_commit_write_pipe:
3957   case Builtin::BIsub_group_commit_read_pipe:
3958   case Builtin::BIsub_group_commit_write_pipe: {
3959     const char *Name;
3960     if (BuiltinID == Builtin::BIcommit_read_pipe)
3961       Name = "__commit_read_pipe";
3962     else if (BuiltinID == Builtin::BIcommit_write_pipe)
3963       Name = "__commit_write_pipe";
3964     else if (BuiltinID == Builtin::BIwork_group_commit_read_pipe)
3965       Name = "__work_group_commit_read_pipe";
3966     else if (BuiltinID == Builtin::BIwork_group_commit_write_pipe)
3967       Name = "__work_group_commit_write_pipe";
3968     else if (BuiltinID == Builtin::BIsub_group_commit_read_pipe)
3969       Name = "__sub_group_commit_read_pipe";
3970     else
3971       Name = "__sub_group_commit_write_pipe";
3972
3973     Value *Arg0 = EmitScalarExpr(E->getArg(0)),
3974           *Arg1 = EmitScalarExpr(E->getArg(1));
3975     CGOpenCLRuntime OpenCLRT(CGM);
3976     Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
3977     Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
3978
3979     // Building the generic function prototype.
3980     llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, Int32Ty};
3981     llvm::FunctionType *FTy =
3982         llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()),
3983                                 llvm::ArrayRef<llvm::Type *>(ArgTys), false);
3984
3985     return RValue::get(
3986         Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
3987                            {Arg0, Arg1, PacketSize, PacketAlign}));
3988   }
3989   // OpenCL v2.0 s6.13.16.4 Built-in pipe query functions
3990   case Builtin::BIget_pipe_num_packets:
3991   case Builtin::BIget_pipe_max_packets: {
3992     const char *BaseName;
3993     const auto *PipeTy = E->getArg(0)->getType()->castAs<PipeType>();
3994     if (BuiltinID == Builtin::BIget_pipe_num_packets)
3995       BaseName = "__get_pipe_num_packets";
3996     else
3997       BaseName = "__get_pipe_max_packets";
3998     std::string Name = std::string(BaseName) +
3999                        std::string(PipeTy->isReadOnly() ? "_ro" : "_wo");
4000
4001     // Building the generic function prototype.
4002     Value *Arg0 = EmitScalarExpr(E->getArg(0));
4003     CGOpenCLRuntime OpenCLRT(CGM);
4004     Value *PacketSize = OpenCLRT.getPipeElemSize(E->getArg(0));
4005     Value *PacketAlign = OpenCLRT.getPipeElemAlign(E->getArg(0));
4006     llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty};
4007     llvm::FunctionType *FTy = llvm::FunctionType::get(
4008         Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
4009
4010     return RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name),
4011                                           {Arg0, PacketSize, PacketAlign}));
4012   }
4013
4014   // OpenCL v2.0 s6.13.9 - Address space qualifier functions.
4015   case Builtin::BIto_global:
4016   case Builtin::BIto_local:
4017   case Builtin::BIto_private: {
4018     auto Arg0 = EmitScalarExpr(E->getArg(0));
4019     auto NewArgT = llvm::PointerType::get(Int8Ty,
4020       CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
4021     auto NewRetT = llvm::PointerType::get(Int8Ty,
4022       CGM.getContext().getTargetAddressSpace(
4023         E->getType()->getPointeeType().getAddressSpace()));
4024     auto FTy = llvm::FunctionType::get(NewRetT, {NewArgT}, false);
4025     llvm::Value *NewArg;
4026     if (Arg0->getType()->getPointerAddressSpace() !=
4027         NewArgT->getPointerAddressSpace())
4028       NewArg = Builder.CreateAddrSpaceCast(Arg0, NewArgT);
4029     else
4030       NewArg = Builder.CreateBitOrPointerCast(Arg0, NewArgT);
4031     auto NewName = std::string("__") + E->getDirectCallee()->getName().str();
4032     auto NewCall =
4033         Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, NewName), {NewArg});
4034     return RValue::get(Builder.CreateBitOrPointerCast(NewCall,
4035       ConvertType(E->getType())));
4036   }
4037
4038   // OpenCL v2.0, s6.13.17 - Enqueue kernel function.
4039   // It contains four different overload formats specified in Table 6.13.17.1.
4040   case Builtin::BIenqueue_kernel: {
4041     StringRef Name; // Generated function call name
4042     unsigned NumArgs = E->getNumArgs();
4043
4044     llvm::Type *QueueTy = ConvertType(getContext().OCLQueueTy);
4045     llvm::Type *GenericVoidPtrTy = Builder.getInt8PtrTy(
4046         getContext().getTargetAddressSpace(LangAS::opencl_generic));
4047
4048     llvm::Value *Queue = EmitScalarExpr(E->getArg(0));
4049     llvm::Value *Flags = EmitScalarExpr(E->getArg(1));
4050     LValue NDRangeL = EmitAggExprToLValue(E->getArg(2));
4051     llvm::Value *Range = NDRangeL.getAddress(*this).getPointer();
4052     llvm::Type *RangeTy = NDRangeL.getAddress(*this).getType();
4053
4054     if (NumArgs == 4) {
4055       // The most basic form of the call with parameters:
4056       // queue_t, kernel_enqueue_flags_t, ndrange_t, block(void)
4057       Name = "__enqueue_kernel_basic";
4058       llvm::Type *ArgTys[] = {QueueTy, Int32Ty, RangeTy, GenericVoidPtrTy,
4059                               GenericVoidPtrTy};
4060       llvm::FunctionType *FTy = llvm::FunctionType::get(
4061           Int32Ty, llvm::ArrayRef<llvm::Type *>(ArgTys), false);
4062
4063       auto Info =
4064           CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(3));
4065       llvm::Value *Kernel =
4066           Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
4067       llvm::Value *Block =
4068           Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
4069
4070       AttrBuilder B;
4071       B.addByValAttr(NDRangeL.getAddress(*this).getElementType());
4072       llvm::AttributeList ByValAttrSet =
4073           llvm::AttributeList::get(CGM.getModule().getContext(), 3U, B);
4074
4075       auto RTCall =
4076           Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name, ByValAttrSet),
4077                              {Queue, Flags, Range, Kernel, Block});
4078       RTCall->setAttributes(ByValAttrSet);
4079       return RValue::get(RTCall);
4080     }
4081     assert(NumArgs >= 5 && "Invalid enqueue_kernel signature");
4082
4083     // Create a temporary array to hold the sizes of local pointer arguments
4084     // for the block. \p First is the position of the first size argument.
4085     auto CreateArrayForSizeVar = [=](unsigned First)
4086         -> std::tuple<llvm::Value *, llvm::Value *, llvm::Value *> {
4087       llvm::APInt ArraySize(32, NumArgs - First);
4088       QualType SizeArrayTy = getContext().getConstantArrayType(
4089           getContext().getSizeType(), ArraySize, nullptr, ArrayType::Normal,
4090           /*IndexTypeQuals=*/0);
4091       auto Tmp = CreateMemTemp(SizeArrayTy, "block_sizes");
4092       llvm::Value *TmpPtr = Tmp.getPointer();
4093       llvm::Value *TmpSize = EmitLifetimeStart(
4094           CGM.getDataLayout().getTypeAllocSize(Tmp.getElementType()), TmpPtr);
4095       llvm::Value *ElemPtr;
4096       // Each of the following arguments specifies the size of the corresponding
4097       // argument passed to the enqueued block.
4098       auto *Zero = llvm::ConstantInt::get(IntTy, 0);
4099       for (unsigned I = First; I < NumArgs; ++I) {
4100         auto *Index = llvm::ConstantInt::get(IntTy, I - First);
4101         auto *GEP = Builder.CreateGEP(TmpPtr, {Zero, Index});
4102         if (I == First)
4103           ElemPtr = GEP;
4104         auto *V =
4105             Builder.CreateZExtOrTrunc(EmitScalarExpr(E->getArg(I)), SizeTy);
4106         Builder.CreateAlignedStore(
4107             V, GEP, CGM.getDataLayout().getPrefTypeAlign(SizeTy));
4108       }
4109       return std::tie(ElemPtr, TmpSize, TmpPtr);
4110     };
4111
4112     // Could have events and/or varargs.
4113     if (E->getArg(3)->getType()->isBlockPointerType()) {
4114       // No events passed, but has variadic arguments.
4115       Name = "__enqueue_kernel_varargs";
4116       auto Info =
4117           CGM.getOpenCLRuntime().emitOpenCLEnqueuedBlock(*this, E->getArg(3));
4118       llvm::Value *Kernel =
4119           Builder.CreatePointerCast(Info.Kernel, GenericVoidPtrTy);
4120       auto *Block = Builder.CreatePointerCast(Info.BlockArg, GenericVoidPtrTy);
4121       llvm::Value *ElemPtr, *TmpSize, *TmpPtr;
4122       std::tie(ElemPtr, TmpSize, TmpPtr) = CreateArrayForSizeVar(4);
4123
4124       // Create a vector of the arguments, as well as a constant value to
4125       // express to the runtime the number of variadic arguments.
4126       llvm::Value *const Args[] = {Queue,  Flags,
4127                                    Range,  Kernel,
4128                                    Block,  ConstantInt::get(IntTy, NumArgs - 4),
4129                                    ElemPtr};
4130       llvm::Type *const ArgTys[] = {
4131           QueueTy,          IntTy, RangeTy,           GenericVoidPtrTy,
4132           GenericVoidPtrTy, IntTy, ElemPtr->getType()};
4133
4134       llvm::FunctionType *FTy = llvm::FunctionType::get(Int32Ty, ArgTys, false);
4135       auto Call = RValue::get(
4136           Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name), Args));
4137       if (TmpSize)
4138         EmitLifetimeEnd(TmpSize, TmpPtr);
4139       return Call;
4140     }
4141     // Any calls now have event arguments passed.
4142     if (NumArgs >= 7) {
4143       llvm::Type *EventTy = ConvertType(getContext().OCLClkEventTy);
4144       llvm::PointerType *EventPtrTy = EventTy->getPointerTo(
4145           CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
4146
4147       llvm::Value *NumEvents =
4148           Builder.CreateZExtOrTrunc(EmitScalarExpr(E->getArg(3)), Int32Ty);
4149
4150       // Since SemaOpenCLBuiltinEnqueueKernel allows fifth and sixth arguments
4151       // to be a null pointer constant (including `0` literal), we can take it
4152       // into account and emit null pointer directly.
4153       llvm::Value *EventWaitList = nullptr;
4154       if (E->getArg(4)->isNullPointerConstant(
4155               getContext(), Expr::NPC_ValueDependentIsNotNull)) {
4156         EventWaitList = llvm::ConstantPointerNull::get(EventPtrTy);
4157       } else {
4158         EventWaitList = E->getArg(4)->getType()->isArrayType()
4159                         ? EmitArrayToPointerDecay(E->getArg(4)).getPointer()
4160                         : EmitScalarExpr(E->getArg(4));
4161         // Convert to generic address space.
4162         EventWaitList = Builder.CreatePointerCast(EventWaitList, EventPtrTy);
4163       }
4164       llvm::Value *EventRet = nullptr;
4165       if (E->getArg(5)->isNullPointerConstant(
4166               getContext(), Expr::NPC_ValueDependentIsNotNull)) {