1 //===- IntegerDivision.cpp - Unit tests for the integer division code -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
22 TEST(IntegerDivision, SDiv) {
23 LLVMContext &C(getGlobalContext());
24 Module M("test division", C);
25 IRBuilder<> Builder(C);
27 SmallVector<Type*, 2> ArgTys(2, Builder.getInt32Ty());
28 Function *F = Function::Create(FunctionType::get(Builder.getInt32Ty(),
30 GlobalValue::ExternalLinkage, "F", &M);
31 assert(F->getArgumentList().size() == 2);
33 BasicBlock *BB = BasicBlock::Create(C, "", F);
34 Builder.SetInsertPoint(BB);
36 Function::arg_iterator AI = F->arg_begin();
40 Value *Div = Builder.CreateSDiv(A, B);
41 EXPECT_TRUE(BB->front().getOpcode() == Instruction::SDiv);
43 Value *Ret = Builder.CreateRet(Div);
45 expandDivision(cast<BinaryOperator>(Div));
46 EXPECT_TRUE(BB->front().getOpcode() == Instruction::AShr);
48 Instruction* Quotient = dyn_cast<Instruction>(cast<User>(Ret)->getOperand(0));
49 EXPECT_TRUE(Quotient && Quotient->getOpcode() == Instruction::Sub);
51 Builder.SetInsertPoint(BB->end());