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#include "llvm/Support/CommandLine.h"
12
13using namespace llvm;
14using namespace fuzzerop;
15
16static cl::opt<bool> UseUndef("use-undef",
17 cl::desc("Use undef when generating programs."),
18 cl::init(Val: false));
19
20void fuzzerop::makeConstantsWithType(Type *T, std::vector<Constant *> &Cs) {
21 if (auto *IntTy = dyn_cast<IntegerType>(Val: T)) {
22 uint64_t W = IntTy->getBitWidth();
23 Cs.push_back(x: ConstantInt::get(Ty: IntTy, V: 0));
24 Cs.push_back(x: ConstantInt::get(Ty: IntTy, V: 1));
25 Cs.push_back(x: ConstantInt::get(Ty: IntTy, V: 42, /*IsSigned=*/false,
26 /*ImplicitTrunc=*/true));
27 Cs.push_back(x: ConstantInt::get(Ty: IntTy, V: APInt::getMaxValue(numBits: W)));
28 Cs.push_back(x: ConstantInt::get(Ty: IntTy, V: APInt::getMinValue(numBits: W)));
29 Cs.push_back(x: ConstantInt::get(Ty: IntTy, V: APInt::getSignedMaxValue(numBits: W)));
30 Cs.push_back(x: ConstantInt::get(Ty: IntTy, V: APInt::getSignedMinValue(numBits: W)));
31 Cs.push_back(x: ConstantInt::get(Ty: IntTy, V: APInt::getOneBitSet(numBits: W, BitNo: W / 2)));
32 } else if (T->isFloatingPointTy()) {
33 auto &Ctx = T->getContext();
34 auto &Sem = T->getFltSemantics();
35 Cs.push_back(x: ConstantFP::get(Context&: Ctx, V: APFloat::getZero(Sem)));
36 Cs.push_back(x: ConstantFP::get(Context&: Ctx, V: APFloat(Sem, 1)));
37 Cs.push_back(x: ConstantFP::get(Context&: Ctx, V: APFloat(Sem, 42)));
38 Cs.push_back(x: ConstantFP::get(Context&: Ctx, V: APFloat::getLargest(Sem)));
39 Cs.push_back(x: ConstantFP::get(Context&: Ctx, V: APFloat::getSmallest(Sem)));
40 Cs.push_back(x: ConstantFP::get(Context&: Ctx, V: APFloat::getInf(Sem)));
41 Cs.push_back(x: ConstantFP::get(Context&: Ctx, V: APFloat::getNaN(Sem)));
42 } else if (VectorType *VecTy = dyn_cast<VectorType>(Val: T)) {
43 std::vector<Constant *> EleCs;
44 Type *EltTy = VecTy->getElementType();
45 makeConstantsWithType(T: EltTy, Cs&: EleCs);
46 ElementCount EC = VecTy->getElementCount();
47 for (Constant *Elt : EleCs) {
48 Cs.push_back(x: ConstantVector::getSplat(EC, Elt));
49 }
50 } else {
51 if (UseUndef)
52 Cs.push_back(x: UndefValue::get(T));
53 Cs.push_back(x: PoisonValue::get(T));
54 }
55}
56
57std::vector<Constant *> fuzzerop::makeConstantsWithType(Type *T) {
58 std::vector<Constant *> Result;
59 makeConstantsWithType(T, Cs&: Result);
60 return Result;
61}
62