@@ -1748,148 +1748,75 @@ void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
1748
1748
llvm_unreachable (" Not scalar type found in printScalarConstant()" );
1749
1749
}
1750
1750
1751
- // These utility functions assure we get the right sequence of bytes for a given
1752
- // type even for big-endian machines
1753
- template <typename T> static void ConvertIntToBytes (unsigned char *p, T val) {
1754
- int64_t vp = (int64_t )val;
1755
- for (unsigned i = 0 ; i < sizeof (T); ++i) {
1756
- p[i] = (unsigned char )vp;
1757
- vp >>= 8 ;
1758
- }
1759
- }
1760
- static void ConvertFloatToBytes (unsigned char *p, float val) {
1761
- int32_t *vp = (int32_t *)&val;
1762
- for (unsigned i = 0 ; i < sizeof (int32_t ); ++i) {
1763
- p[i] = (unsigned char )*vp;
1764
- *vp >>= 8 ;
1765
- }
1766
- }
1767
- static void ConvertDoubleToBytes (unsigned char *p, double val) {
1768
- int64_t *vp = (int64_t *)&val;
1769
- for (unsigned i = 0 ; i < sizeof (int64_t ); ++i) {
1770
- p[i] = (unsigned char )*vp;
1771
- *vp >>= 8 ;
1772
- }
1773
- }
1774
-
1775
1751
void NVPTXAsmPrinter::bufferLEByte (const Constant *CPV, int Bytes,
1776
- AggBuffer *aggBuffer ) {
1752
+ AggBuffer *AggBuffer ) {
1777
1753
const DataLayout &DL = getDataLayout ();
1778
-
1754
+ int AllocSize = DL. getTypeAllocSize (CPV-> getType ());
1779
1755
if (isa<UndefValue>(CPV) || CPV->isNullValue ()) {
1780
- int s = DL.getTypeAllocSize (CPV->getType ());
1781
- if (s < Bytes)
1782
- s = Bytes;
1783
- aggBuffer->addZeros (s);
1756
+ // Non-zero Bytes indicates that we need to zero-fill everything. Otherwise,
1757
+ // only the space allocated by CPV.
1758
+ AggBuffer->addZeros (Bytes ? Bytes : AllocSize);
1784
1759
return ;
1785
1760
}
1786
1761
1787
- unsigned char ptr[8 ];
1788
- switch (CPV->getType ()->getTypeID ()) {
1762
+ // Helper for filling AggBuffer with APInts.
1763
+ auto AddIntToBuffer = [AggBuffer, Bytes](const APInt &Val) {
1764
+ size_t NumBytes = (Val.getBitWidth () + 7 ) / 8 ;
1765
+ SmallVector<unsigned char , 16 > Buf (NumBytes);
1766
+ for (unsigned I = 0 ; I < NumBytes; ++I) {
1767
+ Buf[I] = Val.extractBitsAsZExtValue (8 , I * 8 );
1768
+ }
1769
+ AggBuffer->addBytes (Buf.data (), NumBytes, Bytes);
1770
+ };
1789
1771
1790
- case Type::IntegerTyID: {
1791
- Type *ETy = CPV->getType ();
1792
- if (ETy == Type::getInt8Ty (CPV->getContext ())) {
1793
- unsigned char c = (unsigned char )cast<ConstantInt>(CPV)->getZExtValue ();
1794
- ConvertIntToBytes<>(ptr, c);
1795
- aggBuffer->addBytes (ptr, 1 , Bytes);
1796
- } else if (ETy == Type::getInt16Ty (CPV->getContext ())) {
1797
- short int16 = (short )cast<ConstantInt>(CPV)->getZExtValue ();
1798
- ConvertIntToBytes<>(ptr, int16);
1799
- aggBuffer->addBytes (ptr, 2 , Bytes);
1800
- } else if (ETy == Type::getInt32Ty (CPV->getContext ())) {
1801
- if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
1802
- int int32 = (int )(constInt->getZExtValue ());
1803
- ConvertIntToBytes<>(ptr, int32);
1804
- aggBuffer->addBytes (ptr, 4 , Bytes);
1772
+ switch (CPV->getType ()->getTypeID ()) {
1773
+ case Type::IntegerTyID:
1774
+ if (const auto CI = dyn_cast<ConstantInt>(CPV)) {
1775
+ AddIntToBuffer (CI->getValue ());
1776
+ break ;
1777
+ }
1778
+ if (const auto *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1779
+ if (const auto *CI =
1780
+ dyn_cast<ConstantInt>(ConstantFoldConstant (Cexpr, DL))) {
1781
+ AddIntToBuffer (CI->getValue ());
1805
1782
break ;
1806
- } else if (const auto *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1807
- if (const auto *constInt = dyn_cast<ConstantInt>(
1808
- ConstantFoldConstant (Cexpr, DL))) {
1809
- int int32 = (int )(constInt->getZExtValue ());
1810
- ConvertIntToBytes<>(ptr, int32);
1811
- aggBuffer->addBytes (ptr, 4 , Bytes);
1812
- break ;
1813
- }
1814
- if (Cexpr->getOpcode () == Instruction::PtrToInt) {
1815
- Value *v = Cexpr->getOperand (0 )->stripPointerCasts ();
1816
- aggBuffer->addSymbol (v, Cexpr->getOperand (0 ));
1817
- aggBuffer->addZeros (4 );
1818
- break ;
1819
- }
1820
1783
}
1821
- llvm_unreachable (" unsupported integer const type" );
1822
- } else if (ETy == Type::getInt64Ty (CPV->getContext ())) {
1823
- if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
1824
- long long int64 = (long long )(constInt->getZExtValue ());
1825
- ConvertIntToBytes<>(ptr, int64);
1826
- aggBuffer->addBytes (ptr, 8 , Bytes);
1784
+ if (Cexpr->getOpcode () == Instruction::PtrToInt) {
1785
+ Value *V = Cexpr->getOperand (0 )->stripPointerCasts ();
1786
+ AggBuffer->addSymbol (V, Cexpr->getOperand (0 ));
1787
+ AggBuffer->addZeros (AllocSize);
1827
1788
break ;
1828
- } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1829
- if (const auto *constInt = dyn_cast<ConstantInt>(
1830
- ConstantFoldConstant (Cexpr, DL))) {
1831
- long long int64 = (long long )(constInt->getZExtValue ());
1832
- ConvertIntToBytes<>(ptr, int64);
1833
- aggBuffer->addBytes (ptr, 8 , Bytes);
1834
- break ;
1835
- }
1836
- if (Cexpr->getOpcode () == Instruction::PtrToInt) {
1837
- Value *v = Cexpr->getOperand (0 )->stripPointerCasts ();
1838
- aggBuffer->addSymbol (v, Cexpr->getOperand (0 ));
1839
- aggBuffer->addZeros (8 );
1840
- break ;
1841
- }
1842
1789
}
1843
- llvm_unreachable (" unsupported integer const type" );
1844
- } else
1845
- llvm_unreachable (" unsupported integer const type" );
1790
+ }
1791
+ llvm_unreachable (" unsupported integer const type" );
1846
1792
break ;
1847
- }
1793
+
1848
1794
case Type::HalfTyID:
1849
1795
case Type::FloatTyID:
1850
- case Type::DoubleTyID: {
1851
- const auto *CFP = cast<ConstantFP>(CPV);
1852
- Type *Ty = CFP->getType ();
1853
- if (Ty == Type::getHalfTy (CPV->getContext ())) {
1854
- APInt API = CFP->getValueAPF ().bitcastToAPInt ();
1855
- uint16_t float16 = API.getLoBits (16 ).getZExtValue ();
1856
- ConvertIntToBytes<>(ptr, float16);
1857
- aggBuffer->addBytes (ptr, 2 , Bytes);
1858
- } else if (Ty == Type::getFloatTy (CPV->getContext ())) {
1859
- float float32 = (float ) CFP->getValueAPF ().convertToFloat ();
1860
- ConvertFloatToBytes (ptr, float32);
1861
- aggBuffer->addBytes (ptr, 4 , Bytes);
1862
- } else if (Ty == Type::getDoubleTy (CPV->getContext ())) {
1863
- double float64 = CFP->getValueAPF ().convertToDouble ();
1864
- ConvertDoubleToBytes (ptr, float64);
1865
- aggBuffer->addBytes (ptr, 8 , Bytes);
1866
- } else {
1867
- llvm_unreachable (" unsupported fp const type" );
1868
- }
1796
+ case Type::DoubleTyID:
1797
+ AddIntToBuffer (cast<ConstantFP>(CPV)->getValueAPF ().bitcastToAPInt ());
1869
1798
break ;
1870
- }
1799
+
1871
1800
case Type::PointerTyID: {
1872
1801
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
1873
- aggBuffer ->addSymbol (GVar, GVar);
1802
+ AggBuffer ->addSymbol (GVar, GVar);
1874
1803
} else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1875
1804
const Value *v = Cexpr->stripPointerCasts ();
1876
- aggBuffer ->addSymbol (v, Cexpr);
1805
+ AggBuffer ->addSymbol (v, Cexpr);
1877
1806
}
1878
- unsigned int s = DL.getTypeAllocSize (CPV->getType ());
1879
- aggBuffer->addZeros (s);
1807
+ AggBuffer->addZeros (AllocSize);
1880
1808
break ;
1881
1809
}
1882
1810
1883
1811
case Type::ArrayTyID:
1884
1812
case Type::FixedVectorTyID:
1885
1813
case Type::StructTyID: {
1886
1814
if (isa<ConstantAggregate>(CPV) || isa<ConstantDataSequential>(CPV)) {
1887
- int ElementSize = DL.getTypeAllocSize (CPV->getType ());
1888
- bufferAggregateConstant (CPV, aggBuffer);
1889
- if (Bytes > ElementSize)
1890
- aggBuffer->addZeros (Bytes - ElementSize);
1815
+ bufferAggregateConstant (CPV, AggBuffer);
1816
+ if (Bytes > AllocSize)
1817
+ AggBuffer->addZeros (Bytes - AllocSize);
1891
1818
} else if (isa<ConstantAggregateZero>(CPV))
1892
- aggBuffer ->addZeros (Bytes);
1819
+ AggBuffer ->addZeros (Bytes);
1893
1820
else
1894
1821
llvm_unreachable (" Unexpected Constant type" );
1895
1822
break ;
0 commit comments