diff --git a/tools/mutator-utils/mutator.cpp b/tools/mutator-utils/mutator.cpp index 23243806c..a18283b99 100644 --- a/tools/mutator-utils/mutator.cpp +++ b/tools/mutator-utils/mutator.cpp @@ -499,9 +499,19 @@ llvm::Value *FunctionMutator::getRandomConstant(llvm::Type *ty) { } if (auto vecTy = llvm::dyn_cast(ty); - vecTy && vecTy->getElementType()) { + vecTy && vecTy->getElementType()->isIntegerTy()) { return mutator_util::getRandomLLVMIntegerVector(vecTy); } + + if (auto vecTy = llvm::dyn_cast(ty); + vecTy && vecTy->getElementType()->isFloatTy()) { + return mutator_util::getRandomLLVMFloatVector(vecTy); + } + + if (auto vecTy = llvm::dyn_cast(ty); + vecTy && vecTy->getElementType()->isDoubleTy()) { + return mutator_util::getRandomLLVMDoubleVector(vecTy); + } return llvm::UndefValue::get(ty); } diff --git a/tools/mutator-utils/util.cpp b/tools/mutator-utils/util.cpp index ddc0d2a11..77ed0008d 100644 --- a/tools/mutator-utils/util.cpp +++ b/tools/mutator-utils/util.cpp @@ -180,6 +180,84 @@ mutator_util::getRandomLLVMIntegerVector(llvm::FixedVectorType *ty) { return llvm::ConstantVector::get(result); } +llvm::Constant * +mutator_util::getRandomLLVMFloatVector(llvm::FixedVectorType *ty) { + assert(ty->getElementType()->isFloatTy() && "expect a float vector"); + llvm::Type *floatTy = llvm::Type::getFloatTy(ty->getContext()); + unsigned length = ty->getNumElements(); + llvm::SmallVector result; + for (size_t i = 0; i < length; ++i) { + result.push_back(llvm::ConstantFP::get(floatTy, getRandomLLVMFloat())); + } + // we set threshold to 5, if p is less than threshold , we assign poison to + // the vector + unsigned threshold = 5, p = Random::getRandomUnsigned() % 100; + if (p < threshold) { + unsigned poisonNum = Random::getRandomUnsigned(2) + 1; + poisonNum = std::min(poisonNum, length); + for (size_t times = 0; times < poisonNum; ++times) { + result[Random::getRandomUnsigned() % length] = + llvm::PoisonValue::get(floatTy); + } + } + return llvm::ConstantVector::get(result); +} + +llvm::Constant *mutator_util::updateFloatVector(llvm::ConstantVector *ty) { + unsigned length = ty->getType()->getNumElements(); + assert(ty->getType()->getElementType()->isFloatTy() && + "only support for updating float type"); + llvm::Type *floatTy = llvm::Type::getFloatTy(ty->getContext()); + llvm::SmallVector result; + for (size_t i = 0; i < length; ++i) { + if (Random::getRandomBool()) { + result.push_back(llvm::ConstantFP::get(floatTy, getRandomLLVMFloat())); + } else { + result.push_back(ty->getAggregateElement(i)); + } + } + return llvm::ConstantVector::get(result); +} + +llvm::Constant * +mutator_util::getRandomLLVMDoubleVector(llvm::FixedVectorType *ty) { + assert(ty->getElementType()->isDoubleTy() && "expect a double vector"); + llvm::Type *doubleTy = llvm::Type::getDoubleTy(ty->getContext()); + unsigned length = ty->getNumElements(); + llvm::SmallVector result; + for (size_t i = 0; i < length; ++i) { + result.push_back(llvm::ConstantFP::get(doubleTy, getRandomLLVMDouble())); + } + // we set threshold to 5, if p is less than threshold , we assign poison to + // the vector + unsigned threshold = 5, p = Random::getRandomUnsigned() % 100; + if (p < threshold) { + unsigned poisonNum = Random::getRandomUnsigned(2) + 1; + poisonNum = std::min(poisonNum, length); + for (size_t times = 0; times < poisonNum; ++times) { + result[Random::getRandomUnsigned() % length] = + llvm::PoisonValue::get(doubleTy); + } + } + return llvm::ConstantVector::get(result); +} + +llvm::Constant *mutator_util::updateDoubleVector(llvm::ConstantVector *ty) { + unsigned length = ty->getType()->getNumElements(); + assert(ty->getType()->getElementType()->isDoubleTy() && + "only support for updating double type"); + llvm::Type *doubleTy = llvm::Type::getDoubleTy(ty->getContext()); + llvm::SmallVector result; + for (size_t i = 0; i < length; ++i) { + if (Random::getRandomBool()) { + result.push_back(llvm::ConstantFP::get(doubleTy, getRandomLLVMDouble())); + } else { + result.push_back(ty->getAggregateElement(i)); + } + } + return llvm::ConstantVector::get(result); +} + llvm::Constant *mutator_util::updateIntegerVector(llvm::ConstantVector *ty) { unsigned length = ty->getType()->getNumElements(); assert(ty->getType()->getElementType()->isIntegerTy() && diff --git a/tools/mutator-utils/util.h b/tools/mutator-utils/util.h index ef6f9df83..25300f555 100644 --- a/tools/mutator-utils/util.h +++ b/tools/mutator-utils/util.h @@ -342,4 +342,8 @@ class mutator_util { static llvm::ConstantRange getRandomLLVMConstantRange(llvm::IntegerType *ty); static llvm::Constant *getRandomLLVMIntegerVector(llvm::FixedVectorType *ty); static llvm::Constant *updateIntegerVector(llvm::ConstantVector *ty); + static llvm::Constant *getRandomLLVMFloatVector(llvm::FixedVectorType *ty); + static llvm::Constant *updateFloatVector(llvm::ConstantVector *ty); + static llvm::Constant *getRandomLLVMDoubleVector(llvm::FixedVectorType *ty); + static llvm::Constant *updateDoubleVector(llvm::ConstantVector *ty); }; \ No newline at end of file