10b57cec5SDimitry Andric //===-- IntegerDivision.cpp - Expand integer division ---------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains an implementation of 32bit and 64bit scalar integer
100b57cec5SDimitry Andric // division for targets that don't have native support. It's largely derived
110b57cec5SDimitry Andric // from compiler-rt's implementations of __udivsi3 and __udivmoddi4,
120b57cec5SDimitry Andric // but hand-tuned for targets that prefer less control flow.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric #include "llvm/Transforms/Utils/IntegerDivision.h"
170b57cec5SDimitry Andric #include "llvm/IR/Function.h"
180b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
190b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
200b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric #define DEBUG_TYPE "integer-division"
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric /// Generate code to compute the remainder of two signed integers. Returns the
270b57cec5SDimitry Andric /// remainder, which will have the sign of the dividend. Builder's insert point
280b57cec5SDimitry Andric /// should be pointing where the caller wants code generated, e.g. at the srem
290b57cec5SDimitry Andric /// instruction. This will generate a urem in the process, and Builder's insert
300b57cec5SDimitry Andric /// point will be pointing at the uren (if present, i.e. not folded), ready to
310b57cec5SDimitry Andric /// be expanded if the user wishes
generateSignedRemainderCode(Value * Dividend,Value * Divisor,IRBuilder<> & Builder)320b57cec5SDimitry Andric static Value *generateSignedRemainderCode(Value *Dividend, Value *Divisor,
330b57cec5SDimitry Andric IRBuilder<> &Builder) {
340b57cec5SDimitry Andric unsigned BitWidth = Dividend->getType()->getIntegerBitWidth();
35*bdd1243dSDimitry Andric ConstantInt *Shift = Builder.getIntN(BitWidth, BitWidth - 1);
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric // Following instructions are generated for both i32 (shift 31) and
380b57cec5SDimitry Andric // i64 (shift 63).
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric // ; %dividend_sgn = ashr i32 %dividend, 31
410b57cec5SDimitry Andric // ; %divisor_sgn = ashr i32 %divisor, 31
420b57cec5SDimitry Andric // ; %dvd_xor = xor i32 %dividend, %dividend_sgn
430b57cec5SDimitry Andric // ; %dvs_xor = xor i32 %divisor, %divisor_sgn
440b57cec5SDimitry Andric // ; %u_dividend = sub i32 %dvd_xor, %dividend_sgn
450b57cec5SDimitry Andric // ; %u_divisor = sub i32 %dvs_xor, %divisor_sgn
460b57cec5SDimitry Andric // ; %urem = urem i32 %dividend, %divisor
470b57cec5SDimitry Andric // ; %xored = xor i32 %urem, %dividend_sgn
480b57cec5SDimitry Andric // ; %srem = sub i32 %xored, %dividend_sgn
49*bdd1243dSDimitry Andric Dividend = Builder.CreateFreeze(Dividend);
50*bdd1243dSDimitry Andric Divisor = Builder.CreateFreeze(Divisor);
510b57cec5SDimitry Andric Value *DividendSign = Builder.CreateAShr(Dividend, Shift);
520b57cec5SDimitry Andric Value *DivisorSign = Builder.CreateAShr(Divisor, Shift);
530b57cec5SDimitry Andric Value *DvdXor = Builder.CreateXor(Dividend, DividendSign);
540b57cec5SDimitry Andric Value *DvsXor = Builder.CreateXor(Divisor, DivisorSign);
550b57cec5SDimitry Andric Value *UDividend = Builder.CreateSub(DvdXor, DividendSign);
560b57cec5SDimitry Andric Value *UDivisor = Builder.CreateSub(DvsXor, DivisorSign);
570b57cec5SDimitry Andric Value *URem = Builder.CreateURem(UDividend, UDivisor);
580b57cec5SDimitry Andric Value *Xored = Builder.CreateXor(URem, DividendSign);
590b57cec5SDimitry Andric Value *SRem = Builder.CreateSub(Xored, DividendSign);
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric if (Instruction *URemInst = dyn_cast<Instruction>(URem))
620b57cec5SDimitry Andric Builder.SetInsertPoint(URemInst);
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric return SRem;
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric /// Generate code to compute the remainder of two unsigned integers. Returns the
690b57cec5SDimitry Andric /// remainder. Builder's insert point should be pointing where the caller wants
700b57cec5SDimitry Andric /// code generated, e.g. at the urem instruction. This will generate a udiv in
710b57cec5SDimitry Andric /// the process, and Builder's insert point will be pointing at the udiv (if
720b57cec5SDimitry Andric /// present, i.e. not folded), ready to be expanded if the user wishes
generatedUnsignedRemainderCode(Value * Dividend,Value * Divisor,IRBuilder<> & Builder)730b57cec5SDimitry Andric static Value *generatedUnsignedRemainderCode(Value *Dividend, Value *Divisor,
740b57cec5SDimitry Andric IRBuilder<> &Builder) {
750b57cec5SDimitry Andric // Remainder = Dividend - Quotient*Divisor
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric // Following instructions are generated for both i32 and i64
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric // ; %quotient = udiv i32 %dividend, %divisor
800b57cec5SDimitry Andric // ; %product = mul i32 %divisor, %quotient
810b57cec5SDimitry Andric // ; %remainder = sub i32 %dividend, %product
82*bdd1243dSDimitry Andric Dividend = Builder.CreateFreeze(Dividend);
83*bdd1243dSDimitry Andric Divisor = Builder.CreateFreeze(Divisor);
840b57cec5SDimitry Andric Value *Quotient = Builder.CreateUDiv(Dividend, Divisor);
850b57cec5SDimitry Andric Value *Product = Builder.CreateMul(Divisor, Quotient);
860b57cec5SDimitry Andric Value *Remainder = Builder.CreateSub(Dividend, Product);
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric if (Instruction *UDiv = dyn_cast<Instruction>(Quotient))
890b57cec5SDimitry Andric Builder.SetInsertPoint(UDiv);
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric return Remainder;
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric /// Generate code to divide two signed integers. Returns the quotient, rounded
950b57cec5SDimitry Andric /// towards 0. Builder's insert point should be pointing where the caller wants
960b57cec5SDimitry Andric /// code generated, e.g. at the sdiv instruction. This will generate a udiv in
970b57cec5SDimitry Andric /// the process, and Builder's insert point will be pointing at the udiv (if
980b57cec5SDimitry Andric /// present, i.e. not folded), ready to be expanded if the user wishes.
generateSignedDivisionCode(Value * Dividend,Value * Divisor,IRBuilder<> & Builder)990b57cec5SDimitry Andric static Value *generateSignedDivisionCode(Value *Dividend, Value *Divisor,
1000b57cec5SDimitry Andric IRBuilder<> &Builder) {
1010b57cec5SDimitry Andric // Implementation taken from compiler-rt's __divsi3 and __divdi3
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric unsigned BitWidth = Dividend->getType()->getIntegerBitWidth();
104*bdd1243dSDimitry Andric ConstantInt *Shift = Builder.getIntN(BitWidth, BitWidth - 1);
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric // Following instructions are generated for both i32 (shift 31) and
1070b57cec5SDimitry Andric // i64 (shift 63).
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric // ; %tmp = ashr i32 %dividend, 31
1100b57cec5SDimitry Andric // ; %tmp1 = ashr i32 %divisor, 31
1110b57cec5SDimitry Andric // ; %tmp2 = xor i32 %tmp, %dividend
1120b57cec5SDimitry Andric // ; %u_dvnd = sub nsw i32 %tmp2, %tmp
1130b57cec5SDimitry Andric // ; %tmp3 = xor i32 %tmp1, %divisor
1140b57cec5SDimitry Andric // ; %u_dvsr = sub nsw i32 %tmp3, %tmp1
1150b57cec5SDimitry Andric // ; %q_sgn = xor i32 %tmp1, %tmp
1160b57cec5SDimitry Andric // ; %q_mag = udiv i32 %u_dvnd, %u_dvsr
1170b57cec5SDimitry Andric // ; %tmp4 = xor i32 %q_mag, %q_sgn
1180b57cec5SDimitry Andric // ; %q = sub i32 %tmp4, %q_sgn
119*bdd1243dSDimitry Andric Dividend = Builder.CreateFreeze(Dividend);
120*bdd1243dSDimitry Andric Divisor = Builder.CreateFreeze(Divisor);
1210b57cec5SDimitry Andric Value *Tmp = Builder.CreateAShr(Dividend, Shift);
1220b57cec5SDimitry Andric Value *Tmp1 = Builder.CreateAShr(Divisor, Shift);
1230b57cec5SDimitry Andric Value *Tmp2 = Builder.CreateXor(Tmp, Dividend);
1240b57cec5SDimitry Andric Value *U_Dvnd = Builder.CreateSub(Tmp2, Tmp);
1250b57cec5SDimitry Andric Value *Tmp3 = Builder.CreateXor(Tmp1, Divisor);
1260b57cec5SDimitry Andric Value *U_Dvsr = Builder.CreateSub(Tmp3, Tmp1);
1270b57cec5SDimitry Andric Value *Q_Sgn = Builder.CreateXor(Tmp1, Tmp);
1280b57cec5SDimitry Andric Value *Q_Mag = Builder.CreateUDiv(U_Dvnd, U_Dvsr);
1290b57cec5SDimitry Andric Value *Tmp4 = Builder.CreateXor(Q_Mag, Q_Sgn);
1300b57cec5SDimitry Andric Value *Q = Builder.CreateSub(Tmp4, Q_Sgn);
1310b57cec5SDimitry Andric
1320b57cec5SDimitry Andric if (Instruction *UDiv = dyn_cast<Instruction>(Q_Mag))
1330b57cec5SDimitry Andric Builder.SetInsertPoint(UDiv);
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric return Q;
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric /// Generates code to divide two unsigned scalar 32-bit or 64-bit integers.
1390b57cec5SDimitry Andric /// Returns the quotient, rounded towards 0. Builder's insert point should
1400b57cec5SDimitry Andric /// point where the caller wants code generated, e.g. at the udiv instruction.
generateUnsignedDivisionCode(Value * Dividend,Value * Divisor,IRBuilder<> & Builder)1410b57cec5SDimitry Andric static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
1420b57cec5SDimitry Andric IRBuilder<> &Builder) {
1430b57cec5SDimitry Andric // The basic algorithm can be found in the compiler-rt project's
1440b57cec5SDimitry Andric // implementation of __udivsi3.c. Here, we do a lower-level IR based approach
1450b57cec5SDimitry Andric // that's been hand-tuned to lessen the amount of control flow involved.
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric // Some helper values
1480b57cec5SDimitry Andric IntegerType *DivTy = cast<IntegerType>(Dividend->getType());
1490b57cec5SDimitry Andric unsigned BitWidth = DivTy->getBitWidth();
1500b57cec5SDimitry Andric
151*bdd1243dSDimitry Andric ConstantInt *Zero = ConstantInt::get(DivTy, 0);
152*bdd1243dSDimitry Andric ConstantInt *One = ConstantInt::get(DivTy, 1);
153*bdd1243dSDimitry Andric ConstantInt *NegOne = ConstantInt::getSigned(DivTy, -1);
154*bdd1243dSDimitry Andric ConstantInt *MSB = ConstantInt::get(DivTy, BitWidth - 1);
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric ConstantInt *True = Builder.getTrue();
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andric BasicBlock *IBB = Builder.GetInsertBlock();
1590b57cec5SDimitry Andric Function *F = IBB->getParent();
1600b57cec5SDimitry Andric Function *CTLZ = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
1610b57cec5SDimitry Andric DivTy);
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric // Our CFG is going to look like:
1640b57cec5SDimitry Andric // +---------------------+
1650b57cec5SDimitry Andric // | special-cases |
1660b57cec5SDimitry Andric // | ... |
1670b57cec5SDimitry Andric // +---------------------+
1680b57cec5SDimitry Andric // | |
1690b57cec5SDimitry Andric // | +----------+
1700b57cec5SDimitry Andric // | | bb1 |
1710b57cec5SDimitry Andric // | | ... |
1720b57cec5SDimitry Andric // | +----------+
1730b57cec5SDimitry Andric // | | |
1740b57cec5SDimitry Andric // | | +------------+
1750b57cec5SDimitry Andric // | | | preheader |
1760b57cec5SDimitry Andric // | | | ... |
1770b57cec5SDimitry Andric // | | +------------+
1780b57cec5SDimitry Andric // | | |
1790b57cec5SDimitry Andric // | | | +---+
1800b57cec5SDimitry Andric // | | | | |
1810b57cec5SDimitry Andric // | | +------------+ |
1820b57cec5SDimitry Andric // | | | do-while | |
1830b57cec5SDimitry Andric // | | | ... | |
1840b57cec5SDimitry Andric // | | +------------+ |
1850b57cec5SDimitry Andric // | | | | |
1860b57cec5SDimitry Andric // | +-----------+ +---+
1870b57cec5SDimitry Andric // | | loop-exit |
1880b57cec5SDimitry Andric // | | ... |
1890b57cec5SDimitry Andric // | +-----------+
1900b57cec5SDimitry Andric // | |
1910b57cec5SDimitry Andric // +-------+
1920b57cec5SDimitry Andric // | ... |
1930b57cec5SDimitry Andric // | end |
1940b57cec5SDimitry Andric // +-------+
1950b57cec5SDimitry Andric BasicBlock *SpecialCases = Builder.GetInsertBlock();
1960b57cec5SDimitry Andric SpecialCases->setName(Twine(SpecialCases->getName(), "_udiv-special-cases"));
1970b57cec5SDimitry Andric BasicBlock *End = SpecialCases->splitBasicBlock(Builder.GetInsertPoint(),
1980b57cec5SDimitry Andric "udiv-end");
1990b57cec5SDimitry Andric BasicBlock *LoopExit = BasicBlock::Create(Builder.getContext(),
2000b57cec5SDimitry Andric "udiv-loop-exit", F, End);
2010b57cec5SDimitry Andric BasicBlock *DoWhile = BasicBlock::Create(Builder.getContext(),
2020b57cec5SDimitry Andric "udiv-do-while", F, End);
2030b57cec5SDimitry Andric BasicBlock *Preheader = BasicBlock::Create(Builder.getContext(),
2040b57cec5SDimitry Andric "udiv-preheader", F, End);
2050b57cec5SDimitry Andric BasicBlock *BB1 = BasicBlock::Create(Builder.getContext(),
2060b57cec5SDimitry Andric "udiv-bb1", F, End);
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andric // We'll be overwriting the terminator to insert our extra blocks
2090b57cec5SDimitry Andric SpecialCases->getTerminator()->eraseFromParent();
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric // Same instructions are generated for both i32 (msb 31) and i64 (msb 63).
2120b57cec5SDimitry Andric
2130b57cec5SDimitry Andric // First off, check for special cases: dividend or divisor is zero, divisor
2140b57cec5SDimitry Andric // is greater than dividend, and divisor is 1.
2150b57cec5SDimitry Andric // ; special-cases:
2160b57cec5SDimitry Andric // ; %ret0_1 = icmp eq i32 %divisor, 0
2170b57cec5SDimitry Andric // ; %ret0_2 = icmp eq i32 %dividend, 0
2180b57cec5SDimitry Andric // ; %ret0_3 = or i1 %ret0_1, %ret0_2
2190b57cec5SDimitry Andric // ; %tmp0 = tail call i32 @llvm.ctlz.i32(i32 %divisor, i1 true)
2200b57cec5SDimitry Andric // ; %tmp1 = tail call i32 @llvm.ctlz.i32(i32 %dividend, i1 true)
2210b57cec5SDimitry Andric // ; %sr = sub nsw i32 %tmp0, %tmp1
2220b57cec5SDimitry Andric // ; %ret0_4 = icmp ugt i32 %sr, 31
223*bdd1243dSDimitry Andric // ; %ret0 = select i1 %ret0_3, i1 true, i1 %ret0_4
2240b57cec5SDimitry Andric // ; %retDividend = icmp eq i32 %sr, 31
2250b57cec5SDimitry Andric // ; %retVal = select i1 %ret0, i32 0, i32 %dividend
226*bdd1243dSDimitry Andric // ; %earlyRet = select i1 %ret0, i1 true, %retDividend
2270b57cec5SDimitry Andric // ; br i1 %earlyRet, label %end, label %bb1
2280b57cec5SDimitry Andric Builder.SetInsertPoint(SpecialCases);
229*bdd1243dSDimitry Andric Divisor = Builder.CreateFreeze(Divisor);
230*bdd1243dSDimitry Andric Dividend = Builder.CreateFreeze(Dividend);
2310b57cec5SDimitry Andric Value *Ret0_1 = Builder.CreateICmpEQ(Divisor, Zero);
2320b57cec5SDimitry Andric Value *Ret0_2 = Builder.CreateICmpEQ(Dividend, Zero);
2330b57cec5SDimitry Andric Value *Ret0_3 = Builder.CreateOr(Ret0_1, Ret0_2);
2340b57cec5SDimitry Andric Value *Tmp0 = Builder.CreateCall(CTLZ, {Divisor, True});
2350b57cec5SDimitry Andric Value *Tmp1 = Builder.CreateCall(CTLZ, {Dividend, True});
2360b57cec5SDimitry Andric Value *SR = Builder.CreateSub(Tmp0, Tmp1);
2370b57cec5SDimitry Andric Value *Ret0_4 = Builder.CreateICmpUGT(SR, MSB);
238*bdd1243dSDimitry Andric Value *Ret0 = Builder.CreateLogicalOr(Ret0_3, Ret0_4);
2390b57cec5SDimitry Andric Value *RetDividend = Builder.CreateICmpEQ(SR, MSB);
2400b57cec5SDimitry Andric Value *RetVal = Builder.CreateSelect(Ret0, Zero, Dividend);
241*bdd1243dSDimitry Andric Value *EarlyRet = Builder.CreateLogicalOr(Ret0, RetDividend);
2420b57cec5SDimitry Andric Builder.CreateCondBr(EarlyRet, End, BB1);
2430b57cec5SDimitry Andric
2440b57cec5SDimitry Andric // ; bb1: ; preds = %special-cases
2450b57cec5SDimitry Andric // ; %sr_1 = add i32 %sr, 1
2460b57cec5SDimitry Andric // ; %tmp2 = sub i32 31, %sr
2470b57cec5SDimitry Andric // ; %q = shl i32 %dividend, %tmp2
2480b57cec5SDimitry Andric // ; %skipLoop = icmp eq i32 %sr_1, 0
2490b57cec5SDimitry Andric // ; br i1 %skipLoop, label %loop-exit, label %preheader
2500b57cec5SDimitry Andric Builder.SetInsertPoint(BB1);
2510b57cec5SDimitry Andric Value *SR_1 = Builder.CreateAdd(SR, One);
2520b57cec5SDimitry Andric Value *Tmp2 = Builder.CreateSub(MSB, SR);
2530b57cec5SDimitry Andric Value *Q = Builder.CreateShl(Dividend, Tmp2);
2540b57cec5SDimitry Andric Value *SkipLoop = Builder.CreateICmpEQ(SR_1, Zero);
2550b57cec5SDimitry Andric Builder.CreateCondBr(SkipLoop, LoopExit, Preheader);
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric // ; preheader: ; preds = %bb1
2580b57cec5SDimitry Andric // ; %tmp3 = lshr i32 %dividend, %sr_1
2590b57cec5SDimitry Andric // ; %tmp4 = add i32 %divisor, -1
2600b57cec5SDimitry Andric // ; br label %do-while
2610b57cec5SDimitry Andric Builder.SetInsertPoint(Preheader);
2620b57cec5SDimitry Andric Value *Tmp3 = Builder.CreateLShr(Dividend, SR_1);
2630b57cec5SDimitry Andric Value *Tmp4 = Builder.CreateAdd(Divisor, NegOne);
2640b57cec5SDimitry Andric Builder.CreateBr(DoWhile);
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric // ; do-while: ; preds = %do-while, %preheader
2670b57cec5SDimitry Andric // ; %carry_1 = phi i32 [ 0, %preheader ], [ %carry, %do-while ]
2680b57cec5SDimitry Andric // ; %sr_3 = phi i32 [ %sr_1, %preheader ], [ %sr_2, %do-while ]
2690b57cec5SDimitry Andric // ; %r_1 = phi i32 [ %tmp3, %preheader ], [ %r, %do-while ]
2700b57cec5SDimitry Andric // ; %q_2 = phi i32 [ %q, %preheader ], [ %q_1, %do-while ]
2710b57cec5SDimitry Andric // ; %tmp5 = shl i32 %r_1, 1
2720b57cec5SDimitry Andric // ; %tmp6 = lshr i32 %q_2, 31
2730b57cec5SDimitry Andric // ; %tmp7 = or i32 %tmp5, %tmp6
2740b57cec5SDimitry Andric // ; %tmp8 = shl i32 %q_2, 1
2750b57cec5SDimitry Andric // ; %q_1 = or i32 %carry_1, %tmp8
2760b57cec5SDimitry Andric // ; %tmp9 = sub i32 %tmp4, %tmp7
2770b57cec5SDimitry Andric // ; %tmp10 = ashr i32 %tmp9, 31
2780b57cec5SDimitry Andric // ; %carry = and i32 %tmp10, 1
2790b57cec5SDimitry Andric // ; %tmp11 = and i32 %tmp10, %divisor
2800b57cec5SDimitry Andric // ; %r = sub i32 %tmp7, %tmp11
2810b57cec5SDimitry Andric // ; %sr_2 = add i32 %sr_3, -1
2820b57cec5SDimitry Andric // ; %tmp12 = icmp eq i32 %sr_2, 0
2830b57cec5SDimitry Andric // ; br i1 %tmp12, label %loop-exit, label %do-while
2840b57cec5SDimitry Andric Builder.SetInsertPoint(DoWhile);
2850b57cec5SDimitry Andric PHINode *Carry_1 = Builder.CreatePHI(DivTy, 2);
2860b57cec5SDimitry Andric PHINode *SR_3 = Builder.CreatePHI(DivTy, 2);
2870b57cec5SDimitry Andric PHINode *R_1 = Builder.CreatePHI(DivTy, 2);
2880b57cec5SDimitry Andric PHINode *Q_2 = Builder.CreatePHI(DivTy, 2);
2890b57cec5SDimitry Andric Value *Tmp5 = Builder.CreateShl(R_1, One);
2900b57cec5SDimitry Andric Value *Tmp6 = Builder.CreateLShr(Q_2, MSB);
2910b57cec5SDimitry Andric Value *Tmp7 = Builder.CreateOr(Tmp5, Tmp6);
2920b57cec5SDimitry Andric Value *Tmp8 = Builder.CreateShl(Q_2, One);
2930b57cec5SDimitry Andric Value *Q_1 = Builder.CreateOr(Carry_1, Tmp8);
2940b57cec5SDimitry Andric Value *Tmp9 = Builder.CreateSub(Tmp4, Tmp7);
2950b57cec5SDimitry Andric Value *Tmp10 = Builder.CreateAShr(Tmp9, MSB);
2960b57cec5SDimitry Andric Value *Carry = Builder.CreateAnd(Tmp10, One);
2970b57cec5SDimitry Andric Value *Tmp11 = Builder.CreateAnd(Tmp10, Divisor);
2980b57cec5SDimitry Andric Value *R = Builder.CreateSub(Tmp7, Tmp11);
2990b57cec5SDimitry Andric Value *SR_2 = Builder.CreateAdd(SR_3, NegOne);
3000b57cec5SDimitry Andric Value *Tmp12 = Builder.CreateICmpEQ(SR_2, Zero);
3010b57cec5SDimitry Andric Builder.CreateCondBr(Tmp12, LoopExit, DoWhile);
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric // ; loop-exit: ; preds = %do-while, %bb1
3040b57cec5SDimitry Andric // ; %carry_2 = phi i32 [ 0, %bb1 ], [ %carry, %do-while ]
3050b57cec5SDimitry Andric // ; %q_3 = phi i32 [ %q, %bb1 ], [ %q_1, %do-while ]
3060b57cec5SDimitry Andric // ; %tmp13 = shl i32 %q_3, 1
3070b57cec5SDimitry Andric // ; %q_4 = or i32 %carry_2, %tmp13
3080b57cec5SDimitry Andric // ; br label %end
3090b57cec5SDimitry Andric Builder.SetInsertPoint(LoopExit);
3100b57cec5SDimitry Andric PHINode *Carry_2 = Builder.CreatePHI(DivTy, 2);
3110b57cec5SDimitry Andric PHINode *Q_3 = Builder.CreatePHI(DivTy, 2);
3120b57cec5SDimitry Andric Value *Tmp13 = Builder.CreateShl(Q_3, One);
3130b57cec5SDimitry Andric Value *Q_4 = Builder.CreateOr(Carry_2, Tmp13);
3140b57cec5SDimitry Andric Builder.CreateBr(End);
3150b57cec5SDimitry Andric
3160b57cec5SDimitry Andric // ; end: ; preds = %loop-exit, %special-cases
3170b57cec5SDimitry Andric // ; %q_5 = phi i32 [ %q_4, %loop-exit ], [ %retVal, %special-cases ]
3180b57cec5SDimitry Andric // ; ret i32 %q_5
3190b57cec5SDimitry Andric Builder.SetInsertPoint(End, End->begin());
3200b57cec5SDimitry Andric PHINode *Q_5 = Builder.CreatePHI(DivTy, 2);
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andric // Populate the Phis, since all values have now been created. Our Phis were:
3230b57cec5SDimitry Andric // ; %carry_1 = phi i32 [ 0, %preheader ], [ %carry, %do-while ]
3240b57cec5SDimitry Andric Carry_1->addIncoming(Zero, Preheader);
3250b57cec5SDimitry Andric Carry_1->addIncoming(Carry, DoWhile);
3260b57cec5SDimitry Andric // ; %sr_3 = phi i32 [ %sr_1, %preheader ], [ %sr_2, %do-while ]
3270b57cec5SDimitry Andric SR_3->addIncoming(SR_1, Preheader);
3280b57cec5SDimitry Andric SR_3->addIncoming(SR_2, DoWhile);
3290b57cec5SDimitry Andric // ; %r_1 = phi i32 [ %tmp3, %preheader ], [ %r, %do-while ]
3300b57cec5SDimitry Andric R_1->addIncoming(Tmp3, Preheader);
3310b57cec5SDimitry Andric R_1->addIncoming(R, DoWhile);
3320b57cec5SDimitry Andric // ; %q_2 = phi i32 [ %q, %preheader ], [ %q_1, %do-while ]
3330b57cec5SDimitry Andric Q_2->addIncoming(Q, Preheader);
3340b57cec5SDimitry Andric Q_2->addIncoming(Q_1, DoWhile);
3350b57cec5SDimitry Andric // ; %carry_2 = phi i32 [ 0, %bb1 ], [ %carry, %do-while ]
3360b57cec5SDimitry Andric Carry_2->addIncoming(Zero, BB1);
3370b57cec5SDimitry Andric Carry_2->addIncoming(Carry, DoWhile);
3380b57cec5SDimitry Andric // ; %q_3 = phi i32 [ %q, %bb1 ], [ %q_1, %do-while ]
3390b57cec5SDimitry Andric Q_3->addIncoming(Q, BB1);
3400b57cec5SDimitry Andric Q_3->addIncoming(Q_1, DoWhile);
3410b57cec5SDimitry Andric // ; %q_5 = phi i32 [ %q_4, %loop-exit ], [ %retVal, %special-cases ]
3420b57cec5SDimitry Andric Q_5->addIncoming(Q_4, LoopExit);
3430b57cec5SDimitry Andric Q_5->addIncoming(RetVal, SpecialCases);
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric return Q_5;
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andric /// Generate code to calculate the remainder of two integers, replacing Rem with
3490b57cec5SDimitry Andric /// the generated code. This currently generates code using the udiv expansion,
3500b57cec5SDimitry Andric /// but future work includes generating more specialized code, e.g. when more
351*bdd1243dSDimitry Andric /// information about the operands are known.
3520b57cec5SDimitry Andric ///
3530b57cec5SDimitry Andric /// Replace Rem with generated code.
expandRemainder(BinaryOperator * Rem)3540b57cec5SDimitry Andric bool llvm::expandRemainder(BinaryOperator *Rem) {
3550b57cec5SDimitry Andric assert((Rem->getOpcode() == Instruction::SRem ||
3560b57cec5SDimitry Andric Rem->getOpcode() == Instruction::URem) &&
3570b57cec5SDimitry Andric "Trying to expand remainder from a non-remainder function");
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andric IRBuilder<> Builder(Rem);
3600b57cec5SDimitry Andric
3610b57cec5SDimitry Andric assert(!Rem->getType()->isVectorTy() && "Div over vectors not supported");
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric // First prepare the sign if it's a signed remainder
3640b57cec5SDimitry Andric if (Rem->getOpcode() == Instruction::SRem) {
3650b57cec5SDimitry Andric Value *Remainder = generateSignedRemainderCode(Rem->getOperand(0),
3660b57cec5SDimitry Andric Rem->getOperand(1), Builder);
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric // Check whether this is the insert point while Rem is still valid.
3690b57cec5SDimitry Andric bool IsInsertPoint = Rem->getIterator() == Builder.GetInsertPoint();
3700b57cec5SDimitry Andric Rem->replaceAllUsesWith(Remainder);
3710b57cec5SDimitry Andric Rem->dropAllReferences();
3720b57cec5SDimitry Andric Rem->eraseFromParent();
3730b57cec5SDimitry Andric
3740b57cec5SDimitry Andric // If we didn't actually generate an urem instruction, we're done
3750b57cec5SDimitry Andric // This happens for example if the input were constant. In this case the
3760b57cec5SDimitry Andric // Builder insertion point was unchanged
3770b57cec5SDimitry Andric if (IsInsertPoint)
3780b57cec5SDimitry Andric return true;
3790b57cec5SDimitry Andric
3800b57cec5SDimitry Andric BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
3810b57cec5SDimitry Andric Rem = BO;
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric
3840b57cec5SDimitry Andric Value *Remainder = generatedUnsignedRemainderCode(Rem->getOperand(0),
3850b57cec5SDimitry Andric Rem->getOperand(1),
3860b57cec5SDimitry Andric Builder);
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andric Rem->replaceAllUsesWith(Remainder);
3890b57cec5SDimitry Andric Rem->dropAllReferences();
3900b57cec5SDimitry Andric Rem->eraseFromParent();
3910b57cec5SDimitry Andric
3920b57cec5SDimitry Andric // Expand the udiv
3930b57cec5SDimitry Andric if (BinaryOperator *UDiv = dyn_cast<BinaryOperator>(Builder.GetInsertPoint())) {
3940b57cec5SDimitry Andric assert(UDiv->getOpcode() == Instruction::UDiv && "Non-udiv in expansion?");
3950b57cec5SDimitry Andric expandDivision(UDiv);
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andric return true;
3990b57cec5SDimitry Andric }
4000b57cec5SDimitry Andric
4010b57cec5SDimitry Andric /// Generate code to divide two integers, replacing Div with the generated
4020b57cec5SDimitry Andric /// code. This currently generates code similarly to compiler-rt's
4030b57cec5SDimitry Andric /// implementations, but future work includes generating more specialized code
404*bdd1243dSDimitry Andric /// when more information about the operands are known.
4050b57cec5SDimitry Andric ///
4060b57cec5SDimitry Andric /// Replace Div with generated code.
expandDivision(BinaryOperator * Div)4070b57cec5SDimitry Andric bool llvm::expandDivision(BinaryOperator *Div) {
4080b57cec5SDimitry Andric assert((Div->getOpcode() == Instruction::SDiv ||
4090b57cec5SDimitry Andric Div->getOpcode() == Instruction::UDiv) &&
4100b57cec5SDimitry Andric "Trying to expand division from a non-division function");
4110b57cec5SDimitry Andric
4120b57cec5SDimitry Andric IRBuilder<> Builder(Div);
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andric assert(!Div->getType()->isVectorTy() && "Div over vectors not supported");
4150b57cec5SDimitry Andric
4160b57cec5SDimitry Andric // First prepare the sign if it's a signed division
4170b57cec5SDimitry Andric if (Div->getOpcode() == Instruction::SDiv) {
4180b57cec5SDimitry Andric // Lower the code to unsigned division, and reset Div to point to the udiv.
4190b57cec5SDimitry Andric Value *Quotient = generateSignedDivisionCode(Div->getOperand(0),
4200b57cec5SDimitry Andric Div->getOperand(1), Builder);
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric // Check whether this is the insert point while Div is still valid.
4230b57cec5SDimitry Andric bool IsInsertPoint = Div->getIterator() == Builder.GetInsertPoint();
4240b57cec5SDimitry Andric Div->replaceAllUsesWith(Quotient);
4250b57cec5SDimitry Andric Div->dropAllReferences();
4260b57cec5SDimitry Andric Div->eraseFromParent();
4270b57cec5SDimitry Andric
4280b57cec5SDimitry Andric // If we didn't actually generate an udiv instruction, we're done
4290b57cec5SDimitry Andric // This happens for example if the input were constant. In this case the
4300b57cec5SDimitry Andric // Builder insertion point was unchanged
4310b57cec5SDimitry Andric if (IsInsertPoint)
4320b57cec5SDimitry Andric return true;
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andric BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
4350b57cec5SDimitry Andric Div = BO;
4360b57cec5SDimitry Andric }
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andric // Insert the unsigned division code
4390b57cec5SDimitry Andric Value *Quotient = generateUnsignedDivisionCode(Div->getOperand(0),
4400b57cec5SDimitry Andric Div->getOperand(1),
4410b57cec5SDimitry Andric Builder);
4420b57cec5SDimitry Andric Div->replaceAllUsesWith(Quotient);
4430b57cec5SDimitry Andric Div->dropAllReferences();
4440b57cec5SDimitry Andric Div->eraseFromParent();
4450b57cec5SDimitry Andric
4460b57cec5SDimitry Andric return true;
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric
4490b57cec5SDimitry Andric /// Generate code to compute the remainder of two integers of bitwidth up to
4500b57cec5SDimitry Andric /// 32 bits. Uses the above routines and extends the inputs/truncates the
4510b57cec5SDimitry Andric /// outputs to operate in 32 bits; that is, these routines are good for targets
4520b57cec5SDimitry Andric /// that have no or very little suppport for smaller than 32 bit integer
4530b57cec5SDimitry Andric /// arithmetic.
4540b57cec5SDimitry Andric ///
4550b57cec5SDimitry Andric /// Replace Rem with emulation code.
expandRemainderUpTo32Bits(BinaryOperator * Rem)4560b57cec5SDimitry Andric bool llvm::expandRemainderUpTo32Bits(BinaryOperator *Rem) {
4570b57cec5SDimitry Andric assert((Rem->getOpcode() == Instruction::SRem ||
4580b57cec5SDimitry Andric Rem->getOpcode() == Instruction::URem) &&
4590b57cec5SDimitry Andric "Trying to expand remainder from a non-remainder function");
4600b57cec5SDimitry Andric
4610b57cec5SDimitry Andric Type *RemTy = Rem->getType();
4620b57cec5SDimitry Andric assert(!RemTy->isVectorTy() && "Div over vectors not supported");
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andric unsigned RemTyBitWidth = RemTy->getIntegerBitWidth();
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric assert(RemTyBitWidth <= 32 &&
4670b57cec5SDimitry Andric "Div of bitwidth greater than 32 not supported");
4680b57cec5SDimitry Andric
4690b57cec5SDimitry Andric if (RemTyBitWidth == 32)
4700b57cec5SDimitry Andric return expandRemainder(Rem);
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andric // If bitwidth smaller than 32 extend inputs, extend output and proceed
4730b57cec5SDimitry Andric // with 32 bit division.
4740b57cec5SDimitry Andric IRBuilder<> Builder(Rem);
4750b57cec5SDimitry Andric
4760b57cec5SDimitry Andric Value *ExtDividend;
4770b57cec5SDimitry Andric Value *ExtDivisor;
4780b57cec5SDimitry Andric Value *ExtRem;
4790b57cec5SDimitry Andric Value *Trunc;
4800b57cec5SDimitry Andric Type *Int32Ty = Builder.getInt32Ty();
4810b57cec5SDimitry Andric
4820b57cec5SDimitry Andric if (Rem->getOpcode() == Instruction::SRem) {
4830b57cec5SDimitry Andric ExtDividend = Builder.CreateSExt(Rem->getOperand(0), Int32Ty);
4840b57cec5SDimitry Andric ExtDivisor = Builder.CreateSExt(Rem->getOperand(1), Int32Ty);
4850b57cec5SDimitry Andric ExtRem = Builder.CreateSRem(ExtDividend, ExtDivisor);
4860b57cec5SDimitry Andric } else {
4870b57cec5SDimitry Andric ExtDividend = Builder.CreateZExt(Rem->getOperand(0), Int32Ty);
4880b57cec5SDimitry Andric ExtDivisor = Builder.CreateZExt(Rem->getOperand(1), Int32Ty);
4890b57cec5SDimitry Andric ExtRem = Builder.CreateURem(ExtDividend, ExtDivisor);
4900b57cec5SDimitry Andric }
4910b57cec5SDimitry Andric Trunc = Builder.CreateTrunc(ExtRem, RemTy);
4920b57cec5SDimitry Andric
4930b57cec5SDimitry Andric Rem->replaceAllUsesWith(Trunc);
4940b57cec5SDimitry Andric Rem->dropAllReferences();
4950b57cec5SDimitry Andric Rem->eraseFromParent();
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andric return expandRemainder(cast<BinaryOperator>(ExtRem));
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric
5000b57cec5SDimitry Andric /// Generate code to compute the remainder of two integers of bitwidth up to
5010b57cec5SDimitry Andric /// 64 bits. Uses the above routines and extends the inputs/truncates the
5020b57cec5SDimitry Andric /// outputs to operate in 64 bits.
5030b57cec5SDimitry Andric ///
5040b57cec5SDimitry Andric /// Replace Rem with emulation code.
expandRemainderUpTo64Bits(BinaryOperator * Rem)5050b57cec5SDimitry Andric bool llvm::expandRemainderUpTo64Bits(BinaryOperator *Rem) {
5060b57cec5SDimitry Andric assert((Rem->getOpcode() == Instruction::SRem ||
5070b57cec5SDimitry Andric Rem->getOpcode() == Instruction::URem) &&
5080b57cec5SDimitry Andric "Trying to expand remainder from a non-remainder function");
5090b57cec5SDimitry Andric
5100b57cec5SDimitry Andric Type *RemTy = Rem->getType();
5110b57cec5SDimitry Andric assert(!RemTy->isVectorTy() && "Div over vectors not supported");
5120b57cec5SDimitry Andric
5130b57cec5SDimitry Andric unsigned RemTyBitWidth = RemTy->getIntegerBitWidth();
5140b57cec5SDimitry Andric
515*bdd1243dSDimitry Andric if (RemTyBitWidth >= 64)
5160b57cec5SDimitry Andric return expandRemainder(Rem);
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andric // If bitwidth smaller than 64 extend inputs, extend output and proceed
5190b57cec5SDimitry Andric // with 64 bit division.
5200b57cec5SDimitry Andric IRBuilder<> Builder(Rem);
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric Value *ExtDividend;
5230b57cec5SDimitry Andric Value *ExtDivisor;
5240b57cec5SDimitry Andric Value *ExtRem;
5250b57cec5SDimitry Andric Value *Trunc;
5260b57cec5SDimitry Andric Type *Int64Ty = Builder.getInt64Ty();
5270b57cec5SDimitry Andric
5280b57cec5SDimitry Andric if (Rem->getOpcode() == Instruction::SRem) {
5290b57cec5SDimitry Andric ExtDividend = Builder.CreateSExt(Rem->getOperand(0), Int64Ty);
5300b57cec5SDimitry Andric ExtDivisor = Builder.CreateSExt(Rem->getOperand(1), Int64Ty);
5310b57cec5SDimitry Andric ExtRem = Builder.CreateSRem(ExtDividend, ExtDivisor);
5320b57cec5SDimitry Andric } else {
5330b57cec5SDimitry Andric ExtDividend = Builder.CreateZExt(Rem->getOperand(0), Int64Ty);
5340b57cec5SDimitry Andric ExtDivisor = Builder.CreateZExt(Rem->getOperand(1), Int64Ty);
5350b57cec5SDimitry Andric ExtRem = Builder.CreateURem(ExtDividend, ExtDivisor);
5360b57cec5SDimitry Andric }
5370b57cec5SDimitry Andric Trunc = Builder.CreateTrunc(ExtRem, RemTy);
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric Rem->replaceAllUsesWith(Trunc);
5400b57cec5SDimitry Andric Rem->dropAllReferences();
5410b57cec5SDimitry Andric Rem->eraseFromParent();
5420b57cec5SDimitry Andric
5430b57cec5SDimitry Andric return expandRemainder(cast<BinaryOperator>(ExtRem));
5440b57cec5SDimitry Andric }
5450b57cec5SDimitry Andric
5460b57cec5SDimitry Andric /// Generate code to divide two integers of bitwidth up to 32 bits. Uses the
5470b57cec5SDimitry Andric /// above routines and extends the inputs/truncates the outputs to operate
5480b57cec5SDimitry Andric /// in 32 bits; that is, these routines are good for targets that have no
5490b57cec5SDimitry Andric /// or very little support for smaller than 32 bit integer arithmetic.
5500b57cec5SDimitry Andric ///
5510b57cec5SDimitry Andric /// Replace Div with emulation code.
expandDivisionUpTo32Bits(BinaryOperator * Div)5520b57cec5SDimitry Andric bool llvm::expandDivisionUpTo32Bits(BinaryOperator *Div) {
5530b57cec5SDimitry Andric assert((Div->getOpcode() == Instruction::SDiv ||
5540b57cec5SDimitry Andric Div->getOpcode() == Instruction::UDiv) &&
5550b57cec5SDimitry Andric "Trying to expand division from a non-division function");
5560b57cec5SDimitry Andric
5570b57cec5SDimitry Andric Type *DivTy = Div->getType();
5580b57cec5SDimitry Andric assert(!DivTy->isVectorTy() && "Div over vectors not supported");
5590b57cec5SDimitry Andric
5600b57cec5SDimitry Andric unsigned DivTyBitWidth = DivTy->getIntegerBitWidth();
5610b57cec5SDimitry Andric
5620b57cec5SDimitry Andric assert(DivTyBitWidth <= 32 && "Div of bitwidth greater than 32 not supported");
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andric if (DivTyBitWidth == 32)
5650b57cec5SDimitry Andric return expandDivision(Div);
5660b57cec5SDimitry Andric
5670b57cec5SDimitry Andric // If bitwidth smaller than 32 extend inputs, extend output and proceed
5680b57cec5SDimitry Andric // with 32 bit division.
5690b57cec5SDimitry Andric IRBuilder<> Builder(Div);
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric Value *ExtDividend;
5720b57cec5SDimitry Andric Value *ExtDivisor;
5730b57cec5SDimitry Andric Value *ExtDiv;
5740b57cec5SDimitry Andric Value *Trunc;
5750b57cec5SDimitry Andric Type *Int32Ty = Builder.getInt32Ty();
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andric if (Div->getOpcode() == Instruction::SDiv) {
5780b57cec5SDimitry Andric ExtDividend = Builder.CreateSExt(Div->getOperand(0), Int32Ty);
5790b57cec5SDimitry Andric ExtDivisor = Builder.CreateSExt(Div->getOperand(1), Int32Ty);
5800b57cec5SDimitry Andric ExtDiv = Builder.CreateSDiv(ExtDividend, ExtDivisor);
5810b57cec5SDimitry Andric } else {
5820b57cec5SDimitry Andric ExtDividend = Builder.CreateZExt(Div->getOperand(0), Int32Ty);
5830b57cec5SDimitry Andric ExtDivisor = Builder.CreateZExt(Div->getOperand(1), Int32Ty);
5840b57cec5SDimitry Andric ExtDiv = Builder.CreateUDiv(ExtDividend, ExtDivisor);
5850b57cec5SDimitry Andric }
5860b57cec5SDimitry Andric Trunc = Builder.CreateTrunc(ExtDiv, DivTy);
5870b57cec5SDimitry Andric
5880b57cec5SDimitry Andric Div->replaceAllUsesWith(Trunc);
5890b57cec5SDimitry Andric Div->dropAllReferences();
5900b57cec5SDimitry Andric Div->eraseFromParent();
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andric return expandDivision(cast<BinaryOperator>(ExtDiv));
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric
5950b57cec5SDimitry Andric /// Generate code to divide two integers of bitwidth up to 64 bits. Uses the
5960b57cec5SDimitry Andric /// above routines and extends the inputs/truncates the outputs to operate
5970b57cec5SDimitry Andric /// in 64 bits.
5980b57cec5SDimitry Andric ///
5990b57cec5SDimitry Andric /// Replace Div with emulation code.
expandDivisionUpTo64Bits(BinaryOperator * Div)6000b57cec5SDimitry Andric bool llvm::expandDivisionUpTo64Bits(BinaryOperator *Div) {
6010b57cec5SDimitry Andric assert((Div->getOpcode() == Instruction::SDiv ||
6020b57cec5SDimitry Andric Div->getOpcode() == Instruction::UDiv) &&
6030b57cec5SDimitry Andric "Trying to expand division from a non-division function");
6040b57cec5SDimitry Andric
6050b57cec5SDimitry Andric Type *DivTy = Div->getType();
6060b57cec5SDimitry Andric assert(!DivTy->isVectorTy() && "Div over vectors not supported");
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andric unsigned DivTyBitWidth = DivTy->getIntegerBitWidth();
6090b57cec5SDimitry Andric
610*bdd1243dSDimitry Andric if (DivTyBitWidth >= 64)
6110b57cec5SDimitry Andric return expandDivision(Div);
6120b57cec5SDimitry Andric
6130b57cec5SDimitry Andric // If bitwidth smaller than 64 extend inputs, extend output and proceed
6140b57cec5SDimitry Andric // with 64 bit division.
6150b57cec5SDimitry Andric IRBuilder<> Builder(Div);
6160b57cec5SDimitry Andric
6170b57cec5SDimitry Andric Value *ExtDividend;
6180b57cec5SDimitry Andric Value *ExtDivisor;
6190b57cec5SDimitry Andric Value *ExtDiv;
6200b57cec5SDimitry Andric Value *Trunc;
6210b57cec5SDimitry Andric Type *Int64Ty = Builder.getInt64Ty();
6220b57cec5SDimitry Andric
6230b57cec5SDimitry Andric if (Div->getOpcode() == Instruction::SDiv) {
6240b57cec5SDimitry Andric ExtDividend = Builder.CreateSExt(Div->getOperand(0), Int64Ty);
6250b57cec5SDimitry Andric ExtDivisor = Builder.CreateSExt(Div->getOperand(1), Int64Ty);
6260b57cec5SDimitry Andric ExtDiv = Builder.CreateSDiv(ExtDividend, ExtDivisor);
6270b57cec5SDimitry Andric } else {
6280b57cec5SDimitry Andric ExtDividend = Builder.CreateZExt(Div->getOperand(0), Int64Ty);
6290b57cec5SDimitry Andric ExtDivisor = Builder.CreateZExt(Div->getOperand(1), Int64Ty);
6300b57cec5SDimitry Andric ExtDiv = Builder.CreateUDiv(ExtDividend, ExtDivisor);
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric Trunc = Builder.CreateTrunc(ExtDiv, DivTy);
6330b57cec5SDimitry Andric
6340b57cec5SDimitry Andric Div->replaceAllUsesWith(Trunc);
6350b57cec5SDimitry Andric Div->dropAllReferences();
6360b57cec5SDimitry Andric Div->eraseFromParent();
6370b57cec5SDimitry Andric
6380b57cec5SDimitry Andric return expandDivision(cast<BinaryOperator>(ExtDiv));
6390b57cec5SDimitry Andric }
640