@@ -28,11 +28,11 @@ using namespace llvm;
28
28
// instruction. This will generate a udiv in the process, and Builder's insert
29
29
// point will be pointing at the udiv (if present, i.e. not folded), ready to be
30
30
// expanded if the user wishes.
31
- static Value* GenerateSignedDivisionCode (Value* Dividend, Value* Divisor,
32
- IRBuilder<>& Builder) {
31
+ static Value * GenerateSignedDivisionCode (Value * Dividend, Value * Divisor,
32
+ IRBuilder<> & Builder) {
33
33
// Implementation taken from compiler-rt's __divsi3
34
34
35
- ConstantInt* ThirtyOne = Builder.getInt32 (31 );
35
+ ConstantInt * ThirtyOne = Builder.getInt32 (31 );
36
36
37
37
// ; %tmp = ashr i32 %dividend, 31
38
38
// ; %tmp1 = ashr i32 %divisor, 31
@@ -44,18 +44,18 @@ static Value* GenerateSignedDivisionCode(Value* Dividend, Value* Divisor,
44
44
// ; %q_mag = udiv i32 %u_dvnd, %u_dvsr
45
45
// ; %tmp4 = xor i32 %q_mag, %q_sgn
46
46
// ; %q = sub i32 %tmp4, %q_sgn
47
- Value* Tmp = Builder.CreateAShr (Dividend, ThirtyOne);
48
- Value* Tmp1 = Builder.CreateAShr (Divisor, ThirtyOne);
49
- Value* Tmp2 = Builder.CreateXor (Tmp, Dividend);
50
- Value* U_Dvnd = Builder.CreateSub (Tmp2, Tmp);
51
- Value* Tmp3 = Builder.CreateXor (Tmp1, Divisor);
52
- Value* U_Dvsr = Builder.CreateSub (Tmp3, Tmp1);
53
- Value* Q_Sgn = Builder.CreateXor (Tmp1, Tmp);
54
- Value* Q_Mag = Builder.CreateUDiv (U_Dvnd, U_Dvsr);
55
- Value* Tmp4 = Builder.CreateXor (Q_Mag, Q_Sgn);
56
- Value* Q = Builder.CreateSub (Tmp4, Q_Sgn);
47
+ Value * Tmp = Builder.CreateAShr (Dividend, ThirtyOne);
48
+ Value * Tmp1 = Builder.CreateAShr (Divisor, ThirtyOne);
49
+ Value * Tmp2 = Builder.CreateXor (Tmp, Dividend);
50
+ Value * U_Dvnd = Builder.CreateSub (Tmp2, Tmp);
51
+ Value * Tmp3 = Builder.CreateXor (Tmp1, Divisor);
52
+ Value * U_Dvsr = Builder.CreateSub (Tmp3, Tmp1);
53
+ Value * Q_Sgn = Builder.CreateXor (Tmp1, Tmp);
54
+ Value * Q_Mag = Builder.CreateUDiv (U_Dvnd, U_Dvsr);
55
+ Value * Tmp4 = Builder.CreateXor (Q_Mag, Q_Sgn);
56
+ Value * Q = Builder.CreateSub (Tmp4, Q_Sgn);
57
57
58
- if (Instruction* UDiv = dyn_cast<Instruction>(Q_Mag))
58
+ if (Instruction * UDiv = dyn_cast<Instruction>(Q_Mag))
59
59
Builder.SetInsertPoint (UDiv);
60
60
61
61
return Q;
@@ -64,24 +64,24 @@ static Value* GenerateSignedDivisionCode(Value* Dividend, Value* Divisor,
64
64
// Generates code to divide two unsigned scalar 32-bit integers. Returns the
65
65
// quotient, rounded towards 0. Builder's insert point should be pointing at the
66
66
// udiv instruction.
67
- static Value* GenerateUnsignedDivisionCode (Value* Dividend, Value* Divisor,
68
- IRBuilder<>& Builder) {
67
+ static Value * GenerateUnsignedDivisionCode (Value * Dividend, Value * Divisor,
68
+ IRBuilder<> & Builder) {
69
69
// The basic algorithm can be found in the compiler-rt project's
70
70
// implementation of __udivsi3.c. Here, we do a lower-level IR based approach
71
71
// that's been hand-tuned to lessen the amount of control flow involved.
72
72
73
73
// Some helper values
74
- IntegerType* I32Ty = Builder.getInt32Ty ();
74
+ IntegerType * I32Ty = Builder.getInt32Ty ();
75
75
76
- ConstantInt* Zero = Builder.getInt32 (0 );
77
- ConstantInt* One = Builder.getInt32 (1 );
78
- ConstantInt* ThirtyOne = Builder.getInt32 (31 );
79
- ConstantInt* NegOne = ConstantInt::getSigned (I32Ty, -1 );
80
- ConstantInt* True = Builder.getTrue ();
76
+ ConstantInt * Zero = Builder.getInt32 (0 );
77
+ ConstantInt * One = Builder.getInt32 (1 );
78
+ ConstantInt * ThirtyOne = Builder.getInt32 (31 );
79
+ ConstantInt * NegOne = ConstantInt::getSigned (I32Ty, -1 );
80
+ ConstantInt * True = Builder.getTrue ();
81
81
82
- BasicBlock* IBB = Builder.GetInsertBlock ();
83
- Function* F = IBB->getParent ();
84
- Function* CTLZi32 = Intrinsic::getDeclaration (F->getParent (), Intrinsic::ctlz,
82
+ BasicBlock * IBB = Builder.GetInsertBlock ();
83
+ Function * F = IBB->getParent ();
84
+ Function * CTLZi32 = Intrinsic::getDeclaration (F->getParent (), Intrinsic::ctlz,
85
85
I32Ty);
86
86
87
87
// Our CFG is going to look like:
@@ -116,17 +116,17 @@ static Value* GenerateUnsignedDivisionCode(Value* Dividend, Value* Divisor,
116
116
// | ... |
117
117
// | end |
118
118
// +-------+
119
- BasicBlock* SpecialCases = Builder.GetInsertBlock ();
119
+ BasicBlock * SpecialCases = Builder.GetInsertBlock ();
120
120
SpecialCases->setName (Twine (SpecialCases->getName (), " _udiv-special-cases" ));
121
- BasicBlock* End = SpecialCases->splitBasicBlock (Builder.GetInsertPoint (),
121
+ BasicBlock * End = SpecialCases->splitBasicBlock (Builder.GetInsertPoint (),
122
122
" udiv-end" );
123
- BasicBlock* LoopExit = BasicBlock::Create (Builder.getContext (),
123
+ BasicBlock * LoopExit = BasicBlock::Create (Builder.getContext (),
124
124
" udiv-loop-exit" , F, End);
125
- BasicBlock* DoWhile = BasicBlock::Create (Builder.getContext (),
125
+ BasicBlock * DoWhile = BasicBlock::Create (Builder.getContext (),
126
126
" udiv-do-while" , F, End);
127
- BasicBlock* Preheader = BasicBlock::Create (Builder.getContext (),
127
+ BasicBlock * Preheader = BasicBlock::Create (Builder.getContext (),
128
128
" udiv-preheader" , F, End);
129
- BasicBlock* BB1 = BasicBlock::Create (Builder.getContext (),
129
+ BasicBlock * BB1 = BasicBlock::Create (Builder.getContext (),
130
130
" udiv-bb1" , F, End);
131
131
132
132
// We'll be overwriting the terminator to insert our extra blocks
@@ -148,17 +148,17 @@ static Value* GenerateUnsignedDivisionCode(Value* Dividend, Value* Divisor,
148
148
// ; %earlyRet = or i1 %ret0, %retDividend
149
149
// ; br i1 %earlyRet, label %end, label %bb1
150
150
Builder.SetInsertPoint (SpecialCases);
151
- Value* Ret0_1 = Builder.CreateICmpEQ (Divisor, Zero);
152
- Value* Ret0_2 = Builder.CreateICmpEQ (Dividend, Zero);
153
- Value* Ret0_3 = Builder.CreateOr (Ret0_1, Ret0_2);
154
- Value* Tmp0 = Builder.CreateCall2 (CTLZi32, Divisor, True);
155
- Value* Tmp1 = Builder.CreateCall2 (CTLZi32, Dividend, True);
156
- Value* SR = Builder.CreateSub (Tmp0, Tmp1);
157
- Value* Ret0_4 = Builder.CreateICmpUGT (SR, ThirtyOne);
158
- Value* Ret0 = Builder.CreateOr (Ret0_3, Ret0_4);
159
- Value* RetDividend = Builder.CreateICmpEQ (SR, ThirtyOne);
160
- Value* RetVal = Builder.CreateSelect (Ret0, Zero, Dividend);
161
- Value* EarlyRet = Builder.CreateOr (Ret0, RetDividend);
151
+ Value * Ret0_1 = Builder.CreateICmpEQ (Divisor, Zero);
152
+ Value * Ret0_2 = Builder.CreateICmpEQ (Dividend, Zero);
153
+ Value * Ret0_3 = Builder.CreateOr (Ret0_1, Ret0_2);
154
+ Value * Tmp0 = Builder.CreateCall2 (CTLZi32, Divisor, True);
155
+ Value * Tmp1 = Builder.CreateCall2 (CTLZi32, Dividend, True);
156
+ Value * SR = Builder.CreateSub (Tmp0, Tmp1);
157
+ Value * Ret0_4 = Builder.CreateICmpUGT (SR, ThirtyOne);
158
+ Value * Ret0 = Builder.CreateOr (Ret0_3, Ret0_4);
159
+ Value * RetDividend = Builder.CreateICmpEQ (SR, ThirtyOne);
160
+ Value * RetVal = Builder.CreateSelect (Ret0, Zero, Dividend);
161
+ Value * EarlyRet = Builder.CreateOr (Ret0, RetDividend);
162
162
Builder.CreateCondBr (EarlyRet, End, BB1);
163
163
164
164
// ; bb1: ; preds = %special-cases
@@ -168,19 +168,19 @@ static Value* GenerateUnsignedDivisionCode(Value* Dividend, Value* Divisor,
168
168
// ; %skipLoop = icmp eq i32 %sr_1, 0
169
169
// ; br i1 %skipLoop, label %loop-exit, label %preheader
170
170
Builder.SetInsertPoint (BB1);
171
- Value* SR_1 = Builder.CreateAdd (SR, One);
172
- Value* Tmp2 = Builder.CreateSub (ThirtyOne, SR);
173
- Value* Q = Builder.CreateShl (Dividend, Tmp2);
174
- Value* SkipLoop = Builder.CreateICmpEQ (SR_1, Zero);
171
+ Value * SR_1 = Builder.CreateAdd (SR, One);
172
+ Value * Tmp2 = Builder.CreateSub (ThirtyOne, SR);
173
+ Value * Q = Builder.CreateShl (Dividend, Tmp2);
174
+ Value * SkipLoop = Builder.CreateICmpEQ (SR_1, Zero);
175
175
Builder.CreateCondBr (SkipLoop, LoopExit, Preheader);
176
176
177
177
// ; preheader: ; preds = %bb1
178
178
// ; %tmp3 = lshr i32 %dividend, %sr_1
179
179
// ; %tmp4 = add i32 %divisor, -1
180
180
// ; br label %do-while
181
181
Builder.SetInsertPoint (Preheader);
182
- Value* Tmp3 = Builder.CreateLShr (Dividend, SR_1);
183
- Value* Tmp4 = Builder.CreateAdd (Divisor, NegOne);
182
+ Value * Tmp3 = Builder.CreateLShr (Dividend, SR_1);
183
+ Value * Tmp4 = Builder.CreateAdd (Divisor, NegOne);
184
184
Builder.CreateBr (DoWhile);
185
185
186
186
// ; do-while: ; preds = %do-while, %preheader
@@ -202,22 +202,22 @@ static Value* GenerateUnsignedDivisionCode(Value* Dividend, Value* Divisor,
202
202
// ; %tmp12 = icmp eq i32 %sr_2, 0
203
203
// ; br i1 %tmp12, label %loop-exit, label %do-while
204
204
Builder.SetInsertPoint (DoWhile);
205
- PHINode* Carry_1 = Builder.CreatePHI (I32Ty, 2 );
206
- PHINode* SR_3 = Builder.CreatePHI (I32Ty, 2 );
207
- PHINode* R_1 = Builder.CreatePHI (I32Ty, 2 );
208
- PHINode* Q_2 = Builder.CreatePHI (I32Ty, 2 );
209
- Value* Tmp5 = Builder.CreateShl (R_1, One);
210
- Value* Tmp6 = Builder.CreateLShr (Q_2, ThirtyOne);
211
- Value* Tmp7 = Builder.CreateOr (Tmp5, Tmp6);
212
- Value* Tmp8 = Builder.CreateShl (Q_2, One);
213
- Value* Q_1 = Builder.CreateOr (Carry_1, Tmp8);
214
- Value* Tmp9 = Builder.CreateSub (Tmp4, Tmp7);
215
- Value* Tmp10 = Builder.CreateAShr (Tmp9, 31 );
216
- Value* Carry = Builder.CreateAnd (Tmp10, One);
217
- Value* Tmp11 = Builder.CreateAnd (Tmp10, Divisor);
218
- Value* R = Builder.CreateSub (Tmp7, Tmp11);
219
- Value* SR_2 = Builder.CreateAdd (SR_3, NegOne);
220
- Value* Tmp12 = Builder.CreateICmpEQ (SR_2, Zero);
205
+ PHINode * Carry_1 = Builder.CreatePHI (I32Ty, 2 );
206
+ PHINode * SR_3 = Builder.CreatePHI (I32Ty, 2 );
207
+ PHINode * R_1 = Builder.CreatePHI (I32Ty, 2 );
208
+ PHINode * Q_2 = Builder.CreatePHI (I32Ty, 2 );
209
+ Value * Tmp5 = Builder.CreateShl (R_1, One);
210
+ Value * Tmp6 = Builder.CreateLShr (Q_2, ThirtyOne);
211
+ Value * Tmp7 = Builder.CreateOr (Tmp5, Tmp6);
212
+ Value * Tmp8 = Builder.CreateShl (Q_2, One);
213
+ Value * Q_1 = Builder.CreateOr (Carry_1, Tmp8);
214
+ Value * Tmp9 = Builder.CreateSub (Tmp4, Tmp7);
215
+ Value * Tmp10 = Builder.CreateAShr (Tmp9, 31 );
216
+ Value * Carry = Builder.CreateAnd (Tmp10, One);
217
+ Value * Tmp11 = Builder.CreateAnd (Tmp10, Divisor);
218
+ Value * R = Builder.CreateSub (Tmp7, Tmp11);
219
+ Value * SR_2 = Builder.CreateAdd (SR_3, NegOne);
220
+ Value * Tmp12 = Builder.CreateICmpEQ (SR_2, Zero);
221
221
Builder.CreateCondBr (Tmp12, LoopExit, DoWhile);
222
222
223
223
// ; loop-exit: ; preds = %do-while, %bb1
@@ -227,17 +227,17 @@ static Value* GenerateUnsignedDivisionCode(Value* Dividend, Value* Divisor,
227
227
// ; %q_4 = or i32 %carry_2, %tmp13
228
228
// ; br label %end
229
229
Builder.SetInsertPoint (LoopExit);
230
- PHINode* Carry_2 = Builder.CreatePHI (I32Ty, 2 );
231
- PHINode* Q_3 = Builder.CreatePHI (I32Ty, 2 );
232
- Value* Tmp13 = Builder.CreateShl (Q_3, One);
233
- Value* Q_4 = Builder.CreateOr (Carry_2, Tmp13);
230
+ PHINode * Carry_2 = Builder.CreatePHI (I32Ty, 2 );
231
+ PHINode * Q_3 = Builder.CreatePHI (I32Ty, 2 );
232
+ Value * Tmp13 = Builder.CreateShl (Q_3, One);
233
+ Value * Q_4 = Builder.CreateOr (Carry_2, Tmp13);
234
234
Builder.CreateBr (End);
235
235
236
236
// ; end: ; preds = %loop-exit, %special-cases
237
237
// ; %q_5 = phi i32 [ %q_4, %loop-exit ], [ %retVal, %special-cases ]
238
238
// ; ret i32 %q_5
239
239
Builder.SetInsertPoint (End, End->begin ());
240
- PHINode* Q_5 = Builder.CreatePHI (I32Ty, 2 );
240
+ PHINode * Q_5 = Builder.CreatePHI (I32Ty, 2 );
241
241
242
242
// Populate the Phis, since all values have now been created. Our Phis were:
243
243
// ; %carry_1 = phi i32 [ 0, %preheader ], [ %carry, %do-while ]
@@ -265,7 +265,7 @@ static Value* GenerateUnsignedDivisionCode(Value* Dividend, Value* Divisor,
265
265
return Q_5;
266
266
}
267
267
268
- bool llvm::expandDivision (BinaryOperator* Div) {
268
+ bool llvm::expandDivision (BinaryOperator * Div) {
269
269
assert ((Div->getOpcode () == Instruction::SDiv ||
270
270
Div->getOpcode () == Instruction::UDiv) &&
271
271
" Trying to expand division from a non-division function" );
@@ -278,22 +278,22 @@ bool llvm::expandDivision(BinaryOperator* Div) {
278
278
// First prepare the sign if it's a signed division
279
279
if (Div->getOpcode () == Instruction::SDiv) {
280
280
// Lower the code to unsigned division, and reset Div to point to the udiv.
281
- Value* Quotient = GenerateSignedDivisionCode (Div->getOperand (0 ),
281
+ Value * Quotient = GenerateSignedDivisionCode (Div->getOperand (0 ),
282
282
Div->getOperand (1 ), Builder);
283
283
Div->replaceAllUsesWith (Quotient);
284
284
Div->dropAllReferences ();
285
285
Div->eraseFromParent ();
286
286
287
287
// If we didn't actually generate a udiv instruction, we're done
288
- BinaryOperator* BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint ());
288
+ BinaryOperator * BO = dyn_cast<BinaryOperator>(Builder.GetInsertPoint ());
289
289
if (!BO || BO->getOpcode () != Instruction::UDiv)
290
290
return true ;
291
291
292
292
Div = BO;
293
293
}
294
294
295
295
// Insert the unsigned division code
296
- Value* Quotient = GenerateUnsignedDivisionCode (Div->getOperand (0 ),
296
+ Value * Quotient = GenerateUnsignedDivisionCode (Div->getOperand (0 ),
297
297
Div->getOperand (1 ),
298
298
Builder);
299
299
Div->replaceAllUsesWith (Quotient);
0 commit comments