Commit 3281748c by Karl Schimpf

Simplify LLVM's APInt and APFloat for use in Subzero.

In Subzero, we only need to be able to convert 64 bit constants in bitcode files to the corresponding Ice integer or floating type. This CL extracts the minimal implementation needed for Subzero. The intent of this change is to remove loading unnecessary LLVM code into (minimal) llvm2ice. BUG=None R=stichnot@chromium.org Review URL: https://codereview.chromium.org/797323002
parent 9a04c076
//===-- subzero/src/IceAPFloat.h - Constant float conversions --*- C++ -*--===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file implements a class to represent Subzero float and double
/// values.
///
/// Note: This is a simplified version of
/// llvm/include/llvm/ADT/APFloat.h for use with Subzero.
//===----------------------------------------------------------------------===//
#ifndef SUBZERO_SRC_ICEAPFLOAT_H
#define SUBZERO_SRC_ICEAPFLOAT_H
#include "IceAPInt.h"
namespace Ice {
template <typename IntType, typename FpType>
inline FpType convertAPIntToFp(const APInt &Int) {
static_assert(sizeof(IntType) == sizeof(FpType),
"IntType and FpType should be the same width");
assert(Int.getBitWidth() == sizeof(IntType) * CHAR_BIT);
union {
IntType IntValue;
FpType FpValue;
} Converter;
Converter.IntValue = static_cast<IntType>(Int.getRawData());
return Converter.FpValue;
}
} // end of namespace Ice
#endif // SUBZERO_SRC_ICEAPFLOAT_H
//===-- subzero/src/IceAPInt.h - Constant integer conversions --*- C++ -*--===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file implements a class to represent 64 bit integer constant
/// values, and thier conversion to variable bit sized integers.
///
/// Note: This is a simplified version of llvm/include/llvm/ADT/APInt.h for use
/// with Subzero.
//===----------------------------------------------------------------------===//
#ifndef SUBZERO_SRC_ICEAPINT_H
#define SUBZERO_SRC_ICEAPINT_H
#include "IceDefs.h"
namespace Ice {
class APInt {
public:
/// Bits in an (internal) value.
static const SizeT APINT_BITS_PER_WORD = sizeof(uint64_t) * CHAR_BIT;
APInt(SizeT Bits, uint64_t Val) : BitWidth(Bits), Val(Val) {
assert(Bits && "bitwidth too small");
assert(Bits <= APINT_BITS_PER_WORD && "bitwidth too big");
clearUnusedBits();
}
uint32_t getBitWidth() const { return BitWidth; }
int64_t getSExtValue() const {
return static_cast<int64_t>(Val << (APINT_BITS_PER_WORD - BitWidth)) >>
(APINT_BITS_PER_WORD - BitWidth);
}
uint64_t getRawData() const { return Val; }
private:
uint32_t BitWidth; // The number of bits in this APInt.
uint64_t Val; // The (64-bit) equivalent integer value.
/// Clear unused high order bits.
void clearUnusedBits() {
// If all bits are used, we want to leave the value alone.
if (BitWidth == APINT_BITS_PER_WORD)
return;
// Mask out the high bits.
Val &= ~static_cast<uint64_t>(0) >> (APINT_BITS_PER_WORD - BitWidth);
}
};
} // end of namespace Ice
#endif // SUBZERO_SRC_ICEAPINT_H
......@@ -17,13 +17,14 @@
#include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h"
#include "llvm/Bitcode/NaCl/NaClBitcodeParser.h"
#include "llvm/Bitcode/NaCl/NaClReaderWriter.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "IceAPInt.h"
#include "IceAPFloat.h"
#include "IceCfg.h"
#include "IceCfgNode.h"
#include "IceClFlags.h"
......@@ -2186,9 +2187,8 @@ void FunctionParser::ProcessRecord() {
Error(StrBuf.str());
return;
}
APInt Value(BitWidth,
NaClDecodeSignRotatedValue(Values[ValCaseIndex + 2]),
true);
Ice::APInt Value(BitWidth,
NaClDecodeSignRotatedValue(Values[ValCaseIndex + 2]));
if (isIRGenDisabled)
continue;
Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]);
......@@ -2561,7 +2561,8 @@ void ConstantsParser::ProcessRecord() {
}
if (auto IType = dyn_cast<IntegerType>(
Context->convertToLLVMType(NextConstantType))) {
APInt Value(IType->getBitWidth(), NaClDecodeSignRotatedValue(Values[0]));
Ice::APInt Value(IType->getBitWidth(),
NaClDecodeSignRotatedValue(Values[0]));
if (Ice::Constant *C = getContext()->getConstantInt(
NextConstantType, Value.getSExtValue())) {
FuncParser->setNextConstantID(C);
......@@ -2587,16 +2588,15 @@ void ConstantsParser::ProcessRecord() {
}
switch (NextConstantType) {
case Ice::IceType_f32: {
APFloat Value(APFloat::IEEEsingle,
APInt(32, static_cast<uint32_t>(Values[0])));
FuncParser->setNextConstantID(
getContext()->getConstantFloat(Value.convertToFloat()));
const Ice::APInt IntValue(32, static_cast<uint32_t>(Values[0]));
float FpValue = Ice::convertAPIntToFp<int32_t, float>(IntValue);
FuncParser->setNextConstantID(getContext()->getConstantFloat(FpValue));
return;
}
case Ice::IceType_f64: {
APFloat Value(APFloat::IEEEdouble, APInt(64, Values[0]));
FuncParser->setNextConstantID(
getContext()->getConstantDouble(Value.convertToDouble()));
const Ice::APInt IntValue(64, Values[0]);
double FpValue = Ice::convertAPIntToFp<uint64_t, double>(IntValue);
FuncParser->setNextConstantID(getContext()->getConstantDouble(FpValue));
return;
}
default: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment