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" 11*0fca6ea1SDimitry Andric #include "llvm/Support/CommandLine.h" 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric using namespace llvm; 140b57cec5SDimitry Andric using namespace fuzzerop; 150b57cec5SDimitry Andric 16*0fca6ea1SDimitry Andric static cl::opt<bool> UseUndef("use-undef", 17*0fca6ea1SDimitry Andric cl::desc("Use undef when generating programs."), 18*0fca6ea1SDimitry Andric cl::init(false)); 19*0fca6ea1SDimitry Andric 200b57cec5SDimitry Andric void fuzzerop::makeConstantsWithType(Type *T, std::vector<Constant *> &Cs) { 210b57cec5SDimitry Andric if (auto *IntTy = dyn_cast<IntegerType>(T)) { 220b57cec5SDimitry Andric uint64_t W = IntTy->getBitWidth(); 2306c3fb27SDimitry Andric Cs.push_back(ConstantInt::get(IntTy, 0)); 2406c3fb27SDimitry Andric Cs.push_back(ConstantInt::get(IntTy, 1)); 2506c3fb27SDimitry Andric Cs.push_back(ConstantInt::get(IntTy, 42)); 260b57cec5SDimitry Andric Cs.push_back(ConstantInt::get(IntTy, APInt::getMaxValue(W))); 270b57cec5SDimitry Andric Cs.push_back(ConstantInt::get(IntTy, APInt::getMinValue(W))); 280b57cec5SDimitry Andric Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMaxValue(W))); 290b57cec5SDimitry Andric Cs.push_back(ConstantInt::get(IntTy, APInt::getSignedMinValue(W))); 300b57cec5SDimitry Andric Cs.push_back(ConstantInt::get(IntTy, APInt::getOneBitSet(W, W / 2))); 310b57cec5SDimitry Andric } else if (T->isFloatingPointTy()) { 320b57cec5SDimitry Andric auto &Ctx = T->getContext(); 330b57cec5SDimitry Andric auto &Sem = T->getFltSemantics(); 340b57cec5SDimitry Andric Cs.push_back(ConstantFP::get(Ctx, APFloat::getZero(Sem))); 3506c3fb27SDimitry Andric Cs.push_back(ConstantFP::get(Ctx, APFloat(Sem, 1))); 3606c3fb27SDimitry Andric Cs.push_back(ConstantFP::get(Ctx, APFloat(Sem, 42))); 370b57cec5SDimitry Andric Cs.push_back(ConstantFP::get(Ctx, APFloat::getLargest(Sem))); 380b57cec5SDimitry Andric Cs.push_back(ConstantFP::get(Ctx, APFloat::getSmallest(Sem))); 3906c3fb27SDimitry Andric Cs.push_back(ConstantFP::get(Ctx, APFloat::getInf(Sem))); 4006c3fb27SDimitry Andric Cs.push_back(ConstantFP::get(Ctx, APFloat::getNaN(Sem))); 4106c3fb27SDimitry Andric } else if (VectorType *VecTy = dyn_cast<VectorType>(T)) { 4206c3fb27SDimitry Andric std::vector<Constant *> EleCs; 4306c3fb27SDimitry Andric Type *EltTy = VecTy->getElementType(); 4406c3fb27SDimitry Andric makeConstantsWithType(EltTy, EleCs); 4506c3fb27SDimitry Andric ElementCount EC = VecTy->getElementCount(); 4606c3fb27SDimitry Andric for (Constant *Elt : EleCs) { 4706c3fb27SDimitry Andric Cs.push_back(ConstantVector::getSplat(EC, Elt)); 4806c3fb27SDimitry Andric } 4906c3fb27SDimitry Andric } else { 50*0fca6ea1SDimitry Andric if (UseUndef) 510b57cec5SDimitry Andric Cs.push_back(UndefValue::get(T)); 5206c3fb27SDimitry Andric Cs.push_back(PoisonValue::get(T)); 5306c3fb27SDimitry Andric } 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric std::vector<Constant *> fuzzerop::makeConstantsWithType(Type *T) { 570b57cec5SDimitry Andric std::vector<Constant *> Result; 580b57cec5SDimitry Andric makeConstantsWithType(T, Result); 590b57cec5SDimitry Andric return Result; 600b57cec5SDimitry Andric } 61