1
0
mirror of https://github.com/c64scene-ar/llvm-6502.git synced 2024-12-24 22:32:47 +00:00
llvm-6502/unittests/Transforms/Utils/IntegerDivision.cpp
Chandler Carruth 0b8c9a80f2 Move all of the header files which are involved in modelling the LLVM IR
into their new header subdirectory: include/llvm/IR. This matches the
directory structure of lib, and begins to correct a long standing point
of file layout clutter in LLVM.

There are still more header files to move here, but I wanted to handle
them in separate commits to make tracking what files make sense at each
layer easier.

The only really questionable files here are the target intrinsic
tablegen files. But that's a battle I'd rather not fight today.

I've updated both CMake and Makefile build systems (I think, and my
tests think, but I may have missed something).

I've also re-sorted the includes throughout the project. I'll be
committing updates to Clang, DragonEgg, and Polly momentarily.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171366 91177308-0d34-0410-b5e6-96231b3b80d8
2013-01-02 11:36:10 +00:00

143 lines
4.6 KiB
C++

//===- IntegerDivision.cpp - Unit tests for the integer division code -----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/IntegerDivision.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(IntegerDivision, SDiv) {
LLVMContext &C(getGlobalContext());
Module M("test division", C);
IRBuilder<> Builder(C);
SmallVector<Type*, 2> ArgTys(2, Builder.getInt32Ty());
Function *F = Function::Create(FunctionType::get(Builder.getInt32Ty(),
ArgTys, false),
GlobalValue::ExternalLinkage, "F", &M);
assert(F->getArgumentList().size() == 2);
BasicBlock *BB = BasicBlock::Create(C, "", F);
Builder.SetInsertPoint(BB);
Function::arg_iterator AI = F->arg_begin();
Value *A = AI++;
Value *B = AI++;
Value *Div = Builder.CreateSDiv(A, B);
EXPECT_TRUE(BB->front().getOpcode() == Instruction::SDiv);
Value *Ret = Builder.CreateRet(Div);
expandDivision(cast<BinaryOperator>(Div));
EXPECT_TRUE(BB->front().getOpcode() == Instruction::AShr);
Instruction* Quotient = dyn_cast<Instruction>(cast<User>(Ret)->getOperand(0));
EXPECT_TRUE(Quotient && Quotient->getOpcode() == Instruction::Sub);
}
TEST(IntegerDivision, UDiv) {
LLVMContext &C(getGlobalContext());
Module M("test division", C);
IRBuilder<> Builder(C);
SmallVector<Type*, 2> ArgTys(2, Builder.getInt32Ty());
Function *F = Function::Create(FunctionType::get(Builder.getInt32Ty(),
ArgTys, false),
GlobalValue::ExternalLinkage, "F", &M);
assert(F->getArgumentList().size() == 2);
BasicBlock *BB = BasicBlock::Create(C, "", F);
Builder.SetInsertPoint(BB);
Function::arg_iterator AI = F->arg_begin();
Value *A = AI++;
Value *B = AI++;
Value *Div = Builder.CreateUDiv(A, B);
EXPECT_TRUE(BB->front().getOpcode() == Instruction::UDiv);
Value *Ret = Builder.CreateRet(Div);
expandDivision(cast<BinaryOperator>(Div));
EXPECT_TRUE(BB->front().getOpcode() == Instruction::ICmp);
Instruction* Quotient = dyn_cast<Instruction>(cast<User>(Ret)->getOperand(0));
EXPECT_TRUE(Quotient && Quotient->getOpcode() == Instruction::PHI);
}
TEST(IntegerDivision, SRem) {
LLVMContext &C(getGlobalContext());
Module M("test remainder", C);
IRBuilder<> Builder(C);
SmallVector<Type*, 2> ArgTys(2, Builder.getInt32Ty());
Function *F = Function::Create(FunctionType::get(Builder.getInt32Ty(),
ArgTys, false),
GlobalValue::ExternalLinkage, "F", &M);
assert(F->getArgumentList().size() == 2);
BasicBlock *BB = BasicBlock::Create(C, "", F);
Builder.SetInsertPoint(BB);
Function::arg_iterator AI = F->arg_begin();
Value *A = AI++;
Value *B = AI++;
Value *Rem = Builder.CreateSRem(A, B);
EXPECT_TRUE(BB->front().getOpcode() == Instruction::SRem);
Value *Ret = Builder.CreateRet(Rem);
expandRemainder(cast<BinaryOperator>(Rem));
EXPECT_TRUE(BB->front().getOpcode() == Instruction::AShr);
Instruction* Remainder = dyn_cast<Instruction>(cast<User>(Ret)->getOperand(0));
EXPECT_TRUE(Remainder && Remainder->getOpcode() == Instruction::Sub);
}
TEST(IntegerDivision, URem) {
LLVMContext &C(getGlobalContext());
Module M("test remainder", C);
IRBuilder<> Builder(C);
SmallVector<Type*, 2> ArgTys(2, Builder.getInt32Ty());
Function *F = Function::Create(FunctionType::get(Builder.getInt32Ty(),
ArgTys, false),
GlobalValue::ExternalLinkage, "F", &M);
assert(F->getArgumentList().size() == 2);
BasicBlock *BB = BasicBlock::Create(C, "", F);
Builder.SetInsertPoint(BB);
Function::arg_iterator AI = F->arg_begin();
Value *A = AI++;
Value *B = AI++;
Value *Rem = Builder.CreateURem(A, B);
EXPECT_TRUE(BB->front().getOpcode() == Instruction::URem);
Value *Ret = Builder.CreateRet(Rem);
expandRemainder(cast<BinaryOperator>(Rem));
EXPECT_TRUE(BB->front().getOpcode() == Instruction::ICmp);
Instruction* Remainder = dyn_cast<Instruction>(cast<User>(Ret)->getOperand(0));
EXPECT_TRUE(Remainder && Remainder->getOpcode() == Instruction::Sub);
}
}