|
| 1 | +// |
| 2 | +// Copyright 2020 Mull Project |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | +#include <irm/irm.h> |
| 19 | +#include <llvm/IR/Module.h> |
| 20 | +#include "TestLLVMCompatibility.h" |
| 21 | + |
| 22 | +using namespace irm; |
| 23 | + |
| 24 | +TEST(NegateXORReplacement, canMutate) { |
| 25 | + using Mutator = NegateXORReplacement; |
| 26 | + |
| 27 | + llvm::LLVMContext context; |
| 28 | + llvm::Module module("test", context); |
| 29 | + auto type = llvm::FunctionType::get(llvm::Type::getVoidTy(context), false); |
| 30 | + auto function = test_llvm_compat::internalFunction(type, "test", module); |
| 31 | + auto basicBlock = llvm::BasicBlock::Create(context, "entry", function); |
| 32 | + |
| 33 | + auto op1 = llvm::ConstantInt::get(llvm::IntegerType::get(context, 1), 1, false); |
| 34 | + auto op2 = llvm::ConstantInt::get(llvm::IntegerType::get(context, 1), 0, false); |
| 35 | + auto xorInstruction = llvm::BinaryOperator::CreateXor(op1, op2, "xor", basicBlock); |
| 36 | + |
| 37 | + Mutator mutator; |
| 38 | + ASSERT_TRUE(mutator.canMutate(xorInstruction)); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(NegateXORReplacement, mutate) { |
| 42 | + using Mutator = NegateXORReplacement; |
| 43 | + |
| 44 | + llvm::LLVMContext context; |
| 45 | + llvm::Module module("test", context); |
| 46 | + |
| 47 | + auto type = llvm::FunctionType::get(llvm::Type::getVoidTy(context), false); |
| 48 | + auto function = test_llvm_compat::internalFunction(type, "test", module); |
| 49 | + auto basicBlock = llvm::BasicBlock::Create(context, "entry", function); |
| 50 | + |
| 51 | + auto op1 = llvm::ConstantInt::get(llvm::IntegerType::get(context, 1), 1, false); |
| 52 | + auto op2 = llvm::ConstantInt::get(llvm::IntegerType::get(context, 1), 0, false); |
| 53 | + auto xorInstruction = llvm::BinaryOperator::CreateXor(op1, op2, "xor", basicBlock); |
| 54 | + |
| 55 | + Mutator mutator; |
| 56 | + mutator.mutate(xorInstruction); |
| 57 | + |
| 58 | + /// We expect to have 2 xor instructions instead of one. See the implementation |
| 59 | + /// details of NegateXorReplacement.mutate(). |
| 60 | + /// Before: |
| 61 | + /// entry: |
| 62 | + /// %xor = xor i1 true, false |
| 63 | + /// |
| 64 | + /// After: |
| 65 | + /// entry: |
| 66 | + /// %not = xor i1 false, true |
| 67 | + /// %xor = xor i1 true, %not |
| 68 | + ASSERT_EQ(xorInstruction->getOpcode(), llvm::Instruction::Xor); |
| 69 | + ASSERT_EQ(xorInstruction->getOperand(0), op1); |
| 70 | + ASSERT_EQ(xorInstruction->getOperand(1), xorInstruction->getPrevNode()); |
| 71 | + ASSERT_EQ(xorInstruction->getPrevNode()->getOpcode(), llvm::Instruction::Xor); |
| 72 | + ASSERT_EQ(xorInstruction->getPrevNode()->getOperand(0), op2); |
| 73 | +} |
0 commit comments