blob: 5687afa61e2a603f7f5e15f540c95ee99eeff598 [file] [log] [blame]
Michael Ilseman52059da2012-09-18 22:02:40 +00001//===-- IntegerDivision.cpp - Expand integer division ---------------------===//
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//
Michael Ilsemand930c192013-11-19 06:54:19 +000010// This file contains an implementation of 32bit and 64bit scalar integer
11// division for targets that don't have native support. It's largely derived
12// from compiler-rt's implementations of __udivsi3 and __udivmoddi4,
13// but hand-tuned for targets that prefer less control flow.
Michael Ilseman52059da2012-09-18 22:02:40 +000014//
15//===----------------------------------------------------------------------===//
16
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Transforms/Utils/IntegerDivision.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Function.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Intrinsics.h"
Michael Ilsemand930c192013-11-19 06:54:19 +000022#include <utility>
Michael Ilseman52059da2012-09-18 22:02:40 +000023
24using namespace llvm;
25
Chandler Carruth964daaa2014-04-22 02:55:47 +000026#define DEBUG_TYPE "integer-division"
27
Michael Ilsemana398d4c2012-09-26 01:55:01 +000028/// Generate code to compute the remainder of two signed integers. Returns the
29/// remainder, which will have the sign of the dividend. Builder's insert point
30/// should be pointing where the caller wants code generated, e.g. at the srem
31/// instruction. This will generate a urem in the process, and Builder's insert
32/// point will be pointing at the uren (if present, i.e. not folded), ready to
33/// be expanded if the user wishes
34static Value *generateSignedRemainderCode(Value *Dividend, Value *Divisor,
35 IRBuilder<> &Builder) {
Michael Ilsemand930c192013-11-19 06:54:19 +000036 unsigned BitWidth = Dividend->getType()->getIntegerBitWidth();
37 ConstantInt *Shift;
38
39 if (BitWidth == 64) {
40 Shift = Builder.getInt64(63);
41 } else {
42 assert(BitWidth == 32 && "Unexpected bit width");
43 Shift = Builder.getInt32(31);
44 }
45
46 // Following instructions are generated for both i32 (shift 31) and
47 // i64 (shift 63).
Michael Ilsemana398d4c2012-09-26 01:55:01 +000048
49 // ; %dividend_sgn = ashr i32 %dividend, 31
50 // ; %divisor_sgn = ashr i32 %divisor, 31
51 // ; %dvd_xor = xor i32 %dividend, %dividend_sgn
52 // ; %dvs_xor = xor i32 %divisor, %divisor_sgn
53 // ; %u_dividend = sub i32 %dvd_xor, %dividend_sgn
54 // ; %u_divisor = sub i32 %dvs_xor, %divisor_sgn
55 // ; %urem = urem i32 %dividend, %divisor
56 // ; %xored = xor i32 %urem, %dividend_sgn
57 // ; %srem = sub i32 %xored, %dividend_sgn
Michael Ilsemand930c192013-11-19 06:54:19 +000058 Value *DividendSign = Builder.CreateAShr(Dividend, Shift);
59 Value *DivisorSign = Builder.CreateAShr(Divisor, Shift);
Michael Ilsemana398d4c2012-09-26 01:55:01 +000060 Value *DvdXor = Builder.CreateXor(Dividend, DividendSign);
61 Value *DvsXor = Builder.CreateXor(Divisor, DivisorSign);
62 Value *UDividend = Builder.CreateSub(DvdXor, DividendSign);
63 Value *UDivisor = Builder.CreateSub(DvsXor, DivisorSign);
64 Value *URem = Builder.CreateURem(UDividend, UDivisor);
65 Value *Xored = Builder.CreateXor(URem, DividendSign);
66 Value *SRem = Builder.CreateSub(Xored, DividendSign);
67
68 if (Instruction *URemInst = dyn_cast<Instruction>(URem))
69 Builder.SetInsertPoint(URemInst);
70
71 return SRem;
72}
73
74
75/// Generate code to compute the remainder of two unsigned integers. Returns the
76/// remainder. Builder's insert point should be pointing where the caller wants
77/// code generated, e.g. at the urem instruction. This will generate a udiv in
78/// the process, and Builder's insert point will be pointing at the udiv (if
79/// present, i.e. not folded), ready to be expanded if the user wishes
80static Value *generatedUnsignedRemainderCode(Value *Dividend, Value *Divisor,
81 IRBuilder<> &Builder) {
82 // Remainder = Dividend - Quotient*Divisor
83
Michael Ilsemand930c192013-11-19 06:54:19 +000084 // Following instructions are generated for both i32 and i64
85
Michael Ilsemana398d4c2012-09-26 01:55:01 +000086 // ; %quotient = udiv i32 %dividend, %divisor
87 // ; %product = mul i32 %divisor, %quotient
88 // ; %remainder = sub i32 %dividend, %product
89 Value *Quotient = Builder.CreateUDiv(Dividend, Divisor);
90 Value *Product = Builder.CreateMul(Divisor, Quotient);
91 Value *Remainder = Builder.CreateSub(Dividend, Product);
92
93 if (Instruction *UDiv = dyn_cast<Instruction>(Quotient))
94 Builder.SetInsertPoint(UDiv);
95
96 return Remainder;
97}
98
Michael Ilseman370a1a12012-09-19 16:25:57 +000099/// Generate code to divide two signed integers. Returns the quotient, rounded
Michael Ilsemana398d4c2012-09-26 01:55:01 +0000100/// towards 0. Builder's insert point should be pointing where the caller wants
101/// code generated, e.g. at the sdiv instruction. This will generate a udiv in
102/// the process, and Builder's insert point will be pointing at the udiv (if
103/// present, i.e. not folded), ready to be expanded if the user wishes.
Michael Ilseman5117db52012-09-19 18:14:45 +0000104static Value *generateSignedDivisionCode(Value *Dividend, Value *Divisor,
Michael Ilseman1db690d2012-09-19 16:17:20 +0000105 IRBuilder<> &Builder) {
Michael Ilsemand930c192013-11-19 06:54:19 +0000106 // Implementation taken from compiler-rt's __divsi3 and __divdi3
Michael Ilseman52059da2012-09-18 22:02:40 +0000107
Michael Ilsemand930c192013-11-19 06:54:19 +0000108 unsigned BitWidth = Dividend->getType()->getIntegerBitWidth();
109 ConstantInt *Shift;
110
111 if (BitWidth == 64) {
112 Shift = Builder.getInt64(63);
113 } else {
114 assert(BitWidth == 32 && "Unexpected bit width");
115 Shift = Builder.getInt32(31);
116 }
117
118 // Following instructions are generated for both i32 (shift 31) and
119 // i64 (shift 63).
Michael Ilseman52059da2012-09-18 22:02:40 +0000120
121 // ; %tmp = ashr i32 %dividend, 31
122 // ; %tmp1 = ashr i32 %divisor, 31
123 // ; %tmp2 = xor i32 %tmp, %dividend
124 // ; %u_dvnd = sub nsw i32 %tmp2, %tmp
125 // ; %tmp3 = xor i32 %tmp1, %divisor
126 // ; %u_dvsr = sub nsw i32 %tmp3, %tmp1
127 // ; %q_sgn = xor i32 %tmp1, %tmp
128 // ; %q_mag = udiv i32 %u_dvnd, %u_dvsr
129 // ; %tmp4 = xor i32 %q_mag, %q_sgn
130 // ; %q = sub i32 %tmp4, %q_sgn
Michael Ilsemand930c192013-11-19 06:54:19 +0000131 Value *Tmp = Builder.CreateAShr(Dividend, Shift);
132 Value *Tmp1 = Builder.CreateAShr(Divisor, Shift);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000133 Value *Tmp2 = Builder.CreateXor(Tmp, Dividend);
134 Value *U_Dvnd = Builder.CreateSub(Tmp2, Tmp);
135 Value *Tmp3 = Builder.CreateXor(Tmp1, Divisor);
136 Value *U_Dvsr = Builder.CreateSub(Tmp3, Tmp1);
137 Value *Q_Sgn = Builder.CreateXor(Tmp1, Tmp);
138 Value *Q_Mag = Builder.CreateUDiv(U_Dvnd, U_Dvsr);
139 Value *Tmp4 = Builder.CreateXor(Q_Mag, Q_Sgn);
140 Value *Q = Builder.CreateSub(Tmp4, Q_Sgn);
Michael Ilseman52059da2012-09-18 22:02:40 +0000141
Michael Ilseman1db690d2012-09-19 16:17:20 +0000142 if (Instruction *UDiv = dyn_cast<Instruction>(Q_Mag))
Michael Ilseman52059da2012-09-18 22:02:40 +0000143 Builder.SetInsertPoint(UDiv);
144
145 return Q;
146}
147
Michael Ilsemand930c192013-11-19 06:54:19 +0000148/// Generates code to divide two unsigned scalar 32-bit or 64-bit integers.
149/// Returns the quotient, rounded towards 0. Builder's insert point should
150/// point where the caller wants code generated, e.g. at the udiv instruction.
Michael Ilseman5117db52012-09-19 18:14:45 +0000151static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
Michael Ilseman1db690d2012-09-19 16:17:20 +0000152 IRBuilder<> &Builder) {
Michael Ilseman52059da2012-09-18 22:02:40 +0000153 // The basic algorithm can be found in the compiler-rt project's
154 // implementation of __udivsi3.c. Here, we do a lower-level IR based approach
155 // that's been hand-tuned to lessen the amount of control flow involved.
156
157 // Some helper values
Michael Ilsemand930c192013-11-19 06:54:19 +0000158 IntegerType *DivTy = cast<IntegerType>(Dividend->getType());
159 unsigned BitWidth = DivTy->getBitWidth();
Michael Ilseman52059da2012-09-18 22:02:40 +0000160
Michael Ilsemand930c192013-11-19 06:54:19 +0000161 ConstantInt *Zero;
162 ConstantInt *One;
163 ConstantInt *NegOne;
164 ConstantInt *MSB;
165
166 if (BitWidth == 64) {
167 Zero = Builder.getInt64(0);
168 One = Builder.getInt64(1);
169 NegOne = ConstantInt::getSigned(DivTy, -1);
170 MSB = Builder.getInt64(63);
171 } else {
172 assert(BitWidth == 32 && "Unexpected bit width");
173 Zero = Builder.getInt32(0);
174 One = Builder.getInt32(1);
175 NegOne = ConstantInt::getSigned(DivTy, -1);
176 MSB = Builder.getInt32(31);
177 }
178
179 ConstantInt *True = Builder.getTrue();
Michael Ilseman52059da2012-09-18 22:02:40 +0000180
Michael Ilseman1db690d2012-09-19 16:17:20 +0000181 BasicBlock *IBB = Builder.GetInsertBlock();
182 Function *F = IBB->getParent();
Michael Ilsemand930c192013-11-19 06:54:19 +0000183 Function *CTLZ = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
184 DivTy);
Michael Ilseman52059da2012-09-18 22:02:40 +0000185
186 // Our CFG is going to look like:
187 // +---------------------+
188 // | special-cases |
189 // | ... |
190 // +---------------------+
191 // | |
192 // | +----------+
193 // | | bb1 |
194 // | | ... |
195 // | +----------+
196 // | | |
197 // | | +------------+
198 // | | | preheader |
199 // | | | ... |
200 // | | +------------+
201 // | | |
202 // | | | +---+
203 // | | | | |
204 // | | +------------+ |
205 // | | | do-while | |
206 // | | | ... | |
207 // | | +------------+ |
208 // | | | | |
209 // | +-----------+ +---+
210 // | | loop-exit |
211 // | | ... |
212 // | +-----------+
213 // | |
214 // +-------+
215 // | ... |
216 // | end |
217 // +-------+
Michael Ilseman1db690d2012-09-19 16:17:20 +0000218 BasicBlock *SpecialCases = Builder.GetInsertBlock();
Michael Ilseman52059da2012-09-18 22:02:40 +0000219 SpecialCases->setName(Twine(SpecialCases->getName(), "_udiv-special-cases"));
Michael Ilseman1db690d2012-09-19 16:17:20 +0000220 BasicBlock *End = SpecialCases->splitBasicBlock(Builder.GetInsertPoint(),
Michael Ilseman52059da2012-09-18 22:02:40 +0000221 "udiv-end");
Michael Ilseman1db690d2012-09-19 16:17:20 +0000222 BasicBlock *LoopExit = BasicBlock::Create(Builder.getContext(),
Michael Ilseman52059da2012-09-18 22:02:40 +0000223 "udiv-loop-exit", F, End);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000224 BasicBlock *DoWhile = BasicBlock::Create(Builder.getContext(),
Michael Ilseman52059da2012-09-18 22:02:40 +0000225 "udiv-do-while", F, End);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000226 BasicBlock *Preheader = BasicBlock::Create(Builder.getContext(),
Michael Ilseman52059da2012-09-18 22:02:40 +0000227 "udiv-preheader", F, End);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000228 BasicBlock *BB1 = BasicBlock::Create(Builder.getContext(),
Michael Ilseman52059da2012-09-18 22:02:40 +0000229 "udiv-bb1", F, End);
230
231 // We'll be overwriting the terminator to insert our extra blocks
232 SpecialCases->getTerminator()->eraseFromParent();
233
Michael Ilsemand930c192013-11-19 06:54:19 +0000234 // Same instructions are generated for both i32 (msb 31) and i64 (msb 63).
235
Michael Ilseman52059da2012-09-18 22:02:40 +0000236 // First off, check for special cases: dividend or divisor is zero, divisor
237 // is greater than dividend, and divisor is 1.
238 // ; special-cases:
239 // ; %ret0_1 = icmp eq i32 %divisor, 0
240 // ; %ret0_2 = icmp eq i32 %dividend, 0
241 // ; %ret0_3 = or i1 %ret0_1, %ret0_2
242 // ; %tmp0 = tail call i32 @llvm.ctlz.i32(i32 %divisor, i1 true)
243 // ; %tmp1 = tail call i32 @llvm.ctlz.i32(i32 %dividend, i1 true)
244 // ; %sr = sub nsw i32 %tmp0, %tmp1
245 // ; %ret0_4 = icmp ugt i32 %sr, 31
246 // ; %ret0 = or i1 %ret0_3, %ret0_4
247 // ; %retDividend = icmp eq i32 %sr, 31
248 // ; %retVal = select i1 %ret0, i32 0, i32 %dividend
249 // ; %earlyRet = or i1 %ret0, %retDividend
250 // ; br i1 %earlyRet, label %end, label %bb1
251 Builder.SetInsertPoint(SpecialCases);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000252 Value *Ret0_1 = Builder.CreateICmpEQ(Divisor, Zero);
253 Value *Ret0_2 = Builder.CreateICmpEQ(Dividend, Zero);
254 Value *Ret0_3 = Builder.CreateOr(Ret0_1, Ret0_2);
David Blaikieff6409d2015-05-18 22:13:54 +0000255 Value *Tmp0 = Builder.CreateCall(CTLZ, {Divisor, True});
256 Value *Tmp1 = Builder.CreateCall(CTLZ, {Dividend, True});
Michael Ilseman1db690d2012-09-19 16:17:20 +0000257 Value *SR = Builder.CreateSub(Tmp0, Tmp1);
Michael Ilsemand930c192013-11-19 06:54:19 +0000258 Value *Ret0_4 = Builder.CreateICmpUGT(SR, MSB);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000259 Value *Ret0 = Builder.CreateOr(Ret0_3, Ret0_4);
Michael Ilsemand930c192013-11-19 06:54:19 +0000260 Value *RetDividend = Builder.CreateICmpEQ(SR, MSB);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000261 Value *RetVal = Builder.CreateSelect(Ret0, Zero, Dividend);
262 Value *EarlyRet = Builder.CreateOr(Ret0, RetDividend);
Michael Ilseman52059da2012-09-18 22:02:40 +0000263 Builder.CreateCondBr(EarlyRet, End, BB1);
264
265 // ; bb1: ; preds = %special-cases
266 // ; %sr_1 = add i32 %sr, 1
267 // ; %tmp2 = sub i32 31, %sr
268 // ; %q = shl i32 %dividend, %tmp2
269 // ; %skipLoop = icmp eq i32 %sr_1, 0
270 // ; br i1 %skipLoop, label %loop-exit, label %preheader
271 Builder.SetInsertPoint(BB1);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000272 Value *SR_1 = Builder.CreateAdd(SR, One);
Michael Ilsemand930c192013-11-19 06:54:19 +0000273 Value *Tmp2 = Builder.CreateSub(MSB, SR);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000274 Value *Q = Builder.CreateShl(Dividend, Tmp2);
275 Value *SkipLoop = Builder.CreateICmpEQ(SR_1, Zero);
Michael Ilseman52059da2012-09-18 22:02:40 +0000276 Builder.CreateCondBr(SkipLoop, LoopExit, Preheader);
277
278 // ; preheader: ; preds = %bb1
279 // ; %tmp3 = lshr i32 %dividend, %sr_1
280 // ; %tmp4 = add i32 %divisor, -1
281 // ; br label %do-while
282 Builder.SetInsertPoint(Preheader);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000283 Value *Tmp3 = Builder.CreateLShr(Dividend, SR_1);
284 Value *Tmp4 = Builder.CreateAdd(Divisor, NegOne);
Michael Ilseman52059da2012-09-18 22:02:40 +0000285 Builder.CreateBr(DoWhile);
286
287 // ; do-while: ; preds = %do-while, %preheader
288 // ; %carry_1 = phi i32 [ 0, %preheader ], [ %carry, %do-while ]
289 // ; %sr_3 = phi i32 [ %sr_1, %preheader ], [ %sr_2, %do-while ]
290 // ; %r_1 = phi i32 [ %tmp3, %preheader ], [ %r, %do-while ]
291 // ; %q_2 = phi i32 [ %q, %preheader ], [ %q_1, %do-while ]
292 // ; %tmp5 = shl i32 %r_1, 1
293 // ; %tmp6 = lshr i32 %q_2, 31
294 // ; %tmp7 = or i32 %tmp5, %tmp6
295 // ; %tmp8 = shl i32 %q_2, 1
296 // ; %q_1 = or i32 %carry_1, %tmp8
297 // ; %tmp9 = sub i32 %tmp4, %tmp7
298 // ; %tmp10 = ashr i32 %tmp9, 31
299 // ; %carry = and i32 %tmp10, 1
300 // ; %tmp11 = and i32 %tmp10, %divisor
301 // ; %r = sub i32 %tmp7, %tmp11
302 // ; %sr_2 = add i32 %sr_3, -1
303 // ; %tmp12 = icmp eq i32 %sr_2, 0
304 // ; br i1 %tmp12, label %loop-exit, label %do-while
305 Builder.SetInsertPoint(DoWhile);
Michael Ilsemand930c192013-11-19 06:54:19 +0000306 PHINode *Carry_1 = Builder.CreatePHI(DivTy, 2);
307 PHINode *SR_3 = Builder.CreatePHI(DivTy, 2);
308 PHINode *R_1 = Builder.CreatePHI(DivTy, 2);
309 PHINode *Q_2 = Builder.CreatePHI(DivTy, 2);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000310 Value *Tmp5 = Builder.CreateShl(R_1, One);
Michael Ilsemand930c192013-11-19 06:54:19 +0000311 Value *Tmp6 = Builder.CreateLShr(Q_2, MSB);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000312 Value *Tmp7 = Builder.CreateOr(Tmp5, Tmp6);
313 Value *Tmp8 = Builder.CreateShl(Q_2, One);
314 Value *Q_1 = Builder.CreateOr(Carry_1, Tmp8);
315 Value *Tmp9 = Builder.CreateSub(Tmp4, Tmp7);
Michael Ilsemand930c192013-11-19 06:54:19 +0000316 Value *Tmp10 = Builder.CreateAShr(Tmp9, MSB);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000317 Value *Carry = Builder.CreateAnd(Tmp10, One);
318 Value *Tmp11 = Builder.CreateAnd(Tmp10, Divisor);
319 Value *R = Builder.CreateSub(Tmp7, Tmp11);
320 Value *SR_2 = Builder.CreateAdd(SR_3, NegOne);
321 Value *Tmp12 = Builder.CreateICmpEQ(SR_2, Zero);
Michael Ilseman52059da2012-09-18 22:02:40 +0000322 Builder.CreateCondBr(Tmp12, LoopExit, DoWhile);
323
324 // ; loop-exit: ; preds = %do-while, %bb1
325 // ; %carry_2 = phi i32 [ 0, %bb1 ], [ %carry, %do-while ]
326 // ; %q_3 = phi i32 [ %q, %bb1 ], [ %q_1, %do-while ]
327 // ; %tmp13 = shl i32 %q_3, 1
328 // ; %q_4 = or i32 %carry_2, %tmp13
329 // ; br label %end
330 Builder.SetInsertPoint(LoopExit);
Michael Ilsemand930c192013-11-19 06:54:19 +0000331 PHINode *Carry_2 = Builder.CreatePHI(DivTy, 2);
332 PHINode *Q_3 = Builder.CreatePHI(DivTy, 2);
Michael Ilseman1db690d2012-09-19 16:17:20 +0000333 Value *Tmp13 = Builder.CreateShl(Q_3, One);
334 Value *Q_4 = Builder.CreateOr(Carry_2, Tmp13);
Michael Ilseman52059da2012-09-18 22:02:40 +0000335 Builder.CreateBr(End);
336
337 // ; end: ; preds = %loop-exit, %special-cases
338 // ; %q_5 = phi i32 [ %q_4, %loop-exit ], [ %retVal, %special-cases ]
339 // ; ret i32 %q_5
340 Builder.SetInsertPoint(End, End->begin());
Michael Ilsemand930c192013-11-19 06:54:19 +0000341 PHINode *Q_5 = Builder.CreatePHI(DivTy, 2);
Michael Ilseman52059da2012-09-18 22:02:40 +0000342
343 // Populate the Phis, since all values have now been created. Our Phis were:
344 // ; %carry_1 = phi i32 [ 0, %preheader ], [ %carry, %do-while ]
345 Carry_1->addIncoming(Zero, Preheader);
346 Carry_1->addIncoming(Carry, DoWhile);
347 // ; %sr_3 = phi i32 [ %sr_1, %preheader ], [ %sr_2, %do-while ]
348 SR_3->addIncoming(SR_1, Preheader);
349 SR_3->addIncoming(SR_2, DoWhile);
350 // ; %r_1 = phi i32 [ %tmp3, %preheader ], [ %r, %do-while ]
351 R_1->addIncoming(Tmp3, Preheader);
352 R_1->addIncoming(R, DoWhile);
353 // ; %q_2 = phi i32 [ %q, %preheader ], [ %q_1, %do-while ]
354 Q_2->addIncoming(Q, Preheader);
355 Q_2->addIncoming(Q_1, DoWhile);
356 // ; %carry_2 = phi i32 [ 0, %bb1 ], [ %carry, %do-while ]
357 Carry_2->addIncoming(Zero, BB1);
358 Carry_2->addIncoming(Carry, DoWhile);
359 // ; %q_3 = phi i32 [ %q, %bb1 ], [ %q_1, %do-while ]
360 Q_3->addIncoming(Q, BB1);
361 Q_3->addIncoming(Q_1, DoWhile);
362 // ; %q_5 = phi i32 [ %q_4, %loop-exit ], [ %retVal, %special-cases ]
363 Q_5->addIncoming(Q_4, LoopExit);
364 Q_5->addIncoming(RetVal, SpecialCases);
365
366 return Q_5;
367}
368
Michael Ilsemana398d4c2012-09-26 01:55:01 +0000369/// Generate code to calculate the remainder of two integers, replacing Rem with
370/// the generated code. This currently generates code using the udiv expansion,
371/// but future work includes generating more specialized code, e.g. when more
Michael Ilsemand930c192013-11-19 06:54:19 +0000372/// information about the operands are known. Implements both 32bit and 64bit
373/// scalar division.
Michael Ilsemana398d4c2012-09-26 01:55:01 +0000374///
375/// @brief Replace Rem with generated code.
376bool llvm::expandRemainder(BinaryOperator *Rem) {
377 assert((Rem->getOpcode() == Instruction::SRem ||
378 Rem->getOpcode() == Instruction::URem) &&
379 "Trying to expand remainder from a non-remainder function");
380
381 IRBuilder<> Builder(Rem);
382
Craig Topper582d8ec2015-12-25 02:04:17 +0000383 assert(!Rem->getType()->isVectorTy() && "Div over vectors not supported");
384 assert((Rem->getType()->getIntegerBitWidth() == 32 ||
385 Rem->getType()->getIntegerBitWidth() == 64) &&
386 "Div of bitwidth other than 32 or 64 not supported");
Michael Ilsemand930c192013-11-19 06:54:19 +0000387
Michael Ilsemana398d4c2012-09-26 01:55:01 +0000388 // First prepare the sign if it's a signed remainder
389 if (Rem->getOpcode() == Instruction::SRem) {
390 Value *Remainder = generateSignedRemainderCode(Rem->getOperand(0),
391 Rem->getOperand(1), Builder);
392
393 Rem->replaceAllUsesWith(Remainder);
394 Rem->dropAllReferences();
395 Rem->eraseFromParent();
396
Michael Ilsemana7202bd2014-11-05 21:28:24 +0000397 // If we didn't actually generate an urem instruction, we're done
398 // This happens for example if the input were constant. In this case the
399 // Builder insertion point was unchanged
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000400 if (Rem == Builder.GetInsertPoint().getNodePtrUnchecked())
Michael Ilsemana398d4c2012-09-26 01:55:01 +0000401 return true;
402
Michael Ilsemana7202bd2014-11-05 21:28:24 +0000403 BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
Michael Ilsemana398d4c2012-09-26 01:55:01 +0000404 Rem = BO;
405 }
406
407 Value *Remainder = generatedUnsignedRemainderCode(Rem->getOperand(0),
408 Rem->getOperand(1),
409 Builder);
410
411 Rem->replaceAllUsesWith(Remainder);
412 Rem->dropAllReferences();
413 Rem->eraseFromParent();
414
415 // Expand the udiv
416 if (BinaryOperator *UDiv = dyn_cast<BinaryOperator>(Builder.GetInsertPoint())) {
417 assert(UDiv->getOpcode() == Instruction::UDiv && "Non-udiv in expansion?");
418 expandDivision(UDiv);
419 }
420
421 return true;
422}
423
424
Michael Ilseman370a1a12012-09-19 16:25:57 +0000425/// Generate code to divide two integers, replacing Div with the generated
426/// code. This currently generates code similarly to compiler-rt's
427/// implementations, but future work includes generating more specialized code
Michael Ilsemand930c192013-11-19 06:54:19 +0000428/// when more information about the operands are known. Implements both
429/// 32bit and 64bit scalar division.
Michael Ilseman370a1a12012-09-19 16:25:57 +0000430///
431/// @brief Replace Div with generated code.
Michael Ilseman1db690d2012-09-19 16:17:20 +0000432bool llvm::expandDivision(BinaryOperator *Div) {
Benjamin Kramer47196e62012-09-19 13:03:07 +0000433 assert((Div->getOpcode() == Instruction::SDiv ||
434 Div->getOpcode() == Instruction::UDiv) &&
435 "Trying to expand division from a non-division function");
Michael Ilseman52059da2012-09-18 22:02:40 +0000436
437 IRBuilder<> Builder(Div);
438
Craig Topper582d8ec2015-12-25 02:04:17 +0000439 assert(!Div->getType()->isVectorTy() && "Div over vectors not supported");
440 assert((Div->getType()->getIntegerBitWidth() == 32 ||
441 Div->getType()->getIntegerBitWidth() == 64) &&
442 "Div of bitwidth other than 32 or 64 not supported");
Michael Ilsemand930c192013-11-19 06:54:19 +0000443
Michael Ilseman52059da2012-09-18 22:02:40 +0000444 // First prepare the sign if it's a signed division
445 if (Div->getOpcode() == Instruction::SDiv) {
446 // Lower the code to unsigned division, and reset Div to point to the udiv.
Michael Ilseman5117db52012-09-19 18:14:45 +0000447 Value *Quotient = generateSignedDivisionCode(Div->getOperand(0),
Michael Ilsemana398d4c2012-09-26 01:55:01 +0000448 Div->getOperand(1), Builder);
Michael Ilseman52059da2012-09-18 22:02:40 +0000449 Div->replaceAllUsesWith(Quotient);
450 Div->dropAllReferences();
451 Div->eraseFromParent();
452
Michael Ilsemana7202bd2014-11-05 21:28:24 +0000453 // If we didn't actually generate an udiv instruction, we're done
454 // This happens for example if the input were constant. In this case the
455 // Builder insertion point was unchanged
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000456 if (Div == Builder.GetInsertPoint().getNodePtrUnchecked())
Michael Ilseman52059da2012-09-18 22:02:40 +0000457 return true;
458
Michael Ilsemana7202bd2014-11-05 21:28:24 +0000459 BinaryOperator *BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint());
Michael Ilseman52059da2012-09-18 22:02:40 +0000460 Div = BO;
461 }
462
463 // Insert the unsigned division code
Michael Ilseman5117db52012-09-19 18:14:45 +0000464 Value *Quotient = generateUnsignedDivisionCode(Div->getOperand(0),
Michael Ilseman52059da2012-09-18 22:02:40 +0000465 Div->getOperand(1),
466 Builder);
467 Div->replaceAllUsesWith(Quotient);
468 Div->dropAllReferences();
469 Div->eraseFromParent();
470
471 return true;
472}
Pedro Artigase40467b2013-02-26 23:33:20 +0000473
474/// Generate code to compute the remainder of two integers of bitwidth up to
475/// 32 bits. Uses the above routines and extends the inputs/truncates the
476/// outputs to operate in 32 bits; that is, these routines are good for targets
477/// that have no or very little suppport for smaller than 32 bit integer
478/// arithmetic.
479///
480/// @brief Replace Rem with emulation code.
481bool llvm::expandRemainderUpTo32Bits(BinaryOperator *Rem) {
482 assert((Rem->getOpcode() == Instruction::SRem ||
483 Rem->getOpcode() == Instruction::URem) &&
484 "Trying to expand remainder from a non-remainder function");
485
486 Type *RemTy = Rem->getType();
Craig Topper582d8ec2015-12-25 02:04:17 +0000487 assert(!RemTy->isVectorTy() && "Div over vectors not supported");
Pedro Artigase40467b2013-02-26 23:33:20 +0000488
489 unsigned RemTyBitWidth = RemTy->getIntegerBitWidth();
490
Craig Topper582d8ec2015-12-25 02:04:17 +0000491 assert(RemTyBitWidth <= 32 &&
492 "Div of bitwidth greater than 32 not supported");
Pedro Artigase40467b2013-02-26 23:33:20 +0000493
Craig Topper582d8ec2015-12-25 02:04:17 +0000494 if (RemTyBitWidth == 32)
Pedro Artigase40467b2013-02-26 23:33:20 +0000495 return expandRemainder(Rem);
496
Michael Ilsemand930c192013-11-19 06:54:19 +0000497 // If bitwidth smaller than 32 extend inputs, extend output and proceed
Pedro Artigase40467b2013-02-26 23:33:20 +0000498 // with 32 bit division.
499 IRBuilder<> Builder(Rem);
500
501 Value *ExtDividend;
502 Value *ExtDivisor;
503 Value *ExtRem;
504 Value *Trunc;
505 Type *Int32Ty = Builder.getInt32Ty();
506
507 if (Rem->getOpcode() == Instruction::SRem) {
508 ExtDividend = Builder.CreateSExt(Rem->getOperand(0), Int32Ty);
509 ExtDivisor = Builder.CreateSExt(Rem->getOperand(1), Int32Ty);
510 ExtRem = Builder.CreateSRem(ExtDividend, ExtDivisor);
511 } else {
512 ExtDividend = Builder.CreateZExt(Rem->getOperand(0), Int32Ty);
513 ExtDivisor = Builder.CreateZExt(Rem->getOperand(1), Int32Ty);
514 ExtRem = Builder.CreateURem(ExtDividend, ExtDivisor);
515 }
516 Trunc = Builder.CreateTrunc(ExtRem, RemTy);
517
518 Rem->replaceAllUsesWith(Trunc);
519 Rem->dropAllReferences();
520 Rem->eraseFromParent();
521
522 return expandRemainder(cast<BinaryOperator>(ExtRem));
523}
524
Michael Ilsemand930c192013-11-19 06:54:19 +0000525/// Generate code to compute the remainder of two integers of bitwidth up to
526/// 64 bits. Uses the above routines and extends the inputs/truncates the
527/// outputs to operate in 64 bits.
528///
529/// @brief Replace Rem with emulation code.
530bool llvm::expandRemainderUpTo64Bits(BinaryOperator *Rem) {
531 assert((Rem->getOpcode() == Instruction::SRem ||
532 Rem->getOpcode() == Instruction::URem) &&
533 "Trying to expand remainder from a non-remainder function");
534
535 Type *RemTy = Rem->getType();
Craig Topper582d8ec2015-12-25 02:04:17 +0000536 assert(!RemTy->isVectorTy() && "Div over vectors not supported");
Michael Ilsemand930c192013-11-19 06:54:19 +0000537
538 unsigned RemTyBitWidth = RemTy->getIntegerBitWidth();
539
Craig Topper582d8ec2015-12-25 02:04:17 +0000540 assert(RemTyBitWidth <= 64 && "Div of bitwidth greater than 64 not supported");
Michael Ilsemand930c192013-11-19 06:54:19 +0000541
Craig Topper582d8ec2015-12-25 02:04:17 +0000542 if (RemTyBitWidth == 64)
Michael Ilsemand930c192013-11-19 06:54:19 +0000543 return expandRemainder(Rem);
544
545 // If bitwidth smaller than 64 extend inputs, extend output and proceed
546 // with 64 bit division.
547 IRBuilder<> Builder(Rem);
548
549 Value *ExtDividend;
550 Value *ExtDivisor;
551 Value *ExtRem;
552 Value *Trunc;
553 Type *Int64Ty = Builder.getInt64Ty();
554
555 if (Rem->getOpcode() == Instruction::SRem) {
556 ExtDividend = Builder.CreateSExt(Rem->getOperand(0), Int64Ty);
557 ExtDivisor = Builder.CreateSExt(Rem->getOperand(1), Int64Ty);
558 ExtRem = Builder.CreateSRem(ExtDividend, ExtDivisor);
559 } else {
560 ExtDividend = Builder.CreateZExt(Rem->getOperand(0), Int64Ty);
561 ExtDivisor = Builder.CreateZExt(Rem->getOperand(1), Int64Ty);
562 ExtRem = Builder.CreateURem(ExtDividend, ExtDivisor);
563 }
564 Trunc = Builder.CreateTrunc(ExtRem, RemTy);
565
566 Rem->replaceAllUsesWith(Trunc);
567 Rem->dropAllReferences();
568 Rem->eraseFromParent();
569
570 return expandRemainder(cast<BinaryOperator>(ExtRem));
571}
Pedro Artigase40467b2013-02-26 23:33:20 +0000572
573/// Generate code to divide two integers of bitwidth up to 32 bits. Uses the
574/// above routines and extends the inputs/truncates the outputs to operate
575/// in 32 bits; that is, these routines are good for targets that have no
576/// or very little support for smaller than 32 bit integer arithmetic.
577///
578/// @brief Replace Div with emulation code.
579bool llvm::expandDivisionUpTo32Bits(BinaryOperator *Div) {
580 assert((Div->getOpcode() == Instruction::SDiv ||
581 Div->getOpcode() == Instruction::UDiv) &&
582 "Trying to expand division from a non-division function");
583
584 Type *DivTy = Div->getType();
Craig Topper582d8ec2015-12-25 02:04:17 +0000585 assert(!DivTy->isVectorTy() && "Div over vectors not supported");
Pedro Artigase40467b2013-02-26 23:33:20 +0000586
587 unsigned DivTyBitWidth = DivTy->getIntegerBitWidth();
588
Craig Topper582d8ec2015-12-25 02:04:17 +0000589 assert(DivTyBitWidth <= 32 && "Div of bitwidth greater than 32 not supported");
Pedro Artigase40467b2013-02-26 23:33:20 +0000590
591 if (DivTyBitWidth == 32)
592 return expandDivision(Div);
593
Michael Ilsemand930c192013-11-19 06:54:19 +0000594 // If bitwidth smaller than 32 extend inputs, extend output and proceed
Pedro Artigase40467b2013-02-26 23:33:20 +0000595 // with 32 bit division.
596 IRBuilder<> Builder(Div);
597
598 Value *ExtDividend;
599 Value *ExtDivisor;
600 Value *ExtDiv;
601 Value *Trunc;
602 Type *Int32Ty = Builder.getInt32Ty();
603
604 if (Div->getOpcode() == Instruction::SDiv) {
605 ExtDividend = Builder.CreateSExt(Div->getOperand(0), Int32Ty);
606 ExtDivisor = Builder.CreateSExt(Div->getOperand(1), Int32Ty);
607 ExtDiv = Builder.CreateSDiv(ExtDividend, ExtDivisor);
608 } else {
609 ExtDividend = Builder.CreateZExt(Div->getOperand(0), Int32Ty);
610 ExtDivisor = Builder.CreateZExt(Div->getOperand(1), Int32Ty);
611 ExtDiv = Builder.CreateUDiv(ExtDividend, ExtDivisor);
612 }
613 Trunc = Builder.CreateTrunc(ExtDiv, DivTy);
614
615 Div->replaceAllUsesWith(Trunc);
616 Div->dropAllReferences();
617 Div->eraseFromParent();
618
619 return expandDivision(cast<BinaryOperator>(ExtDiv));
620}
Michael Ilsemand930c192013-11-19 06:54:19 +0000621
622/// Generate code to divide two integers of bitwidth up to 64 bits. Uses the
623/// above routines and extends the inputs/truncates the outputs to operate
624/// in 64 bits.
625///
626/// @brief Replace Div with emulation code.
627bool llvm::expandDivisionUpTo64Bits(BinaryOperator *Div) {
628 assert((Div->getOpcode() == Instruction::SDiv ||
629 Div->getOpcode() == Instruction::UDiv) &&
630 "Trying to expand division from a non-division function");
631
632 Type *DivTy = Div->getType();
Craig Topper582d8ec2015-12-25 02:04:17 +0000633 assert(!DivTy->isVectorTy() && "Div over vectors not supported");
Michael Ilsemand930c192013-11-19 06:54:19 +0000634
635 unsigned DivTyBitWidth = DivTy->getIntegerBitWidth();
636
Craig Topper582d8ec2015-12-25 02:04:17 +0000637 assert(DivTyBitWidth <= 64 &&
638 "Div of bitwidth greater than 64 not supported");
Michael Ilsemand930c192013-11-19 06:54:19 +0000639
640 if (DivTyBitWidth == 64)
641 return expandDivision(Div);
642
643 // If bitwidth smaller than 64 extend inputs, extend output and proceed
644 // with 64 bit division.
645 IRBuilder<> Builder(Div);
646
647 Value *ExtDividend;
648 Value *ExtDivisor;
649 Value *ExtDiv;
650 Value *Trunc;
651 Type *Int64Ty = Builder.getInt64Ty();
652
653 if (Div->getOpcode() == Instruction::SDiv) {
654 ExtDividend = Builder.CreateSExt(Div->getOperand(0), Int64Ty);
655 ExtDivisor = Builder.CreateSExt(Div->getOperand(1), Int64Ty);
656 ExtDiv = Builder.CreateSDiv(ExtDividend, ExtDivisor);
657 } else {
658 ExtDividend = Builder.CreateZExt(Div->getOperand(0), Int64Ty);
659 ExtDivisor = Builder.CreateZExt(Div->getOperand(1), Int64Ty);
660 ExtDiv = Builder.CreateUDiv(ExtDividend, ExtDivisor);
661 }
662 Trunc = Builder.CreateTrunc(ExtDiv, DivTy);
663
664 Div->replaceAllUsesWith(Trunc);
665 Div->dropAllReferences();
666 Div->eraseFromParent();
667
668 return expandDivision(cast<BinaryOperator>(ExtDiv));
669}