1522600a2SDimitry Andric //===-- IntegerDivision.cpp - Expand integer division ---------------------===//
2522600a2SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6522600a2SDimitry Andric //
7522600a2SDimitry Andric //===----------------------------------------------------------------------===//
8522600a2SDimitry Andric //
95ca98fd9SDimitry Andric // This file contains an implementation of 32bit and 64bit scalar integer
105ca98fd9SDimitry Andric // division for targets that don't have native support. It's largely derived
115ca98fd9SDimitry Andric // from compiler-rt's implementations of __udivsi3 and __udivmoddi4,
125ca98fd9SDimitry Andric // but hand-tuned for targets that prefer less control flow.
13522600a2SDimitry Andric //
14522600a2SDimitry Andric //===----------------------------------------------------------------------===//
15522600a2SDimitry Andric
16522600a2SDimitry Andric #include "llvm/Transforms/Utils/IntegerDivision.h"
174a16efa3SDimitry Andric #include "llvm/IR/Function.h"
184a16efa3SDimitry Andric #include "llvm/IR/IRBuilder.h"
194a16efa3SDimitry Andric #include "llvm/IR/Instructions.h"
204a16efa3SDimitry Andric #include "llvm/IR/Intrinsics.h"
21522600a2SDimitry Andric
22522600a2SDimitry Andric using namespace llvm;
23522600a2SDimitry Andric
245ca98fd9SDimitry Andric #define DEBUG_TYPE "integer-division"
255ca98fd9SDimitry Andric
26522600a2SDimitry Andric /// Generate code to compute the remainder of two signed integers. Returns the
27522600a2SDimitry Andric /// remainder, which will have the sign of the dividend. Builder's insert point
28522600a2SDimitry Andric /// should be pointing where the caller wants code generated, e.g. at the srem
29522600a2SDimitry Andric /// instruction. This will generate a urem in the process, and Builder's insert
30522600a2SDimitry Andric /// point will be pointing at the uren (if present, i.e. not folded), ready to
31522600a2SDimitry Andric /// be expanded if the user wishes
generateSignedRemainderCode(Value * Dividend,Value * Divisor,IRBuilder<> & Builder)32522600a2SDimitry Andric static Value *generateSignedRemainderCode(Value *Dividend, Value *Divisor,
33522600a2SDimitry Andric IRBuilder<> &Builder) {
345ca98fd9SDimitry Andric unsigned BitWidth = Dividend->getType()->getIntegerBitWidth();
35e3b55780SDimitry Andric ConstantInt *Shift = Builder.getIntN(BitWidth, BitWidth - 1);
365ca98fd9SDimitry Andric
375ca98fd9SDimitry Andric // Following instructions are generated for both i32 (shift 31) and
385ca98fd9SDimitry Andric // i64 (shift 63).
39522600a2SDimitry Andric
40522600a2SDimitry Andric // ; %dividend_sgn = ashr i32 %dividend, 31
41522600a2SDimitry Andric // ; %divisor_sgn = ashr i32 %divisor, 31
42522600a2SDimitry Andric // ; %dvd_xor = xor i32 %dividend, %dividend_sgn
43522600a2SDimitry Andric // ; %dvs_xor = xor i32 %divisor, %divisor_sgn
44522600a2SDimitry Andric // ; %u_dividend = sub i32 %dvd_xor, %dividend_sgn
45522600a2SDimitry Andric // ; %u_divisor = sub i32 %dvs_xor, %divisor_sgn
46522600a2SDimitry Andric // ; %urem = urem i32 %dividend, %divisor
47522600a2SDimitry Andric // ; %xored = xor i32 %urem, %dividend_sgn
48522600a2SDimitry Andric // ; %srem = sub i32 %xored, %dividend_sgn
49e3b55780SDimitry Andric Dividend = Builder.CreateFreeze(Dividend);
50e3b55780SDimitry Andric Divisor = Builder.CreateFreeze(Divisor);
515ca98fd9SDimitry Andric Value *DividendSign = Builder.CreateAShr(Dividend, Shift);
525ca98fd9SDimitry Andric Value *DivisorSign = Builder.CreateAShr(Divisor, Shift);
53522600a2SDimitry Andric Value *DvdXor = Builder.CreateXor(Dividend, DividendSign);
54522600a2SDimitry Andric Value *DvsXor = Builder.CreateXor(Divisor, DivisorSign);
55522600a2SDimitry Andric Value *UDividend = Builder.CreateSub(DvdXor, DividendSign);
56522600a2SDimitry Andric Value *UDivisor = Builder.CreateSub(DvsXor, DivisorSign);
57522600a2SDimitry Andric Value *URem = Builder.CreateURem(UDividend, UDivisor);
58522600a2SDimitry Andric Value *Xored = Builder.CreateXor(URem, DividendSign);
59522600a2SDimitry Andric Value *SRem = Builder.CreateSub(Xored, DividendSign);
60522600a2SDimitry Andric
61522600a2SDimitry Andric if (Instruction *URemInst = dyn_cast<Instruction>(URem))
62522600a2SDimitry Andric Builder.SetInsertPoint(URemInst);
63522600a2SDimitry Andric
64522600a2SDimitry Andric return SRem;
65522600a2SDimitry Andric }
66522600a2SDimitry Andric
67522600a2SDimitry Andric
68522600a2SDimitry Andric /// Generate code to compute the remainder of two unsigned integers. Returns the
69522600a2SDimitry Andric /// remainder. Builder's insert point should be pointing where the caller wants
70522600a2SDimitry Andric /// code generated, e.g. at the urem instruction. This will generate a udiv in
71522600a2SDimitry Andric /// the process, and Builder's insert point will be pointing at the udiv (if
72522600a2SDimitry Andric /// present, i.e. not folded), ready to be expanded if the user wishes
generatedUnsignedRemainderCode(Value * Dividend,Value * Divisor,IRBuilder<> & Builder)73522600a2SDimitry Andric static Value *generatedUnsignedRemainderCode(Value *Dividend, Value *Divisor,
74522600a2SDimitry Andric IRBuilder<> &Builder) {
75522600a2SDimitry Andric // Remainder = Dividend - Quotient*Divisor
76522600a2SDimitry Andric
775ca98fd9SDimitry Andric // Following instructions are generated for both i32 and i64
785ca98fd9SDimitry Andric
79522600a2SDimitry Andric // ; %quotient = udiv i32 %dividend, %divisor
80522600a2SDimitry Andric // ; %product = mul i32 %divisor, %quotient
81522600a2SDimitry Andric // ; %remainder = sub i32 %dividend, %product
82e3b55780SDimitry Andric Dividend = Builder.CreateFreeze(Dividend);
83e3b55780SDimitry Andric Divisor = Builder.CreateFreeze(Divisor);
84522600a2SDimitry Andric Value *Quotient = Builder.CreateUDiv(Dividend, Divisor);
85522600a2SDimitry Andric Value *Product = Builder.CreateMul(Divisor, Quotient);
86522600a2SDimitry Andric Value *Remainder = Builder.CreateSub(Dividend, Product);
87522600a2SDimitry Andric
88522600a2SDimitry Andric if (Instruction *UDiv = dyn_cast<Instruction>(Quotient))
89522600a2SDimitry Andric Builder.SetInsertPoint(UDiv);
90522600a2SDimitry Andric
91522600a2SDimitry Andric return Remainder;
92522600a2SDimitry Andric }
93522600a2SDimitry Andric
94522600a2SDimitry Andric /// Generate code to divide two signed integers. Returns the quotient, rounded
95522600a2SDimitry Andric /// towards 0. Builder's insert point should be pointing where the caller wants
96522600a2SDimitry Andric /// code generated, e.g. at the sdiv instruction. This will generate a udiv in
97522600a2SDimitry Andric /// the process, and Builder's insert point will be pointing at the udiv (if
98522600a2SDimitry Andric /// present, i.e. not folded), ready to be expanded if the user wishes.
generateSignedDivisionCode(Value * Dividend,Value * Divisor,IRBuilder<> & Builder)99522600a2SDimitry Andric static Value *generateSignedDivisionCode(Value *Dividend, Value *Divisor,
100522600a2SDimitry Andric IRBuilder<> &Builder) {
1015ca98fd9SDimitry Andric // Implementation taken from compiler-rt's __divsi3 and __divdi3
102522600a2SDimitry Andric
1035ca98fd9SDimitry Andric unsigned BitWidth = Dividend->getType()->getIntegerBitWidth();
104e3b55780SDimitry Andric ConstantInt *Shift = Builder.getIntN(BitWidth, BitWidth - 1);
1055ca98fd9SDimitry Andric
1065ca98fd9SDimitry Andric // Following instructions are generated for both i32 (shift 31) and
1075ca98fd9SDimitry Andric // i64 (shift 63).
108522600a2SDimitry Andric
109522600a2SDimitry Andric // ; %tmp = ashr i32 %dividend, 31
110522600a2SDimitry Andric // ; %tmp1 = ashr i32 %divisor, 31
111522600a2SDimitry Andric // ; %tmp2 = xor i32 %tmp, %dividend
112522600a2SDimitry Andric // ; %u_dvnd = sub nsw i32 %tmp2, %tmp
113522600a2SDimitry Andric // ; %tmp3 = xor i32 %tmp1, %divisor
114522600a2SDimitry Andric // ; %u_dvsr = sub nsw i32 %tmp3, %tmp1
115522600a2SDimitry Andric // ; %q_sgn = xor i32 %tmp1, %tmp
116522600a2SDimitry Andric // ; %q_mag = udiv i32 %u_dvnd, %u_dvsr
117522600a2SDimitry Andric // ; %tmp4 = xor i32 %q_mag, %q_sgn
118522600a2SDimitry Andric // ; %q = sub i32 %tmp4, %q_sgn
119e3b55780SDimitry Andric Dividend = Builder.CreateFreeze(Dividend);
120e3b55780SDimitry Andric Divisor = Builder.CreateFreeze(Divisor);
1215ca98fd9SDimitry Andric Value *Tmp = Builder.CreateAShr(Dividend, Shift);
1225ca98fd9SDimitry Andric Value *Tmp1 = Builder.CreateAShr(Divisor, Shift);
123522600a2SDimitry Andric Value *Tmp2 = Builder.CreateXor(Tmp, Dividend);
124522600a2SDimitry Andric Value *U_Dvnd = Builder.CreateSub(Tmp2, Tmp);
125522600a2SDimitry Andric Value *Tmp3 = Builder.CreateXor(Tmp1, Divisor);
126522600a2SDimitry Andric Value *U_Dvsr = Builder.CreateSub(Tmp3, Tmp1);
127522600a2SDimitry Andric Value *Q_Sgn = Builder.CreateXor(Tmp1, Tmp);
128522600a2SDimitry Andric Value *Q_Mag = Builder.CreateUDiv(U_Dvnd, U_Dvsr);
129522600a2SDimitry Andric Value *Tmp4 = Builder.CreateXor(Q_Mag, Q_Sgn);
130522600a2SDimitry Andric Value *Q = Builder.CreateSub(Tmp4, Q_Sgn);
131522600a2SDimitry Andric
132522600a2SDimitry Andric if (Instruction *UDiv = dyn_cast<Instruction>(Q_Mag))
133522600a2SDimitry Andric Builder.SetInsertPoint(UDiv);
134522600a2SDimitry Andric
135522600a2SDimitry Andric return Q;
136522600a2SDimitry Andric }
137522600a2SDimitry Andric
1385ca98fd9SDimitry Andric /// Generates code to divide two unsigned scalar 32-bit or 64-bit integers.
1395ca98fd9SDimitry Andric /// Returns the quotient, rounded towards 0. Builder's insert point should
1405ca98fd9SDimitry Andric /// point where the caller wants code generated, e.g. at the udiv instruction.
generateUnsignedDivisionCode(Value * Dividend,Value * Divisor,IRBuilder<> & Builder)141522600a2SDimitry Andric static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
142522600a2SDimitry Andric IRBuilder<> &Builder) {
143522600a2SDimitry Andric // The basic algorithm can be found in the compiler-rt project's
144522600a2SDimitry Andric // implementation of __udivsi3.c. Here, we do a lower-level IR based approach
145522600a2SDimitry Andric // that's been hand-tuned to lessen the amount of control flow involved.
146522600a2SDimitry Andric
147522600a2SDimitry Andric // Some helper values
1485ca98fd9SDimitry Andric IntegerType *DivTy = cast<IntegerType>(Dividend->getType());
1495ca98fd9SDimitry Andric unsigned BitWidth = DivTy->getBitWidth();
150522600a2SDimitry Andric
151e3b55780SDimitry Andric ConstantInt *Zero = ConstantInt::get(DivTy, 0);
152e3b55780SDimitry Andric ConstantInt *One = ConstantInt::get(DivTy, 1);
153e3b55780SDimitry Andric ConstantInt *NegOne = ConstantInt::getSigned(DivTy, -1);
154e3b55780SDimitry Andric ConstantInt *MSB = ConstantInt::get(DivTy, BitWidth - 1);
1555ca98fd9SDimitry Andric
156522600a2SDimitry Andric ConstantInt *True = Builder.getTrue();
157522600a2SDimitry Andric
158522600a2SDimitry Andric BasicBlock *IBB = Builder.GetInsertBlock();
159522600a2SDimitry Andric Function *F = IBB->getParent();
1605ca98fd9SDimitry Andric Function *CTLZ = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
1615ca98fd9SDimitry Andric DivTy);
162522600a2SDimitry Andric
163522600a2SDimitry Andric // Our CFG is going to look like:
164522600a2SDimitry Andric // +---------------------+
165522600a2SDimitry Andric // | special-cases |
166522600a2SDimitry Andric // | ... |
167522600a2SDimitry Andric // +---------------------+
168522600a2SDimitry Andric // | |
169522600a2SDimitry Andric // | +----------+
170522600a2SDimitry Andric // | | bb1 |
171522600a2SDimitry Andric // | | ... |
172522600a2SDimitry Andric // | +----------+
173522600a2SDimitry Andric // | | |
174522600a2SDimitry Andric // | | +------------+
175522600a2SDimitry Andric // | | | preheader |
176522600a2SDimitry Andric // | | | ... |
177522600a2SDimitry Andric // | | +------------+
178522600a2SDimitry Andric // | | |
179522600a2SDimitry Andric // | | | +---+
180522600a2SDimitry Andric // | | | | |
181522600a2SDimitry Andric // | | +------------+ |
182522600a2SDimitry Andric // | | | do-while | |
183522600a2SDimitry Andric // | | | ... | |
184522600a2SDimitry Andric // | | +------------+ |
185522600a2SDimitry Andric // | | | | |
186522600a2SDimitry Andric // | +-----------+ +---+
187522600a2SDimitry Andric // | | loop-exit |
188522600a2SDimitry Andric // | | ... |
189522600a2SDimitry Andric // | +-----------+
190522600a2SDimitry Andric // | |
191522600a2SDimitry Andric // +-------+
192522600a2SDimitry Andric // | ... |
193522600a2SDimitry Andric // | end |
194522600a2SDimitry Andric // +-------+
195522600a2SDimitry Andric BasicBlock *SpecialCases = Builder.GetInsertBlock();
196522600a2SDimitry Andric SpecialCases->setName(Twine(SpecialCases->getName(), "_udiv-special-cases"));
197522600a2SDimitry Andric BasicBlock *End = SpecialCases->splitBasicBlock(Builder.GetInsertPoint(),
198522600a2SDimitry Andric "udiv-end");
199522600a2SDimitry Andric BasicBlock *LoopExit = BasicBlock::Create(Builder.getContext(),
200522600a2SDimitry Andric "udiv-loop-exit", F, End);
201522600a2SDimitry Andric BasicBlock *DoWhile = BasicBlock::Create(Builder.getContext(),
202522600a2SDimitry Andric "udiv-do-while", F, End);
203522600a2SDimitry Andric BasicBlock *Preheader = BasicBlock::Create(Builder.getContext(),
204522600a2SDimitry Andric "udiv-preheader", F, End);
205522600a2SDimitry Andric BasicBlock *BB1 = BasicBlock::Create(Builder.getContext(),
206522600a2SDimitry Andric "udiv-bb1", F, End);
207522600a2SDimitry Andric
208522600a2SDimitry Andric // We'll be overwriting the terminator to insert our extra blocks
209522600a2SDimitry Andric SpecialCases->getTerminator()->eraseFromParent();
210522600a2SDimitry Andric
2115ca98fd9SDimitry Andric // Same instructions are generated for both i32 (msb 31) and i64 (msb 63).
2125ca98fd9SDimitry Andric
213522600a2SDimitry Andric // First off, check for special cases: dividend or divisor is zero, divisor
214522600a2SDimitry Andric // is greater than dividend, and divisor is 1.
215522600a2SDimitry Andric // ; special-cases:
216522600a2SDimitry Andric // ; %ret0_1 = icmp eq i32 %divisor, 0
217522600a2SDimitry Andric // ; %ret0_2 = icmp eq i32 %dividend, 0
218522600a2SDimitry Andric // ; %ret0_3 = or i1 %ret0_1, %ret0_2
219522600a2SDimitry Andric // ; %tmp0 = tail call i32 @llvm.ctlz.i32(i32 %divisor, i1 true)
220522600a2SDimitry Andric // ; %tmp1 = tail call i32 @llvm.ctlz.i32(i32 %dividend, i1 true)
221522600a2SDimitry Andric // ; %sr = sub nsw i32 %tmp0, %tmp1
222522600a2SDimitry Andric // ; %ret0_4 = icmp ugt i32 %sr, 31
223e3b55780SDimitry Andric // ; %ret0 = select i1 %ret0_3, i1 true, i1 %ret0_4
224522600a2SDimitry Andric // ; %retDividend = icmp eq i32 %sr, 31
225522600a2SDimitry Andric // ; %retVal = select i1 %ret0, i32 0, i32 %dividend
226e3b55780SDimitry Andric // ; %earlyRet = select i1 %ret0, i1 true, %retDividend
227522600a2SDimitry Andric // ; br i1 %earlyRet, label %end, label %bb1
228522600a2SDimitry Andric Builder.SetInsertPoint(SpecialCases);
229e3b55780SDimitry Andric Divisor = Builder.CreateFreeze(Divisor);
230e3b55780SDimitry Andric Dividend = Builder.CreateFreeze(Dividend);
231522600a2SDimitry Andric Value *Ret0_1 = Builder.CreateICmpEQ(Divisor, Zero);
232522600a2SDimitry Andric Value *Ret0_2 = Builder.CreateICmpEQ(Dividend, Zero);
233522600a2SDimitry Andric Value *Ret0_3 = Builder.CreateOr(Ret0_1, Ret0_2);
2345a5ac124SDimitry Andric Value *Tmp0 = Builder.CreateCall(CTLZ, {Divisor, True});
2355a5ac124SDimitry Andric Value *Tmp1 = Builder.CreateCall(CTLZ, {Dividend, True});
236522600a2SDimitry Andric Value *SR = Builder.CreateSub(Tmp0, Tmp1);
2375ca98fd9SDimitry Andric Value *Ret0_4 = Builder.CreateICmpUGT(SR, MSB);
238e3b55780SDimitry Andric Value *Ret0 = Builder.CreateLogicalOr(Ret0_3, Ret0_4);
2395ca98fd9SDimitry Andric Value *RetDividend = Builder.CreateICmpEQ(SR, MSB);
240522600a2SDimitry Andric Value *RetVal = Builder.CreateSelect(Ret0, Zero, Dividend);
241e3b55780SDimitry Andric Value *EarlyRet = Builder.CreateLogicalOr(Ret0, RetDividend);
242522600a2SDimitry Andric Builder.CreateCondBr(EarlyRet, End, BB1);
243522600a2SDimitry Andric
244522600a2SDimitry Andric // ; bb1: ; preds = %special-cases
245522600a2SDimitry Andric // ; %sr_1 = add i32 %sr, 1
246522600a2SDimitry Andric // ; %tmp2 = sub i32 31, %sr
247522600a2SDimitry Andric // ; %q = shl i32 %dividend, %tmp2
248522600a2SDimitry Andric // ; %skipLoop = icmp eq i32 %sr_1, 0
249522600a2SDimitry Andric // ; br i1 %skipLoop, label %loop-exit, label %preheader
250522600a2SDimitry Andric Builder.SetInsertPoint(BB1);
251522600a2SDimitry Andric Value *SR_1 = Builder.CreateAdd(SR, One);
2525ca98fd9SDimitry Andric Value *Tmp2 = Builder.CreateSub(MSB, SR);
253522600a2SDimitry Andric Value *Q = Builder.CreateShl(Dividend, Tmp2);
254522600a2SDimitry Andric Value *SkipLoop = Builder.CreateICmpEQ(SR_1, Zero);
255522600a2SDimitry Andric Builder.CreateCondBr(SkipLoop, LoopExit, Preheader);
256522600a2SDimitry Andric
257522600a2SDimitry Andric // ; preheader: ; preds = %bb1
258522600a2SDimitry Andric // ; %tmp3 = lshr i32 %dividend, %sr_1
259522600a2SDimitry Andric // ; %tmp4 = add i32 %divisor, -1
260522600a2SDimitry Andric // ; br label %do-while
261522600a2SDimitry Andric Builder.SetInsertPoint(Preheader);
262522600a2SDimitry Andric Value *Tmp3 = Builder.CreateLShr(Dividend, SR_1);
263522600a2SDimitry Andric Value *Tmp4 = Builder.CreateAdd(Divisor, NegOne);
264522600a2SDimitry Andric Builder.CreateBr(DoWhile);
265522600a2SDimitry Andric
266522600a2SDimitry Andric // ; do-while: ; preds = %do-while, %preheader
267522600a2SDimitry Andric // ; %carry_1 = phi i32 [ 0, %preheader ], [ %carry, %do-while ]
268522600a2SDimitry Andric // ; %sr_3 = phi i32 [ %sr_1, %preheader ], [ %sr_2, %do-while ]
269522600a2SDimitry Andric // ; %r_1 = phi i32 [ %tmp3, %preheader ], [ %r, %do-while ]
270522600a2SDimitry Andric // ; %q_2 = phi i32 [ %q, %preheader ], [ %q_1, %do-while ]
271522600a2SDimitry Andric // ; %tmp5 = shl i32 %r_1, 1
272522600a2SDimitry Andric // ; %tmp6 = lshr i32 %q_2, 31
273522600a2SDimitry Andric // ; %tmp7 = or i32 %tmp5, %tmp6
274522600a2SDimitry Andric // ; %tmp8 = shl i32 %q_2, 1
275522600a2SDimitry Andric // ; %q_1 = or i32 %carry_1, %tmp8
276522600a2SDimitry Andric // ; %tmp9 = sub i32 %tmp4, %tmp7
277522600a2SDimitry Andric // ; %tmp10 = ashr i32 %tmp9, 31
278522600a2SDimitry Andric // ; %carry = and i32 %tmp10, 1
279522600a2SDimitry Andric // ; %tmp11 = and i32 %tmp10, %divisor
280522600a2SDimitry Andric // ; %r = sub i32 %tmp7, %tmp11
281522600a2SDimitry Andric // ; %sr_2 = add i32 %sr_3, -1
282522600a2SDimitry Andric // ; %tmp12 = icmp eq i32 %sr_2, 0
283522600a2SDimitry Andric // ; br i1 %tmp12, label %loop-exit, label %do-while
284522600a2SDimitry Andric Builder.SetInsertPoint(DoWhile);
2855ca98fd9SDimitry Andric PHINode *Carry_1 = Builder.CreatePHI(DivTy, 2);
2865ca98fd9SDimitry Andric PHINode *SR_3 = Builder.CreatePHI(DivTy, 2);
2875ca98fd9SDimitry Andric PHINode *R_1 = Builder.CreatePHI(DivTy, 2);
2885ca98fd9SDimitry Andric PHINode *Q_2 = Builder.CreatePHI(DivTy, 2);
289522600a2SDimitry Andric Value *Tmp5 = Builder.CreateShl(R_1, One);
2905ca98fd9SDimitry Andric Value *Tmp6 = Builder.CreateLShr(Q_2, MSB);
291522600a2SDimitry Andric Value *Tmp7 = Builder.CreateOr(Tmp5, Tmp6);
292522600a2SDimitry Andric Value *Tmp8 = Builder.CreateShl(Q_2, One);
293522600a2SDimitry Andric Value *Q_1 = Builder.CreateOr(Carry_1, Tmp8);
294522600a2SDimitry Andric Value *Tmp9 = Builder.CreateSub(Tmp4, Tmp7);
2955ca98fd9SDimitry Andric Value *Tmp10 = Builder.CreateAShr(Tmp9, MSB);
296522600a2SDimitry Andric Value *Carry = Builder.CreateAnd(Tmp10, One);
297522600a2SDimitry Andric Value *Tmp11 = Builder.CreateAnd(Tmp10, Divisor);
298522600a2SDimitry Andric Value *R = Builder.CreateSub(Tmp7, Tmp11);
299522600a2SDimitry Andric Value *SR_2 = Builder.CreateAdd(SR_3, NegOne);
300522600a2SDimitry Andric Value *Tmp12 = Builder.CreateICmpEQ(SR_2, Zero);
301522600a2SDimitry Andric Builder.CreateCondBr(Tmp12, LoopExit, DoWhile);
302522600a2SDimitry Andric
303522600a2SDimitry Andric // ; loop-exit: ; preds = %do-while, %bb1
304522600a2SDimitry Andric // ; %carry_2 = phi i32 [ 0, %bb1 ], [ %carry, %do-while ]
305522600a2SDimitry Andric // ; %q_3 = phi i32 [ %q, %bb1 ], [ %q_1, %do-while ]
306522600a2SDimitry Andric // ; %tmp13 = shl i32 %q_3, 1
307522600a2SDimitry Andric // ; %q_4 = or i32 %carry_2, %tmp13
308522600a2SDimitry Andric // ; br label %end
309522600a2SDimitry Andric Builder.SetInsertPoint(LoopExit);
3105ca98fd9SDimitry Andric PHINode *Carry_2 = Builder.CreatePHI(DivTy, 2);
3115ca98fd9SDimitry Andric PHINode *Q_3 = Builder.CreatePHI(DivTy, 2);
312522600a2SDimitry Andric Value *Tmp13 = Builder.CreateShl(Q_3, One);
313522600a2SDimitry Andric Value *Q_4 = Builder.CreateOr(Carry_2, Tmp13);
314522600a2SDimitry Andric Builder.CreateBr(End);
315522600a2SDimitry Andric
316522600a2SDimitry Andric // ; end: ; preds = %loop-exit, %special-cases
317522600a2SDimitry Andric // ; %q_5 = phi i32 [ %q_4, %loop-exit ], [ %retVal, %special-cases ]
318522600a2SDimitry Andric // ; ret i32 %q_5
319522600a2SDimitry Andric Builder.SetInsertPoint(End, End->begin());
3205ca98fd9SDimitry Andric PHINode *Q_5 = Builder.CreatePHI(DivTy, 2);
321522600a2SDimitry Andric
322522600a2SDimitry Andric // Populate the Phis, since all values have now been created. Our Phis were:
323522600a2SDimitry Andric // ; %carry_1 = phi i32 [ 0, %preheader ], [ %carry, %do-while ]
324522600a2SDimitry Andric Carry_1->addIncoming(Zero, Preheader);
325522600a2SDimitry Andric Carry_1->addIncoming(Carry, DoWhile);
326522600a2SDimitry Andric // ; %sr_3 = phi i32 [ %sr_1, %preheader ], [ %sr_2, %do-while ]
327522600a2SDimitry Andric SR_3->addIncoming(SR_1, Preheader);
328522600a2SDimitry Andric SR_3->addIncoming(SR_2, DoWhile);
329522600a2SDimitry Andric // ; %r_1 = phi i32 [ %tmp3, %preheader ], [ %r, %do-while ]
330522600a2SDimitry Andric R_1->addIncoming(Tmp3, Preheader);
331522600a2SDimitry Andric R_1->addIncoming(R, DoWhile);
332522600a2SDimitry Andric // ; %q_2 = phi i32 [ %q, %preheader ], [ %q_1, %do-while ]
333522600a2SDimitry Andric Q_2->addIncoming(Q, Preheader);
334522600a2SDimitry Andric Q_2->addIncoming(Q_1, DoWhile);
335522600a2SDimitry Andric // ; %carry_2 = phi i32 [ 0, %bb1 ], [ %carry, %do-while ]
336522600a2SDimitry Andric Carry_2->addIncoming(Zero, BB1);
337522600a2SDimitry Andric Carry_2->addIncoming(Carry, DoWhile);
338522600a2SDimitry Andric // ; %q_3 = phi i32 [ %q, %bb1 ], [ %q_1, %do-while ]
339522600a2SDimitry Andric Q_3->addIncoming(Q, BB1);
340522600a2SDimitry Andric Q_3->addIncoming(Q_1, DoWhile);
341522600a2SDimitry Andric // ; %q_5 = phi i32 [ %q_4, %loop-exit ], [ %retVal, %special-cases ]
342522600a2SDimitry Andric Q_5->addIncoming(Q_4, LoopExit);
343522600a2SDimitry Andric Q_5->addIncoming(RetVal, SpecialCases);
344522600a2SDimitry Andric
345522600a2SDimitry Andric return Q_5;
346522600a2SDimitry Andric }
347522600a2SDimitry Andric
348522600a2SDimitry Andric /// Generate code to calculate the remainder of two integers, replacing Rem with
349522600a2SDimitry Andric /// the generated code. This currently generates code using the udiv expansion,
350522600a2SDimitry Andric /// but future work includes generating more specialized code, e.g. when more
351e3b55780SDimitry Andric /// information about the operands are known.
352522600a2SDimitry Andric ///
353eb11fae6SDimitry Andric /// Replace Rem with generated code.
expandRemainder(BinaryOperator * Rem)354522600a2SDimitry Andric bool llvm::expandRemainder(BinaryOperator *Rem) {
355522600a2SDimitry Andric assert((Rem->getOpcode() == Instruction::SRem ||
356522600a2SDimitry Andric Rem->getOpcode() == Instruction::URem) &&
357522600a2SDimitry Andric "Trying to expand remainder from a non-remainder function");
358522600a2SDimitry Andric
359522600a2SDimitry Andric IRBuilder<> Builder(Rem);
360522600a2SDimitry Andric
361dd58ef01SDimitry Andric assert(!Rem->getType()->isVectorTy() && "Div over vectors not supported");
3625ca98fd9SDimitry Andric
363522600a2SDimitry Andric // First prepare the sign if it's a signed remainder
364522600a2SDimitry Andric if (Rem->getOpcode() == Instruction::SRem) {
365522600a2SDimitry Andric Value *Remainder = generateSignedRemainderCode(Rem->getOperand(0),
366522600a2SDimitry Andric Rem->getOperand(1), Builder);
367522600a2SDimitry Andric
36801095a5dSDimitry Andric // Check whether this is the insert point while Rem is still valid.
36901095a5dSDimitry Andric bool IsInsertPoint = Rem->getIterator() == Builder.GetInsertPoint();
370522600a2SDimitry Andric Rem->replaceAllUsesWith(Remainder);
371522600a2SDimitry Andric Rem->dropAllReferences();
372522600a2SDimitry Andric Rem->eraseFromParent();
373522600a2SDimitry Andric
37467c32a98SDimitry Andric // If we didn't actually generate an urem instruction, we're done
37567c32a98SDimitry Andric // This happens for example if the input were constant. In this case the
37667c32a98SDimitry Andric // Builder insertion point was unchanged
37701095a5dSDimitry Andric if (IsInsertPoint)
378522600a2SDimitry Andric return true;
379522600a2SDimitry Andric
38067c32a98SDimitry Andric BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
381522600a2SDimitry Andric Rem = BO;
382522600a2SDimitry Andric }
383522600a2SDimitry Andric
384522600a2SDimitry Andric Value *Remainder = generatedUnsignedRemainderCode(Rem->getOperand(0),
385522600a2SDimitry Andric Rem->getOperand(1),
386522600a2SDimitry Andric Builder);
387522600a2SDimitry Andric
388522600a2SDimitry Andric Rem->replaceAllUsesWith(Remainder);
389522600a2SDimitry Andric Rem->dropAllReferences();
390522600a2SDimitry Andric Rem->eraseFromParent();
391522600a2SDimitry Andric
392522600a2SDimitry Andric // Expand the udiv
393522600a2SDimitry Andric if (BinaryOperator *UDiv = dyn_cast<BinaryOperator>(Builder.GetInsertPoint())) {
394522600a2SDimitry Andric assert(UDiv->getOpcode() == Instruction::UDiv && "Non-udiv in expansion?");
395522600a2SDimitry Andric expandDivision(UDiv);
396522600a2SDimitry Andric }
397522600a2SDimitry Andric
398522600a2SDimitry Andric return true;
399522600a2SDimitry Andric }
400522600a2SDimitry Andric
401522600a2SDimitry Andric /// Generate code to divide two integers, replacing Div with the generated
402522600a2SDimitry Andric /// code. This currently generates code similarly to compiler-rt's
403522600a2SDimitry Andric /// implementations, but future work includes generating more specialized code
404e3b55780SDimitry Andric /// when more information about the operands are known.
405522600a2SDimitry Andric ///
406eb11fae6SDimitry Andric /// Replace Div with generated code.
expandDivision(BinaryOperator * Div)407522600a2SDimitry Andric bool llvm::expandDivision(BinaryOperator *Div) {
408522600a2SDimitry Andric assert((Div->getOpcode() == Instruction::SDiv ||
409522600a2SDimitry Andric Div->getOpcode() == Instruction::UDiv) &&
410522600a2SDimitry Andric "Trying to expand division from a non-division function");
411522600a2SDimitry Andric
412522600a2SDimitry Andric IRBuilder<> Builder(Div);
413522600a2SDimitry Andric
414dd58ef01SDimitry Andric assert(!Div->getType()->isVectorTy() && "Div over vectors not supported");
4155ca98fd9SDimitry Andric
416522600a2SDimitry Andric // First prepare the sign if it's a signed division
417522600a2SDimitry Andric if (Div->getOpcode() == Instruction::SDiv) {
418522600a2SDimitry Andric // Lower the code to unsigned division, and reset Div to point to the udiv.
419522600a2SDimitry Andric Value *Quotient = generateSignedDivisionCode(Div->getOperand(0),
420522600a2SDimitry Andric Div->getOperand(1), Builder);
42101095a5dSDimitry Andric
42201095a5dSDimitry Andric // Check whether this is the insert point while Div is still valid.
42301095a5dSDimitry Andric bool IsInsertPoint = Div->getIterator() == Builder.GetInsertPoint();
424522600a2SDimitry Andric Div->replaceAllUsesWith(Quotient);
425522600a2SDimitry Andric Div->dropAllReferences();
426522600a2SDimitry Andric Div->eraseFromParent();
427522600a2SDimitry Andric
42867c32a98SDimitry Andric // If we didn't actually generate an udiv instruction, we're done
42967c32a98SDimitry Andric // This happens for example if the input were constant. In this case the
43067c32a98SDimitry Andric // Builder insertion point was unchanged
43101095a5dSDimitry Andric if (IsInsertPoint)
432522600a2SDimitry Andric return true;
433522600a2SDimitry Andric
43467c32a98SDimitry Andric BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
435522600a2SDimitry Andric Div = BO;
436522600a2SDimitry Andric }
437522600a2SDimitry Andric
438522600a2SDimitry Andric // Insert the unsigned division code
439522600a2SDimitry Andric Value *Quotient = generateUnsignedDivisionCode(Div->getOperand(0),
440522600a2SDimitry Andric Div->getOperand(1),
441522600a2SDimitry Andric Builder);
442522600a2SDimitry Andric Div->replaceAllUsesWith(Quotient);
443522600a2SDimitry Andric Div->dropAllReferences();
444522600a2SDimitry Andric Div->eraseFromParent();
445522600a2SDimitry Andric
446522600a2SDimitry Andric return true;
447522600a2SDimitry Andric }
4484a16efa3SDimitry Andric
4494a16efa3SDimitry Andric /// Generate code to compute the remainder of two integers of bitwidth up to
4504a16efa3SDimitry Andric /// 32 bits. Uses the above routines and extends the inputs/truncates the
4514a16efa3SDimitry Andric /// outputs to operate in 32 bits; that is, these routines are good for targets
4524a16efa3SDimitry Andric /// that have no or very little suppport for smaller than 32 bit integer
4534a16efa3SDimitry Andric /// arithmetic.
4544a16efa3SDimitry Andric ///
455eb11fae6SDimitry Andric /// Replace Rem with emulation code.
expandRemainderUpTo32Bits(BinaryOperator * Rem)4564a16efa3SDimitry Andric bool llvm::expandRemainderUpTo32Bits(BinaryOperator *Rem) {
4574a16efa3SDimitry Andric assert((Rem->getOpcode() == Instruction::SRem ||
4584a16efa3SDimitry Andric Rem->getOpcode() == Instruction::URem) &&
4594a16efa3SDimitry Andric "Trying to expand remainder from a non-remainder function");
4604a16efa3SDimitry Andric
4614a16efa3SDimitry Andric Type *RemTy = Rem->getType();
462dd58ef01SDimitry Andric assert(!RemTy->isVectorTy() && "Div over vectors not supported");
4634a16efa3SDimitry Andric
4644a16efa3SDimitry Andric unsigned RemTyBitWidth = RemTy->getIntegerBitWidth();
4654a16efa3SDimitry Andric
466dd58ef01SDimitry Andric assert(RemTyBitWidth <= 32 &&
467dd58ef01SDimitry Andric "Div of bitwidth greater than 32 not supported");
4684a16efa3SDimitry Andric
4694a16efa3SDimitry Andric if (RemTyBitWidth == 32)
4704a16efa3SDimitry Andric return expandRemainder(Rem);
4714a16efa3SDimitry Andric
4725ca98fd9SDimitry Andric // If bitwidth smaller than 32 extend inputs, extend output and proceed
4734a16efa3SDimitry Andric // with 32 bit division.
4744a16efa3SDimitry Andric IRBuilder<> Builder(Rem);
4754a16efa3SDimitry Andric
4764a16efa3SDimitry Andric Value *ExtDividend;
4774a16efa3SDimitry Andric Value *ExtDivisor;
4784a16efa3SDimitry Andric Value *ExtRem;
4794a16efa3SDimitry Andric Value *Trunc;
4804a16efa3SDimitry Andric Type *Int32Ty = Builder.getInt32Ty();
4814a16efa3SDimitry Andric
4824a16efa3SDimitry Andric if (Rem->getOpcode() == Instruction::SRem) {
4834a16efa3SDimitry Andric ExtDividend = Builder.CreateSExt(Rem->getOperand(0), Int32Ty);
4844a16efa3SDimitry Andric ExtDivisor = Builder.CreateSExt(Rem->getOperand(1), Int32Ty);
4854a16efa3SDimitry Andric ExtRem = Builder.CreateSRem(ExtDividend, ExtDivisor);
4864a16efa3SDimitry Andric } else {
4874a16efa3SDimitry Andric ExtDividend = Builder.CreateZExt(Rem->getOperand(0), Int32Ty);
4884a16efa3SDimitry Andric ExtDivisor = Builder.CreateZExt(Rem->getOperand(1), Int32Ty);
4894a16efa3SDimitry Andric ExtRem = Builder.CreateURem(ExtDividend, ExtDivisor);
4904a16efa3SDimitry Andric }
4914a16efa3SDimitry Andric Trunc = Builder.CreateTrunc(ExtRem, RemTy);
4924a16efa3SDimitry Andric
4934a16efa3SDimitry Andric Rem->replaceAllUsesWith(Trunc);
4944a16efa3SDimitry Andric Rem->dropAllReferences();
4954a16efa3SDimitry Andric Rem->eraseFromParent();
4964a16efa3SDimitry Andric
4974a16efa3SDimitry Andric return expandRemainder(cast<BinaryOperator>(ExtRem));
4984a16efa3SDimitry Andric }
4994a16efa3SDimitry Andric
5005ca98fd9SDimitry Andric /// Generate code to compute the remainder of two integers of bitwidth up to
5015ca98fd9SDimitry Andric /// 64 bits. Uses the above routines and extends the inputs/truncates the
5025ca98fd9SDimitry Andric /// outputs to operate in 64 bits.
5035ca98fd9SDimitry Andric ///
504eb11fae6SDimitry Andric /// Replace Rem with emulation code.
expandRemainderUpTo64Bits(BinaryOperator * Rem)5055ca98fd9SDimitry Andric bool llvm::expandRemainderUpTo64Bits(BinaryOperator *Rem) {
5065ca98fd9SDimitry Andric assert((Rem->getOpcode() == Instruction::SRem ||
5075ca98fd9SDimitry Andric Rem->getOpcode() == Instruction::URem) &&
5085ca98fd9SDimitry Andric "Trying to expand remainder from a non-remainder function");
5095ca98fd9SDimitry Andric
5105ca98fd9SDimitry Andric Type *RemTy = Rem->getType();
511dd58ef01SDimitry Andric assert(!RemTy->isVectorTy() && "Div over vectors not supported");
5125ca98fd9SDimitry Andric
5135ca98fd9SDimitry Andric unsigned RemTyBitWidth = RemTy->getIntegerBitWidth();
5145ca98fd9SDimitry Andric
515e3b55780SDimitry Andric if (RemTyBitWidth >= 64)
5165ca98fd9SDimitry Andric return expandRemainder(Rem);
5175ca98fd9SDimitry Andric
5185ca98fd9SDimitry Andric // If bitwidth smaller than 64 extend inputs, extend output and proceed
5195ca98fd9SDimitry Andric // with 64 bit division.
5205ca98fd9SDimitry Andric IRBuilder<> Builder(Rem);
5215ca98fd9SDimitry Andric
5225ca98fd9SDimitry Andric Value *ExtDividend;
5235ca98fd9SDimitry Andric Value *ExtDivisor;
5245ca98fd9SDimitry Andric Value *ExtRem;
5255ca98fd9SDimitry Andric Value *Trunc;
5265ca98fd9SDimitry Andric Type *Int64Ty = Builder.getInt64Ty();
5275ca98fd9SDimitry Andric
5285ca98fd9SDimitry Andric if (Rem->getOpcode() == Instruction::SRem) {
5295ca98fd9SDimitry Andric ExtDividend = Builder.CreateSExt(Rem->getOperand(0), Int64Ty);
5305ca98fd9SDimitry Andric ExtDivisor = Builder.CreateSExt(Rem->getOperand(1), Int64Ty);
5315ca98fd9SDimitry Andric ExtRem = Builder.CreateSRem(ExtDividend, ExtDivisor);
5325ca98fd9SDimitry Andric } else {
5335ca98fd9SDimitry Andric ExtDividend = Builder.CreateZExt(Rem->getOperand(0), Int64Ty);
5345ca98fd9SDimitry Andric ExtDivisor = Builder.CreateZExt(Rem->getOperand(1), Int64Ty);
5355ca98fd9SDimitry Andric ExtRem = Builder.CreateURem(ExtDividend, ExtDivisor);
5365ca98fd9SDimitry Andric }
5375ca98fd9SDimitry Andric Trunc = Builder.CreateTrunc(ExtRem, RemTy);
5385ca98fd9SDimitry Andric
5395ca98fd9SDimitry Andric Rem->replaceAllUsesWith(Trunc);
5405ca98fd9SDimitry Andric Rem->dropAllReferences();
5415ca98fd9SDimitry Andric Rem->eraseFromParent();
5425ca98fd9SDimitry Andric
5435ca98fd9SDimitry Andric return expandRemainder(cast<BinaryOperator>(ExtRem));
5445ca98fd9SDimitry Andric }
5454a16efa3SDimitry Andric
5464a16efa3SDimitry Andric /// Generate code to divide two integers of bitwidth up to 32 bits. Uses the
5474a16efa3SDimitry Andric /// above routines and extends the inputs/truncates the outputs to operate
5484a16efa3SDimitry Andric /// in 32 bits; that is, these routines are good for targets that have no
5494a16efa3SDimitry Andric /// or very little support for smaller than 32 bit integer arithmetic.
5504a16efa3SDimitry Andric ///
551eb11fae6SDimitry Andric /// Replace Div with emulation code.
expandDivisionUpTo32Bits(BinaryOperator * Div)5524a16efa3SDimitry Andric bool llvm::expandDivisionUpTo32Bits(BinaryOperator *Div) {
5534a16efa3SDimitry Andric assert((Div->getOpcode() == Instruction::SDiv ||
5544a16efa3SDimitry Andric Div->getOpcode() == Instruction::UDiv) &&
5554a16efa3SDimitry Andric "Trying to expand division from a non-division function");
5564a16efa3SDimitry Andric
5574a16efa3SDimitry Andric Type *DivTy = Div->getType();
558dd58ef01SDimitry Andric assert(!DivTy->isVectorTy() && "Div over vectors not supported");
5594a16efa3SDimitry Andric
5604a16efa3SDimitry Andric unsigned DivTyBitWidth = DivTy->getIntegerBitWidth();
5614a16efa3SDimitry Andric
562dd58ef01SDimitry Andric assert(DivTyBitWidth <= 32 && "Div of bitwidth greater than 32 not supported");
5634a16efa3SDimitry Andric
5644a16efa3SDimitry Andric if (DivTyBitWidth == 32)
5654a16efa3SDimitry Andric return expandDivision(Div);
5664a16efa3SDimitry Andric
5675ca98fd9SDimitry Andric // If bitwidth smaller than 32 extend inputs, extend output and proceed
5684a16efa3SDimitry Andric // with 32 bit division.
5694a16efa3SDimitry Andric IRBuilder<> Builder(Div);
5704a16efa3SDimitry Andric
5714a16efa3SDimitry Andric Value *ExtDividend;
5724a16efa3SDimitry Andric Value *ExtDivisor;
5734a16efa3SDimitry Andric Value *ExtDiv;
5744a16efa3SDimitry Andric Value *Trunc;
5754a16efa3SDimitry Andric Type *Int32Ty = Builder.getInt32Ty();
5764a16efa3SDimitry Andric
5774a16efa3SDimitry Andric if (Div->getOpcode() == Instruction::SDiv) {
5784a16efa3SDimitry Andric ExtDividend = Builder.CreateSExt(Div->getOperand(0), Int32Ty);
5794a16efa3SDimitry Andric ExtDivisor = Builder.CreateSExt(Div->getOperand(1), Int32Ty);
5804a16efa3SDimitry Andric ExtDiv = Builder.CreateSDiv(ExtDividend, ExtDivisor);
5814a16efa3SDimitry Andric } else {
5824a16efa3SDimitry Andric ExtDividend = Builder.CreateZExt(Div->getOperand(0), Int32Ty);
5834a16efa3SDimitry Andric ExtDivisor = Builder.CreateZExt(Div->getOperand(1), Int32Ty);
5844a16efa3SDimitry Andric ExtDiv = Builder.CreateUDiv(ExtDividend, ExtDivisor);
5854a16efa3SDimitry Andric }
5864a16efa3SDimitry Andric Trunc = Builder.CreateTrunc(ExtDiv, DivTy);
5874a16efa3SDimitry Andric
5884a16efa3SDimitry Andric Div->replaceAllUsesWith(Trunc);
5894a16efa3SDimitry Andric Div->dropAllReferences();
5904a16efa3SDimitry Andric Div->eraseFromParent();
5914a16efa3SDimitry Andric
5924a16efa3SDimitry Andric return expandDivision(cast<BinaryOperator>(ExtDiv));
5934a16efa3SDimitry Andric }
5945ca98fd9SDimitry Andric
5955ca98fd9SDimitry Andric /// Generate code to divide two integers of bitwidth up to 64 bits. Uses the
5965ca98fd9SDimitry Andric /// above routines and extends the inputs/truncates the outputs to operate
5975ca98fd9SDimitry Andric /// in 64 bits.
5985ca98fd9SDimitry Andric ///
599eb11fae6SDimitry Andric /// Replace Div with emulation code.
expandDivisionUpTo64Bits(BinaryOperator * Div)6005ca98fd9SDimitry Andric bool llvm::expandDivisionUpTo64Bits(BinaryOperator *Div) {
6015ca98fd9SDimitry Andric assert((Div->getOpcode() == Instruction::SDiv ||
6025ca98fd9SDimitry Andric Div->getOpcode() == Instruction::UDiv) &&
6035ca98fd9SDimitry Andric "Trying to expand division from a non-division function");
6045ca98fd9SDimitry Andric
6055ca98fd9SDimitry Andric Type *DivTy = Div->getType();
606dd58ef01SDimitry Andric assert(!DivTy->isVectorTy() && "Div over vectors not supported");
6075ca98fd9SDimitry Andric
6085ca98fd9SDimitry Andric unsigned DivTyBitWidth = DivTy->getIntegerBitWidth();
6095ca98fd9SDimitry Andric
610e3b55780SDimitry Andric if (DivTyBitWidth >= 64)
6115ca98fd9SDimitry Andric return expandDivision(Div);
6125ca98fd9SDimitry Andric
6135ca98fd9SDimitry Andric // If bitwidth smaller than 64 extend inputs, extend output and proceed
6145ca98fd9SDimitry Andric // with 64 bit division.
6155ca98fd9SDimitry Andric IRBuilder<> Builder(Div);
6165ca98fd9SDimitry Andric
6175ca98fd9SDimitry Andric Value *ExtDividend;
6185ca98fd9SDimitry Andric Value *ExtDivisor;
6195ca98fd9SDimitry Andric Value *ExtDiv;
6205ca98fd9SDimitry Andric Value *Trunc;
6215ca98fd9SDimitry Andric Type *Int64Ty = Builder.getInt64Ty();
6225ca98fd9SDimitry Andric
6235ca98fd9SDimitry Andric if (Div->getOpcode() == Instruction::SDiv) {
6245ca98fd9SDimitry Andric ExtDividend = Builder.CreateSExt(Div->getOperand(0), Int64Ty);
6255ca98fd9SDimitry Andric ExtDivisor = Builder.CreateSExt(Div->getOperand(1), Int64Ty);
6265ca98fd9SDimitry Andric ExtDiv = Builder.CreateSDiv(ExtDividend, ExtDivisor);
6275ca98fd9SDimitry Andric } else {
6285ca98fd9SDimitry Andric ExtDividend = Builder.CreateZExt(Div->getOperand(0), Int64Ty);
6295ca98fd9SDimitry Andric ExtDivisor = Builder.CreateZExt(Div->getOperand(1), Int64Ty);
6305ca98fd9SDimitry Andric ExtDiv = Builder.CreateUDiv(ExtDividend, ExtDivisor);
6315ca98fd9SDimitry Andric }
6325ca98fd9SDimitry Andric Trunc = Builder.CreateTrunc(ExtDiv, DivTy);
6335ca98fd9SDimitry Andric
6345ca98fd9SDimitry Andric Div->replaceAllUsesWith(Trunc);
6355ca98fd9SDimitry Andric Div->dropAllReferences();
6365ca98fd9SDimitry Andric Div->eraseFromParent();
6375ca98fd9SDimitry Andric
6385ca98fd9SDimitry Andric return expandDivision(cast<BinaryOperator>(ExtDiv));
6395ca98fd9SDimitry Andric }
640