Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/BinaryOperatorReplacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void BinaryOperatorReplacement::mutate(llvm::Instruction *instruction) {
auto lhs = instruction->getOperand(0);
auto rhs = instruction->getOperand(1);
auto replacement = llvm::BinaryOperator::Create(to, lhs, rhs, "");
replacement->setDebugLoc(instruction->getDebugLoc());
replacement->insertAfter(instruction);
instruction->replaceAllUsesWith(replacement);
instruction->eraseFromParent();
Expand Down
1 change: 1 addition & 0 deletions lib/CmpInstPredicateReplacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void CmpInstPredicateReplacement::mutate(llvm::Instruction *instruction) {
auto lhs = instruction->getOperand(0);
auto rhs = instruction->getOperand(1);
auto replacement = llvm::CmpInst::Create(cmpType, to, lhs, rhs, "");
replacement->setDebugLoc(instruction->getDebugLoc());
replacement->insertAfter(instruction);
instruction->replaceAllUsesWith(replacement);
instruction->eraseFromParent();
Expand Down
1 change: 1 addition & 0 deletions lib/IntrinsicReplacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void IntrinsicReplacement::mutate(llvm::Instruction *instruction) {
}

auto call = llvm::CallInst::Create(replacement, arguments, "");
call->setDebugLoc(instruction->getDebugLoc());
call->insertAfter(instruction);
instruction->replaceAllUsesWith(call);
instruction->eraseFromParent();
Expand Down
1 change: 1 addition & 0 deletions lib/NegateTrunkReplacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void NegateTruncReplacement::mutate(llvm::Instruction *instruction) {
llvm::TruncInst *truncInst = llvm::cast<llvm::TruncInst>(instruction);

auto notInstruction = llvm::BinaryOperator::CreateNot(truncInst, "not");
notInstruction->setDebugLoc(truncInst->getDebugLoc());
notInstruction->insertAfter(truncInst);

truncInst->replaceAllUsesWith(notInstruction);
Expand Down
1 change: 1 addition & 0 deletions lib/NegateXORReplacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ void NegateXORReplacement::mutate(llvm::Instruction *instruction) {
#endif
auto rhs = instruction->getOperand(1);
auto notInstruction = llvm::BinaryOperator::CreateNot(rhs, "not", position);
notInstruction->setDebugLoc(instruction->getDebugLoc());
instruction->setOperand(1, notInstruction);
}
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set (SOURCES
Mutations/CmpInstPredicateReplacementTests.cpp
Mutations/ConcurrentDescribeTests.cpp
Mutations/ConstantReplacementTests.cpp
Mutations/DebugLocationTests.cpp
Mutations/InstrinsicReplacementTests.cpp
Mutations/NegateTruncReplacementTests.cpp
Mutations/NegateXORReplacementTests.cpp
Expand Down
100 changes: 100 additions & 0 deletions tests/Mutations/DebugLocationTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <gtest/gtest.h>
#include <irm/irm.h>
#include <llvm/IR/DIBuilder.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/Module.h>

using namespace irm;

class DebugLocation : public ::testing::Test {
protected:
void SetUp() override {
auto i8 = llvm::Type::getInt8Ty(context);
auto i1 = llvm::Type::getInt1Ty(context);
auto type = llvm::FunctionType::get(llvm::Type::getVoidTy(context), { i8, i8, i1 }, false);
function = llvm::Function::Create(type, llvm::Function::ExternalLinkage, "test", module);

llvm::DIBuilder builder(module);
auto file = builder.createFile("test.c", "/");
#if LLVM_VERSION_MAJOR >= 22
auto unit = builder.createCompileUnit(
llvm::DISourceLanguageName(llvm::dwarf::DW_LANG_C), file, "", false, "", 0);
#else
auto unit = builder.createCompileUnit(llvm::dwarf::DW_LANG_C, file, "", false, "", 0);
#endif
auto subprogram = builder.createFunction(unit,
"test",
"test",
file,
1,
builder.createSubroutineType({}),
1,
llvm::DINode::FlagZero,
llvm::DISubprogram::SPFlagDefinition);
function->setSubprogram(subprogram);
builder.finalize();
location = llvm::DILocation::get(context, 7, 3, subprogram);

entry = llvm::BasicBlock::Create(context, "entry", function);
terminator = llvm::ReturnInst::Create(context, entry);
}

void expectAllLocated() {
for (auto &instruction : *entry) {
if (&instruction != terminator) {
EXPECT_EQ(instruction.getDebugLoc().get(), location);
}
}
}

llvm::Instruction *locate(llvm::Value *value) {
auto instruction = llvm::cast<llvm::Instruction>(value);
instruction->setDebugLoc(location);
return instruction;
}

llvm::LLVMContext context;
llvm::Module module{ "test", context };
llvm::Function *function = nullptr;
llvm::BasicBlock *entry = nullptr;
llvm::Instruction *terminator = nullptr;
llvm::DILocation *location = nullptr;
};

TEST_F(DebugLocation, binaryOperatorReplacement) {
llvm::IRBuilder<> builder(terminator);
auto add = locate(builder.CreateAdd(function->getArg(0), function->getArg(1)));
AddToSub().mutate(add);
expectAllLocated();
}

TEST_F(DebugLocation, cmpInstPredicateReplacement) {
llvm::IRBuilder<> builder(terminator);
auto cmp = locate(builder.CreateICmpEQ(function->getArg(0), function->getArg(1)));
ICMP_EQToICMP_NE().mutate(cmp);
expectAllLocated();
}

TEST_F(DebugLocation, intrinsicReplacement) {
llvm::IRBuilder<> builder(terminator);
auto sadd = locate(builder.CreateBinaryIntrinsic(
llvm::Intrinsic::sadd_with_overflow, function->getArg(0), function->getArg(1)));
sadd_with_overflowTossub_with_overflow().mutate(sadd);
expectAllLocated();
}

TEST_F(DebugLocation, negateTruncReplacement) {
llvm::IRBuilder<> builder(terminator);
auto trunc = locate(builder.CreateTrunc(function->getArg(0), builder.getInt1Ty()));
NegateTruncReplacement().mutate(trunc);
expectAllLocated();
}

TEST_F(DebugLocation, negateXORReplacement) {
llvm::IRBuilder<> builder(terminator);
auto xorInstruction = locate(builder.CreateXor(function->getArg(2), function->getArg(2)));
NegateXORReplacement().mutate(xorInstruction);
expectAllLocated();
}
Loading