xref: /freebsd/contrib/llvm-project/llvm/lib/FuzzMutate/OpDescriptor.cpp (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 //===-- OpDescriptor.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/FuzzMutate/OpDescriptor.h"
10 #include "llvm/IR/Constants.h"
11 
12 using namespace llvm;
13 using namespace fuzzerop;
14 
15 void fuzzerop::makeConstantsWithType(Type *T, std::vector<Constant *> &Cs) {
16   if (auto *IntTy = dyn_cast<IntegerType>(T)) {
17     uint64_t W = IntTy->getBitWidth();
18     Cs.push_back(ConstantInt::get(IntTy, 0));
19     Cs.push_back(ConstantInt::get(IntTy, 1));
20     Cs.push_back(ConstantInt::get(IntTy, 42));
21     Cs.push_back(ConstantInt::get(IntTy, APInt::getMaxValue(W)));
22     Cs.push_back(ConstantInt::get(IntTy, APInt::getMinValue(W)));
23     Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMaxValue(W)));
24     Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMinValue(W)));
25     Cs.push_back(ConstantInt::get(IntTy, APInt::getOneBitSet(W, W / 2)));
26   } else if (T->isFloatingPointTy()) {
27     auto &Ctx = T->getContext();
28     auto &Sem = T->getFltSemantics();
29     Cs.push_back(ConstantFP::get(Ctx, APFloat::getZero(Sem)));
30     Cs.push_back(ConstantFP::get(Ctx, APFloat(Sem, 1)));
31     Cs.push_back(ConstantFP::get(Ctx, APFloat(Sem, 42)));
32     Cs.push_back(ConstantFP::get(Ctx, APFloat::getLargest(Sem)));
33     Cs.push_back(ConstantFP::get(Ctx, APFloat::getSmallest(Sem)));
34     Cs.push_back(ConstantFP::get(Ctx, APFloat::getInf(Sem)));
35     Cs.push_back(ConstantFP::get(Ctx, APFloat::getNaN(Sem)));
36   } else if (VectorType *VecTy = dyn_cast<VectorType>(T)) {
37     std::vector<Constant *> EleCs;
38     Type *EltTy = VecTy->getElementType();
39     makeConstantsWithType(EltTy, EleCs);
40     ElementCount EC = VecTy->getElementCount();
41     for (Constant *Elt : EleCs) {
42       Cs.push_back(ConstantVector::getSplat(EC, Elt));
43     }
44   } else {
45     Cs.push_back(UndefValue::get(T));
46     Cs.push_back(PoisonValue::get(T));
47   }
48 }
49 
50 std::vector<Constant *> fuzzerop::makeConstantsWithType(Type *T) {
51   std::vector<Constant *> Result;
52   makeConstantsWithType(T, Result);
53   return Result;
54 }
55