diff --git a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp --- a/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ b/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -1748,135 +1748,62 @@ llvm_unreachable("Not scalar type found in printScalarConstant()"); } -// These utility functions assure we get the right sequence of bytes for a given -// type even for big-endian machines -template static void ConvertIntToBytes(unsigned char *p, T val) { - int64_t vp = (int64_t)val; - for (unsigned i = 0; i < sizeof(T); ++i) { - p[i] = (unsigned char)vp; - vp >>= 8; - } -} -static void ConvertFloatToBytes(unsigned char *p, float val) { - int32_t *vp = (int32_t *)&val; - for (unsigned i = 0; i < sizeof(int32_t); ++i) { - p[i] = (unsigned char)*vp; - *vp >>= 8; - } -} -static void ConvertDoubleToBytes(unsigned char *p, double val) { - int64_t *vp = (int64_t *)&val; - for (unsigned i = 0; i < sizeof(int64_t); ++i) { - p[i] = (unsigned char)*vp; - *vp >>= 8; - } -} - void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes, - AggBuffer *aggBuffer) { + AggBuffer *AggBuffer) { const DataLayout &DL = getDataLayout(); - + int AllocSize = DL.getTypeAllocSize(CPV->getType()); if (isa(CPV) || CPV->isNullValue()) { - int s = DL.getTypeAllocSize(CPV->getType()); - if (s < Bytes) - s = Bytes; - aggBuffer->addZeros(s); + AggBuffer->addZeros(std::max(AllocSize, Bytes)); return; } - unsigned char ptr[8]; + // Helper for filling AggBuffer with APInts. + auto AddIntToBuffer = [AggBuffer, Bytes](const APInt &Val) { + std::array Buf; + size_t NumBytes = (Val.getBitWidth() + 7) / 8; + assert(NumBytes <= Buf.size() && "value is too large."); + for (unsigned i = 0; i < NumBytes; ++i) { + Buf[i] = Val.extractBitsAsZExtValue(8, i * 8); + } + AggBuffer->addBytes(Buf.data(), NumBytes, Bytes); + }; + switch (CPV->getType()->getTypeID()) { - - case Type::IntegerTyID: { - Type *ETy = CPV->getType(); - if (ETy == Type::getInt8Ty(CPV->getContext())) { - unsigned char c = (unsigned char)cast(CPV)->getZExtValue(); - ConvertIntToBytes<>(ptr, c); - aggBuffer->addBytes(ptr, 1, Bytes); - } else if (ETy == Type::getInt16Ty(CPV->getContext())) { - short int16 = (short)cast(CPV)->getZExtValue(); - ConvertIntToBytes<>(ptr, int16); - aggBuffer->addBytes(ptr, 2, Bytes); - } else if (ETy == Type::getInt32Ty(CPV->getContext())) { - if (const ConstantInt *constInt = dyn_cast(CPV)) { - int int32 = (int)(constInt->getZExtValue()); - ConvertIntToBytes<>(ptr, int32); - aggBuffer->addBytes(ptr, 4, Bytes); + case Type::IntegerTyID: + if (const auto CI = dyn_cast(CPV)) { + AddIntToBuffer(CI->getValue()); + break; + } + if (const auto *Cexpr = dyn_cast(CPV)) { + if (const auto *CI = + dyn_cast(ConstantFoldConstant(Cexpr, DL))) { + AddIntToBuffer(CI->getValue()); break; - } else if (const auto *Cexpr = dyn_cast(CPV)) { - if (const auto *constInt = dyn_cast( - ConstantFoldConstant(Cexpr, DL))) { - int int32 = (int)(constInt->getZExtValue()); - ConvertIntToBytes<>(ptr, int32); - aggBuffer->addBytes(ptr, 4, Bytes); - break; - } - if (Cexpr->getOpcode() == Instruction::PtrToInt) { - Value *v = Cexpr->getOperand(0)->stripPointerCasts(); - aggBuffer->addSymbol(v, Cexpr->getOperand(0)); - aggBuffer->addZeros(4); - break; - } } - llvm_unreachable("unsupported integer const type"); - } else if (ETy == Type::getInt64Ty(CPV->getContext())) { - if (const ConstantInt *constInt = dyn_cast(CPV)) { - long long int64 = (long long)(constInt->getZExtValue()); - ConvertIntToBytes<>(ptr, int64); - aggBuffer->addBytes(ptr, 8, Bytes); + if (Cexpr->getOpcode() == Instruction::PtrToInt) { + Value *V = Cexpr->getOperand(0)->stripPointerCasts(); + AggBuffer->addSymbol(V, Cexpr->getOperand(0)); + AggBuffer->addZeros(AllocSize); break; - } else if (const ConstantExpr *Cexpr = dyn_cast(CPV)) { - if (const auto *constInt = dyn_cast( - ConstantFoldConstant(Cexpr, DL))) { - long long int64 = (long long)(constInt->getZExtValue()); - ConvertIntToBytes<>(ptr, int64); - aggBuffer->addBytes(ptr, 8, Bytes); - break; - } - if (Cexpr->getOpcode() == Instruction::PtrToInt) { - Value *v = Cexpr->getOperand(0)->stripPointerCasts(); - aggBuffer->addSymbol(v, Cexpr->getOperand(0)); - aggBuffer->addZeros(8); - break; - } } - llvm_unreachable("unsupported integer const type"); - } else - llvm_unreachable("unsupported integer const type"); + } + llvm_unreachable("unsupported integer const type"); break; - } + case Type::HalfTyID: case Type::FloatTyID: - case Type::DoubleTyID: { - const auto *CFP = cast(CPV); - Type *Ty = CFP->getType(); - if (Ty == Type::getHalfTy(CPV->getContext())) { - APInt API = CFP->getValueAPF().bitcastToAPInt(); - uint16_t float16 = API.getLoBits(16).getZExtValue(); - ConvertIntToBytes<>(ptr, float16); - aggBuffer->addBytes(ptr, 2, Bytes); - } else if (Ty == Type::getFloatTy(CPV->getContext())) { - float float32 = (float) CFP->getValueAPF().convertToFloat(); - ConvertFloatToBytes(ptr, float32); - aggBuffer->addBytes(ptr, 4, Bytes); - } else if (Ty == Type::getDoubleTy(CPV->getContext())) { - double float64 = CFP->getValueAPF().convertToDouble(); - ConvertDoubleToBytes(ptr, float64); - aggBuffer->addBytes(ptr, 8, Bytes); - } else { - llvm_unreachable("unsupported fp const type"); - } + case Type::DoubleTyID: + AddIntToBuffer(cast(CPV)->getValueAPF().bitcastToAPInt()); break; - } + case Type::PointerTyID: { if (const GlobalValue *GVar = dyn_cast(CPV)) { - aggBuffer->addSymbol(GVar, GVar); + AggBuffer->addSymbol(GVar, GVar); } else if (const ConstantExpr *Cexpr = dyn_cast(CPV)) { const Value *v = Cexpr->stripPointerCasts(); - aggBuffer->addSymbol(v, Cexpr); + AggBuffer->addSymbol(v, Cexpr); } - unsigned int s = DL.getTypeAllocSize(CPV->getType()); - aggBuffer->addZeros(s); + AggBuffer->addZeros(AllocSize); break; } @@ -1884,12 +1811,11 @@ case Type::FixedVectorTyID: case Type::StructTyID: { if (isa(CPV) || isa(CPV)) { - int ElementSize = DL.getTypeAllocSize(CPV->getType()); - bufferAggregateConstant(CPV, aggBuffer); - if (Bytes > ElementSize) - aggBuffer->addZeros(Bytes - ElementSize); + bufferAggregateConstant(CPV, AggBuffer); + if (Bytes > AllocSize) + AggBuffer->addZeros(Bytes - AllocSize); } else if (isa(CPV)) - aggBuffer->addZeros(Bytes); + AggBuffer->addZeros(Bytes); else llvm_unreachable("Unexpected Constant type"); break;