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