LLVM: lib/Analysis/ConstantFolding.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
31#include "llvm/Config/config.h"
45#include "llvm/IR/IntrinsicsAArch64.h"
46#include "llvm/IR/IntrinsicsAMDGPU.h"
47#include "llvm/IR/IntrinsicsARM.h"
48#include "llvm/IR/IntrinsicsNVPTX.h"
49#include "llvm/IR/IntrinsicsWebAssembly.h"
50#include "llvm/IR/IntrinsicsX86.h"
59#include
60#include
61#include
62#include
63#include
64
65using namespace llvm;
66
67namespace {
68
69
70
71
72
73static Constant *foldConstVectorToAPInt(APInt &Result, Type *DestTy,
75 unsigned NumSrcElts,
77
78
79 unsigned BitShift = DL.getTypeSizeInBits(SrcEltTy);
80 for (unsigned i = 0; i != NumSrcElts; ++i) {
82 if (DL.isLittleEndian())
83 Element = C->getAggregateElement(NumSrcElts - i - 1);
84 else
85 Element = C->getAggregateElement(i);
86
87 if (isa_and_nonnull(Element)) {
89 continue;
90 }
91
92 auto *ElementCI = dyn_cast_or_null(Element);
93 if (!ElementCI)
95
97 Result |= ElementCI->getValue().zext(Result.getBitWidth());
98 }
99
100 return nullptr;
101}
102
103
104
105
108 "Invalid constantexpr bitcast!");
109
110
112 return Res;
113
114 if (auto *VTy = dyn_cast(C->getType())) {
115
117 unsigned NumSrcElts = cast(VTy)->getNumElements();
118 Type *SrcEltTy = VTy->getElementType();
119
120
121
126
128 }
129
131 if (Constant *CE = foldConstVectorToAPInt(Result, DestTy, C,
132 SrcEltTy, NumSrcElts, DL))
133 return CE;
134
135 if (isa(DestTy))
136 return ConstantInt::get(DestTy, Result);
137
139 return ConstantFP::get(DestTy->getContext(), FP);
140 }
141 }
142
143
144 auto *DestVTy = dyn_cast(DestTy);
145 if (!DestVTy)
147
148
149
150 if (!isa(C->getType()) &&
154 }
155
156
157
158 if (!isa(C->getType()))
160
161
165
166
167 unsigned NumDstElt = cast(DestVTy)->getNumElements();
168 unsigned NumSrcElt = cast(C->getType())->getNumElements();
169 if (NumDstElt == NumSrcElt)
171
172 Type *SrcEltTy = cast(C->getType())->getElementType();
173 Type *DstEltTy = DestVTy->getElementType();
174
175
176
177
178
179
180
181
182
183
184
186
190
192
193
195 }
196
197
198
203
207 "Constant folding cannot fail for plain fp->int bitcast!");
208 }
209
210
211
212
213
214 bool isLittleEndian = DL.isLittleEndian();
215
217 if (NumDstElt < NumSrcElt) {
218
220 unsigned Ratio = NumSrcElt/NumDstElt;
222 unsigned SrcElt = 0;
223 for (unsigned i = 0; i != NumDstElt; ++i) {
224
226 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
227 for (unsigned j = 0; j != Ratio; ++j) {
228 Constant *Src = C->getAggregateElement(SrcElt++);
229 if (isa_and_nonnull(Src))
231 cast(C->getType())->getElementType());
232 else
233 Src = dyn_cast_or_null(Src);
234 if (!Src)
236
237
240 assert(Src && "Constant folding cannot fail on plain integers");
241
242
244 Instruction::Shl, Src, ConstantInt::get(Src->getType(), ShiftAmt),
246 assert(Src && "Constant folding cannot fail on plain integers");
247
248 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
249
250
252 assert(Elt && "Constant folding cannot fail on plain integers");
253 }
254 Result.push_back(Elt);
255 }
257 }
258
259
260 unsigned Ratio = NumDstElt/NumSrcElt;
261 unsigned DstBitSize = DL.getTypeSizeInBits(DstEltTy);
262
263
264 for (unsigned i = 0; i != NumSrcElt; ++i) {
265 auto *Element = C->getAggregateElement(i);
266
267 if (!Element)
269
270 if (isa(Element)) {
271
273 continue;
274 }
275
276 auto *Src = dyn_cast(Element);
277 if (!Src)
279
280 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
281 for (unsigned j = 0; j != Ratio; ++j) {
282
283
284 APInt Elt = Src->getValue().lshr(ShiftAmt);
285 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
286
287
288 Result.push_back(ConstantInt::get(DstEltTy, Elt.trunc(DstBitSize)));
289 }
290 }
291
293}
294
295}
296
297
298
302 if (DSOEquiv)
303 *DSOEquiv = nullptr;
304
305
306 if ((GV = dyn_cast(C))) {
309 return true;
310 }
311
312 if (auto *FoundDSOEquiv = dyn_cast(C)) {
313 if (DSOEquiv)
314 *DSOEquiv = FoundDSOEquiv;
315 GV = FoundDSOEquiv->getGlobalValue();
318 return true;
319 }
320
321
322 auto *CE = dyn_cast(C);
323 if (!CE) return false;
324
325
326 if (CE->getOpcode() == Instruction::PtrToInt ||
327 CE->getOpcode() == Instruction::BitCast)
329 DSOEquiv);
330
331
332 auto *GEP = dyn_cast(CE);
333 if ()
334 return false;
335
336 unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
338
339
341 DSOEquiv))
342 return false;
343
344
345 if (->accumulateConstantOffset(DL, TmpOffset))
346 return false;
347
349 return true;
350}
351
354 do {
355 Type *SrcTy = C->getType();
356 if (SrcTy == DestTy)
357 return C;
358
359 TypeSize DestSize = DL.getTypeSizeInBits(DestTy);
360 TypeSize SrcSize = DL.getTypeSizeInBits(SrcTy);
361 if (!TypeSize::isKnownGE(SrcSize, DestSize))
362 return nullptr;
363
364
365
367 return Res;
368
369
370
371
372 if (SrcSize == DestSize &&
376
377
379 Cast = Instruction::IntToPtr;
381 Cast = Instruction::PtrToInt;
382
385 }
386
387
388
390 return nullptr;
391
392
393
394
395
397
398
399 unsigned Elem = 0;
401 do {
402 ElemC = C->getAggregateElement(Elem++);
403 } while (ElemC && DL.getTypeSizeInBits(ElemC->getType()).isZero());
404 C = ElemC;
405 } else {
406
407
408 if (auto *VT = dyn_cast(SrcTy))
409 if (.typeSizeEqualsStoreSize(VT->getElementType()))
410 return nullptr;
411
412 C = C->getAggregateElement(0u);
413 }
414 } while (C);
415
416 return nullptr;
417}
418
419namespace {
420
421
422
423
424
425bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, unsigned char *CurPtr,
427 assert(ByteOffset <= DL.getTypeAllocSize(C->getType()) &&
428 "Out of range access");
429
430
431
433 return true;
434
435 if (auto *CI = dyn_cast(C)) {
436 if ((CI->getBitWidth() & 7) != 0)
437 return false;
438 const APInt &Val = CI->getValue();
439 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
440
441 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
442 unsigned n = ByteOffset;
443 if (.isLittleEndian())
444 n = IntBytes - n - 1;
446 ++ByteOffset;
447 }
448 return true;
449 }
450
451 if (auto *CFP = dyn_cast(C)) {
452 if (CFP->getType()->isDoubleTy()) {
454 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
455 }
456 if (CFP->getType()->isFloatTy()){
458 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
459 }
460 if (CFP->getType()->isHalfTy()){
462 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
463 }
464 return false;
465 }
466
467 if (auto *CS = dyn_cast(C)) {
468 const StructLayout *SL = DL.getStructLayout(CS->getType());
471 ByteOffset -= CurEltOffset;
472
473 while (true) {
474
475
476 uint64_t EltSize = DL.getTypeAllocSize(CS->getOperand(Index)->getType());
477
478 if (ByteOffset < EltSize &&
479 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
480 BytesLeft, DL))
481 return false;
482
484
485
486 if (Index == CS->getType()->getNumElements())
487 return true;
488
489
491
492 if (BytesLeft <= NextEltOffset - CurEltOffset - ByteOffset)
493 return true;
494
495
496 CurPtr += NextEltOffset - CurEltOffset - ByteOffset;
497 BytesLeft -= NextEltOffset - CurEltOffset - ByteOffset;
498 ByteOffset = 0;
499 CurEltOffset = NextEltOffset;
500 }
501
502 }
503
505 isa(C)) {
508 if (auto *AT = dyn_cast(C->getType())) {
509 NumElts = AT->getNumElements();
510 EltTy = AT->getElementType();
511 EltSize = DL.getTypeAllocSize(EltTy);
512 } else {
513 NumElts = cast(C->getType())->getNumElements();
514 EltTy = cast(C->getType())->getElementType();
515
516
517 if (.typeSizeEqualsStoreSize(EltTy))
518 return false;
519
520 EltSize = DL.getTypeStoreSize(EltTy);
521 }
524
526 if (!ReadDataFromGlobal(C->getAggregateElement(Index), Offset, CurPtr,
527 BytesLeft, DL))
528 return false;
529
531 assert(BytesWritten <= EltSize && "Not indexing into this element?");
532 if (BytesWritten >= BytesLeft)
533 return true;
534
536 BytesLeft -= BytesWritten;
537 CurPtr += BytesWritten;
538 }
539 return true;
540 }
541
542 if (auto *CE = dyn_cast(C)) {
543 if (CE->getOpcode() == Instruction::IntToPtr &&
544 CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) {
545 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
546 BytesLeft, DL);
547 }
548 }
549
550
551 return false;
552}
553
556
557 if (isa(LoadTy))
558 return nullptr;
559
560 auto *IntType = dyn_cast(LoadTy);
561
562
563 if (!IntType) {
564
565
566
567
570 return nullptr;
571
573 DL.getTypeSizeInBits(LoadTy).getFixedValue());
574 if (Constant *Res = FoldReinterpretLoadFromConst(C, MapTy, Offset, DL)) {
575 if (Res->isNullValue() && !LoadTy->isX86_AMXTy())
576
581
582 if (Res->isNullValue() && !LoadTy->isX86_AMXTy())
584 if (DL.isNonIntegralPointerType(LoadTy->getScalarType()))
585
586 return nullptr;
588 }
589 return Res;
590 }
591 return nullptr;
592 }
593
594 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
595 if (BytesLoaded > 32 || BytesLoaded == 0)
596 return nullptr;
597
598
599 if (Offset <= -1 * static_cast<int64_t>(BytesLoaded))
601
602
603 TypeSize InitializerSize = DL.getTypeAllocSize(C->getType());
605 return nullptr;
606
607
610
611 unsigned char RawBytes[32] = {0};
612 unsigned char *CurPtr = RawBytes;
613 unsigned BytesLeft = BytesLoaded;
614
615
620 }
621
622 if (!ReadDataFromGlobal(C, Offset, CurPtr, BytesLeft, DL))
623 return nullptr;
624
625 APInt ResultVal = APInt(IntType->getBitWidth(), 0);
626 if (DL.isLittleEndian()) {
627 ResultVal = RawBytes[BytesLoaded - 1];
628 for (unsigned i = 1; i != BytesLoaded; ++i) {
629 ResultVal <<= 8;
630 ResultVal |= RawBytes[BytesLoaded - 1 - i];
631 }
632 } else {
633 ResultVal = RawBytes[0];
634 for (unsigned i = 1; i != BytesLoaded; ++i) {
635 ResultVal <<= 8;
636 ResultVal |= RawBytes[i];
637 }
638 }
639
640 return ConstantInt::get(IntType->getContext(), ResultVal);
641}
642
643}
644
645
646
647
651 return nullptr;
652
655 TypeSize InitSize = DL.getTypeAllocSize(Init->getType());
656 if (InitSize < Offset)
657 return nullptr;
658
660 if (NBytes > UINT16_MAX)
661
662
663
664
665 return nullptr;
666
668 unsigned char *CurPtr = RawBytes.data();
669
670 if (!ReadDataFromGlobal(Init, Offset, CurPtr, NBytes, DL))
671 return nullptr;
672
674}
675
676
677
682
683 if (!isa(Base) && !isa(Base))
684 return nullptr;
685
686 Type *ElemTy = Base->getType();
688 if (.isZero() || !Indices[0].isZero())
689 return nullptr;
690
693 if (Index.isNegative() || Index.getActiveBits() >= 32)
694 return nullptr;
695
696 C = C->getAggregateElement(Index.getZExtValue());
697 if ()
698 return nullptr;
699 }
700
701 return C;
702}
703
709 return Result;
710
711
712
714 if (.isScalable() && Offset.sge(Size.getFixedValue()))
716
717
719 return Result;
720
721
722 if (Offset.getSignificantBits() <= 64)
724 FoldReinterpretLoadFromConst(C, Ty, Offset.getSExtValue(), DL))
725 return Result;
726
727 return nullptr;
728}
729
733}
734
738
739
741 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
742 return nullptr;
743
744 C = cast(C->stripAndAccumulateConstantOffsets(
746
747 if (C == GV)
750 return Result;
751
752
753
755}
756
759 APInt Offset(DL.getIndexTypeSizeInBits(C->getType()), 0);
761}
762
765 if (isa(C))
767 if (isa(C))
769
770
771 if (.typeSizeEqualsStoreSize(C->getType()))
772 return nullptr;
775 if (C->isAllOnesValue() &&
778 return nullptr;
779}
780
781namespace {
782
783
784
785
786
789
790
791
792
793
794
795 if (Opc == Instruction::And) {
798 if ((Known1.One | Known0.Zero).isAllOnes()) {
799
800 return Op0;
801 }
802 if ((Known0.One | Known1.Zero).isAllOnes()) {
803
804 return Op1;
805 }
806
807 Known0 &= Known1;
810 }
811
812
813
814 if (Opc == Instruction::Sub) {
816 APInt Offs1, Offs2;
817
820 unsigned OpSize = DL.getTypeSizeInBits(Op0->getType());
821
822
823
824
827 }
828 }
829
830 return nullptr;
831}
832
833
834
837 std::optional InRange,
839 Type *IntIdxTy = DL.getIndexType(ResultTy);
841
842 bool Any = false;
844 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
845 if ((i == 1 ||
847 SrcElemTy, Ops.slice(1, i - 1)))) &&
848 Ops[i]->getType()->getScalarType() != IntIdxScalarTy) {
849 Any = true;
850 Type *NewType =
851 Ops[i]->getType()->isVectorTy() ? IntIdxTy : IntIdxScalarTy;
855 if (!NewIdx)
856 return nullptr;
858 } else
860 }
861
862 if ()
863 return nullptr;
864
868}
869
870
875 Type *SrcElemTy = GEP->getSourceElementType();
876 Type *ResTy = GEP->getType();
877 if (!SrcElemTy->isSized() || isa(SrcElemTy))
878 return nullptr;
879
880 if (Constant *C = CastGEPIndices(SrcElemTy, Ops, ResTy, GEP->getNoWrapFlags(),
881 GEP->getInRange(), DL, TLI))
882 return C;
883
885 if (->getType()->isPointerTy())
886 return nullptr;
887
888 Type *IntIdxTy = DL.getIndexType(Ptr->getType());
889
890 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
891 if (!isa(Ops[i]) || !Ops[i]->getType()->isIntegerTy())
892 return nullptr;
893
894 unsigned BitWidth = DL.getTypeSizeInBits(IntIdxTy);
897 DL.getIndexedOffsetInType(
899 true, true);
900
901 std::optional InRange = GEP->getInRange();
904
905
907 bool Overflow = false;
908 while (auto *GEP = dyn_cast(Ptr)) {
909 NW &= GEP->getNoWrapFlags();
910
912
913
914 bool AllConstantInt = true;
915 for (Value *NestedOp : NestedOps)
916 if (!isa(NestedOp)) {
917 AllConstantInt = false;
918 break;
919 }
920 if (!AllConstantInt)
921 break;
922
923
927
929 }
930
931 Ptr = cast(GEP->getOperand(0));
932 SrcElemTy = GEP->getSourceElementType();
934 APInt(BitWidth, DL.getIndexedOffsetInType(SrcElemTy, NestedOps),
935 true, true),
936 Overflow);
937 }
938
939
940
943
944
945
947 if (auto *CE = dyn_cast(Ptr)) {
948 if (CE->getOpcode() == Instruction::IntToPtr) {
949 if (auto *Base = dyn_cast(CE->getOperand(0)))
951 }
952 }
953
954 auto *PTy = cast(Ptr->getType());
955 if ((Ptr->isNullValue() || BasePtr != 0) &&
956 .isNonIntegralPointerType(PTy)) {
959 }
960
961
963 bool CanBeNull, CanBeFreed;
965 Ptr->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
966 if (DerefBytes != 0 && !CanBeNull && Offset.sle(DerefBytes))
968 }
969
970
973
974
977 ConstantInt::get(Ctx, Offset), NW,
979}
980
981
982
983
984
985
986Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
990 bool AllowNonDeterministic) {
992
995
997 switch (Opcode) {
998 default:
999 break;
1000 case Instruction::FAdd:
1001 case Instruction::FSub:
1002 case Instruction::FMul:
1003 case Instruction::FDiv:
1004 case Instruction::FRem:
1005
1006
1007
1008 if (const auto *I = dyn_cast(InstOrCE)) {
1010 AllowNonDeterministic);
1011 }
1012 }
1014 }
1015
1018
1019 if (auto *GEP = dyn_cast(InstOrCE)) {
1020 Type *SrcElemTy = GEP->getSourceElementType();
1022 return nullptr;
1023
1024 if (Constant *C = SymbolicallyEvaluateGEP(GEP, Ops, DL, TLI))
1025 return C;
1026
1028 GEP->getNoWrapFlags(),
1029 GEP->getInRange());
1030 }
1031
1032 if (auto *CE = dyn_cast(InstOrCE))
1033 return CE->getWithOperands(Ops);
1034
1035 switch (Opcode) {
1036 default: return nullptr;
1037 case Instruction::ICmp:
1038 case Instruction::FCmp: {
1039 auto *C = cast(InstOrCE);
1042 }
1043 case Instruction::Freeze:
1045 case Instruction::Call:
1046 if (auto *F = dyn_cast(Ops.back())) {
1047 const auto *Call = cast(InstOrCE);
1050 AllowNonDeterministic);
1051 }
1052 return nullptr;
1053 case Instruction::Select:
1055 case Instruction::ExtractElement:
1057 case Instruction::ExtractValue:
1059 Ops[0], cast(InstOrCE)->getIndices());
1060 case Instruction::InsertElement:
1062 case Instruction::InsertValue:
1064 Ops[0], Ops[1], cast(InstOrCE)->getIndices());
1065 case Instruction::ShuffleVector:
1067 Ops[0], Ops[1], cast(InstOrCE)->getShuffleMask());
1068 case Instruction::Load: {
1069 const auto *LI = dyn_cast(InstOrCE);
1070 if (LI->isVolatile())
1071 return nullptr;
1073 }
1074 }
1075}
1076
1077}
1078
1079
1080
1081
1082
1083namespace {
1084
1090 return const_cast<Constant *>(C);
1091
1093 for (const Use &OldU : C->operands()) {
1094 Constant *OldC = cast(&OldU);
1096
1097
1098 if (isa(OldC) || isa(OldC)) {
1099 auto It = FoldedOps.find(OldC);
1100 if (It == FoldedOps.end()) {
1101 NewC = ConstantFoldConstantImpl(OldC, DL, TLI, FoldedOps);
1102 FoldedOps.insert({OldC, NewC});
1103 } else {
1104 NewC = It->second;
1105 }
1106 }
1108 }
1109
1110 if (auto *CE = dyn_cast(C)) {
1111 if (Constant *Res = ConstantFoldInstOperandsImpl(
1112 CE, CE->getOpcode(), Ops, DL, TLI, true))
1113 return Res;
1114 return const_cast<Constant *>(C);
1115 }
1116
1119}
1120
1121}
1122
1125
1126 if (auto *PN = dyn_cast(I)) {
1127 Constant *CommonValue = nullptr;
1128
1131
1132
1133
1134
1135 if (isa(Incoming))
1136 continue;
1137
1138 auto *C = dyn_cast(Incoming);
1139 if ()
1140 return nullptr;
1141
1142 C = ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1143
1144
1145 if (CommonValue && C != CommonValue)
1146 return nullptr;
1147 CommonValue = C;
1148 }
1149
1150
1151 return CommonValue ? CommonValue : UndefValue::get(PN->getType());
1152 }
1153
1154
1155
1156 if ((I->operands(), [](Use &U) { return isa(U); }))
1157 return nullptr;
1158
1161 for (const Use &OpU : I->operands()) {
1162 auto *Op = cast(&OpU);
1163
1164 Op = ConstantFoldConstantImpl(Op, DL, TLI, FoldedOps);
1166 }
1167
1169}
1170
1174 return ConstantFoldConstantImpl(C, DL, TLI, FoldedOps);
1175}
1176
1181 bool AllowNonDeterministic) {
1182 return ConstantFoldInstOperandsImpl(I, I->getOpcode(), Ops, DL, TLI,
1183 AllowNonDeterministic);
1184}
1185
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200 if (auto *CE0 = dyn_cast(Ops0)) {
1202 if (CE0->getOpcode() == Instruction::IntToPtr) {
1203 Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1204
1205
1207 false, DL)) {
1210 }
1211 }
1212
1213
1214
1215 if (CE0->getOpcode() == Instruction::PtrToInt) {
1216 Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1217 if (CE0->getType() == IntPtrTy) {
1218 Constant *C = CE0->getOperand(0);
1221 }
1222 }
1223 }
1224
1225 if (auto *CE1 = dyn_cast(Ops1)) {
1226 if (CE0->getOpcode() == CE1->getOpcode()) {
1227 if (CE0->getOpcode() == Instruction::IntToPtr) {
1228 Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
1229
1230
1231
1233 false, DL);
1235 false, DL);
1236 if (C0 && C1)
1238 }
1239
1240
1241
1242 if (CE0->getOpcode() == Instruction::PtrToInt) {
1243 Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
1244 if (CE0->getType() == IntPtrTy &&
1245 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
1247 Predicate, CE0->getOperand(0), CE1->getOperand(0), DL, TLI);
1248 }
1249 }
1250 }
1251 }
1252
1253
1254
1255
1256
1257
1258 if (Ops0->getType()->isPointerTy() && !ICmpInst::isSigned(Predicate)) {
1259 unsigned IndexWidth = DL.getIndexTypeSizeInBits(Ops0->getType());
1260 APInt Offset0(IndexWidth, 0);
1261 Value *Stripped0 =
1263 APInt Offset1(IndexWidth, 0);
1264 Value *Stripped1 =
1266 if (Stripped0 == Stripped1)
1271 }
1272 } else if (isa(Ops1)) {
1273
1274
1275 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1277 }
1278
1280
1281
1283 if (!Ops0)
1284 return nullptr;
1286 if (!Ops1)
1287 return nullptr;
1288 }
1289
1291}
1292
1296
1298}
1299
1304 if (isa(LHS) || isa(RHS))
1305 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, LHS, RHS, DL))
1306 return C;
1307
1311}
1312
1315 switch (Mode) {
1317 return nullptr;
1319 return ConstantFP::get(Ty->getContext(), APF);
1321 return ConstantFP::get(
1325 return ConstantFP::get(Ty->getContext(),
1327 default:
1328 break;
1329 }
1330
1332}
1333
1334
1335
1340}
1341
1344 bool IsOutput) {
1347 return CFP;
1348
1351 IsOutput ? Mode.Output : Mode.Input);
1352}
1353
1355 bool IsOutput) {
1356 if (ConstantFP *CFP = dyn_cast(Operand))
1358
1359 if (isa<ConstantAggregateZero, UndefValue, ConstantExpr>(Operand))
1360 return Operand;
1361
1363 VectorType *VecTy = dyn_cast(Ty);
1364 if (VecTy) {
1365 if (auto *Splat = dyn_cast_or_null(Operand->getSplatValue())) {
1367 if (!Folded)
1368 return nullptr;
1370 }
1371
1373 }
1374
1375 if (const auto *CV = dyn_cast(Operand)) {
1377 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1379 if (isa(Element)) {
1381 continue;
1382 }
1383
1384 ConstantFP *CFP = dyn_cast(Element);
1385 if (!CFP)
1386 return nullptr;
1387
1389 if (!Folded)
1390 return nullptr;
1392 }
1393
1395 }
1396
1397 if (const auto *CDV = dyn_cast(Operand)) {
1399 for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I) {
1400 const APFloat &Elt = CDV->getElementAsAPFloat(I);
1402 NewElts.push_back(ConstantFP::get(Ty, Elt));
1403 } else {
1407 if (!Folded)
1408 return nullptr;
1410 }
1411 }
1412
1414 }
1415
1416 return nullptr;
1417}
1418
1422 bool AllowNonDeterministic) {
1424
1426 if (!Op0)
1427 return nullptr;
1429 if (!Op1)
1430 return nullptr;
1431
1432
1433
1434
1435 if (!AllowNonDeterministic)
1436 if (auto *FP = dyn_cast_or_null(I))
1437 if (FP->hasNoSignedZeros() || FP->hasAllowReassoc() ||
1438 FP->hasAllowContract() || FP->hasAllowReciprocal())
1439 return nullptr;
1440
1441
1443 if ()
1444 return nullptr;
1445
1446
1448 if ()
1449 return nullptr;
1450
1451
1452 if (!AllowNonDeterministic && C->isNaN())
1453 return nullptr;
1454
1455 return C;
1456 }
1457
1458
1460}
1461
1465 switch (Opcode) {
1466 default:
1468 case Instruction::PtrToInt:
1469 if (auto *CE = dyn_cast(C)) {
1470 Constant *FoldedValue = nullptr;
1471
1472
1473 if (CE->getOpcode() == Instruction::IntToPtr) {
1474
1476 DL.getIntPtrType(CE->getType()),
1477 false, DL);
1478 } else if (auto *GEP = dyn_cast(CE)) {
1479
1480
1481
1482 unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
1484 auto *Base = cast(GEP->stripAndAccumulateConstantOffsets(
1485 DL, BaseOffset, true));
1486 if (Base->isNullValue()) {
1487 FoldedValue = ConstantInt::get(CE->getContext(), BaseOffset);
1488 } else {
1489
1490 if (GEP->getNumIndices() == 1 &&
1491 GEP->getSourceElementType()->isIntegerTy(8)) {
1492 auto *Ptr = cast(GEP->getPointerOperand());
1493 auto *Sub = dyn_cast(GEP->getOperand(1));
1494 Type *IntIdxTy = DL.getIndexType(Ptr->getType());
1495 if (Sub && Sub->getType() == IntIdxTy &&
1496 Sub->getOpcode() == Instruction::Sub &&
1497 Sub->getOperand(0)->isNullValue())
1500 }
1501 }
1502 }
1503 if (FoldedValue) {
1504
1506 DL);
1507 }
1508 }
1509 break;
1510 case Instruction::IntToPtr:
1511
1512
1513
1514
1515 if (auto *CE = dyn_cast(C)) {
1516 if (CE->getOpcode() == Instruction::PtrToInt) {
1517 Constant *SrcPtr = CE->getOperand(0);
1518 unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
1519 unsigned MidIntSize = CE->getType()->getScalarSizeInBits();
1520
1521 if (MidIntSize >= SrcPtrSize) {
1524 return FoldBitCast(CE->getOperand(0), DestTy, DL);
1525 }
1526 }
1527 }
1528 break;
1529 case Instruction::Trunc:
1530 case Instruction::ZExt:
1531 case Instruction::SExt:
1532 case Instruction::FPTrunc:
1533 case Instruction::FPExt:
1534 case Instruction::UIToFP:
1535 case Instruction::SIToFP:
1536 case Instruction::FPToUI:
1537 case Instruction::FPToSI:
1538 case Instruction::AddrSpaceCast:
1539 break;
1540 case Instruction::BitCast:
1542 }
1543
1547}
1548
1551 Type *SrcTy = C->getType();
1552 if (SrcTy == DestTy)
1553 return C;
1556 if (IsSigned)
1559}
1560
1561
1562
1563
1564
1566 if (Call->isNoBuiltin())
1567 return false;
1568 if (Call->getFunctionType() != F->getFunctionType())
1569 return false;
1570 switch (F->getIntrinsicID()) {
1571
1572
1573 case Intrinsic::bswap:
1574 case Intrinsic::ctpop:
1575 case Intrinsic::ctlz:
1576 case Intrinsic::cttz:
1577 case Intrinsic::fshl:
1578 case Intrinsic::fshr:
1579 case Intrinsic::launder_invariant_group:
1580 case Intrinsic::strip_invariant_group:
1581 case Intrinsic::masked_load:
1582 case Intrinsic::get_active_lane_mask:
1583 case Intrinsic::abs:
1584 case Intrinsic::smax:
1585 case Intrinsic::smin:
1586 case Intrinsic::umax:
1587 case Intrinsic::umin:
1588 case Intrinsic::scmp:
1589 case Intrinsic::ucmp:
1590 case Intrinsic::sadd_with_overflow:
1591 case Intrinsic::uadd_with_overflow:
1592 case Intrinsic::ssub_with_overflow:
1593 case Intrinsic::usub_with_overflow:
1594 case Intrinsic::smul_with_overflow:
1595 case Intrinsic::umul_with_overflow:
1596 case Intrinsic::sadd_sat:
1597 case Intrinsic::uadd_sat:
1598 case Intrinsic::ssub_sat:
1599 case Intrinsic::usub_sat:
1600 case Intrinsic::smul_fix:
1601 case Intrinsic::smul_fix_sat:
1602 case Intrinsic::bitreverse:
1603 case Intrinsic::is_constant:
1604 case Intrinsic::vector_reduce_add:
1605 case Intrinsic::vector_reduce_mul:
1606 case Intrinsic::vector_reduce_and:
1607 case Intrinsic::vector_reduce_or:
1608 case Intrinsic::vector_reduce_xor:
1609 case Intrinsic::vector_reduce_smin:
1610 case Intrinsic::vector_reduce_smax:
1611 case Intrinsic::vector_reduce_umin:
1612 case Intrinsic::vector_reduce_umax:
1613
1614 case Intrinsic::amdgcn_perm:
1615 case Intrinsic::amdgcn_wave_reduce_umin:
1616 case Intrinsic::amdgcn_wave_reduce_umax:
1617 case Intrinsic::amdgcn_s_wqm:
1618 case Intrinsic::amdgcn_s_quadmask:
1619 case Intrinsic::amdgcn_s_bitreplicate:
1620 case Intrinsic::arm_mve_vctp8:
1621 case Intrinsic::arm_mve_vctp16:
1622 case Intrinsic::arm_mve_vctp32:
1623 case Intrinsic::arm_mve_vctp64:
1624 case Intrinsic::aarch64_sve_convert_from_svbool:
1625
1626 case Intrinsic::wasm_trunc_signed:
1627 case Intrinsic::wasm_trunc_unsigned:
1628 return true;
1629
1630
1631
1632 case Intrinsic::minnum:
1633 case Intrinsic::maxnum:
1634 case Intrinsic::minimum:
1635 case Intrinsic::maximum:
1636 case Intrinsic:🪵
1637 case Intrinsic::log2:
1638 case Intrinsic::log10:
1639 case Intrinsic::exp:
1640 case Intrinsic::exp2:
1641 case Intrinsic::exp10:
1642 case Intrinsic::sqrt:
1643 case Intrinsic::sin:
1644 case Intrinsic::cos:
1645 case Intrinsic::sincos:
1646 case Intrinsic::pow:
1647 case Intrinsic::powi:
1648 case Intrinsic::ldexp:
1649 case Intrinsic::fma:
1650 case Intrinsic::fmuladd:
1651 case Intrinsic::frexp:
1652 case Intrinsic::fptoui_sat:
1653 case Intrinsic::fptosi_sat:
1654 case Intrinsic::convert_from_fp16:
1655 case Intrinsic::convert_to_fp16:
1656 case Intrinsic::amdgcn_cos:
1657 case Intrinsic::amdgcn_cubeid:
1658 case Intrinsic::amdgcn_cubema:
1659 case Intrinsic::amdgcn_cubesc:
1660 case Intrinsic::amdgcn_cubetc:
1661 case Intrinsic::amdgcn_fmul_legacy:
1662 case Intrinsic::amdgcn_fma_legacy:
1663 case Intrinsic::amdgcn_fract:
1664 case Intrinsic::amdgcn_sin:
1665
1666 case Intrinsic::x86_sse_cvtss2si:
1667 case Intrinsic::x86_sse_cvtss2si64:
1668 case Intrinsic::x86_sse_cvttss2si:
1669 case Intrinsic::x86_sse_cvttss2si64:
1670 case Intrinsic::x86_sse2_cvtsd2si:
1671 case Intrinsic::x86_sse2_cvtsd2si64:
1672 case Intrinsic::x86_sse2_cvttsd2si:
1673 case Intrinsic::x86_sse2_cvttsd2si64:
1674 case Intrinsic::x86_avx512_vcvtss2si32:
1675 case Intrinsic::x86_avx512_vcvtss2si64:
1676 case Intrinsic::x86_avx512_cvttss2si:
1677 case Intrinsic::x86_avx512_cvttss2si64:
1678 case Intrinsic::x86_avx512_vcvtsd2si32:
1679 case Intrinsic::x86_avx512_vcvtsd2si64:
1680 case Intrinsic::x86_avx512_cvttsd2si:
1681 case Intrinsic::x86_avx512_cvttsd2si64:
1682 case Intrinsic::x86_avx512_vcvtss2usi32:
1683 case Intrinsic::x86_avx512_vcvtss2usi64:
1684 case Intrinsic::x86_avx512_cvttss2usi:
1685 case Intrinsic::x86_avx512_cvttss2usi64:
1686 case Intrinsic::x86_avx512_vcvtsd2usi32:
1687 case Intrinsic::x86_avx512_vcvtsd2usi64:
1688 case Intrinsic::x86_avx512_cvttsd2usi:
1689 case Intrinsic::x86_avx512_cvttsd2usi64:
1690 return !Call->isStrictFP();
1691
1692
1693 case Intrinsic::nvvm_fmax_d:
1694 case Intrinsic::nvvm_fmax_f:
1695 case Intrinsic::nvvm_fmax_ftz_f:
1696 case Intrinsic::nvvm_fmax_ftz_nan_f:
1697 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
1698 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
1699 case Intrinsic::nvvm_fmax_nan_f:
1700 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
1701 case Intrinsic::nvvm_fmax_xorsign_abs_f:
1702
1703
1704 case Intrinsic::nvvm_fmin_d:
1705 case Intrinsic::nvvm_fmin_f:
1706 case Intrinsic::nvvm_fmin_ftz_f:
1707 case Intrinsic::nvvm_fmin_ftz_nan_f:
1708 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
1709 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
1710 case Intrinsic::nvvm_fmin_nan_f:
1711 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
1712 case Intrinsic::nvvm_fmin_xorsign_abs_f:
1713
1714
1715 case Intrinsic::nvvm_f2i_rm:
1716 case Intrinsic::nvvm_f2i_rn:
1717 case Intrinsic::nvvm_f2i_rp:
1718 case Intrinsic::nvvm_f2i_rz:
1719 case Intrinsic::nvvm_f2i_rm_ftz:
1720 case Intrinsic::nvvm_f2i_rn_ftz:
1721 case Intrinsic::nvvm_f2i_rp_ftz:
1722 case Intrinsic::nvvm_f2i_rz_ftz:
1723 case Intrinsic::nvvm_f2ui_rm:
1724 case Intrinsic::nvvm_f2ui_rn:
1725 case Intrinsic::nvvm_f2ui_rp:
1726 case Intrinsic::nvvm_f2ui_rz:
1727 case Intrinsic::nvvm_f2ui_rm_ftz:
1728 case Intrinsic::nvvm_f2ui_rn_ftz:
1729 case Intrinsic::nvvm_f2ui_rp_ftz:
1730 case Intrinsic::nvvm_f2ui_rz_ftz:
1731 case Intrinsic::nvvm_d2i_rm:
1732 case Intrinsic::nvvm_d2i_rn:
1733 case Intrinsic::nvvm_d2i_rp:
1734 case Intrinsic::nvvm_d2i_rz:
1735 case Intrinsic::nvvm_d2ui_rm:
1736 case Intrinsic::nvvm_d2ui_rn:
1737 case Intrinsic::nvvm_d2ui_rp:
1738 case Intrinsic::nvvm_d2ui_rz:
1739
1740
1741 case Intrinsic::nvvm_f2ll_rm:
1742 case Intrinsic::nvvm_f2ll_rn:
1743 case Intrinsic::nvvm_f2ll_rp:
1744 case Intrinsic::nvvm_f2ll_rz:
1745 case Intrinsic::nvvm_f2ll_rm_ftz:
1746 case Intrinsic::nvvm_f2ll_rn_ftz:
1747 case Intrinsic::nvvm_f2ll_rp_ftz:
1748 case Intrinsic::nvvm_f2ll_rz_ftz:
1749 case Intrinsic::nvvm_f2ull_rm:
1750 case Intrinsic::nvvm_f2ull_rn:
1751 case Intrinsic::nvvm_f2ull_rp:
1752 case Intrinsic::nvvm_f2ull_rz:
1753 case Intrinsic::nvvm_f2ull_rm_ftz:
1754 case Intrinsic::nvvm_f2ull_rn_ftz:
1755 case Intrinsic::nvvm_f2ull_rp_ftz:
1756 case Intrinsic::nvvm_f2ull_rz_ftz:
1757 case Intrinsic::nvvm_d2ll_rm:
1758 case Intrinsic::nvvm_d2ll_rn:
1759 case Intrinsic::nvvm_d2ll_rp:
1760 case Intrinsic::nvvm_d2ll_rz:
1761 case Intrinsic::nvvm_d2ull_rm:
1762 case Intrinsic::nvvm_d2ull_rn:
1763 case Intrinsic::nvvm_d2ull_rp:
1764 case Intrinsic::nvvm_d2ull_rz:
1765
1766
1767
1768 case Intrinsic::fabs:
1769 case Intrinsic::copysign:
1770 case Intrinsic::is_fpclass:
1771
1772
1773 case Intrinsic::ceil:
1774 case Intrinsic:🤣
1775 case Intrinsic::round:
1776 case Intrinsic::roundeven:
1777 case Intrinsic::trunc:
1778 case Intrinsic::nearbyint:
1779 case Intrinsic::rint:
1780 case Intrinsic::canonicalize:
1781
1782
1783 case Intrinsic::experimental_constrained_fma:
1784 case Intrinsic::experimental_constrained_fmuladd:
1785 case Intrinsic::experimental_constrained_fadd:
1786 case Intrinsic::experimental_constrained_fsub:
1787 case Intrinsic::experimental_constrained_fmul:
1788 case Intrinsic::experimental_constrained_fdiv:
1789 case Intrinsic::experimental_constrained_frem:
1790 case Intrinsic::experimental_constrained_ceil:
1791 case Intrinsic::experimental_constrained_floor:
1792 case Intrinsic::experimental_constrained_round:
1793 case Intrinsic::experimental_constrained_roundeven:
1794 case Intrinsic::experimental_constrained_trunc:
1795 case Intrinsic::experimental_constrained_nearbyint:
1796 case Intrinsic::experimental_constrained_rint:
1797 case Intrinsic::experimental_constrained_fcmp:
1798 case Intrinsic::experimental_constrained_fcmps:
1799 return true;
1800 default:
1801 return false;
1803 }
1804
1805 if (->hasName() || Call->isStrictFP())
1806 return false;
1807
1808
1809
1810
1812 switch (Name[0]) {
1813 default:
1814 return false;
1815 case 'a':
1816 return Name == "acos" || Name == "acosf" ||
1817 Name == "asin" || Name == "asinf" ||
1818 Name == "atan" || Name == "atanf" ||
1819 Name == "atan2" || Name == "atan2f";
1820 case 'c':
1821 return Name == "ceil" || Name == "ceilf" ||
1822 Name == "cos" || Name == "cosf" ||
1823 Name == "cosh" || Name == "coshf";
1824 case 'e':
1825 return Name == "exp" || Name == "expf" || Name == "exp2" ||
1826 Name == "exp2f" || Name == "erf" || Name == "erff";
1827 case 'f':
1828 return Name == "fabs" || Name == "fabsf" ||
1829 Name == "floor" || Name == "floorf" ||
1830 Name == "fmod" || Name == "fmodf";
1831 case 'i':
1832 return Name == "ilogb" || Name == "ilogbf";
1833 case 'l':
1834 return Name == "log" || Name == "logf" || Name == "logl" ||
1835 Name == "log2" || Name == "log2f" || Name == "log10" ||
1836 Name == "log10f" || Name == "logb" || Name == "logbf" ||
1837 Name == "log1p" || Name == "log1pf";
1838 case 'n':
1839 return Name == "nearbyint" || Name == "nearbyintf";
1840 case 'p':
1841 return Name == "pow" || Name == "powf";
1842 case 'r':
1843 return Name == "remainder" || Name == "remainderf" ||
1844 Name == "rint" || Name == "rintf" ||
1845 Name == "round" || Name == "roundf";
1846 case 's':
1847 return Name == "sin" || Name == "sinf" ||
1848 Name == "sinh" || Name == "sinhf" ||
1849 Name == "sqrt" || Name == "sqrtf";
1850 case 't':
1851 return Name == "tan" || Name == "tanf" ||
1852 Name == "tanh" || Name == "tanhf" ||
1853 Name == "trunc" || Name == "truncf";
1854 case '_':
1855
1856
1857
1858
1859
1860
1861 if (Name.size() < 12 || Name[1] != '_')
1862 return false;
1863 switch (Name[2]) {
1864 default:
1865 return false;
1866 case 'a':
1867 return Name == "__acos_finite" || Name == "__acosf_finite" ||
1868 Name == "__asin_finite" || Name == "__asinf_finite" ||
1869 Name == "__atan2_finite" || Name == "__atan2f_finite";
1870 case 'c':
1871 return Name == "__cosh_finite" || Name == "__coshf_finite";
1872 case 'e':
1873 return Name == "__exp_finite" || Name == "__expf_finite" ||
1874 Name == "__exp2_finite" || Name == "__exp2f_finite";
1875 case 'l':
1876 return Name == "__log_finite" || Name == "__logf_finite" ||
1877 Name == "__log10_finite" || Name == "__log10f_finite";
1878 case 'p':
1879 return Name == "__pow_finite" || Name == "__powf_finite";
1880 case 's':
1881 return Name == "__sinh_finite" || Name == "__sinhf_finite";
1882 }
1883 }
1884}
1885
1886namespace {
1887
1888Constant *GetConstantFoldFPValue(double V, Type *Ty) {
1891 bool unused;
1892 APF.convert(Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &unused);
1893 return ConstantFP::get(Ty->getContext(), APF);
1894 }
1897 llvm_unreachable("Can only constant fold half/float/double");
1898}
1899
1900#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
1901Constant *GetConstantFoldFPValue128(float128 V, Type *Ty) {
1903 return ConstantFP::get(Ty, V);
1905}
1906#endif
1907
1908
1909inline void llvm_fenv_clearexcept() {
1910#if HAVE_DECL_FE_ALL_EXCEPT
1911 feclearexcept(FE_ALL_EXCEPT);
1912#endif
1913 errno = 0;
1914}
1915
1916
1917inline bool llvm_fenv_testexcept() {
1918 int errno_val = errno;
1919 if (errno_val == ERANGE || errno_val == EDOM)
1920 return true;
1921#if HAVE_DECL_FE_ALL_EXCEPT && HAVE_DECL_FE_INEXACT
1922 if (fetestexcept(FE_ALL_EXCEPT & ~FE_INEXACT))
1923 return true;
1924#endif
1925 return false;
1926}
1927
1928static const APFloat FTZPreserveSign(const APFloat &V) {
1929 if (V.isDenormal())
1931 return V;
1932}
1933
1934Constant *ConstantFoldFP(double (*NativeFP)(double), const APFloat &V,
1936 llvm_fenv_clearexcept();
1937 double Result = NativeFP(V.convertToDouble());
1938 if (llvm_fenv_testexcept()) {
1939 llvm_fenv_clearexcept();
1940 return nullptr;
1941 }
1942
1943 return GetConstantFoldFPValue(Result, Ty);
1944}
1945
1946#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
1947Constant *ConstantFoldFP128(float128 (*NativeFP)(float128), const APFloat &V,
1949 llvm_fenv_clearexcept();
1950 float128 Result = NativeFP(V.convertToQuad());
1951 if (llvm_fenv_testexcept()) {
1952 llvm_fenv_clearexcept();
1953 return nullptr;
1954 }
1955
1956 return GetConstantFoldFPValue128(Result, Ty);
1957}
1958#endif
1959
1960Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
1962 llvm_fenv_clearexcept();
1963 double Result = NativeFP(V.convertToDouble(), W.convertToDouble());
1964 if (llvm_fenv_testexcept()) {
1965 llvm_fenv_clearexcept();
1966 return nullptr;
1967 }
1968
1969 return GetConstantFoldFPValue(Result, Ty);
1970}
1971
1974 if (!VT)
1975 return nullptr;
1976
1977
1978
1979 if (isa(Op))
1981
1982
1983 if (isa(Op) || Op->containsPoisonElement())
1985
1986
1987 if (!isa(Op) && !isa(Op))
1988 return nullptr;
1989
1990 auto *EltC = dyn_cast(Op->getAggregateElement(0U));
1991 if (!EltC)
1992 return nullptr;
1993
1994 APInt Acc = EltC->getValue();
1996 if (!(EltC = dyn_cast(Op->getAggregateElement(I))))
1997 return nullptr;
1998 const APInt &X = EltC->getValue();
1999 switch (IID) {
2000 case Intrinsic::vector_reduce_add:
2001 Acc = Acc + X;
2002 break;
2003 case Intrinsic::vector_reduce_mul:
2004 Acc = Acc * X;
2005 break;
2006 case Intrinsic::vector_reduce_and:
2007 Acc = Acc & X;
2008 break;
2009 case Intrinsic::vector_reduce_or:
2010 Acc = Acc | X;
2011 break;
2012 case Intrinsic::vector_reduce_xor:
2013 Acc = Acc ^ X;
2014 break;
2015 case Intrinsic::vector_reduce_smin:
2017 break;
2018 case Intrinsic::vector_reduce_smax:
2020 break;
2021 case Intrinsic::vector_reduce_umin:
2023 break;
2024 case Intrinsic::vector_reduce_umax:
2026 break;
2027 }
2028 }
2029
2030 return ConstantInt::get(Op->getContext(), Acc);
2031}
2032
2033
2034
2035
2036
2037
2038
2039
2040Constant *ConstantFoldSSEConvertToInt(const APFloat &Val, bool roundTowardZero,
2041 Type *Ty, bool IsSigned) {
2042
2044 assert(ResultWidth <= 64 &&
2045 "Can only constant fold conversions to 64 and 32 bit ints");
2046
2048 bool isExact = false;
2050 : APFloat::rmNearestTiesToEven;
2053 IsSigned, mode, &isExact);
2054 if (status != APFloat::opOK &&
2055 (!roundTowardZero || status != APFloat::opInexact))
2056 return nullptr;
2057 return ConstantInt::get(Ty, UIntVal, IsSigned);
2058}
2059
2061 Type *Ty = Op->getType();
2062
2064 return Op->getValueAPF().convertToDouble();
2065
2066 bool unused;
2067 APFloat APF = Op->getValueAPF();
2068 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &unused);
2070}
2071
2072static bool getConstIntOrUndef(Value *Op, const APInt *&C) {
2073 if (auto *CI = dyn_cast(Op)) {
2074 C = &CI->getValue();
2075 return true;
2076 }
2077 if (isa(Op)) {
2078 C = nullptr;
2079 return true;
2080 }
2081 return false;
2082}
2083
2084
2085
2086
2087
2088
2091 std::optional ORM = CI->getRoundingMode();
2093
2094
2095
2096 if (St == APFloat::opStatus::opOK)
2097 return true;
2098
2099
2100
2101 if (ORM && *ORM == RoundingMode::Dynamic)
2102 return false;
2103
2104
2105
2106 if (EB && *EB != fp::ExceptionBehavior::ebStrict)
2107 return true;
2108
2109
2110
2111 return false;
2112}
2113
2114
2117 std::optional ORM = CI->getRoundingMode();
2118 if (!ORM || *ORM == RoundingMode::Dynamic)
2119
2120
2121
2122
2123 return RoundingMode::NearestTiesToEven;
2124 return *ORM;
2125}
2126
2127
2128static Constant *constantFoldCanonicalize(const Type *Ty, const CallBase *CI,
2130
2131 if (Src.isZero()) {
2132
2133 return ConstantFP::get(
2136 }
2137
2139 return nullptr;
2140
2141
2142
2143
2144
2145 if (Src.isNormal() || Src.isInfinity())
2146 return ConstantFP::get(CI->getContext(), Src);
2147
2151
2153 return ConstantFP::get(CI->getContext(), Src);
2154
2156 return nullptr;
2157
2158
2163 return nullptr;
2164
2165 bool IsPositive =
2169
2170 return ConstantFP::get(CI->getContext(),
2172 }
2173
2174 return nullptr;
2175}
2176
2183 assert(Operands.size() == 1 && "Wrong number of operands.");
2184
2185 if (IntrinsicID == Intrinsic::is_constant) {
2186
2187
2188
2189 if (Operands[0]->isManifestConstant())
2191 return nullptr;
2192 }
2193
2194 if (isa(Operands[0])) {
2195
2196 if (IntrinsicID == Intrinsic::canonicalize)
2198 }
2199
2200 if (isa(Operands[0])) {
2201
2202
2203
2204 if (IntrinsicID == Intrinsic::cos ||
2205 IntrinsicID == Intrinsic::ctpop ||
2206 IntrinsicID == Intrinsic::fptoui_sat ||
2207 IntrinsicID == Intrinsic::fptosi_sat ||
2208 IntrinsicID == Intrinsic::canonicalize)
2210 if (IntrinsicID == Intrinsic::bswap ||
2211 IntrinsicID == Intrinsic::bitreverse ||
2212 IntrinsicID == Intrinsic::launder_invariant_group ||
2213 IntrinsicID == Intrinsic::strip_invariant_group)
2215 }
2216
2217 if (isa(Operands[0])) {
2218
2219 if (IntrinsicID == Intrinsic::launder_invariant_group ||
2220 IntrinsicID == Intrinsic::strip_invariant_group) {
2221
2222
2223
2225 Call->getParent() ? Call->getCaller() : nullptr;
2226 if (Caller &&
2230 }
2231 return nullptr;
2232 }
2233 }
2234
2235 if (auto *Op = dyn_cast(Operands[0])) {
2236 if (IntrinsicID == Intrinsic::convert_to_fp16) {
2238
2239 bool lost = false;
2240 Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &lost);
2241
2243 }
2244
2246
2247 if (IntrinsicID == Intrinsic::wasm_trunc_signed ||
2248 IntrinsicID == Intrinsic::wasm_trunc_unsigned) {
2249 bool Signed = IntrinsicID == Intrinsic::wasm_trunc_signed;
2250
2251 if (U.isNaN())
2252 return nullptr;
2253
2256 bool IsExact = false;
2258 U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
2259
2260 if (Status == APFloat::opOK || Status == APFloat::opInexact)
2261 return ConstantInt::get(Ty, Int);
2262
2263 return nullptr;
2264 }
2265
2266 if (IntrinsicID == Intrinsic::fptoui_sat ||
2267 IntrinsicID == Intrinsic::fptosi_sat) {
2268
2270 IntrinsicID == Intrinsic::fptoui_sat);
2271 bool IsExact;
2272 U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
2273 return ConstantInt::get(Ty, Int);
2274 }
2275
2276 if (IntrinsicID == Intrinsic::canonicalize)
2277 return constantFoldCanonicalize(Ty, Call, U);
2278
2279#if defined(HAS_IEE754_FLOAT128) && defined(HAS_LOGF128)
2281 if (IntrinsicID == Intrinsic::log) {
2282 float128 Result = logf128(Op->getValueAPF().convertToQuad());
2283 return GetConstantFoldFPValue128(Result, Ty);
2284 }
2285
2287 if (TLI && TLI->getLibFunc(Name, Fp128Func) && TLI->has(Fp128Func) &&
2288 Fp128Func == LibFunc_logl)
2289 return ConstantFoldFP128(logf128, Op->getValueAPF(), Ty);
2290 }
2291#endif
2292
2295 return nullptr;
2296
2297
2298
2299 if (IntrinsicID == Intrinsic::nearbyint || IntrinsicID == Intrinsic::rint) {
2300 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2301 return ConstantFP::get(Ty->getContext(), U);
2302 }
2303
2304 if (IntrinsicID == Intrinsic::round) {
2305 U.roundToIntegral(APFloat::rmNearestTiesToAway);
2306 return ConstantFP::get(Ty->getContext(), U);
2307 }
2308
2309 if (IntrinsicID == Intrinsic::roundeven) {
2310 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2311 return ConstantFP::get(Ty->getContext(), U);
2312 }
2313
2314 if (IntrinsicID == Intrinsic::ceil) {
2315 U.roundToIntegral(APFloat::rmTowardPositive);
2316 return ConstantFP::get(Ty->getContext(), U);
2317 }
2318
2319 if (IntrinsicID == Intrinsic::floor) {
2320 U.roundToIntegral(APFloat::rmTowardNegative);
2321 return ConstantFP::get(Ty->getContext(), U);
2322 }
2323
2324 if (IntrinsicID == Intrinsic::trunc) {
2325 U.roundToIntegral(APFloat::rmTowardZero);
2326 return ConstantFP::get(Ty->getContext(), U);
2327 }
2328
2329 if (IntrinsicID == Intrinsic::fabs) {
2330 U.clearSign();
2331 return ConstantFP::get(Ty->getContext(), U);
2332 }
2333
2334 if (IntrinsicID == Intrinsic::amdgcn_fract) {
2335
2336
2337
2338
2340 FloorU.roundToIntegral(APFloat::rmTowardNegative);
2341 APFloat FractU(U - FloorU);
2342 APFloat AlmostOne(U.getSemantics(), 1);
2343 AlmostOne.next( true);
2344 return ConstantFP::get(Ty->getContext(), minimum(FractU, AlmostOne));
2345 }
2346
2347
2348
2349
2350 std::optionalAPFloat::roundingMode RM;
2351 switch (IntrinsicID) {
2352 default:
2353 break;
2354 case Intrinsic::experimental_constrained_nearbyint:
2355 case Intrinsic::experimental_constrained_rint: {
2356 auto CI = cast(Call);
2357 RM = CI->getRoundingMode();
2358 if (!RM || *RM == RoundingMode::Dynamic)
2359 return nullptr;
2360 break;
2361 }
2362 case Intrinsic::experimental_constrained_round:
2363 RM = APFloat::rmNearestTiesToAway;
2364 break;
2365 case Intrinsic::experimental_constrained_ceil:
2366 RM = APFloat::rmTowardPositive;
2367 break;
2368 case Intrinsic::experimental_constrained_floor:
2369 RM = APFloat::rmTowardNegative;
2370 break;
2371 case Intrinsic::experimental_constrained_trunc:
2372 RM = APFloat::rmTowardZero;
2373 break;
2374 }
2375 if (RM) {
2376 auto CI = cast(Call);
2377 if (U.isFinite()) {
2379 if (IntrinsicID == Intrinsic::experimental_constrained_rint &&
2380 St == APFloat::opInexact) {
2381 std::optionalfp::ExceptionBehavior EB = CI->getExceptionBehavior();
2383 return nullptr;
2384 }
2385 } else if (U.isSignaling()) {
2386 std::optionalfp::ExceptionBehavior EB = CI->getExceptionBehavior();
2388 return nullptr;
2390 }
2391 return ConstantFP::get(Ty->getContext(), U);
2392 }
2393
2394
2395 switch (IntrinsicID) {
2396
2397 case Intrinsic::nvvm_f2i_rm:
2398 case Intrinsic::nvvm_f2i_rn:
2399 case Intrinsic::nvvm_f2i_rp:
2400 case Intrinsic::nvvm_f2i_rz:
2401 case Intrinsic::nvvm_f2i_rm_ftz:
2402 case Intrinsic::nvvm_f2i_rn_ftz:
2403 case Intrinsic::nvvm_f2i_rp_ftz:
2404 case Intrinsic::nvvm_f2i_rz_ftz:
2405
2406 case Intrinsic::nvvm_f2ui_rm:
2407 case Intrinsic::nvvm_f2ui_rn:
2408 case Intrinsic::nvvm_f2ui_rp:
2409 case Intrinsic::nvvm_f2ui_rz:
2410 case Intrinsic::nvvm_f2ui_rm_ftz:
2411 case Intrinsic::nvvm_f2ui_rn_ftz:
2412 case Intrinsic::nvvm_f2ui_rp_ftz:
2413 case Intrinsic::nvvm_f2ui_rz_ftz:
2414
2415 case Intrinsic::nvvm_d2i_rm:
2416 case Intrinsic::nvvm_d2i_rn:
2417 case Intrinsic::nvvm_d2i_rp:
2418 case Intrinsic::nvvm_d2i_rz:
2419
2420 case Intrinsic::nvvm_d2ui_rm:
2421 case Intrinsic::nvvm_d2ui_rn:
2422 case Intrinsic::nvvm_d2ui_rp:
2423 case Intrinsic::nvvm_d2ui_rz:
2424
2425 case Intrinsic::nvvm_f2ll_rm:
2426 case Intrinsic::nvvm_f2ll_rn:
2427 case Intrinsic::nvvm_f2ll_rp:
2428 case Intrinsic::nvvm_f2ll_rz:
2429 case Intrinsic::nvvm_f2ll_rm_ftz:
2430 case Intrinsic::nvvm_f2ll_rn_ftz:
2431 case Intrinsic::nvvm_f2ll_rp_ftz:
2432 case Intrinsic::nvvm_f2ll_rz_ftz:
2433
2434 case Intrinsic::nvvm_f2ull_rm:
2435 case Intrinsic::nvvm_f2ull_rn:
2436 case Intrinsic::nvvm_f2ull_rp:
2437 case Intrinsic::nvvm_f2ull_rz:
2438 case Intrinsic::nvvm_f2ull_rm_ftz:
2439 case Intrinsic::nvvm_f2ull_rn_ftz:
2440 case Intrinsic::nvvm_f2ull_rp_ftz:
2441 case Intrinsic::nvvm_f2ull_rz_ftz:
2442
2443 case Intrinsic::nvvm_d2ll_rm:
2444 case Intrinsic::nvvm_d2ll_rn:
2445 case Intrinsic::nvvm_d2ll_rp:
2446 case Intrinsic::nvvm_d2ll_rz:
2447
2448 case Intrinsic::nvvm_d2ull_rm:
2449 case Intrinsic::nvvm_d2ull_rn:
2450 case Intrinsic::nvvm_d2ull_rp:
2451 case Intrinsic::nvvm_d2ull_rz: {
2452
2453 if (U.isNaN())
2454 return ConstantInt::get(Ty, 0);
2455
2460
2462 auto FloatToRound = IsFTZ ? FTZPreserveSign(U) : U;
2463
2464 bool IsExact = false;
2466 FloatToRound.convertToInteger(ResInt, RMode, &IsExact);
2467
2468 if (Status != APFloat::opInvalidOp)
2469 return ConstantInt::get(Ty, ResInt);
2470 return nullptr;
2471 }
2472 }
2473
2474
2475
2476
2477 if (.isFinite())
2478 return nullptr;
2479
2480
2481
2482
2483
2484 const APFloat &APF = Op->getValueAPF();
2485
2486 switch (IntrinsicID) {
2487 default: break;
2488 case Intrinsic:🪵
2489 return ConstantFoldFP(log, APF, Ty);
2490 case Intrinsic::log2:
2491
2492 return ConstantFoldFP(log2, APF, Ty);
2493 case Intrinsic::log10:
2494
2495 return ConstantFoldFP(log10, APF, Ty);
2496 case Intrinsic::exp:
2497 return ConstantFoldFP(exp, APF, Ty);
2498 case Intrinsic::exp2:
2499
2500 return ConstantFoldBinaryFP(pow, APFloat(2.0), APF, Ty);
2501 case Intrinsic::exp10:
2502
2503 return ConstantFoldBinaryFP(pow, APFloat(10.0), APF, Ty);
2504 case Intrinsic::sin:
2505 return ConstantFoldFP(sin, APF, Ty);
2506 case Intrinsic::cos:
2507 return ConstantFoldFP(cos, APF, Ty);
2508 case Intrinsic::sqrt:
2509 return ConstantFoldFP(sqrt, APF, Ty);
2510 case Intrinsic::amdgcn_cos:
2511 case Intrinsic::amdgcn_sin: {
2512 double V = getValueAsDouble(Op);
2513 if (V < -256.0 || V > 256.0)
2514
2515
2516
2517 return nullptr;
2518 bool IsCos = IntrinsicID == Intrinsic::amdgcn_cos;
2520 if (V4 == floor(V4)) {
2521
2522 const double SinVals[4] = { 0.0, 1.0, 0.0, -1.0 };
2523 V = SinVals[((int)V4 + (IsCos ? 1 : 0)) & 3];
2524 } else {
2525 if (IsCos)
2527 else
2529 }
2530 return GetConstantFoldFPValue(V, Ty);
2531 }
2532 }
2533
2534 if (!TLI)
2535 return nullptr;
2536
2539 return nullptr;
2540
2541 switch (Func) {
2542 default:
2543 break;
2544 case LibFunc_acos:
2545 case LibFunc_acosf:
2546 case LibFunc_acos_finite:
2547 case LibFunc_acosf_finite:
2548 if (TLI->has(Func))
2549 return ConstantFoldFP(acos, APF, Ty);
2550 break;
2551 case LibFunc_asin:
2552 case LibFunc_asinf:
2553 case LibFunc_asin_finite:
2554 case LibFunc_asinf_finite:
2555 if (TLI->has(Func))
2556 return ConstantFoldFP(asin, APF, Ty);
2557 break;
2558 case LibFunc_atan:
2559 case LibFunc_atanf:
2560 if (TLI->has(Func))
2561 return ConstantFoldFP(atan, APF, Ty);
2562 break;
2563 case LibFunc_ceil:
2564 case LibFunc_ceilf:
2565 if (TLI->has(Func)) {
2566 U.roundToIntegral(APFloat::rmTowardPositive);
2567 return ConstantFP::get(Ty->getContext(), U);
2568 }
2569 break;
2570 case LibFunc_cos:
2571 case LibFunc_cosf:
2572 if (TLI->has(Func))
2573 return ConstantFoldFP(cos, APF, Ty);
2574 break;
2575 case LibFunc_cosh:
2576 case LibFunc_coshf:
2577 case LibFunc_cosh_finite:
2578 case LibFunc_coshf_finite:
2579 if (TLI->has(Func))
2580 return ConstantFoldFP(cosh, APF, Ty);
2581 break;
2582 case LibFunc_exp:
2583 case LibFunc_expf:
2584 case LibFunc_exp_finite:
2585 case LibFunc_expf_finite:
2586 if (TLI->has(Func))
2587 return ConstantFoldFP(exp, APF, Ty);
2588 break;
2589 case LibFunc_exp2:
2590 case LibFunc_exp2f:
2591 case LibFunc_exp2_finite:
2592 case LibFunc_exp2f_finite:
2593 if (TLI->has(Func))
2594
2595 return ConstantFoldBinaryFP(pow, APFloat(2.0), APF, Ty);
2596 break;
2597 case LibFunc_fabs:
2598 case LibFunc_fabsf:
2599 if (TLI->has(Func)) {
2600 U.clearSign();
2601 return ConstantFP::get(Ty->getContext(), U);
2602 }
2603 break;
2604 case LibFunc_floor:
2605 case LibFunc_floorf:
2606 if (TLI->has(Func)) {
2607 U.roundToIntegral(APFloat::rmTowardNegative);
2608 return ConstantFP::get(Ty->getContext(), U);
2609 }
2610 break;
2611 case LibFunc_log:
2612 case LibFunc_logf:
2613 case LibFunc_log_finite:
2614 case LibFunc_logf_finite:
2616 return ConstantFoldFP(log, APF, Ty);
2617 break;
2618 case LibFunc_log2:
2619 case LibFunc_log2f:
2620 case LibFunc_log2_finite:
2621 case LibFunc_log2f_finite:
2623
2624 return ConstantFoldFP(log2, APF, Ty);
2625 break;
2626 case LibFunc_log10:
2627 case LibFunc_log10f:
2628 case LibFunc_log10_finite:
2629 case LibFunc_log10f_finite:
2631
2632 return ConstantFoldFP(log10, APF, Ty);
2633 break;
2634 case LibFunc_ilogb:
2635 case LibFunc_ilogbf:
2636 if (!APF.isZero() && TLI->has(Func))
2637 return ConstantInt::get(Ty, ilogb(APF), true);
2638 break;
2639 case LibFunc_logb:
2640 case LibFunc_logbf:
2641 if (!APF.isZero() && TLI->has(Func))
2642 return ConstantFoldFP(logb, APF, Ty);
2643 break;
2644 case LibFunc_log1p:
2645 case LibFunc_log1pf:
2646
2647 if (U.isZero())
2648 return ConstantFP::get(Ty->getContext(), U);
2650 return ConstantFoldFP(log1p, APF, Ty);
2651 break;
2652 case LibFunc_logl:
2653 return nullptr;
2654 case LibFunc_erf:
2655 case LibFunc_erff:
2656 if (TLI->has(Func))
2657 return ConstantFoldFP(erf, APF, Ty);
2658 break;
2659 case LibFunc_nearbyint:
2660 case LibFunc_nearbyintf:
2661 case LibFunc_rint:
2662 case LibFunc_rintf:
2663 if (TLI->has(Func)) {
2664 U.roundToIntegral(APFloat::rmNearestTiesToEven);
2665 return ConstantFP::get(Ty->getContext(), U);
2666 }
2667 break;
2668 case LibFunc_round:
2669 case LibFunc_roundf:
2670 if (TLI->has(Func)) {
2671 U.roundToIntegral(APFloat::rmNearestTiesToAway);
2672 return ConstantFP::get(Ty->getContext(), U);
2673 }
2674 break;
2675 case LibFunc_sin:
2676 case LibFunc_sinf:
2677 if (TLI->has(Func))
2678 return ConstantFoldFP(sin, APF, Ty);
2679 break;
2680 case LibFunc_sinh:
2681 case LibFunc_sinhf:
2682 case LibFunc_sinh_finite:
2683 case LibFunc_sinhf_finite:
2684 if (TLI->has(Func))
2685 return ConstantFoldFP(sinh, APF, Ty);
2686 break;
2687 case LibFunc_sqrt:
2688 case LibFunc_sqrtf:
2690 return ConstantFoldFP(sqrt, APF, Ty);
2691 break;
2692 case LibFunc_tan:
2693 case LibFunc_tanf:
2694 if (TLI->has(Func))
2695 return ConstantFoldFP(tan, APF, Ty);
2696 break;
2697 case LibFunc_tanh:
2698 case LibFunc_tanhf:
2699 if (TLI->has(Func))
2700 return ConstantFoldFP(tanh, APF, Ty);
2701 break;
2702 case LibFunc_trunc:
2703 case LibFunc_truncf:
2704 if (TLI->has(Func)) {
2705 U.roundToIntegral(APFloat::rmTowardZero);
2706 return ConstantFP::get(Ty->getContext(), U);
2707 }
2708 break;
2709 }
2710 return nullptr;
2711 }
2712
2713 if (auto *Op = dyn_cast(Operands[0])) {
2714 switch (IntrinsicID) {
2715 case Intrinsic::bswap:
2716 return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
2717 case Intrinsic::ctpop:
2718 return ConstantInt::get(Ty, Op->getValue().popcount());
2719 case Intrinsic::bitreverse:
2720 return ConstantInt::get(Ty->getContext(), Op->getValue().reverseBits());
2721 case Intrinsic::convert_from_fp16: {
2722 APFloat Val(APFloat::IEEEhalf(), Op->getValue());
2723
2724 bool lost = false;
2726 Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &lost);
2727
2728
2729 (void)status;
2730 assert(status != APFloat::opInexact && !lost &&
2731 "Precision lost during fp16 constfolding");
2732
2733 return ConstantFP::get(Ty->getContext(), Val);
2734 }
2735
2736 case Intrinsic::amdgcn_s_wqm: {
2738 Val |= (Val & 0x5555555555555555ULL) << 1 |
2739 ((Val >> 1) & 0x5555555555555555ULL);
2740 Val |= (Val & 0x3333333333333333ULL) << 2 |
2741 ((Val >> 2) & 0x3333333333333333ULL);
2742 return ConstantInt::get(Ty, Val);
2743 }
2744
2745 case Intrinsic::amdgcn_s_quadmask: {
2748 for (unsigned I = 0; I < Op->getBitWidth() / 4; ++I, Val >>= 4) {
2749 if (!(Val & 0xF))
2750 continue;
2751
2752 QuadMask |= (1ULL << I);
2753 }
2754 return ConstantInt::get(Ty, QuadMask);
2755 }
2756
2757 case Intrinsic::amdgcn_s_bitreplicate: {
2759 Val = (Val & 0x000000000000FFFFULL) | (Val & 0x00000000FFFF0000ULL) << 16;
2760 Val = (Val & 0x000000FF000000FFULL) | (Val & 0x0000FF000000FF00ULL) << 8;
2761 Val = (Val & 0x000F000F000F000FULL) | (Val & 0x00F000F000F000F0ULL) << 4;
2762 Val = (Val & 0x0303030303030303ULL) | (Val & 0x0C0C0C0C0C0C0C0CULL) << 2;
2763 Val = (Val & 0x1111111111111111ULL) | (Val & 0x2222222222222222ULL) << 1;
2764 Val = Val | Val << 1;
2765 return ConstantInt::get(Ty, Val);
2766 }
2767
2768 default:
2769 return nullptr;
2770 }
2771 }
2772
2773 switch (IntrinsicID) {
2774 default: break;
2775 case Intrinsic::vector_reduce_add:
2776 case Intrinsic::vector_reduce_mul:
2777 case Intrinsic::vector_reduce_and:
2778 case Intrinsic::vector_reduce_or:
2779 case Intrinsic::vector_reduce_xor:
2780 case Intrinsic::vector_reduce_smin:
2781 case Intrinsic::vector_reduce_smax:
2782 case Intrinsic::vector_reduce_umin:
2783 case Intrinsic::vector_reduce_umax:
2784 if (Constant *C = constantFoldVectorReduce(IntrinsicID, Operands[0]))
2785 return C;
2786 break;
2787 }
2788
2789
2790 if (isa(Operands[0]) ||
2791 isa(Operands[0])) {
2792 auto *Op = cast(Operands[0]);
2793 switch (IntrinsicID) {
2794 default: break;
2795 case Intrinsic::x86_sse_cvtss2si:
2796 case Intrinsic::x86_sse_cvtss2si64:
2797 case Intrinsic::x86_sse2_cvtsd2si:
2798 case Intrinsic::x86_sse2_cvtsd2si64:
2800 dyn_cast_or_null(Op->getAggregateElement(0U)))
2801 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2802 false, Ty,
2803 true);
2804 break;
2805 case Intrinsic::x86_sse_cvttss2si:
2806 case Intrinsic::x86_sse_cvttss2si64:
2807 case Intrinsic::x86_sse2_cvttsd2si:
2808 case Intrinsic::x86_sse2_cvttsd2si64:
2810 dyn_cast_or_null(Op->getAggregateElement(0U)))
2811 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
2812 true, Ty,
2813 true);
2814 break;
2815 }
2816 }
2817
2818 return nullptr;
2819}
2820
2824 auto *FCmp = cast(Call);
2826 if (FCmp->isSignaling()) {
2828 St = APFloat::opInvalidOp;
2829 } else {
2831 St = APFloat::opInvalidOp;
2832 }
2835 return ConstantInt::get(Call->getType()->getScalarType(), Result);
2836 return nullptr;
2837}
2838
2842 if (!TLI)
2843 return nullptr;
2844
2847 return nullptr;
2848
2849 const auto *Op1 = dyn_cast(Operands[0]);
2850 if (!Op1)
2851 return nullptr;
2852
2853 const auto *Op2 = dyn_cast(Operands[1]);
2854 if (!Op2)
2855 return nullptr;
2856
2857 const APFloat &Op1V = Op1->getValueAPF();
2858 const APFloat &Op2V = Op2->getValueAPF();
2859
2860 switch (Func) {
2861 default:
2862 break;
2863 case LibFunc_pow:
2864 case LibFunc_powf:
2865 case LibFunc_pow_finite:
2866 case LibFunc_powf_finite:
2867 if (TLI->has(Func))
2868 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
2869 break;
2870 case LibFunc_fmod:
2871 case LibFunc_fmodf:
2872 if (TLI->has(Func)) {
2873 APFloat V = Op1->getValueAPF();
2874 if (APFloat::opStatus::opOK == V.mod(Op2->getValueAPF()))
2875 return ConstantFP::get(Ty->getContext(), V);
2876 }
2877 break;
2878 case LibFunc_remainder:
2879 case LibFunc_remainderf:
2880 if (TLI->has(Func)) {
2881 APFloat V = Op1->getValueAPF();
2882 if (APFloat::opStatus::opOK == V.remainder(Op2->getValueAPF()))
2883 return ConstantFP::get(Ty->getContext(), V);
2884 }
2885 break;
2886 case LibFunc_atan2:
2887 case LibFunc_atan2f:
2888
2889
2891 return nullptr;
2892 [[fallthrough]];
2893 case LibFunc_atan2_finite:
2894 case LibFunc_atan2f_finite:
2895 if (TLI->has(Func))
2896 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
2897 break;
2898 }
2899
2900 return nullptr;
2901}
2902
2906 assert(Operands.size() == 2 && "Wrong number of operands.");
2907
2909
2910
2911 bool IsOp0Undef = isa(Operands[0]);
2912 bool IsOp1Undef = isa(Operands[1]);
2913 switch (IntrinsicID) {
2914 case Intrinsic::maxnum:
2915 case Intrinsic::minnum:
2916 case Intrinsic::maximum:
2917 case Intrinsic::minimum:
2918 case Intrinsic::nvvm_fmax_d:
2919 case Intrinsic::nvvm_fmin_d:
2920
2921 if (IsOp0Undef)
2923 if (IsOp1Undef)
2925 break;
2926
2927 case Intrinsic::nvvm_fmax_f:
2928 case Intrinsic::nvvm_fmax_ftz_f:
2929 case Intrinsic::nvvm_fmax_ftz_nan_f:
2930 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
2931 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
2932 case Intrinsic::nvvm_fmax_nan_f:
2933 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
2934 case Intrinsic::nvvm_fmax_xorsign_abs_f:
2935
2936 case Intrinsic::nvvm_fmin_f:
2937 case Intrinsic::nvvm_fmin_ftz_f:
2938 case Intrinsic::nvvm_fmin_ftz_nan_f:
2939 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
2940 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
2941 case Intrinsic::nvvm_fmin_nan_f:
2942 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
2943 case Intrinsic::nvvm_fmin_xorsign_abs_f:
2944
2945
2946
2947 if (!IsOp0Undef && !IsOp1Undef)
2948 break;
2949 if (auto *Op = dyn_cast(Operands[IsOp0Undef ? 1 : 0])) {
2950 if (Op->isNaN()) {
2951 APInt NVCanonicalNaN(32, 0x7fffffff);
2952 return ConstantFP::get(
2954 }
2956 return ConstantFP::get(Ty, FTZPreserveSign(Op->getValueAPF()));
2957 else
2958 return Op;
2959 }
2960 break;
2961 }
2962 }
2963
2964 if (const auto *Op1 = dyn_cast(Operands[0])) {
2965 const APFloat &Op1V = Op1->getValueAPF();
2966
2967 if (const auto *Op2 = dyn_cast(Operands[1])) {
2968 if (Op2->getType() != Op1->getType())
2969 return nullptr;
2970 const APFloat &Op2V = Op2->getValueAPF();
2971
2972 if (const auto *ConstrIntr =
2973 dyn_cast_if_present(Call)) {
2974 RoundingMode RM = getEvaluationRoundingMode(ConstrIntr);
2977 switch (IntrinsicID) {
2978 default:
2979 return nullptr;
2980 case Intrinsic::experimental_constrained_fadd:
2981 St = Res.add(Op2V, RM);
2982 break;
2983 case Intrinsic::experimental_constrained_fsub:
2984 St = Res.subtract(Op2V, RM);
2985 break;
2986 case Intrinsic::experimental_constrained_fmul:
2987 St = Res.multiply(Op2V, RM);
2988 break;
2989 case Intrinsic::experimental_constrained_fdiv:
2990 St = Res.divide(Op2V, RM);
2991 break;
2992 case Intrinsic::experimental_constrained_frem:
2993 St = Res.mod(Op2V);
2994 break;
2995 case Intrinsic::experimental_constrained_fcmp:
2996 case Intrinsic::experimental_constrained_fcmps:
2997 return evaluateCompare(Op1V, Op2V, ConstrIntr);
2998 }
3000 St))
3001 return ConstantFP::get(Ty->getContext(), Res);
3002 return nullptr;
3003 }
3004
3005 switch (IntrinsicID) {
3006 default:
3007 break;
3008 case Intrinsic::copysign:
3010 case Intrinsic::minnum:
3011 return ConstantFP::get(Ty->getContext(), minnum(Op1V, Op2V));
3012 case Intrinsic::maxnum:
3013 return ConstantFP::get(Ty->getContext(), maxnum(Op1V, Op2V));
3014 case Intrinsic::minimum:
3016 case Intrinsic::maximum:
3018
3019 case Intrinsic::nvvm_fmax_d:
3020 case Intrinsic::nvvm_fmax_f:
3021 case Intrinsic::nvvm_fmax_ftz_f:
3022 case Intrinsic::nvvm_fmax_ftz_nan_f:
3023 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
3024 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
3025 case Intrinsic::nvvm_fmax_nan_f:
3026 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
3027 case Intrinsic::nvvm_fmax_xorsign_abs_f:
3028
3029 case Intrinsic::nvvm_fmin_d:
3030 case Intrinsic::nvvm_fmin_f:
3031 case Intrinsic::nvvm_fmin_ftz_f:
3032 case Intrinsic::nvvm_fmin_ftz_nan_f:
3033 case Intrinsic::nvvm_fmin_ftz_nan_xorsign_abs_f:
3034 case Intrinsic::nvvm_fmin_ftz_xorsign_abs_f:
3035 case Intrinsic::nvvm_fmin_nan_f:
3036 case Intrinsic::nvvm_fmin_nan_xorsign_abs_f:
3037 case Intrinsic::nvvm_fmin_xorsign_abs_f: {
3038
3039 bool ShouldCanonicalizeNaNs = !(IntrinsicID == Intrinsic::nvvm_fmax_d ||
3040 IntrinsicID == Intrinsic::nvvm_fmin_d);
3044
3045 APFloat A = IsFTZ ? FTZPreserveSign(Op1V) : Op1V;
3046 APFloat B = IsFTZ ? FTZPreserveSign(Op2V) : Op2V;
3047
3048 bool XorSign = false;
3049 if (IsXorSignAbs) {
3050 XorSign = A.isNegative() ^ B.isNegative();
3053 }
3054
3055 bool IsFMax = false;
3056 switch (IntrinsicID) {
3057 case Intrinsic::nvvm_fmax_d:
3058 case Intrinsic::nvvm_fmax_f:
3059 case Intrinsic::nvvm_fmax_ftz_f:
3060 case Intrinsic::nvvm_fmax_ftz_nan_f:
3061 case Intrinsic::nvvm_fmax_ftz_nan_xorsign_abs_f:
3062 case Intrinsic::nvvm_fmax_ftz_xorsign_abs_f:
3063 case Intrinsic::nvvm_fmax_nan_f:
3064 case Intrinsic::nvvm_fmax_nan_xorsign_abs_f:
3065 case Intrinsic::nvvm_fmax_xorsign_abs_f:
3066 IsFMax = true;
3067 break;
3068 }
3070
3071 if (ShouldCanonicalizeNaNs) {
3073 if (A.isNaN() && B.isNaN())
3074 return ConstantFP::get(Ty, NVCanonicalNaN);
3075 else if (IsNaNPropagating && (A.isNaN() || B.isNaN()))
3076 return ConstantFP::get(Ty, NVCanonicalNaN);
3077 }
3078
3079 if (A.isNaN() && B.isNaN())
3081 else if (A.isNaN())
3082 Res = B;
3083 else if (B.isNaN())
3084 Res = A;
3085
3086 if (IsXorSignAbs && XorSign != Res.isNegative())
3088
3089 return ConstantFP::get(Ty->getContext(), Res);
3090 }
3091 }
3092
3094 return nullptr;
3095
3096 switch (IntrinsicID) {
3097 default:
3098 break;
3099 case Intrinsic::pow:
3100 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
3101 case Intrinsic::amdgcn_fmul_legacy:
3102
3103
3106 return ConstantFP::get(Ty->getContext(), Op1V * Op2V);
3107 }
3108
3109 } else if (auto *Op2C = dyn_cast(Operands[1])) {
3110 switch (IntrinsicID) {
3111 case Intrinsic::ldexp: {
3112 return ConstantFP::get(
3114 scalbn(Op1V, Op2C->getSExtValue(), APFloat::rmNearestTiesToEven));
3115 }
3116 case Intrinsic::is_fpclass: {
3129 return ConstantInt::get(Ty, Result);
3130 }
3131 case Intrinsic::powi: {
3132 int Exp = static_cast<int>(Op2C->getSExtValue());
3139 Res.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
3140 &Unused);
3141 }
3142 return ConstantFP::get(Ty->getContext(), Res);
3143 }
3145 return ConstantFP::get(Ty, std::pow(Op1V.convertToDouble(), Exp));
3146 default:
3147 return nullptr;
3148 }
3149 }
3150 default:
3151 break;
3152 }
3153 }
3154 return nullptr;
3155 }
3156
3159 const APInt *C0, *C1;
3160 if (!getConstIntOrUndef(Operands[0], C0) ||
3161 !getConstIntOrUndef(Operands[1], C1))
3162 return nullptr;
3163
3164 switch (IntrinsicID) {
3165 default: break;
3166 case Intrinsic::smax:
3167 case Intrinsic::smin:
3168 case Intrinsic::umax:
3169 case Intrinsic::umin:
3170
3171
3172 if (isa(Operands[0]) || isa(Operands[1]))
3174
3175 if (!C0 && !C1)
3177 if (!C0 || !C1)
3179 return ConstantInt::get(
3182 ? *C0
3183 : *C1);
3184
3185 case Intrinsic::scmp:
3186 case Intrinsic::ucmp:
3187 if (isa(Operands[0]) || isa(Operands[1]))
3189
3190 if (!C0 || !C1)
3191 return ConstantInt::get(Ty, 0);
3192
3193 int Res;
3194 if (IntrinsicID == Intrinsic::scmp)
3195 Res = C0->sgt(*C1) ? 1 : C0->slt(*C1) ? -1 : 0;
3196 else
3197 Res = C0->ugt(*C1) ? 1 : C0->ult(*C1) ? -1 : 0;
3198 return ConstantInt::get(Ty, Res, true);
3199
3200 case Intrinsic::usub_with_overflow:
3201 case Intrinsic::ssub_with_overflow:
3202
3203
3204 if (!C0 || !C1)
3206 [[fallthrough]];
3207 case Intrinsic::uadd_with_overflow:
3208 case Intrinsic::sadd_with_overflow:
3209
3210
3211 if (!C0 || !C1) {
3213 cast(Ty),
3216 }
3217 [[fallthrough]];
3218 case Intrinsic::smul_with_overflow:
3219 case Intrinsic::umul_with_overflow: {
3220
3221
3222 if (!C0 || !C1)
3224
3226 bool Overflow;
3227 switch (IntrinsicID) {
3229 case Intrinsic::sadd_with_overflow:
3230 Res = C0->sadd_ov(*C1, Overflow);
3231 break;
3232 case Intrinsic::uadd_with_overflow:
3233 Res = C0->uadd_ov(*C1, Overflow);
3234 break;
3235 case Intrinsic::ssub_with_overflow:
3236 Res = C0->ssub_ov(*C1, Overflow);
3237 break;
3238 case Intrinsic::usub_with_overflow:
3239 Res = C0->usub_ov(*C1, Overflow);
3240 break;
3241 case Intrinsic::smul_with_overflow:
3242 Res = C0->smul_ov(*C1, Overflow);
3243 break;
3244 case Intrinsic::umul_with_overflow:
3245 Res = C0->umul_ov(*C1, Overflow);
3246 break;
3247 }
3249 ConstantInt::get(Ty->getContext(), Res),
3251 };
3253 }
3254 case Intrinsic::uadd_sat:
3255 case Intrinsic::sadd_sat:
3256
3257
3258 if (isa(Operands[0]) || isa(Operands[1]))
3260
3261 if (!C0 && !C1)
3263 if (!C0 || !C1)
3265 if (IntrinsicID == Intrinsic::uadd_sat)
3266 return ConstantInt::get(Ty, C0->uadd_sat(*C1));
3267 else
3268 return ConstantInt::get(Ty, C0->sadd_sat(*C1));
3269 case Intrinsic::usub_sat:
3270 case Intrinsic::ssub_sat:
3271
3272
3273 if (isa(Operands[0]) || isa(Operands[1]))
3275
3276 if (!C0 && !C1)
3278 if (!C0 || !C1)
3280 if (IntrinsicID == Intrinsic::usub_sat)
3281 return ConstantInt::get(Ty, C0->usub_sat(*C1));
3282 else
3283 return ConstantInt::get(Ty, C0->ssub_sat(*C1));
3284 case Intrinsic::cttz:
3285 case Intrinsic::ctlz:
3286 assert(C1 && "Must be constant int");
3287
3288
3289 if (C1->isOne() && (!C0 || C0->isZero()))
3291 if (!C0)
3293 if (IntrinsicID == Intrinsic::cttz)
3294 return ConstantInt::get(Ty, C0->countr_zero());
3295 else
3296 return ConstantInt::get(Ty, C0->countl_zero());
3297
3298 case Intrinsic::abs:
3299 assert(C1 && "Must be constant int");
3301
3302
3305
3306
3307 if (!C0)
3309
3310 return ConstantInt::get(Ty, C0->abs());
3311 case Intrinsic::amdgcn_wave_reduce_umin:
3312 case Intrinsic::amdgcn_wave_reduce_umax:
3313 return dyn_cast(Operands[0]);
3314 }
3315
3316 return nullptr;
3317 }
3318
3319
3320 if ((isa(Operands[0]) ||
3321 isa(Operands[0])) &&
3322
3323
3324 isa(Operands[1]) &&
3325 cast(Operands[1])->getValue() == 4) {
3326 auto *Op = cast(Operands[0]);
3327 switch (IntrinsicID) {
3328 default: break;
3329 case Intrinsic::x86_avx512_vcvtss2si32:
3330 case Intrinsic::x86_avx512_vcvtss2si64:
3331 case Intrinsic::x86_avx512_vcvtsd2si32:
3332 case Intrinsic::x86_avx512_vcvtsd2si64:
3334 dyn_cast_or_null(Op->getAggregateElement(0U)))
3335 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3336 false, Ty,
3337 true);
3338 break;
3339 case Intrinsic::x86_avx512_vcvtss2usi32:
3340 case Intrinsic::x86_avx512_vcvtss2usi64:
3341 case Intrinsic::x86_avx512_vcvtsd2usi32:
3342 case Intrinsic::x86_avx512_vcvtsd2usi64:
3344 dyn_cast_or_null(Op->getAggregateElement(0U)))
3345 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3346 false, Ty,
3347 false);
3348 break;
3349 case Intrinsic::x86_avx512_cvttss2si:
3350 case Intrinsic::x86_avx512_cvttss2si64:
3351 case Intrinsic::x86_avx512_cvttsd2si:
3352 case Intrinsic::x86_avx512_cvttsd2si64:
3354 dyn_cast_or_null(Op->getAggregateElement(0U)))
3355 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3356 true, Ty,
3357 true);
3358 break;
3359 case Intrinsic::x86_avx512_cvttss2usi:
3360 case Intrinsic::x86_avx512_cvttss2usi64:
3361 case Intrinsic::x86_avx512_cvttsd2usi:
3362 case Intrinsic::x86_avx512_cvttsd2usi64:
3364 dyn_cast_or_null(Op->getAggregateElement(0U)))
3365 return ConstantFoldSSEConvertToInt(FPOp->getValueAPF(),
3366 true, Ty,
3367 false);
3368 break;
3369 }
3370 }
3371 return nullptr;
3372}
3373
3378 unsigned ID;
3380 APFloat MA(Sem), SC(Sem), TC(Sem);
3383
3384 ID = 5;
3385 SC = -S0;
3386 } else {
3387 ID = 4;
3388 SC = S0;
3389 }
3390 MA = S2;
3391 TC = -S1;
3392 } else if (abs(S1) >= abs(S0)) {
3393 if (S1.isNegative() && S1.isNonZero() && .isNaN()) {
3394
3395 ID = 3;
3396 TC = -S2;
3397 } else {
3398 ID = 2;
3399 TC = S2;
3400 }
3401 MA = S1;
3402 SC = S0;
3403 } else {
3405
3406 ID = 1;
3407 SC = S2;
3408 } else {
3409 ID = 0;
3410 SC = -S2;
3411 }
3412 MA = S0;
3413 TC = -S1;
3414 }
3415 switch (IntrinsicID) {
3416 default:
3418 case Intrinsic::amdgcn_cubeid:
3420 case Intrinsic::amdgcn_cubema:
3421 return MA + MA;
3422 case Intrinsic::amdgcn_cubesc:
3423 return SC;
3424 case Intrinsic::amdgcn_cubetc:
3425 return TC;
3426 }
3427}
3428
3431 const APInt *C0, *C1, *C2;
3432 if (!getConstIntOrUndef(Operands[0], C0) ||
3433 !getConstIntOrUndef(Operands[1], C1) ||
3434 !getConstIntOrUndef(Operands[2], C2))
3435 return nullptr;
3436
3437 if (!C2)
3439
3440 APInt Val(32, 0);
3441 unsigned NumUndefBytes = 0;
3442 for (unsigned I = 0; I < 32; I += 8) {
3444 unsigned B = 0;
3445
3446 if (Sel >= 13)
3447 B = 0xff;
3448 else if (Sel == 12)
3449 B = 0x00;
3450 else {
3451 const APInt *Src = ((Sel & 10) == 10 || (Sel & 12) == 4) ? C0 : C1;
3452 if (!Src)
3453 ++NumUndefBytes;
3454 else if (Sel < 8)
3455 B = Src->extractBitsAsZExtValue(8, (Sel & 3) * 8);
3456 else
3457 B = Src->extractBitsAsZExtValue(1, (Sel & 1) ? 31 : 15) * 0xff;
3458 }
3459
3461 }
3462
3463 if (NumUndefBytes == 4)
3465
3466 return ConstantInt::get(Ty, Val);
3467}
3468
3475 assert(Operands.size() == 3 && "Wrong number of operands.");
3476
3477 if (const auto *Op1 = dyn_cast(Operands[0])) {
3478 if (const auto *Op2 = dyn_cast(Operands[1])) {
3479 if (const auto *Op3 = dyn_cast(Operands[2])) {
3480 const APFloat &C1 = Op1->getValueAPF();
3481 const APFloat &C2 = Op2->getValueAPF();
3482 const APFloat &C3 = Op3->getValueAPF();
3483
3484 if (const auto *ConstrIntr = dyn_cast(Call)) {
3485 RoundingMode RM = getEvaluationRoundingMode(ConstrIntr);
3488 switch (IntrinsicID) {
3489 default:
3490 return nullptr;
3491 case Intrinsic::experimental_constrained_fma:
3492 case Intrinsic::experimental_constrained_fmuladd:
3494 break;
3495 }
3496 if (mayFoldConstrained(
3498 return ConstantFP::get(Ty->getContext(), Res);
3499 return nullptr;
3500 }
3501
3502 switch (IntrinsicID) {
3503 default: break;
3504 case Intrinsic::amdgcn_fma_legacy: {
3505
3506
3508
3509
3511 }
3512 [[fallthrough]];
3513 }
3514 case Intrinsic::fma:
3515 case Intrinsic::fmuladd: {
3517 V.fusedMultiplyAdd(C2, C3, APFloat::rmNearestTiesToEven);
3518 return ConstantFP::get(Ty->getContext(), V);
3519 }
3520 case Intrinsic::amdgcn_cubeid:
3521 case Intrinsic::amdgcn_cubema:
3522 case Intrinsic::amdgcn_cubesc:
3523 case Intrinsic::amdgcn_cubetc: {
3524 APFloat V = ConstantFoldAMDGCNCubeIntrinsic(IntrinsicID, C1, C2, C3);
3525 return ConstantFP::get(Ty->getContext(), V);
3526 }
3527 }
3528 }
3529 }
3530 }
3531
3532 if (IntrinsicID == Intrinsic::smul_fix ||
3533 IntrinsicID == Intrinsic::smul_fix_sat) {
3534
3535
3536 if (isa(Operands[0]) || isa(Operands[1]))
3538
3539 const APInt *C0, *C1;
3540 if (!getConstIntOrUndef(Operands[0], C0) ||
3541 !getConstIntOrUndef(Operands[1], C1))
3542 return nullptr;
3543
3544
3545
3546 if (!C0 || !C1)
3548
3549
3550
3551
3552
3553
3554
3555 unsigned Scale = cast(Operands[2])->getZExtValue();
3557 assert(Scale < Width && "Illegal scale.");
3558 unsigned ExtendedWidth = Width * 2;
3560 (C0->sext(ExtendedWidth) * C1->sext(ExtendedWidth)).ashr(Scale);
3561 if (IntrinsicID == Intrinsic::smul_fix_sat) {
3566 }
3568 }
3569
3570 if (IntrinsicID == Intrinsic::fshl || IntrinsicID == Intrinsic::fshr) {
3571 const APInt *C0, *C1, *C2;
3572 if (!getConstIntOrUndef(Operands[0], C0) ||
3573 !getConstIntOrUndef(Operands[1], C1) ||
3574 !getConstIntOrUndef(Operands[2], C2))
3575 return nullptr;
3576
3577 bool IsRight = IntrinsicID == Intrinsic::fshr;
3578 if (!C2)
3579 return Operands[IsRight ? 1 : 0];
3580 if (!C0 && !C1)
3582
3583
3584
3587 if (!ShAmt)
3588 return Operands[IsRight ? 1 : 0];
3589
3590
3591 unsigned LshrAmt = IsRight ? ShAmt : BitWidth - ShAmt;
3592 unsigned ShlAmt = !IsRight ? ShAmt : BitWidth - ShAmt;
3593 if (!C0)
3594 return ConstantInt::get(Ty, C1->lshr(LshrAmt));
3595 if (!C1)
3596 return ConstantInt::get(Ty, C0->shl(ShlAmt));
3597 return ConstantInt::get(Ty, C0->shl(ShlAmt) | C1->lshr(LshrAmt));
3598 }
3599
3600 if (IntrinsicID == Intrinsic::amdgcn_perm)
3601 return ConstantFoldAMDGCNPermIntrinsic(Operands, Ty);
3602
3603 return nullptr;
3604}
3605
3613 return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call);
3614
3616 if (Constant *FoldedLibCall =
3617 ConstantFoldLibCall2(Name, Ty, Operands, TLI)) {
3618 return FoldedLibCall;
3619 }
3620 return ConstantFoldIntrinsicCall2(IntrinsicID, Ty, Operands, Call);
3621 }
3622
3624 return ConstantFoldScalarCall3(Name, IntrinsicID, Ty, Operands, TLI, Call);
3625
3626 return nullptr;
3627}
3628
3629static Constant *ConstantFoldFixedVectorCall(
3636
3637 switch (IntrinsicID) {
3638 case Intrinsic::masked_load: {
3641 auto *Passthru = Operands[3];
3642
3644
3647 auto *MaskElt = Mask->getAggregateElement(I);
3648 if (!MaskElt)
3649 break;
3650 auto *PassthruElt = Passthru->getAggregateElement(I);
3652 if (isa(MaskElt)) {
3653 if (PassthruElt)
3654 NewElements.push_back(PassthruElt);
3655 else if (VecElt)
3657 else
3658 return nullptr;
3659 }
3660 if (MaskElt->isNullValue()) {
3661 if (!PassthruElt)
3662 return nullptr;
3663 NewElements.push_back(PassthruElt);
3664 } else if (MaskElt->isOneValue()) {
3665 if (!VecElt)
3666 return nullptr;
3668 } else {
3669 return nullptr;
3670 }
3671 }
3673 return nullptr;
3675 }
3676 case Intrinsic::arm_mve_vctp8:
3677 case Intrinsic::arm_mve_vctp16:
3678 case Intrinsic::arm_mve_vctp32:
3679 case Intrinsic::arm_mve_vctp64: {
3680 if (auto *Op = dyn_cast(Operands[0])) {
3682 uint64_t Limit = Op->getZExtValue();
3683
3685 for (unsigned i = 0; i < Lanes; i++) {
3686 if (i < Limit)
3688 else
3690 }
3692 }
3693 return nullptr;
3694 }
3695 case Intrinsic::get_active_lane_mask: {
3696 auto *Op0 = dyn_cast(Operands[0]);
3697 auto *Op1 = dyn_cast(Operands[1]);
3698 if (Op0 && Op1) {
3701 uint64_t Limit = Op1->getZExtValue();
3702
3704 for (unsigned i = 0; i < Lanes; i++) {
3705 if (Base + i < Limit)
3707 else
3709 }
3711 }
3712 return nullptr;
3713 }
3714 default:
3715 break;
3716 }
3717
3719
3720 for (unsigned J = 0, JE = Operands.size(); J != JE; ++J) {
3721
3724 continue;
3725 }
3726
3728 if (!Agg)
3729 return nullptr;
3730
3731 Lane[J] = Agg;
3732 }
3733
3734
3736 ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, Call);
3737 if (!Folded)
3738 return nullptr;
3740 }
3741
3743}
3744
3745static Constant *ConstantFoldScalableVectorCall(
3749 switch (IntrinsicID) {
3750 case Intrinsic::aarch64_sve_convert_from_svbool: {
3751 auto *Src = dyn_cast(Operands[0]);
3752 if (!Src || !Src->isNullValue())
3753 break;
3754
3756 }
3757 default:
3758 break;
3759 }
3760 return nullptr;
3761}
3762
3763static std::pair<Constant *, Constant *>
3764ConstantFoldScalarFrexpCall(Constant *Op, Type *IntTy) {
3765 if (isa(Op))
3767
3768 auto *ConstFP = dyn_cast(Op);
3769 if (!ConstFP)
3770 return {};
3771
3772 const APFloat &U = ConstFP->getValueAPF();
3773 int FrexpExp;
3774 APFloat FrexpMant = frexp(U, FrexpExp, APFloat::rmNearestTiesToEven);
3775 Constant *Result0 = ConstantFP::get(ConstFP->getType(), FrexpMant);
3776
3777
3778
3782 return {Result0, Result1};
3783}
3784
3785
3791
3792 switch (IntrinsicID) {
3793 case Intrinsic::frexp: {
3796
3797 if (auto *FVTy0 = dyn_cast(Ty0)) {
3800
3801 for (unsigned I = 0, E = FVTy0->getNumElements(); I != E; ++I) {
3803 std::tie(Results0[I], Results1[I]) =
3804 ConstantFoldScalarFrexpCall(Lane, Ty1);
3805 if (!Results0[I])
3806 return nullptr;
3807 }
3808
3811 }
3812
3813 auto [Result0, Result1] = ConstantFoldScalarFrexpCall(Operands[0], Ty1);
3814 if (!Result0)
3815 return nullptr;
3817 }
3818 case Intrinsic::sincos: {
3821
3822 auto ConstantFoldScalarSincosCall =
3823 [&](Constant *Op) -> std::pair<Constant *, Constant *> {
3825 ConstantFoldScalarCall(Name, Intrinsic::sin, TyScalar, Op, TLI, Call);
3827 ConstantFoldScalarCall(Name, Intrinsic::cos, TyScalar, Op, TLI, Call);
3828 return std::make_pair(SinResult, CosResult);
3829 };
3830
3831 if (auto *FVTy = dyn_cast(Ty)) {
3834
3837 std::tie(SinResults[I], CosResults[I]) =
3838 ConstantFoldScalarSincosCall(Lane);
3839 if (!SinResults[I] || !CosResults[I])
3840 return nullptr;
3841 }
3842
3845 }
3846
3847 auto [SinResult, CosResult] = ConstantFoldScalarSincosCall(Operands[0]);
3848 if (!SinResult || !CosResult)
3849 return nullptr;
3851 }
3852 default:
3853
3854
3855 return ConstantFoldScalarCall(Name, IntrinsicID, StTy, Operands, TLI, Call);
3856 }
3857
3858 return nullptr;
3859}
3860
3861}
3862
3866 return ConstantFoldIntrinsicCall2(ID, Ty, {LHS, RHS},
3867 dyn_cast_if_present(FMFSource));
3868}
3869
3873 bool AllowNonDeterministic) {
3874 if (Call->isNoBuiltin())
3875 return nullptr;
3876 if (->hasName())
3877 return nullptr;
3878
3879
3882 if (!TLI)
3883 return nullptr;
3886 return nullptr;
3887 }
3888
3889
3890
3891 Type *Ty = F->getReturnType();
3893 return nullptr;
3894
3896 if (auto *FVTy = dyn_cast(Ty))
3897 return ConstantFoldFixedVectorCall(
3898 Name, IID, FVTy, Operands, F->getDataLayout(), TLI, Call);
3899
3900 if (auto *SVTy = dyn_cast(Ty))
3901 return ConstantFoldScalableVectorCall(
3902 Name, IID, SVTy, Operands, F->getDataLayout(), TLI, Call);
3903
3904 if (auto *StTy = dyn_cast(Ty))
3905 return ConstantFoldStructCall(Name, IID, StTy, Operands,
3906 F->getDataLayout(), TLI, Call);
3907
3908
3909
3910
3911 return ConstantFoldScalarCall(Name, IID, Ty, Operands, TLI, Call);
3912}
3913
3916
3917
3918 if (Call->isNoBuiltin() || Call->isStrictFP())
3919 return false;
3920 Function *F = Call->getCalledFunction();
3921 if ()
3922 return false;
3923
3926 return false;
3927
3928 if (Call->arg_size() == 1) {
3929 if (ConstantFP *OpC = dyn_cast(Call->getArgOperand(0))) {
3930 const APFloat &Op = OpC->getValueAPF();
3931 switch (Func) {
3932 case LibFunc_logl:
3933 case LibFunc_log:
3934 case LibFunc_logf:
3935 case LibFunc_log2l:
3936 case LibFunc_log2:
3937 case LibFunc_log2f:
3938 case LibFunc_log10l:
3939 case LibFunc_log10:
3940 case LibFunc_log10f:
3941 return Op.isNaN() || (.isZero() &&
.isNegative());
3942
3943 case LibFunc_ilogb:
3944 return .isNaN() &&
.isZero() &&
.isInfinity();
3945
3946 case LibFunc_expl:
3947 case LibFunc_exp:
3948 case LibFunc_expf:
3949
3950 if (OpC->getType()->isDoubleTy())
3952 if (OpC->getType()->isFloatTy())
3954 break;
3955
3956 case LibFunc_exp2l:
3957 case LibFunc_exp2:
3958 case LibFunc_exp2f:
3959
3960 if (OpC->getType()->isDoubleTy())
3962 if (OpC->getType()->isFloatTy())
3964 break;
3965
3966 case LibFunc_sinl:
3967 case LibFunc_sin:
3968 case LibFunc_sinf:
3969 case LibFunc_cosl:
3970 case LibFunc_cos:
3971 case LibFunc_cosf:
3972 return .isInfinity();
3973
3974 case LibFunc_tanl:
3975 case LibFunc_tan:
3976 case LibFunc_tanf: {
3977
3978
3979 Type *Ty = OpC->getType();
3981 return ConstantFoldFP(tan, OpC->getValueAPF(), Ty) != nullptr;
3982 break;
3983 }
3984
3985 case LibFunc_atan:
3986 case LibFunc_atanf:
3987 case LibFunc_atanl:
3988
3989 return true;
3990
3991 case LibFunc_asinl:
3992 case LibFunc_asin:
3993 case LibFunc_asinf:
3994 case LibFunc_acosl:
3995 case LibFunc_acos:
3996 case LibFunc_acosf:
3999
4000 case LibFunc_sinh:
4001 case LibFunc_cosh:
4002 case LibFunc_sinhf:
4003 case LibFunc_coshf:
4004 case LibFunc_sinhl:
4005 case LibFunc_coshl:
4006
4007 if (OpC->getType()->isDoubleTy())
4009 if (OpC->getType()->isFloatTy())
4011 break;
4012
4013 case LibFunc_sqrtl:
4014 case LibFunc_sqrt:
4015 case LibFunc_sqrtf:
4016 return Op.isNaN() || Op.isZero() || .isNegative();
4017
4018
4019
4020 default:
4021 break;
4022 }
4023 }
4024 }
4025
4026 if (Call->arg_size() == 2) {
4027 ConstantFP *Op0C = dyn_cast(Call->getArgOperand(0));
4028 ConstantFP *Op1C = dyn_cast(Call->getArgOperand(1));
4029 if (Op0C && Op1C) {
4032
4033 switch (Func) {
4034 case LibFunc_powl:
4035 case LibFunc_pow:
4036 case LibFunc_powf: {
4037
4038
4041 if (Ty == Op1C->getType())
4042 return ConstantFoldBinaryFP(pow, Op0, Op1, Ty) != nullptr;
4043 }
4044 break;
4045 }
4046
4047 case LibFunc_fmodl:
4048 case LibFunc_fmod:
4049 case LibFunc_fmodf:
4050 case LibFunc_remainderl:
4051 case LibFunc_remainder:
4052 case LibFunc_remainderf:
4053 return Op0.isNaN() || Op1.isNaN() ||
4055
4056 case LibFunc_atan2:
4057 case LibFunc_atan2f:
4058 case LibFunc_atan2l:
4059
4060
4061
4062
4064
4065 default:
4066 break;
4067 }
4068 }
4069 }
4070
4071 return false;
4072}
4073
4074void TargetFolder::anchor() {}
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static Constant * FoldBitCast(Constant *V, Type *DestTy)
static ConstantFP * flushDenormalConstant(Type *Ty, const APFloat &APF, DenormalMode::DenormalModeKind Mode)
Constant * getConstantAtOffset(Constant *Base, APInt Offset, const DataLayout &DL)
If this Offset points exactly to the start of an aggregate element, return that element,...
static ConstantFP * flushDenormalConstantFP(ConstantFP *CFP, const Instruction *Inst, bool IsOutput)
static DenormalMode getInstrDenormalMode(const Instruction *CtxI, Type *Ty)
Return the denormal mode that can be assumed when executing a floating point operation at CtxI.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
amode Optimize addressing mode
mir Rename Register Operands
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
This file contains the definitions of the enumerations and flags associated with NVVM Intrinsics,...
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
opStatus divide(const APFloat &RHS, roundingMode RM)
void copySign(const APFloat &RHS)
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
opStatus subtract(const APFloat &RHS, roundingMode RM)
double convertToDouble() const
Converts this APFloat to host double value.
bool isPosInfinity() const
opStatus add(const APFloat &RHS, roundingMode RM)
const fltSemantics & getSemantics() const
static APFloat getOne(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative One.
opStatus multiply(const APFloat &RHS, roundingMode RM)
float convertToFloat() const
Converts this APFloat to host float value.
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
APInt bitcastToAPInt() const
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
opStatus mod(const APFloat &RHS)
bool isNegInfinity() const
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Class for arbitrary precision integers.
APInt umul_ov(const APInt &RHS, bool &Overflow) const
APInt usub_sat(const APInt &RHS) const
bool isMinSignedValue() const
Determine if this is the smallest signed value.
uint64_t getZExtValue() const
Get zero extended value.
uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
APInt trunc(unsigned width) const
Truncate to new width.
APInt abs() const
Get the absolute value.
APInt sadd_sat(const APInt &RHS) const
bool sgt(const APInt &RHS) const
Signed greater than comparison.
APInt usub_ov(const APInt &RHS, bool &Overflow) const
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
APInt sadd_ov(const APInt &RHS, bool &Overflow) const
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
unsigned countr_zero() const
Count the number of trailing zero bits.
unsigned countl_zero() const
The APInt version of std::countl_zero.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
APInt uadd_sat(const APInt &RHS) const
APInt smul_ov(const APInt &RHS, bool &Overflow) const
APInt sext(unsigned width) const
Sign extend to a new width.
APInt shl(unsigned shiftAmt) const
Left-shift function.
bool slt(const APInt &RHS) const
Signed less than comparison.
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
APInt ssub_ov(const APInt &RHS, bool &Overflow) const
bool isOne() const
Determine if this is a value of 1.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
APInt ssub_sat(const APInt &RHS) const
An arbitrary precision integer that knows its signedness.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & back() const
back - Get the last element.
size_t size() const
size - Get the array size.
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
static Instruction::CastOps getCastOpcode(const Value *Val, bool SrcIsSigned, Type *Ty, bool DstIsSigned)
Returns the opcode necessary to cast Val into Ty using usual casting rules.
static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy)
This method can be used to determine if a cast from SrcTy to DstTy using Opcode op is valid or not.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
bool isFPPredicate() const
static Constant * get(LLVMContext &Context, ArrayRef< ElementTy > Elts)
get() constructor - Return a constant with array type with an element count and element type matching...
static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
static bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
const APFloat & getValueAPF() const
static Constant * getZero(Type *Ty, bool Negative=false)
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
static ConstantInt * getFalse(LLVMContext &Context)
static ConstantInt * getBool(LLVMContext &Context, bool V)
static Constant * get(StructType *T, ArrayRef< Constant * > V)
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static Constant * getAllOnesValue(Type *Ty)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Constrained floating point compare intrinsics.
This is the common base class for constrained floating point intrinsics.
std::optional< fp::ExceptionBehavior > getExceptionBehavior() const
std::optional< RoundingMode > getRoundingMode() const
Wrapper for a function that represents a value that functionally represents the original function.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
static bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This provides a helper for copying FMF from an instruction or setting specified flags.
Class to represent fixed width SIMD vectors.
unsigned getNumElements() const
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags inBounds()
GEPNoWrapFlags withoutNoUnsignedSignedWrap() const
static GEPNoWrapFlags noUnsignedWrap()
bool hasNoUnsignedSignedWrap() const
static Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
PointerType * getType() const
Global values are always pointers.
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
const Function * getFunction() const
Return the function this instruction belongs to.
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
This is an important class for using LLVM in a threaded context.
static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits)
Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values, so there is a certain thre...
ICmpInst::Predicate getPredicate() const
Returns the comparison predicate underlying the intrinsic.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Class to represent scalable SIMD vectors.
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
unsigned getElementContainingOffset(uint64_t FixedOffset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
TypeSize getElementOffset(unsigned Idx) const
Class to represent struct types.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
unsigned getIntegerBitWidth() const
Type * getStructElementType(unsigned N) const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
bool isPointerTy() const
True if this is an instance of PointerType.
static IntegerType * getInt1Ty(LLVMContext &C)
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
@ HalfTyID
16-bit floating point type
@ FloatTyID
32-bit floating point type
@ DoubleTyID
64-bit floating point type
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
bool isFP128Ty() const
Return true if this is 'fp128'.
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isStructTy() const
True if this is an instance of StructType.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
static IntegerType * getInt16Ty(LLVMContext &C)
bool isAggregateType() const
Return true if the type is an aggregate type.
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static IntegerType * getInt8Ty(LLVMContext &C)
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
bool isX86_AMXTy() const
Return true if this is X86 AMX.
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
TypeID getTypeID() const
Return the type id for the type.
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
bool isIEEELikeFPTy() const
Return true if this is a well-behaved IEEE-like type, which has a IEEE compatible layout as defined b...
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
LLVMContext & getContext() const
All values hold a context through their type.
Base class of all SIMD vector types.
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Type * getElementType() const
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
const ParentTy * getParent() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ C
The default llvm calling convention, compatible with C.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
@ CE
Windows NT (Windows on ARM)
int ilogb(const IEEEFloat &Arg)
@ ebStrict
This corresponds to "fpexcept.strict".
@ ebIgnore
This corresponds to "fpexcept.ignore".
bool FPToIntegerIntrinsicResultIsSigned(Intrinsic::ID IntrinsicID)
APFloat::roundingMode GetFPToIntegerRoundingMode(Intrinsic::ID IntrinsicID)
bool FPToIntegerIntrinsicShouldFTZ(Intrinsic::ID IntrinsicID)
bool FMinFMaxIsXorSignAbs(Intrinsic::ID IntrinsicID)
bool FMinFMaxShouldFTZ(Intrinsic::ID IntrinsicID)
bool FMinFMaxPropagatesNaNs(Intrinsic::ID IntrinsicID)
NodeAddr< FuncNode * > Func
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Constant * ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS, Constant *RHS, Type *Ty, Instruction *FMFSource)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Constant * ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL)
ConstantFoldLoadThroughBitcast - try to cast constant to destination type returning null if unsuccess...
static double log2(double V)
Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
Constant * ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL, const Instruction *I, bool AllowNonDeterministic=true)
Attempt to constant fold a floating point binary operation with the specified operands,...
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
unsigned getPointerAddressSpace(const Type *T)
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset, const DataLayout &DL, DSOLocalEquivalent **DSOEquiv=nullptr)
If this constant is a constant offset from a global, return the global and the constant.
bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)
Check whether the given call has no side-effects.
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
Constant * ConstantFoldCall(const CallBase *Call, Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 maximumNumber semantics.
Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty, const DataLayout &DL)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
Constant * ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op, const DataLayout &DL)
Attempt to constant fold a unary operation with the specified operand.
Constant * FlushFPConstant(Constant *Operand, const Instruction *I, bool IsOutput)
Attempt to flush float point constant according to denormal mode set in the instruction's parent func...
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2019 minimumNumber semantics.
bool isVectorIntrinsicWithScalarOpAtArg(Intrinsic::ID ID, unsigned ScalarOpdIdx, const TargetTransformInfo *TTI)
Identifies if the vector form of the intrinsic has a scalar operand.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
RoundingMode
Rounding mode.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
constexpr unsigned BitWidth
Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
opStatus
IEEE-754R 7: Default exception handling.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getDynamic()
static constexpr DenormalMode getIEEE()
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
bool isConstant() const
Returns true if we know the value of all bits.
const APInt & getConstant() const
Returns the value when all bits have a known value.