10b57cec5SDimitry Andric //===-- OpDescriptor.cpp --------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/FuzzMutate/OpDescriptor.h"
100b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric using namespace llvm;
130b57cec5SDimitry Andric using namespace fuzzerop;
140b57cec5SDimitry Andric 
makeConstantsWithType(Type * T,std::vector<Constant * > & Cs)150b57cec5SDimitry Andric void fuzzerop::makeConstantsWithType(Type *T, std::vector<Constant *> &Cs) {
160b57cec5SDimitry Andric   if (auto *IntTy = dyn_cast<IntegerType>(T)) {
170b57cec5SDimitry Andric     uint64_t W = IntTy->getBitWidth();
18*fe013be4SDimitry Andric     Cs.push_back(ConstantInt::get(IntTy, 0));
19*fe013be4SDimitry Andric     Cs.push_back(ConstantInt::get(IntTy, 1));
20*fe013be4SDimitry Andric     Cs.push_back(ConstantInt::get(IntTy, 42));
210b57cec5SDimitry Andric     Cs.push_back(ConstantInt::get(IntTy, APInt::getMaxValue(W)));
220b57cec5SDimitry Andric     Cs.push_back(ConstantInt::get(IntTy, APInt::getMinValue(W)));
230b57cec5SDimitry Andric     Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMaxValue(W)));
240b57cec5SDimitry Andric     Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMinValue(W)));
250b57cec5SDimitry Andric     Cs.push_back(ConstantInt::get(IntTy, APInt::getOneBitSet(W, W / 2)));
260b57cec5SDimitry Andric   } else if (T->isFloatingPointTy()) {
270b57cec5SDimitry Andric     auto &Ctx = T->getContext();
280b57cec5SDimitry Andric     auto &Sem = T->getFltSemantics();
290b57cec5SDimitry Andric     Cs.push_back(ConstantFP::get(Ctx, APFloat::getZero(Sem)));
30*fe013be4SDimitry Andric     Cs.push_back(ConstantFP::get(Ctx, APFloat(Sem, 1)));
31*fe013be4SDimitry Andric     Cs.push_back(ConstantFP::get(Ctx, APFloat(Sem, 42)));
320b57cec5SDimitry Andric     Cs.push_back(ConstantFP::get(Ctx, APFloat::getLargest(Sem)));
330b57cec5SDimitry Andric     Cs.push_back(ConstantFP::get(Ctx, APFloat::getSmallest(Sem)));
34*fe013be4SDimitry Andric     Cs.push_back(ConstantFP::get(Ctx, APFloat::getInf(Sem)));
35*fe013be4SDimitry Andric     Cs.push_back(ConstantFP::get(Ctx, APFloat::getNaN(Sem)));
36*fe013be4SDimitry Andric   } else if (VectorType *VecTy = dyn_cast<VectorType>(T)) {
37*fe013be4SDimitry Andric     std::vector<Constant *> EleCs;
38*fe013be4SDimitry Andric     Type *EltTy = VecTy->getElementType();
39*fe013be4SDimitry Andric     makeConstantsWithType(EltTy, EleCs);
40*fe013be4SDimitry Andric     ElementCount EC = VecTy->getElementCount();
41*fe013be4SDimitry Andric     for (Constant *Elt : EleCs) {
42*fe013be4SDimitry Andric       Cs.push_back(ConstantVector::getSplat(EC, Elt));
43*fe013be4SDimitry Andric     }
44*fe013be4SDimitry Andric   } else {
450b57cec5SDimitry Andric     Cs.push_back(UndefValue::get(T));
46*fe013be4SDimitry Andric     Cs.push_back(PoisonValue::get(T));
47*fe013be4SDimitry Andric   }
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
makeConstantsWithType(Type * T)500b57cec5SDimitry Andric std::vector<Constant *> fuzzerop::makeConstantsWithType(Type *T) {
510b57cec5SDimitry Andric   std::vector<Constant *> Result;
520b57cec5SDimitry Andric   makeConstantsWithType(T, Result);
530b57cec5SDimitry Andric   return Result;
540b57cec5SDimitry Andric }
55