llvm.org GIT mirror llvm / ad4da0f
Move the SplatByte helper to APInt and generalize it a bit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175621 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 6 years ago
4 changed file(s) with 29 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
504504 return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
505505 }
506506
507 /// \brief Return a value containing V broadcasted over NewLen bits.
508 static APInt getSplat(unsigned NewLen, const APInt &V) {
509 assert(NewLen >= V.getBitWidth() && "Can't splat to smaller bit width!");
510
511 APInt Val = V.zextOrSelf(NewLen);
512 for (unsigned I = V.getBitWidth(); I < NewLen; I <<= 1)
513 Val |= Val << I;
514
515 return Val;
516 }
517
507518 /// \brief Determine if two APInts have the same value, after zero-extending
508519 /// one of them (if needed!) to ensure that the bit-widths match.
509520 static bool isSameValue(const APInt &I1, const APInt &I2) {
25382538 }
25392539 }
25402540
2541 /// SplatByte - Distribute ByteVal over NumBits bits.
2542 // FIXME: Move this helper to a common place.
2543 static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
2544 APInt Val = APInt(NumBits, ByteVal);
2545 unsigned Shift = 8;
2546 for (unsigned i = NumBits; i > 8; i >>= 1) {
2547 Val = (Val << Shift) | Val;
2548 Shift <<= 1;
2549 }
2550 return Val;
2551 }
2552
25532541 /// ExpandBitCount - Expand the specified bitcount instruction into operations.
25542542 ///
25552543 SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op,
25672555 // This is the "best" algorithm from
25682556 // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
25692557
2570 SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT);
2571 SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT);
2572 SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT);
2573 SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT);
2558 SDValue Mask55 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x55)), VT);
2559 SDValue Mask33 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x33)), VT);
2560 SDValue Mask0F = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x0F)), VT);
2561 SDValue Mask01 = DAG.getConstant(APInt::getSplat(Len, APInt(8, 0x01)), VT);
25742562
25752563 // v = v - ((v >> 1) & 0x55555555...)
25762564 Op = DAG.getNode(ISD::SUB, dl, VT, Op,
33763376 &ArgChains[0], ArgChains.size());
33773377 }
33783378
3379 /// SplatByte - Distribute ByteVal over NumBits bits.
3380 static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) {
3381 APInt Val = APInt(NumBits, ByteVal);
3382 unsigned Shift = 8;
3383 for (unsigned i = NumBits; i > 8; i >>= 1) {
3384 Val = (Val << Shift) | Val;
3385 Shift <<= 1;
3386 }
3387 return Val;
3388 }
3389
33903379 /// getMemsetValue - Vectorized representation of the memset value
33913380 /// operand.
33923381 static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
33953384
33963385 unsigned NumBits = VT.getScalarType().getSizeInBits();
33973386 if (ConstantSDNode *C = dyn_cast(Value)) {
3398 APInt Val = SplatByte(NumBits, C->getZExtValue() & 255);
3387 assert(C->getAPIntValue().getBitWidth() == 8);
3388 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
33993389 if (VT.isInteger())
34003390 return DAG.getConstant(Val, VT);
34013391 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), VT);
34053395 if (NumBits > 8) {
34063396 // Use a multiplication with 0x010101... to extend the input to the
34073397 // required length.
3408 APInt Magic = SplatByte(NumBits, 0x01);
3398 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
34093399 Value = DAG.getNode(ISD::MUL, dl, VT, Value, DAG.getConstant(Magic, VT));
34103400 }
34113401
521521 EXPECT_EQ(Rot, Big.rotr(144));
522522 }
523523
524 }
524 TEST(APIntTest, Splat) {
525 APInt ValA(8, 0x01);
526 EXPECT_EQ(ValA, APInt::getSplat(8, ValA));
527 EXPECT_EQ(APInt(64, 0x0101010101010101ULL), APInt::getSplat(64, ValA));
528
529 APInt ValB(3, 5);
530 EXPECT_EQ(APInt(4, 0xD), APInt::getSplat(4, ValB));
531 EXPECT_EQ(APInt(15, 0xDB6D), APInt::getSplat(15, ValB));
532 }
533
534 }