forked from llvm/clangir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIRGenBuiltin.cpp
2710 lines (2407 loc) · 106 KB
/
CIRGenBuiltin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===---- CIRGenBuiltin.cpp - Emit CIR for builtins -----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This contains code to emit Builtin calls as CIR or a function call to be
// later resolved.
//
//===----------------------------------------------------------------------===//
#include "CIRGenCXXABI.h"
#include "CIRGenCall.h"
#include "CIRGenCstEmitter.h"
#include "CIRGenFunction.h"
#include "CIRGenModule.h"
#include "TargetInfo.h"
#include "clang/CIR/MissingFeatures.h"
// TODO(cir): we shouldn't need this but we currently reuse intrinsic IDs for
// convenience.
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "llvm/IR/Intrinsics.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/TargetBuiltins.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Value.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "llvm/Support/ErrorHandling.h"
using namespace clang;
using namespace clang::CIRGen;
using namespace cir;
using namespace llvm;
static RValue emitLibraryCall(CIRGenFunction &CGF, const FunctionDecl *FD,
const CallExpr *E, mlir::Operation *calleeValue) {
auto callee = CIRGenCallee::forDirect(calleeValue, GlobalDecl(FD));
return CGF.emitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
}
static mlir::Value tryUseTestFPKind(CIRGenFunction &CGF, unsigned BuiltinID,
mlir::Value V) {
if (CGF.getBuilder().getIsFPConstrained() &&
CGF.getBuilder().getDefaultConstrainedExcept() != cir::fp::ebIgnore) {
if (mlir::Value Result = CGF.getTargetHooks().testFPKind(
V, BuiltinID, CGF.getBuilder(), CGF.CGM))
return Result;
}
return nullptr;
}
template <class Operation>
static RValue emitUnaryFPBuiltin(CIRGenFunction &CGF, const CallExpr &E) {
auto Arg = CGF.emitScalarExpr(E.getArg(0));
CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(CGF, &E);
if (CGF.getBuilder().getIsFPConstrained())
llvm_unreachable("constraint FP operations are NYI");
auto Call =
CGF.getBuilder().create<Operation>(Arg.getLoc(), Arg.getType(), Arg);
return RValue::get(Call->getResult(0));
}
template <typename Op>
static RValue emitUnaryMaybeConstrainedFPToIntBuiltin(CIRGenFunction &CGF,
const CallExpr &E) {
auto ResultType = CGF.convertType(E.getType());
auto Src = CGF.emitScalarExpr(E.getArg(0));
if (CGF.getBuilder().getIsFPConstrained())
llvm_unreachable("constraint FP operations are NYI");
auto Call = CGF.getBuilder().create<Op>(Src.getLoc(), ResultType, Src);
return RValue::get(Call->getResult(0));
}
template <typename Op>
static RValue emitBinaryFPBuiltin(CIRGenFunction &CGF, const CallExpr &E) {
auto Arg0 = CGF.emitScalarExpr(E.getArg(0));
auto Arg1 = CGF.emitScalarExpr(E.getArg(1));
auto Loc = CGF.getLoc(E.getExprLoc());
auto Ty = CGF.convertType(E.getType());
auto Call = CGF.getBuilder().create<Op>(Loc, Ty, Arg0, Arg1);
return RValue::get(Call->getResult(0));
}
template <typename Op>
static mlir::Value emitBinaryMaybeConstrainedFPBuiltin(CIRGenFunction &CGF,
const CallExpr &E) {
auto Arg0 = CGF.emitScalarExpr(E.getArg(0));
auto Arg1 = CGF.emitScalarExpr(E.getArg(1));
auto Loc = CGF.getLoc(E.getExprLoc());
auto Ty = CGF.convertType(E.getType());
if (CGF.getBuilder().getIsFPConstrained()) {
CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(CGF, &E);
llvm_unreachable("constrained FP operations are NYI");
} else {
auto Call = CGF.getBuilder().create<Op>(Loc, Ty, Arg0, Arg1);
return Call->getResult(0);
}
}
template <typename Op>
static RValue
emitBuiltinBitOp(CIRGenFunction &CGF, const CallExpr *E,
std::optional<CIRGenFunction::BuiltinCheckKind> CK) {
mlir::Value arg;
if (CK.has_value())
arg = CGF.emitCheckedArgForBuiltin(E->getArg(0), *CK);
else
arg = CGF.emitScalarExpr(E->getArg(0));
auto resultTy = CGF.convertType(E->getType());
auto op =
CGF.getBuilder().create<Op>(CGF.getLoc(E->getExprLoc()), resultTy, arg);
return RValue::get(op);
}
// Initialize the alloca with the given size and alignment according to the lang
// opts. Supporting only the trivial non-initialization for now.
static void initializeAlloca(CIRGenFunction &CGF,
[[maybe_unused]] mlir::Value AllocaAddr,
[[maybe_unused]] mlir::Value Size,
[[maybe_unused]] CharUnits AlignmentInBytes) {
switch (CGF.getLangOpts().getTrivialAutoVarInit()) {
case LangOptions::TrivialAutoVarInitKind::Uninitialized:
// Nothing to initialize.
return;
case LangOptions::TrivialAutoVarInitKind::Zero:
case LangOptions::TrivialAutoVarInitKind::Pattern:
assert(false && "unexpected trivial auto var init kind NYI");
return;
}
}
namespace {
struct WidthAndSignedness {
unsigned Width;
bool Signed;
};
} // namespace
static WidthAndSignedness
getIntegerWidthAndSignedness(const clang::ASTContext &astContext,
const clang::QualType Type) {
assert(Type->isIntegerType() && "Given type is not an integer.");
unsigned Width = Type->isBooleanType() ? 1
: Type->isBitIntType() ? astContext.getIntWidth(Type)
: astContext.getTypeInfo(Type).Width;
bool Signed = Type->isSignedIntegerType();
return {Width, Signed};
}
// Given one or more integer types, this function produces an integer type that
// encompasses them: any value in one of the given types could be expressed in
// the encompassing type.
static struct WidthAndSignedness
EncompassingIntegerType(ArrayRef<struct WidthAndSignedness> Types) {
assert(Types.size() > 0 && "Empty list of types.");
// If any of the given types is signed, we must return a signed type.
bool Signed = false;
for (const auto &Type : Types) {
Signed |= Type.Signed;
}
// The encompassing type must have a width greater than or equal to the width
// of the specified types. Additionally, if the encompassing type is signed,
// its width must be strictly greater than the width of any unsigned types
// given.
unsigned Width = 0;
for (const auto &Type : Types) {
unsigned MinWidth = Type.Width + (Signed && !Type.Signed);
if (Width < MinWidth) {
Width = MinWidth;
}
}
return {Width, Signed};
}
/// Emit the conversions required to turn the given value into an
/// integer of the given size.
static mlir::Value emitToInt(CIRGenFunction &CGF, mlir::Value v, QualType t,
cir::IntType intType) {
v = CGF.emitToMemory(v, t);
if (isa<cir::PointerType>(v.getType()))
return CGF.getBuilder().createPtrToInt(v, intType);
assert(v.getType() == intType);
return v;
}
static mlir::Value emitFromInt(CIRGenFunction &CGF, mlir::Value v, QualType t,
mlir::Type resultType) {
v = CGF.emitFromMemory(v, t);
if (isa<cir::PointerType>(resultType))
return CGF.getBuilder().createIntToPtr(v, resultType);
assert(v.getType() == resultType);
return v;
}
static mlir::Value emitSignBit(mlir::Location loc, CIRGenFunction &CGF,
mlir::Value val) {
assert(!::cir::MissingFeatures::isPPC_FP128Ty());
auto ret = CGF.getBuilder().createSignBit(loc, val);
return ret->getResult(0);
}
static Address checkAtomicAlignment(CIRGenFunction &CGF, const CallExpr *E) {
ASTContext &astContext = CGF.getContext();
Address ptr = CGF.emitPointerWithAlignment(E->getArg(0));
unsigned bytes =
isa<cir::PointerType>(ptr.getElementType())
? astContext.getTypeSizeInChars(astContext.VoidPtrTy).getQuantity()
: CGF.CGM.getDataLayout().getTypeSizeInBits(ptr.getElementType()) / 8;
unsigned align = ptr.getAlignment().getQuantity();
if (align % bytes != 0) {
DiagnosticsEngine &diags = CGF.CGM.getDiags();
diags.Report(E->getBeginLoc(), diag::warn_sync_op_misaligned);
// Force address to be at least naturally-aligned.
return ptr.withAlignment(CharUnits::fromQuantity(bytes));
}
return ptr;
}
/// Utility to insert an atomic instruction based on Intrinsic::ID
/// and the expression node.
static mlir::Value makeBinaryAtomicValue(
CIRGenFunction &cgf, cir::AtomicFetchKind kind, const CallExpr *expr,
mlir::Value *neededValP = nullptr, mlir::Type *neededValT = nullptr,
cir::MemOrder ordering = cir::MemOrder::SequentiallyConsistent) {
QualType typ = expr->getType();
assert(expr->getArg(0)->getType()->isPointerType());
assert(cgf.getContext().hasSameUnqualifiedType(
typ, expr->getArg(0)->getType()->getPointeeType()));
assert(
cgf.getContext().hasSameUnqualifiedType(typ, expr->getArg(1)->getType()));
Address destAddr = checkAtomicAlignment(cgf, expr);
auto &builder = cgf.getBuilder();
auto intType =
expr->getArg(0)->getType()->getPointeeType()->isUnsignedIntegerType()
? builder.getUIntNTy(cgf.getContext().getTypeSize(typ))
: builder.getSIntNTy(cgf.getContext().getTypeSize(typ));
mlir::Value val = cgf.emitScalarExpr(expr->getArg(1));
mlir::Type valueType = val.getType();
val = emitToInt(cgf, val, typ, intType);
// These output arguments are needed for post atomic fetch operations
// that calculate the result of the operation as return value of
// <binop>_and_fetch builtins. The `AtomicFetch` operation only updates the
// memory location and returns the old value.
if (neededValP) {
assert(neededValT);
*neededValP = val;
*neededValT = valueType;
}
auto rmwi = builder.create<cir::AtomicFetch>(
cgf.getLoc(expr->getSourceRange()), destAddr.emitRawPointer(), val, kind,
ordering, false, /* is volatile */
true); /* fetch first */
return emitFromInt(cgf, rmwi->getResult(0), typ, valueType);
}
static RValue emitBinaryAtomic(CIRGenFunction &CGF, cir::AtomicFetchKind kind,
const CallExpr *E) {
return RValue::get(makeBinaryAtomicValue(CGF, kind, E));
}
static RValue emitBinaryAtomicPost(CIRGenFunction &cgf,
cir::AtomicFetchKind atomicOpkind,
const CallExpr *e,
cir::BinOpKind binopKind) {
mlir::Value val;
mlir::Type valueType;
clang::QualType typ = e->getType();
mlir::Value result =
makeBinaryAtomicValue(cgf, atomicOpkind, e, &val, &valueType);
clang::CIRGen::CIRGenBuilderTy &builder = cgf.getBuilder();
result = builder.create<cir::BinOp>(result.getLoc(), binopKind, result, val);
result = emitFromInt(cgf, result, typ, valueType);
// FIXME: Some callers of this function expect the result to be inverted,
// which would need invert flag passed in and do the inversion here like
// traditional clang code gen does. When we implment those caller builtins
// we should implement the inversion here.
assert(!MissingFeatures::emitBinaryAtomicPostHasInvert());
return RValue::get(result);
}
static mlir::Value MakeAtomicCmpXchgValue(CIRGenFunction &cgf,
const CallExpr *expr,
bool returnBool) {
QualType typ = returnBool ? expr->getArg(1)->getType() : expr->getType();
Address destAddr = checkAtomicAlignment(cgf, expr);
auto &builder = cgf.getBuilder();
auto intType =
expr->getArg(0)->getType()->getPointeeType()->isUnsignedIntegerType()
? builder.getUIntNTy(cgf.getContext().getTypeSize(typ))
: builder.getSIntNTy(cgf.getContext().getTypeSize(typ));
auto cmpVal = cgf.emitScalarExpr(expr->getArg(1));
cmpVal = emitToInt(cgf, cmpVal, typ, intType);
auto newVal =
emitToInt(cgf, cgf.emitScalarExpr(expr->getArg(2)), typ, intType);
auto op = builder.create<cir::AtomicCmpXchg>(
cgf.getLoc(expr->getSourceRange()), cmpVal.getType(), builder.getBoolTy(),
destAddr.getPointer(), cmpVal, newVal,
MemOrderAttr::get(&cgf.getMLIRContext(), cir::MemOrder::SequentiallyConsistent),
MemOrderAttr::get(&cgf.getMLIRContext(), cir::MemOrder::SequentiallyConsistent),
builder.getI64IntegerAttr(destAddr.getAlignment().getAsAlign().value()));
return returnBool ? op.getResult(1) : op.getResult(0);
}
static bool
typeRequiresBuiltinLaunderImp(const ASTContext &astContext, QualType ty,
llvm::SmallPtrSetImpl<const Decl *> &seen) {
if (const auto *arr = astContext.getAsArrayType(ty))
ty = astContext.getBaseElementType(arr);
const auto *record = ty->getAsCXXRecordDecl();
if (!record)
return false;
// We've already checked this type, or are in the process of checking it.
if (!seen.insert(record).second)
return false;
assert(record->hasDefinition() &&
"Incomplete types should already be diagnosed");
if (record->isDynamicClass())
return true;
for (FieldDecl *fld : record->fields()) {
if (typeRequiresBuiltinLaunderImp(astContext, fld->getType(), seen))
return true;
}
return false;
}
/// Determine if the specified type requires laundering by checking if it is a
/// dynamic class type or contains a subobject which is a dynamic class type.
static bool typeRequiresBuiltinLaunder(clang::CIRGen::CIRGenModule &cgm,
QualType ty) {
if (!cgm.getCodeGenOpts().StrictVTablePointers)
return false;
llvm::SmallPtrSet<const Decl *, 16> seen;
return typeRequiresBuiltinLaunderImp(cgm.getASTContext(), ty, seen);
}
RValue CIRGenFunction::emitRotate(const CallExpr *E, bool IsRotateRight) {
auto src = emitScalarExpr(E->getArg(0));
auto shiftAmt = emitScalarExpr(E->getArg(1));
// The builtin's shift arg may have a different type than the source arg and
// result, but the CIR ops uses the same type for all values.
auto ty = src.getType();
shiftAmt = builder.createIntCast(shiftAmt, ty);
auto r =
builder.create<cir::RotateOp>(getLoc(E->getSourceRange()), src, shiftAmt);
if (!IsRotateRight)
r->setAttr("left", mlir::UnitAttr::get(src.getContext()));
return RValue::get(r);
}
static bool isMemBuiltinOutOfBoundPossible(const clang::Expr *sizeArg,
const clang::Expr *dstSizeArg,
clang::ASTContext &astContext,
llvm::APSInt &size) {
clang::Expr::EvalResult sizeResult, dstSizeResult;
if (!sizeArg->EvaluateAsInt(sizeResult, astContext) ||
!dstSizeArg->EvaluateAsInt(dstSizeResult, astContext))
return true;
size = sizeResult.Val.getInt();
llvm::APSInt dstSize = dstSizeResult.Val.getInt();
return size.ugt(dstSize);
}
RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
const CallExpr *E,
ReturnValueSlot ReturnValue) {
const FunctionDecl *FD = GD.getDecl()->getAsFunction();
// See if we can constant fold this builtin. If so, don't emit it at all.
// TODO: Extend this handling to all builtin calls that we can constant-fold.
Expr::EvalResult Result;
if (E->isPRValue() && E->EvaluateAsRValue(Result, CGM.getASTContext()) &&
!Result.hasSideEffects()) {
if (Result.Val.isInt()) {
return RValue::get(builder.getConstInt(getLoc(E->getSourceRange()),
Result.Val.getInt()));
}
if (Result.Val.isFloat()) {
// Note: we are using result type of CallExpr to determine the type of
// the constant. Clang Codegen uses the result value to make judgement
// of the type. We feel it should be Ok to use expression type because
// it is hard to imagine a builtin function evaluates to
// a value that over/underflows its own defined type.
mlir::Type resTy = convertType(E->getType());
return RValue::get(builder.getConstFP(getLoc(E->getExprLoc()), resTy,
Result.Val.getFloat()));
}
}
// If current long-double semantics is IEEE 128-bit, replace math builtins
// of long-double with f128 equivalent.
// TODO: This mutation should also be applied to other targets other than PPC,
// after backend supports IEEE 128-bit style libcalls.
if (getTarget().getTriple().isPPC64() &&
&getTarget().getLongDoubleFormat() == &llvm::APFloat::IEEEquad())
llvm_unreachable("NYI");
// If the builtin has been declared explicitly with an assembler label,
// disable the specialized emitting below. Ideally we should communicate the
// rename in IR, or at least avoid generating the intrinsic calls that are
// likely to get lowered to the renamed library functions.
const unsigned BuiltinIDIfNoAsmLabel =
FD->hasAttr<AsmLabelAttr>() ? 0 : BuiltinID;
std::optional<bool> ErrnoOverriden;
// ErrnoOverriden is true if math-errno is overriden via the
// '#pragma float_control(precise, on)'. This pragma disables fast-math,
// which implies math-errno.
if (E->hasStoredFPFeatures()) {
FPOptionsOverride OP = E->getFPFeatures();
if (OP.hasMathErrnoOverride())
ErrnoOverriden = OP.getMathErrnoOverride();
}
// True if 'atttibute__((optnone)) is used. This attibute overrides
// fast-math which implies math-errno.
bool OptNone = CurFuncDecl && CurFuncDecl->hasAttr<OptimizeNoneAttr>();
// True if we are compiling at -O2 and errno has been disabled
// using the '#pragma float_control(precise, off)', and
// attribute opt-none hasn't been seen.
[[maybe_unused]] bool ErrnoOverridenToFalseWithOpt =
ErrnoOverriden.has_value() && !ErrnoOverriden.value() && !OptNone &&
CGM.getCodeGenOpts().OptimizationLevel != 0;
// There are LLVM math intrinsics/instructions corresponding to math library
// functions except the LLVM op will never set errno while the math library
// might. Also, math builtins have the same semantics as their math library
// twins. Thus, we can transform math library and builtin calls to their
// LLVM counterparts if the call is marked 'const' (known to never set errno).
// In case FP exceptions are enabled, the experimental versions of the
// intrinsics model those.
[[maybe_unused]] bool ConstAlways =
getContext().BuiltinInfo.isConst(BuiltinID);
// There's a special case with the fma builtins where they are always const
// if the target environment is GNU or the target is OS is Windows and we're
// targeting the MSVCRT.dll environment.
// FIXME: This list can be become outdated. Need to find a way to get it some
// other way.
switch (BuiltinID) {
case Builtin::BI__builtin_fma:
case Builtin::BI__builtin_fmaf:
case Builtin::BI__builtin_fmal:
case Builtin::BIfma:
case Builtin::BIfmaf:
case Builtin::BIfmal: {
auto &Trip = CGM.getTriple();
if (Trip.isGNUEnvironment() || Trip.isOSMSVCRT())
ConstAlways = true;
break;
}
case Builtin::BI__builtin_fmaf16:
llvm_unreachable("Builtin::BI__builtin_fmaf16 NYI");
break;
default:
break;
}
bool ConstWithoutErrnoAndExceptions =
getContext().BuiltinInfo.isConstWithoutErrnoAndExceptions(BuiltinID);
bool ConstWithoutExceptions =
getContext().BuiltinInfo.isConstWithoutExceptions(BuiltinID);
// ConstAttr is enabled in fast-math mode. In fast-math mode, math-errno is
// disabled.
// Math intrinsics are generated only when math-errno is disabled. Any pragmas
// or attributes that affect math-errno should prevent or allow math
// intrincs to be generated. Intrinsics are generated:
// 1- In fast math mode, unless math-errno is overriden
// via '#pragma float_control(precise, on)', or via an
// 'attribute__((optnone))'.
// 2- If math-errno was enabled on command line but overriden
// to false via '#pragma float_control(precise, off))' and
// 'attribute__((optnone))' hasn't been used.
// 3- If we are compiling with optimization and errno has been disabled
// via '#pragma float_control(precise, off)', and
// 'attribute__((optnone))' hasn't been used.
bool ConstWithoutErrnoOrExceptions =
ConstWithoutErrnoAndExceptions || ConstWithoutExceptions;
bool GenerateIntrinsics =
(ConstAlways && !OptNone) ||
(!getLangOpts().MathErrno &&
!(ErrnoOverriden.has_value() && ErrnoOverriden.value()) && !OptNone);
if (!GenerateIntrinsics) {
GenerateIntrinsics =
ConstWithoutErrnoOrExceptions && !ConstWithoutErrnoAndExceptions;
if (!GenerateIntrinsics)
GenerateIntrinsics =
ConstWithoutErrnoOrExceptions &&
(!getLangOpts().MathErrno &&
!(ErrnoOverriden.has_value() && ErrnoOverriden.value()) && !OptNone);
if (!GenerateIntrinsics)
GenerateIntrinsics =
ConstWithoutErrnoOrExceptions && ErrnoOverridenToFalseWithOpt;
}
if (GenerateIntrinsics) {
switch (BuiltinIDIfNoAsmLabel) {
case Builtin::BIacos:
case Builtin::BIacosf:
case Builtin::BIacosl:
case Builtin::BI__builtin_acos:
case Builtin::BI__builtin_acosf:
case Builtin::BI__builtin_acosf16:
case Builtin::BI__builtin_acosl:
case Builtin::BI__builtin_acosf128:
llvm_unreachable("Builtin::BIacos like NYI");
case Builtin::BIasin:
case Builtin::BIasinf:
case Builtin::BIasinl:
case Builtin::BI__builtin_asin:
case Builtin::BI__builtin_asinf:
case Builtin::BI__builtin_asinf16:
case Builtin::BI__builtin_asinl:
case Builtin::BI__builtin_asinf128:
llvm_unreachable("Builtin::BIasin like NYI");
case Builtin::BIatan:
case Builtin::BIatanf:
case Builtin::BIatanl:
case Builtin::BI__builtin_atan:
case Builtin::BI__builtin_atanf:
case Builtin::BI__builtin_atanf16:
case Builtin::BI__builtin_atanl:
case Builtin::BI__builtin_atanf128:
llvm_unreachable("Builtin::BIatan like NYI");
case Builtin::BIceil:
case Builtin::BIceilf:
case Builtin::BIceill:
case Builtin::BI__builtin_ceil:
case Builtin::BI__builtin_ceilf:
case Builtin::BI__builtin_ceilf16:
case Builtin::BI__builtin_ceill:
case Builtin::BI__builtin_ceilf128:
return emitUnaryFPBuiltin<cir::CeilOp>(*this, *E);
case Builtin::BIcopysign:
case Builtin::BIcopysignf:
case Builtin::BIcopysignl:
case Builtin::BI__builtin_copysign:
case Builtin::BI__builtin_copysignf:
case Builtin::BI__builtin_copysignl:
return emitBinaryFPBuiltin<cir::CopysignOp>(*this, *E);
case Builtin::BI__builtin_copysignf16:
case Builtin::BI__builtin_copysignf128:
llvm_unreachable("BI__builtin_copysignf16 like NYI");
case Builtin::BIcos:
case Builtin::BIcosf:
case Builtin::BIcosl:
case Builtin::BI__builtin_cos:
case Builtin::BI__builtin_cosf:
case Builtin::BI__builtin_cosf16:
case Builtin::BI__builtin_cosl:
case Builtin::BI__builtin_cosf128:
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryFPBuiltin<cir::CosOp>(*this, *E);
case Builtin::BIcosh:
case Builtin::BIcoshf:
case Builtin::BIcoshl:
case Builtin::BI__builtin_cosh:
case Builtin::BI__builtin_coshf:
case Builtin::BI__builtin_coshf16:
case Builtin::BI__builtin_coshl:
case Builtin::BI__builtin_coshf128:
llvm_unreachable("Builtin::BIcosh like NYI");
case Builtin::BIexp:
case Builtin::BIexpf:
case Builtin::BIexpl:
case Builtin::BI__builtin_exp:
case Builtin::BI__builtin_expf:
case Builtin::BI__builtin_expf16:
case Builtin::BI__builtin_expl:
case Builtin::BI__builtin_expf128:
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryFPBuiltin<cir::ExpOp>(*this, *E);
case Builtin::BIexp2:
case Builtin::BIexp2f:
case Builtin::BIexp2l:
case Builtin::BI__builtin_exp2:
case Builtin::BI__builtin_exp2f:
case Builtin::BI__builtin_exp2f16:
case Builtin::BI__builtin_exp2l:
case Builtin::BI__builtin_exp2f128:
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryFPBuiltin<cir::Exp2Op>(*this, *E);
case Builtin::BI__builtin_exp10:
case Builtin::BI__builtin_exp10f:
case Builtin::BI__builtin_exp10f16:
case Builtin::BI__builtin_exp10l:
case Builtin::BI__builtin_exp10f128:
llvm_unreachable("BI__builtin_exp10 like NYI");
case Builtin::BIfabs:
case Builtin::BIfabsf:
case Builtin::BIfabsl:
case Builtin::BI__builtin_fabs:
case Builtin::BI__builtin_fabsf:
case Builtin::BI__builtin_fabsf16:
case Builtin::BI__builtin_fabsl:
case Builtin::BI__builtin_fabsf128:
return emitUnaryFPBuiltin<cir::FAbsOp>(*this, *E);
case Builtin::BIfloor:
case Builtin::BIfloorf:
case Builtin::BIfloorl:
case Builtin::BI__builtin_floor:
case Builtin::BI__builtin_floorf:
case Builtin::BI__builtin_floorf16:
case Builtin::BI__builtin_floorl:
case Builtin::BI__builtin_floorf128:
return emitUnaryFPBuiltin<cir::FloorOp>(*this, *E);
case Builtin::BIfma:
case Builtin::BIfmaf:
case Builtin::BIfmal:
case Builtin::BI__builtin_fma:
case Builtin::BI__builtin_fmaf:
case Builtin::BI__builtin_fmaf16:
case Builtin::BI__builtin_fmal:
case Builtin::BI__builtin_fmaf128:
llvm_unreachable("Builtin::BIfma like NYI");
case Builtin::BIfmax:
case Builtin::BIfmaxf:
case Builtin::BIfmaxl:
case Builtin::BI__builtin_fmax:
case Builtin::BI__builtin_fmaxf:
case Builtin::BI__builtin_fmaxl:
return RValue::get(
emitBinaryMaybeConstrainedFPBuiltin<cir::FMaxNumOp>(*this, *E));
case Builtin::BI__builtin_fmaxf16:
case Builtin::BI__builtin_fmaxf128:
llvm_unreachable("BI__builtin_fmaxf16 like NYI");
case Builtin::BIfmin:
case Builtin::BIfminf:
case Builtin::BIfminl:
case Builtin::BI__builtin_fmin:
case Builtin::BI__builtin_fminf:
case Builtin::BI__builtin_fminl:
return RValue::get(
emitBinaryMaybeConstrainedFPBuiltin<cir::FMinNumOp>(*this, *E));
case Builtin::BI__builtin_fminf16:
case Builtin::BI__builtin_fminf128:
llvm_unreachable("BI__builtin_fminf16 like NYI");
// fmod() is a special-case. It maps to the frem instruction rather than an
// LLVM intrinsic.
case Builtin::BIfmod:
case Builtin::BIfmodf:
case Builtin::BIfmodl:
case Builtin::BI__builtin_fmod:
case Builtin::BI__builtin_fmodf:
case Builtin::BI__builtin_fmodl:
assert(!cir::MissingFeatures::fastMathFlags());
return emitBinaryFPBuiltin<cir::FModOp>(*this, *E);
case Builtin::BI__builtin_fmodf16:
case Builtin::BI__builtin_fmodf128:
case Builtin::BI__builtin_elementwise_fmod:
llvm_unreachable("BI__builtin_fmodf16 like NYI");
case Builtin::BIlog:
case Builtin::BIlogf:
case Builtin::BIlogl:
case Builtin::BI__builtin_log:
case Builtin::BI__builtin_logf:
case Builtin::BI__builtin_logf16:
case Builtin::BI__builtin_logl:
case Builtin::BI__builtin_logf128:
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryFPBuiltin<cir::LogOp>(*this, *E);
case Builtin::BIlog10:
case Builtin::BIlog10f:
case Builtin::BIlog10l:
case Builtin::BI__builtin_log10:
case Builtin::BI__builtin_log10f:
case Builtin::BI__builtin_log10f16:
case Builtin::BI__builtin_log10l:
case Builtin::BI__builtin_log10f128:
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryFPBuiltin<cir::Log10Op>(*this, *E);
case Builtin::BIlog2:
case Builtin::BIlog2f:
case Builtin::BIlog2l:
case Builtin::BI__builtin_log2:
case Builtin::BI__builtin_log2f:
case Builtin::BI__builtin_log2f16:
case Builtin::BI__builtin_log2l:
case Builtin::BI__builtin_log2f128:
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryFPBuiltin<cir::Log2Op>(*this, *E);
case Builtin::BInearbyint:
case Builtin::BInearbyintf:
case Builtin::BInearbyintl:
case Builtin::BI__builtin_nearbyint:
case Builtin::BI__builtin_nearbyintf:
case Builtin::BI__builtin_nearbyintl:
case Builtin::BI__builtin_nearbyintf128:
return emitUnaryFPBuiltin<cir::NearbyintOp>(*this, *E);
case Builtin::BIpow:
case Builtin::BIpowf:
case Builtin::BIpowl:
case Builtin::BI__builtin_pow:
case Builtin::BI__builtin_powf:
case Builtin::BI__builtin_powl:
assert(!cir::MissingFeatures::fastMathFlags());
return RValue::get(
emitBinaryMaybeConstrainedFPBuiltin<cir::PowOp>(*this, *E));
case Builtin::BI__builtin_powf16:
case Builtin::BI__builtin_powf128:
llvm_unreachable("BI__builtin_powf16 like NYI");
case Builtin::BIrint:
case Builtin::BIrintf:
case Builtin::BIrintl:
case Builtin::BI__builtin_rint:
case Builtin::BI__builtin_rintf:
case Builtin::BI__builtin_rintf16:
case Builtin::BI__builtin_rintl:
case Builtin::BI__builtin_rintf128:
return emitUnaryFPBuiltin<cir::RintOp>(*this, *E);
case Builtin::BIround:
case Builtin::BIroundf:
case Builtin::BIroundl:
case Builtin::BI__builtin_round:
case Builtin::BI__builtin_roundf:
case Builtin::BI__builtin_roundf16:
case Builtin::BI__builtin_roundl:
case Builtin::BI__builtin_roundf128:
return emitUnaryFPBuiltin<cir::RoundOp>(*this, *E);
case Builtin::BIroundeven:
case Builtin::BIroundevenf:
case Builtin::BIroundevenl:
case Builtin::BI__builtin_roundeven:
case Builtin::BI__builtin_roundevenf:
case Builtin::BI__builtin_roundevenf16:
case Builtin::BI__builtin_roundevenl:
case Builtin::BI__builtin_roundevenf128:
llvm_unreachable("Builtin::BIroundeven like NYI");
case Builtin::BIsin:
case Builtin::BIsinf:
case Builtin::BIsinl:
case Builtin::BI__builtin_sin:
case Builtin::BI__builtin_sinf:
case Builtin::BI__builtin_sinf16:
case Builtin::BI__builtin_sinl:
case Builtin::BI__builtin_sinf128:
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryFPBuiltin<cir::SinOp>(*this, *E);
case Builtin::BIsqrt:
case Builtin::BIsqrtf:
case Builtin::BIsqrtl:
case Builtin::BI__builtin_sqrt:
case Builtin::BI__builtin_sqrtf:
case Builtin::BI__builtin_sqrtf16:
case Builtin::BI__builtin_sqrtl:
case Builtin::BI__builtin_sqrtf128:
assert(!cir::MissingFeatures::fastMathFlags());
return emitUnaryFPBuiltin<cir::SqrtOp>(*this, *E);
case Builtin::BI__builtin_elementwise_sqrt:
llvm_unreachable("BI__builtin_elementwise_sqrt NYI");
case Builtin::BItan:
case Builtin::BItanf:
case Builtin::BItanl:
case Builtin::BI__builtin_tan:
case Builtin::BI__builtin_tanf:
case Builtin::BI__builtin_tanf16:
case Builtin::BI__builtin_tanl:
case Builtin::BI__builtin_tanf128:
llvm_unreachable("Builtin::BItan like NYI");
case Builtin::BItanh:
case Builtin::BItanhf:
case Builtin::BItanhl:
case Builtin::BI__builtin_tanh:
case Builtin::BI__builtin_tanhf:
case Builtin::BI__builtin_tanhf16:
case Builtin::BI__builtin_tanhl:
case Builtin::BI__builtin_tanhf128:
llvm_unreachable("Builtin::BItanh like NYI");
case Builtin::BItrunc:
case Builtin::BItruncf:
case Builtin::BItruncl:
case Builtin::BI__builtin_trunc:
case Builtin::BI__builtin_truncf:
case Builtin::BI__builtin_truncf16:
case Builtin::BI__builtin_truncl:
case Builtin::BI__builtin_truncf128:
return emitUnaryFPBuiltin<cir::TruncOp>(*this, *E);
case Builtin::BIlround:
case Builtin::BIlroundf:
case Builtin::BIlroundl:
case Builtin::BI__builtin_lround:
case Builtin::BI__builtin_lroundf:
case Builtin::BI__builtin_lroundl:
return emitUnaryMaybeConstrainedFPToIntBuiltin<cir::LroundOp>(*this, *E);
case Builtin::BI__builtin_lroundf128:
llvm_unreachable("BI__builtin_lroundf128 NYI");
case Builtin::BIllround:
case Builtin::BIllroundf:
case Builtin::BIllroundl:
case Builtin::BI__builtin_llround:
case Builtin::BI__builtin_llroundf:
case Builtin::BI__builtin_llroundl:
return emitUnaryMaybeConstrainedFPToIntBuiltin<cir::LLroundOp>(*this, *E);
case Builtin::BI__builtin_llroundf128:
llvm_unreachable("BI__builtin_llroundf128 NYI");
case Builtin::BIlrint:
case Builtin::BIlrintf:
case Builtin::BIlrintl:
case Builtin::BI__builtin_lrint:
case Builtin::BI__builtin_lrintf:
case Builtin::BI__builtin_lrintl:
return emitUnaryMaybeConstrainedFPToIntBuiltin<cir::LrintOp>(*this, *E);
case Builtin::BI__builtin_lrintf128:
llvm_unreachable("BI__builtin_lrintf128 NYI");
case Builtin::BIllrint:
case Builtin::BIllrintf:
case Builtin::BIllrintl:
case Builtin::BI__builtin_llrint:
case Builtin::BI__builtin_llrintf:
case Builtin::BI__builtin_llrintl:
return emitUnaryMaybeConstrainedFPToIntBuiltin<cir::LLrintOp>(*this, *E);
case Builtin::BI__builtin_llrintf128:
llvm_unreachable("BI__builtin_llrintf128 NYI");
case Builtin::BI__builtin_ldexp:
case Builtin::BI__builtin_ldexpf:
case Builtin::BI__builtin_ldexpl:
case Builtin::BI__builtin_ldexpf16:
case Builtin::BI__builtin_ldexpf128:
llvm_unreachable("Builtin::BI__builtin_ldexp NYI");
default:
break;
}
}
switch (BuiltinIDIfNoAsmLabel) {
default:
break;
case Builtin::BI__builtin___CFStringMakeConstantString:
case Builtin::BI__builtin___NSStringMakeConstantString:
llvm_unreachable("BI__builtin___CFStringMakeConstantString like NYI");
// C stdarg builtins.
case Builtin::BI__builtin_stdarg_start:
case Builtin::BI__builtin_va_start:
case Builtin::BI__va_start:
case Builtin::BI__builtin_va_end: {
emitVAStartEnd(BuiltinID == Builtin::BI__va_start
? emitScalarExpr(E->getArg(0))
: emitVAListRef(E->getArg(0)).getPointer(),
BuiltinID != Builtin::BI__builtin_va_end);
return {};
}
case Builtin::BI__builtin_va_copy: {
auto dstPtr = emitVAListRef(E->getArg(0)).getPointer();
auto srcPtr = emitVAListRef(E->getArg(1)).getPointer();
builder.create<cir::VACopyOp>(dstPtr.getLoc(), dstPtr, srcPtr);
return {};
}
case Builtin::BIabs:
case Builtin::BIlabs:
case Builtin::BIllabs:
case Builtin::BI__builtin_abs:
case Builtin::BI__builtin_labs:
case Builtin::BI__builtin_llabs: {
bool SanitizeOverflow = SanOpts.has(SanitizerKind::SignedIntegerOverflow);
auto Arg = emitScalarExpr(E->getArg(0));
mlir::Value Result;
switch (getLangOpts().getSignedOverflowBehavior()) {
case LangOptions::SOB_Defined: {
auto Call = getBuilder().create<cir::AbsOp>(getLoc(E->getExprLoc()),
Arg.getType(), Arg, false);
Result = Call->getResult(0);
break;
}
case LangOptions::SOB_Undefined: {
if (!SanitizeOverflow) {
auto Call = getBuilder().create<cir::AbsOp>(getLoc(E->getExprLoc()),
Arg.getType(), Arg, true);
Result = Call->getResult(0);
break;
}
llvm_unreachable("BI__builtin_abs with LangOptions::SOB_Undefined when "
"SanitizeOverflow is true");
}
[[fallthrough]];
case LangOptions::SOB_Trapping:
llvm_unreachable("BI__builtin_abs with LangOptions::SOB_Trapping");
}
return RValue::get(Result);
}
case Builtin::BI__builtin_complex: {
mlir::Value Real = emitScalarExpr(E->getArg(0));
mlir::Value Imag = emitScalarExpr(E->getArg(1));
mlir::Value Complex =
builder.createComplexCreate(getLoc(E->getExprLoc()), Real, Imag);
return RValue::getComplex(Complex);
}
case Builtin::BI__builtin_conj:
case Builtin::BI__builtin_conjf:
case Builtin::BI__builtin_conjl:
case Builtin::BIconj:
case Builtin::BIconjf:
case Builtin::BIconjl: {
mlir::Value ComplexVal = emitComplexExpr(E->getArg(0));
mlir::Value Conj = builder.createUnaryOp(getLoc(E->getExprLoc()),
cir::UnaryOpKind::Not, ComplexVal);
return RValue::getComplex(Conj);
}
case Builtin::BI__builtin_creal:
case Builtin::BI__builtin_crealf:
case Builtin::BI__builtin_creall:
case Builtin::BIcreal:
case Builtin::BIcrealf:
case Builtin::BIcreall: {
mlir::Value ComplexVal = emitComplexExpr(E->getArg(0));
mlir::Value Real =
builder.createComplexReal(getLoc(E->getExprLoc()), ComplexVal);
return RValue::get(Real);
}
case Builtin::BI__builtin_preserve_access_index:
llvm_unreachable("Builtin::BI__builtin_preserve_access_index NYI");
case Builtin::BI__builtin_cimag:
case Builtin::BI__builtin_cimagf: