Michael Ilseman | 02a6bab | 2012-09-25 01:33:33 +0000 | [diff] [blame] | 1 | //===- IntegerDivision.cpp - Unit tests for the integer division code -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "gtest/gtest.h" |
| 11 | #include "llvm/BasicBlock.h" |
| 12 | #include "llvm/GlobalValue.h" |
| 13 | #include "llvm/Function.h" |
| 14 | #include "llvm/IRBuilder.h" |
| 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Transforms/Utils/IntegerDivision.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | TEST(IntegerDivision, SDiv) { |
| 23 | LLVMContext &C(getGlobalContext()); |
| 24 | Module M("test division", C); |
| 25 | IRBuilder<> Builder(C); |
| 26 | |
| 27 | SmallVector<Type*, 2> ArgTys(2, Builder.getInt32Ty()); |
| 28 | Function *F = Function::Create(FunctionType::get(Builder.getInt32Ty(), |
| 29 | ArgTys, false), |
| 30 | GlobalValue::ExternalLinkage, "F", &M); |
| 31 | assert(F->getArgumentList().size() == 2); |
| 32 | |
| 33 | BasicBlock *BB = BasicBlock::Create(C, "", F); |
| 34 | Builder.SetInsertPoint(BB); |
| 35 | |
| 36 | Function::arg_iterator AI = F->arg_begin(); |
| 37 | Value *A = AI++; |
| 38 | Value *B = AI++; |
| 39 | |
| 40 | Value *Div = Builder.CreateSDiv(A, B); |
| 41 | EXPECT_TRUE(BB->front().getOpcode() == Instruction::SDiv); |
| 42 | |
| 43 | Value *Ret = Builder.CreateRet(Div); |
| 44 | |
| 45 | expandDivision(cast<BinaryOperator>(Div)); |
| 46 | EXPECT_TRUE(BB->front().getOpcode() == Instruction::AShr); |
| 47 | |
| 48 | Instruction* Quotient = dyn_cast<Instruction>(cast<User>(Ret)->getOperand(0)); |
| 49 | EXPECT_TRUE(Quotient && Quotient->getOpcode() == Instruction::Sub); |
| 50 | |
| 51 | Builder.SetInsertPoint(BB->end()); |
| 52 | } |
| 53 | |
Michael Ilseman | 02a6bab | 2012-09-25 01:33:33 +0000 | [diff] [blame] | 54 | } |