36
36
#include " llvm/Support/Casting.h"
37
37
#include " llvm/Support/Debug.h"
38
38
#include " llvm/Support/KnownBits.h"
39
+ #include " llvm/Support/MathExtras.h"
39
40
#include " llvm/Support/raw_ostream.h"
40
41
#include < algorithm>
41
42
#include < cstdint>
@@ -164,10 +165,24 @@ void DemandedBits::determineLiveOperandBits(
164
165
}
165
166
break ;
166
167
case Instruction::Mul:
167
- // Find the highest live output bit. We don't need any more input
168
- // bits than that (adds, and thus subtracts, ripple only to the
169
- // left).
170
- AB = APInt::getLowBitsSet (BitWidth, AOut.getActiveBits ());
168
+ const APInt *C;
169
+ if (OperandNo == 0 ) {
170
+ // to have output bits 0...H-1 we need the input bits
171
+ // 0...(H - ceiling(log_2))
172
+ if (match (UserI->getOperand (1 ), m_APInt (C))) {
173
+ auto LogC = C->isOne () ? 0 : C->logBase2 () + 1 ;
174
+ unsigned Need =
175
+ AOut.getActiveBits () > LogC ? AOut.getActiveBits () - LogC : 0 ;
176
+ AB = APInt::getLowBitsSet (BitWidth, Need);
177
+ } else { // TODO: we can possibly check for Op0 constant too
178
+ AB = APInt::getLowBitsSet (BitWidth, AOut.getActiveBits ());
179
+ }
180
+ } else {
181
+ // Find the highest live output bit. We don't need any more input
182
+ // bits than that (adds, and thus subtracts, ripple only to the
183
+ // left).
184
+ AB = APInt::getLowBitsSet (BitWidth, AOut.getActiveBits ());
185
+ }
171
186
break ;
172
187
case Instruction::Shl:
173
188
if (OperandNo == 0 ) {
@@ -183,6 +198,17 @@ void DemandedBits::determineLiveOperandBits(
183
198
AB |= APInt::getHighBitsSet (BitWidth, ShiftAmt+1 );
184
199
else if (S->hasNoUnsignedWrap ())
185
200
AB |= APInt::getHighBitsSet (BitWidth, ShiftAmt);
201
+ } else {
202
+ ComputeKnownBits (BitWidth, UserI->getOperand (1 ), nullptr );
203
+ unsigned Min = Known.getMinValue ().getLimitedValue (BitWidth - 1 );
204
+ unsigned Max = Known.getMaxValue ().getLimitedValue (BitWidth - 1 );
205
+ // similar to Lshr case
206
+ AB = (AOut.lshr (Min) | AOut.lshr (Max));
207
+ const auto *S = cast<ShlOperator>(UserI);
208
+ if (S->hasNoSignedWrap ())
209
+ AB |= APInt::getHighBitsSet (BitWidth, Max + 1 );
210
+ else if (S->hasNoUnsignedWrap ())
211
+ AB |= APInt::getHighBitsSet (BitWidth, Max);
186
212
}
187
213
}
188
214
break ;
@@ -197,6 +223,19 @@ void DemandedBits::determineLiveOperandBits(
197
223
// (they must be zero).
198
224
if (cast<LShrOperator>(UserI)->isExact ())
199
225
AB |= APInt::getLowBitsSet (BitWidth, ShiftAmt);
226
+ } else {
227
+ ComputeKnownBits (BitWidth, UserI->getOperand (1 ), nullptr );
228
+ unsigned Min = Known.getMinValue ().getLimitedValue (BitWidth - 1 );
229
+ unsigned Max = Known.getMaxValue ().getLimitedValue (BitWidth - 1 );
230
+ // Suppose AOut == 0b0000 0011
231
+ // [min, max] = [1, 3]
232
+ // shift by 1 we get 0b0000 0110
233
+ // shift by 2 we get 0b0000 1100
234
+ // shift by 3 we get 0b0001 1000
235
+ // we take the or here because need to cover all the above possibilities
236
+ AB = (AOut.shl (Min) | AOut.shl (Max));
237
+ if (cast<LShrOperator>(UserI)->isExact ())
238
+ AB |= APInt::getLowBitsSet (BitWidth, Max);
200
239
}
201
240
}
202
241
break ;
@@ -217,6 +256,27 @@ void DemandedBits::determineLiveOperandBits(
217
256
// (they must be zero).
218
257
if (cast<AShrOperator>(UserI)->isExact ())
219
258
AB |= APInt::getLowBitsSet (BitWidth, ShiftAmt);
259
+ } else {
260
+ ComputeKnownBits (BitWidth, UserI->getOperand (1 ), nullptr );
261
+ unsigned Min = Known.getMinValue ().getLimitedValue (BitWidth - 1 );
262
+ unsigned Max = Known.getMaxValue ().getLimitedValue (BitWidth - 1 );
263
+ AB = (AOut.shl (Min) | AOut.shl (Max));
264
+
265
+ if (Max) {
266
+ // Suppose AOut = 0011 1100
267
+ // [min, max] = [1, 3]
268
+ // ShiftAmount = 1 : Mask is 1000 0000
269
+ // ShiftAmount = 2 : Mask is 1100 0000
270
+ // ShiftAmount = 3 : Mask is 1110 0000
271
+ // The Mask with Max covers every case in [min, max],
272
+ // so we are done
273
+ if ((AOut & APInt::getHighBitsSet (BitWidth, Max)).getBoolValue ())
274
+ AB.setSignBit ();
275
+ }
276
+ // If the shift is exact, then the low bits are not dead
277
+ // (they must be zero).
278
+ if (cast<AShrOperator>(UserI)->isExact ())
279
+ AB |= APInt::getLowBitsSet (BitWidth, Max);
220
280
}
221
281
}
222
282
break ;
@@ -246,6 +306,35 @@ void DemandedBits::determineLiveOperandBits(
246
306
else
247
307
AB &= ~(Known.One & ~Known2.One );
248
308
break ;
309
+ case Instruction::UDiv:
310
+ case Instruction::URem:
311
+ case Instruction::SDiv:
312
+ case Instruction::SRem: {
313
+ auto Opc = UserI->getOpcode ();
314
+ auto IsDiv = Opc == Instruction::UDiv || Opc == Instruction::SDiv;
315
+ bool IsSigned = Opc == Instruction::SDiv || Opc == Instruction::SRem;
316
+ if (OperandNo == 0 ) {
317
+ const APInt *DivAmnt;
318
+ if (match (UserI->getOperand (1 ), m_APInt (DivAmnt))) {
319
+ uint64_t D = DivAmnt->getZExtValue ();
320
+ if (isPowerOf2_64 (D)) {
321
+ unsigned Sh = Log2_64 (D);
322
+ if (IsDiv) {
323
+ AB = AOut.shl (Sh);
324
+ } else {
325
+ AB = AOut & APInt::getLowBitsSet (BitWidth, Sh);
326
+ }
327
+ } else { // Non power of 2 constant div
328
+ unsigned LowQ = AOut.getActiveBits ();
329
+ unsigned Need = LowQ + Log2_64_Ceil (D);
330
+ if (IsSigned)
331
+ Need++;
332
+ AB = APInt::getLowBitsSet (BitWidth, std::min (BitWidth, Need));
333
+ }
334
+ }
335
+ }
336
+ break ;
337
+ }
249
338
case Instruction::Xor:
250
339
case Instruction::PHI:
251
340
AB = AOut;
0 commit comments