17d449d31SJustin Bogner //===-- OpDescriptor.cpp --------------------------------------------------===//
27d449d31SJustin Bogner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67d449d31SJustin Bogner //
77d449d31SJustin Bogner //===----------------------------------------------------------------------===//
87d449d31SJustin Bogner
97d449d31SJustin Bogner #include "llvm/FuzzMutate/OpDescriptor.h"
107d449d31SJustin Bogner #include "llvm/IR/Constants.h"
11*16ae493fSPeter Rong #include "llvm/Support/CommandLine.h"
127d449d31SJustin Bogner
137d449d31SJustin Bogner using namespace llvm;
147d449d31SJustin Bogner using namespace fuzzerop;
157d449d31SJustin Bogner
16*16ae493fSPeter Rong static cl::opt<bool> UseUndef("use-undef",
17*16ae493fSPeter Rong cl::desc("Use undef when generating programs."),
18*16ae493fSPeter Rong cl::init(false));
19*16ae493fSPeter Rong
makeConstantsWithType(Type * T,std::vector<Constant * > & Cs)207d449d31SJustin Bogner void fuzzerop::makeConstantsWithType(Type *T, std::vector<Constant *> &Cs) {
217d449d31SJustin Bogner if (auto *IntTy = dyn_cast<IntegerType>(T)) {
227d449d31SJustin Bogner uint64_t W = IntTy->getBitWidth();
23c06adaebSPeter Rong Cs.push_back(ConstantInt::get(IntTy, 0));
24c06adaebSPeter Rong Cs.push_back(ConstantInt::get(IntTy, 1));
25c06adaebSPeter Rong Cs.push_back(ConstantInt::get(IntTy, 42));
267d449d31SJustin Bogner Cs.push_back(ConstantInt::get(IntTy, APInt::getMaxValue(W)));
277d449d31SJustin Bogner Cs.push_back(ConstantInt::get(IntTy, APInt::getMinValue(W)));
287d449d31SJustin Bogner Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMaxValue(W)));
297d449d31SJustin Bogner Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMinValue(W)));
307d449d31SJustin Bogner Cs.push_back(ConstantInt::get(IntTy, APInt::getOneBitSet(W, W / 2)));
317d449d31SJustin Bogner } else if (T->isFloatingPointTy()) {
327d449d31SJustin Bogner auto &Ctx = T->getContext();
337d449d31SJustin Bogner auto &Sem = T->getFltSemantics();
347d449d31SJustin Bogner Cs.push_back(ConstantFP::get(Ctx, APFloat::getZero(Sem)));
35c06adaebSPeter Rong Cs.push_back(ConstantFP::get(Ctx, APFloat(Sem, 1)));
36c06adaebSPeter Rong Cs.push_back(ConstantFP::get(Ctx, APFloat(Sem, 42)));
377d449d31SJustin Bogner Cs.push_back(ConstantFP::get(Ctx, APFloat::getLargest(Sem)));
387d449d31SJustin Bogner Cs.push_back(ConstantFP::get(Ctx, APFloat::getSmallest(Sem)));
39c06adaebSPeter Rong Cs.push_back(ConstantFP::get(Ctx, APFloat::getInf(Sem)));
40c06adaebSPeter Rong Cs.push_back(ConstantFP::get(Ctx, APFloat::getNaN(Sem)));
41c06adaebSPeter Rong } else if (VectorType *VecTy = dyn_cast<VectorType>(T)) {
42c06adaebSPeter Rong std::vector<Constant *> EleCs;
43c06adaebSPeter Rong Type *EltTy = VecTy->getElementType();
44c06adaebSPeter Rong makeConstantsWithType(EltTy, EleCs);
45c06adaebSPeter Rong ElementCount EC = VecTy->getElementCount();
46c06adaebSPeter Rong for (Constant *Elt : EleCs) {
47c06adaebSPeter Rong Cs.push_back(ConstantVector::getSplat(EC, Elt));
48c06adaebSPeter Rong }
49c06adaebSPeter Rong } else {
50*16ae493fSPeter Rong if (UseUndef)
517d449d31SJustin Bogner Cs.push_back(UndefValue::get(T));
52c06adaebSPeter Rong Cs.push_back(PoisonValue::get(T));
53c06adaebSPeter Rong }
547d449d31SJustin Bogner }
557d449d31SJustin Bogner
makeConstantsWithType(Type * T)567d449d31SJustin Bogner std::vector<Constant *> fuzzerop::makeConstantsWithType(Type *T) {
577d449d31SJustin Bogner std::vector<Constant *> Result;
587d449d31SJustin Bogner makeConstantsWithType(T, Result);
597d449d31SJustin Bogner return Result;
607d449d31SJustin Bogner }
61