Culling out use of unions for converting FP to bits and vice versa.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22838 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jim Laskey
2005-08-17 19:34:49 +00:00
parent 8482dd894d
commit cb6682fa44
13 changed files with 65 additions and 209 deletions

View File

@@ -25,6 +25,7 @@
#include "llvm/ADT/GraphTraits.h" #include "llvm/ADT/GraphTraits.h"
#include "llvm/ADT/iterator" #include "llvm/ADT/iterator"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/Support/MathExtras.h"
#include <cassert> #include <cassert>
#include <vector> #include <vector>
@@ -742,17 +743,7 @@ public:
/// As such, this method can be used to do an exact bit-for-bit comparison of /// As such, this method can be used to do an exact bit-for-bit comparison of
/// two floating point values. /// two floating point values.
bool isExactlyValue(double V) const { bool isExactlyValue(double V) const {
union { return DoubleToBits(V) == DoubleToBits(Value);
double V;
uint64_t I;
} T1;
T1.V = Value;
union {
double V;
uint64_t I;
} T2;
T2.V = V;
return T1.I == T2.I;
} }
static bool classof(const ConstantFPSDNode *) { return true; } static bool classof(const ConstantFPSDNode *) { return true; }

View File

@@ -23,6 +23,7 @@
#include "llvm/Constant.h" #include "llvm/Constant.h"
#include "llvm/Type.h" #include "llvm/Type.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Support/DataTypes.h"
#include "llvm/Support/MathExtras.h"
namespace llvm { namespace llvm {
@@ -277,12 +278,7 @@ public:
/// getNullValue. Don't depend on == for doubles to tell us it's zero, it /// getNullValue. Don't depend on == for doubles to tell us it's zero, it
/// considers -0.0 to be null as well as 0.0. :( /// considers -0.0 to be null as well as 0.0. :(
virtual bool isNullValue() const { virtual bool isNullValue() const {
union { return DoubleToBits(Val) == 0;
double V;
uint64_t I;
} T;
T.V = Val;
return T.I == 0;
} }
/// isExactlyValue - We don't rely on operator== working on double values, as /// isExactlyValue - We don't rely on operator== working on double values, as
@@ -290,17 +286,7 @@ public:
/// As such, this method can be used to do an exact bit-for-bit comparison of /// As such, this method can be used to do an exact bit-for-bit comparison of
/// two floating point values. /// two floating point values.
bool isExactlyValue(double V) const { bool isExactlyValue(double V) const {
union { return DoubleToBits(V) == DoubleToBits(Val);
double V;
uint64_t I;
} T1;
T1.V = Val;
union {
double V;
uint64_t I;
} T2;
T2.V = V;
return T1.I == T2.I;
} }
/// Methods for support type inquiry through isa, cast, and dyn_cast: /// Methods for support type inquiry through isa, cast, and dyn_cast:

View File

@@ -27,6 +27,7 @@
#include "llvm/Config/alloca.h" #include "llvm/Config/alloca.h"
#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/Compressor.h" #include "llvm/Support/Compressor.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
@@ -162,29 +163,19 @@ inline void BytecodeReader::read_data(void *Ptr, void *End) {
inline void BytecodeReader::read_float(float& FloatVal) { inline void BytecodeReader::read_float(float& FloatVal) {
/// FIXME: This isn't optimal, it has size problems on some platforms /// FIXME: This isn't optimal, it has size problems on some platforms
/// where FP is not IEEE. /// where FP is not IEEE.
union { FloatVal = BitsToFloat(At[0] | (At[1] << 8) | (At[2] << 16) | (At[3] << 24));
float f;
uint32_t i;
} FloatUnion;
FloatUnion.i = At[0] | (At[1] << 8) | (At[2] << 16) | (At[3] << 24);
At+=sizeof(uint32_t); At+=sizeof(uint32_t);
FloatVal = FloatUnion.f;
} }
/// Read a double value in little-endian order /// Read a double value in little-endian order
inline void BytecodeReader::read_double(double& DoubleVal) { inline void BytecodeReader::read_double(double& DoubleVal) {
/// FIXME: This isn't optimal, it has size problems on some platforms /// FIXME: This isn't optimal, it has size problems on some platforms
/// where FP is not IEEE. /// where FP is not IEEE.
union { DoubleVal = BitsToDouble((uint64_t(At[0]) << 0) | (uint64_t(At[1]) << 8) |
double d; (uint64_t(At[2]) << 16) | (uint64_t(At[3]) << 24) |
uint64_t i; (uint64_t(At[4]) << 32) | (uint64_t(At[5]) << 40) |
} DoubleUnion; (uint64_t(At[6]) << 48) | (uint64_t(At[7]) << 56));
DoubleUnion.i = (uint64_t(At[0]) << 0) | (uint64_t(At[1]) << 8) |
(uint64_t(At[2]) << 16) | (uint64_t(At[3]) << 24) |
(uint64_t(At[4]) << 32) | (uint64_t(At[5]) << 40) |
(uint64_t(At[6]) << 48) | (uint64_t(At[7]) << 56);
At+=sizeof(uint64_t); At+=sizeof(uint64_t);
DoubleVal = DoubleUnion.d;
} }
/// Read a block header and obtain its type and size /// Read a block header and obtain its type and size

View File

@@ -27,6 +27,7 @@
#include "llvm/SymbolTable.h" #include "llvm/SymbolTable.h"
#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/Compressor.h" #include "llvm/Support/Compressor.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
#include <cstring> #include <cstring>
@@ -139,33 +140,25 @@ inline void BytecodeWriter::output_data(const void *Ptr, const void *End) {
inline void BytecodeWriter::output_float(float& FloatVal) { inline void BytecodeWriter::output_float(float& FloatVal) {
/// FIXME: This isn't optimal, it has size problems on some platforms /// FIXME: This isn't optimal, it has size problems on some platforms
/// where FP is not IEEE. /// where FP is not IEEE.
union { uint32_t i = FloatToBits(FloatVal);
float f; Out.push_back( static_cast<unsigned char>( (i & 0xFF )));
uint32_t i; Out.push_back( static_cast<unsigned char>( (i >> 8) & 0xFF));
} FloatUnion; Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
FloatUnion.f = FloatVal; Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
Out.push_back( static_cast<unsigned char>( (FloatUnion.i & 0xFF )));
Out.push_back( static_cast<unsigned char>( (FloatUnion.i >> 8) & 0xFF));
Out.push_back( static_cast<unsigned char>( (FloatUnion.i >> 16) & 0xFF));
Out.push_back( static_cast<unsigned char>( (FloatUnion.i >> 24) & 0xFF));
} }
inline void BytecodeWriter::output_double(double& DoubleVal) { inline void BytecodeWriter::output_double(double& DoubleVal) {
/// FIXME: This isn't optimal, it has size problems on some platforms /// FIXME: This isn't optimal, it has size problems on some platforms
/// where FP is not IEEE. /// where FP is not IEEE.
union { uint64_t i = DoubleToBits(DoubleVal);
double d; Out.push_back( static_cast<unsigned char>( (i & 0xFF )));
uint64_t i; Out.push_back( static_cast<unsigned char>( (i >> 8) & 0xFF));
} DoubleUnion; Out.push_back( static_cast<unsigned char>( (i >> 16) & 0xFF));
DoubleUnion.d = DoubleVal; Out.push_back( static_cast<unsigned char>( (i >> 24) & 0xFF));
Out.push_back( static_cast<unsigned char>( (DoubleUnion.i & 0xFF ))); Out.push_back( static_cast<unsigned char>( (i >> 32) & 0xFF));
Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 8) & 0xFF)); Out.push_back( static_cast<unsigned char>( (i >> 40) & 0xFF));
Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 16) & 0xFF)); Out.push_back( static_cast<unsigned char>( (i >> 48) & 0xFF));
Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 24) & 0xFF)); Out.push_back( static_cast<unsigned char>( (i >> 56) & 0xFF));
Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 32) & 0xFF));
Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 40) & 0xFF));
Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 48) & 0xFF));
Out.push_back( static_cast<unsigned char>( (DoubleUnion.i >> 56) & 0xFF));
} }
inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter& w, inline BytecodeBlock::BytecodeBlock(unsigned ID, BytecodeWriter& w,

View File

@@ -15,6 +15,7 @@
#include "llvm/Constants.h" #include "llvm/Constants.h"
#include "llvm/Instruction.h" #include "llvm/Instruction.h"
#include "llvm/Support/Mangler.h" #include "llvm/Support/Mangler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetMachine.h"
using namespace llvm; using namespace llvm;
@@ -222,39 +223,27 @@ void AsmPrinter::emitGlobalConstant(const Constant *CV) {
// precision... // precision...
double Val = CFP->getValue(); double Val = CFP->getValue();
if (CFP->getType() == Type::DoubleTy) { if (CFP->getType() == Type::DoubleTy) {
union DU { // Abide by C TBAA rules
double FVal;
uint64_t UVal;
} U;
U.FVal = Val;
if (Data64bitsDirective) if (Data64bitsDirective)
O << Data64bitsDirective << U.UVal << "\t" << CommentString O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
<< " double value: " << Val << "\n"; << " double value: " << Val << "\n";
else if (TD.isBigEndian()) { else if (TD.isBigEndian()) {
O << Data32bitsDirective << unsigned(U.UVal >> 32) O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
<< "\t" << CommentString << " double most significant word " << "\t" << CommentString << " double most significant word "
<< Val << "\n"; << Val << "\n";
O << Data32bitsDirective << unsigned(U.UVal) O << Data32bitsDirective << unsigned(DoubleToBits(Val))
<< "\t" << CommentString << " double least significant word " << "\t" << CommentString << " double least significant word "
<< Val << "\n"; << Val << "\n";
} else { } else {
O << Data32bitsDirective << unsigned(U.UVal) O << Data32bitsDirective << unsigned(DoubleToBits(Val))
<< "\t" << CommentString << " double least significant word " << Val << "\t" << CommentString << " double least significant word " << Val
<< "\n"; << "\n";
O << Data32bitsDirective << unsigned(U.UVal >> 32) O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
<< "\t" << CommentString << " double most significant word " << Val << "\t" << CommentString << " double most significant word " << Val
<< "\n"; << "\n";
} }
return; return;
} else { } else {
union FU { // Abide by C TBAA rules O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
float FVal;
int32_t UVal;
} U;
U.FVal = (float)Val;
O << Data32bitsDirective << U.UVal << "\t" << CommentString
<< " float " << Val << "\n"; << " float " << Val << "\n";
return; return;
} }

View File

@@ -975,23 +975,17 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
// Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
if (CFP->getValueType(0) == MVT::f32) { if (CFP->getValueType(0) == MVT::f32) {
union {
unsigned I;
float F;
} V;
V.F = CFP->getValue();
Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
DAG.getConstant(V.I, MVT::i32), Tmp2, DAG.getConstant(FloatToBits(CFP->getValue()),
MVT::i32),
Tmp2,
Node->getOperand(3)); Node->getOperand(3));
} else { } else {
assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
union {
uint64_t I;
double F;
} V;
V.F = CFP->getValue();
Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
DAG.getConstant(V.I, MVT::i64), Tmp2, DAG.getConstant(DoubleToBits(CFP->getValue()),
MVT::i64),
Tmp2,
Node->getOperand(3)); Node->getOperand(3));
} }
Node = Result.Val; Node = Result.Val;

View File

@@ -224,12 +224,8 @@ void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
N->getValueType(0))); N->getValueType(0)));
break; break;
case ISD::ConstantFP: { case ISD::ConstantFP: {
union { uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
double DV; ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
uint64_t IV;
};
DV = cast<ConstantFPSDNode>(N)->getValue();
ConstantFPs.erase(std::make_pair(IV, N->getValueType(0)));
break; break;
} }
case ISD::CONDCODE: case ISD::CONDCODE:
@@ -385,14 +381,7 @@ SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
// Do the map lookup using the actual bit pattern for the floating point // Do the map lookup using the actual bit pattern for the floating point
// value, so that we don't have problems with 0.0 comparing equal to -0.0, and // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
// we don't have issues with SNANs. // we don't have issues with SNANs.
union { SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
double DV;
uint64_t IV;
};
DV = Val;
SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
if (N) return SDOperand(N, 0); if (N) return SDOperand(N, 0);
N = new ConstantFPSDNode(Val, VT); N = new ConstantFPSDNode(Val, VT);
AllNodes.push_back(N); AllNodes.push_back(N);

View File

@@ -32,6 +32,7 @@
#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/InstVisitor.h" #include "llvm/Support/InstVisitor.h"
#include "llvm/Support/Mangler.h" #include "llvm/Support/Mangler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
@@ -585,14 +586,10 @@ void CWriter::printConstant(Constant *CPV) {
const unsigned long SignalNaN = 0x7ff4UL; const unsigned long SignalNaN = 0x7ff4UL;
// We need to grab the first part of the FP # // We need to grab the first part of the FP #
union {
double d;
uint64_t ll;
} DHex;
char Buffer[100]; char Buffer[100];
DHex.d = FPC->getValue(); uint64_t ll = DoubleToBits(FPC->getValue());
sprintf(Buffer, "0x%llx", (unsigned long long)DHex.ll); sprintf(Buffer, "0x%llx", (unsigned long long)ll);
std::string Num(&Buffer[0], &Buffer[6]); std::string Num(&Buffer[0], &Buffer[6]);
unsigned long Val = strtoul(Num.c_str(), 0, 16); unsigned long Val = strtoul(Num.c_str(), 0, 16);
@@ -953,16 +950,6 @@ bool CWriter::doInitialization(Module &M) {
/// Output all floating point constants that cannot be printed accurately... /// Output all floating point constants that cannot be printed accurately...
void CWriter::printFloatingPointConstants(Function &F) { void CWriter::printFloatingPointConstants(Function &F) {
union {
double D;
uint64_t U;
} DBLUnion;
union {
float F;
unsigned U;
} FLTUnion;
// Scan the module for floating point constants. If any FP constant is used // Scan the module for floating point constants. If any FP constant is used
// in the function, we want to redirect it here so that we do not depend on // in the function, we want to redirect it here so that we do not depend on
// the precision of the printed form, unless the printed form preserves // the precision of the printed form, unless the printed form preserves
@@ -979,14 +966,12 @@ void CWriter::printFloatingPointConstants(Function &F) {
FPConstantMap[FPC] = FPCounter; // Number the FP constants FPConstantMap[FPC] = FPCounter; // Number the FP constants
if (FPC->getType() == Type::DoubleTy) { if (FPC->getType() == Type::DoubleTy) {
DBLUnion.D = Val;
Out << "static const ConstantDoubleTy FPConstant" << FPCounter++ Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
<< " = 0x" << std::hex << DBLUnion.U << std::dec << " = 0x" << std::hex << DoubleToBits(Val) << std::dec
<< "ULL; /* " << Val << " */\n"; << "ULL; /* " << Val << " */\n";
} else if (FPC->getType() == Type::FloatTy) { } else if (FPC->getType() == Type::FloatTy) {
FLTUnion.F = Val;
Out << "static const ConstantFloatTy FPConstant" << FPCounter++ Out << "static const ConstantFloatTy FPConstant" << FPCounter++
<< " = 0x" << std::hex << FLTUnion.U << std::dec << " = 0x" << std::hex << FloatToBits(Val) << std::dec
<< "U; /* " << Val << " */\n"; << "U; /* " << Val << " */\n";
} else } else
assert(0 && "Unknown float type!"); assert(0 && "Unknown float type!");

View File

@@ -32,6 +32,7 @@
#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/InstVisitor.h" #include "llvm/Support/InstVisitor.h"
#include "llvm/Support/Mangler.h" #include "llvm/Support/Mangler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h" #include "llvm/Support/MathExtras.h"
@@ -585,14 +586,10 @@ void CWriter::printConstant(Constant *CPV) {
const unsigned long SignalNaN = 0x7ff4UL; const unsigned long SignalNaN = 0x7ff4UL;
// We need to grab the first part of the FP # // We need to grab the first part of the FP #
union {
double d;
uint64_t ll;
} DHex;
char Buffer[100]; char Buffer[100];
DHex.d = FPC->getValue(); uint64_t ll = DoubleToBits(FPC->getValue());
sprintf(Buffer, "0x%llx", (unsigned long long)DHex.ll); sprintf(Buffer, "0x%llx", (unsigned long long)ll);
std::string Num(&Buffer[0], &Buffer[6]); std::string Num(&Buffer[0], &Buffer[6]);
unsigned long Val = strtoul(Num.c_str(), 0, 16); unsigned long Val = strtoul(Num.c_str(), 0, 16);
@@ -953,16 +950,6 @@ bool CWriter::doInitialization(Module &M) {
/// Output all floating point constants that cannot be printed accurately... /// Output all floating point constants that cannot be printed accurately...
void CWriter::printFloatingPointConstants(Function &F) { void CWriter::printFloatingPointConstants(Function &F) {
union {
double D;
uint64_t U;
} DBLUnion;
union {
float F;
unsigned U;
} FLTUnion;
// Scan the module for floating point constants. If any FP constant is used // Scan the module for floating point constants. If any FP constant is used
// in the function, we want to redirect it here so that we do not depend on // in the function, we want to redirect it here so that we do not depend on
// the precision of the printed form, unless the printed form preserves // the precision of the printed form, unless the printed form preserves
@@ -979,14 +966,12 @@ void CWriter::printFloatingPointConstants(Function &F) {
FPConstantMap[FPC] = FPCounter; // Number the FP constants FPConstantMap[FPC] = FPCounter; // Number the FP constants
if (FPC->getType() == Type::DoubleTy) { if (FPC->getType() == Type::DoubleTy) {
DBLUnion.D = Val;
Out << "static const ConstantDoubleTy FPConstant" << FPCounter++ Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
<< " = 0x" << std::hex << DBLUnion.U << std::dec << " = 0x" << std::hex << DoubleToBits(Val) << std::dec
<< "ULL; /* " << Val << " */\n"; << "ULL; /* " << Val << " */\n";
} else if (FPC->getType() == Type::FloatTy) { } else if (FPC->getType() == Type::FloatTy) {
FLTUnion.F = Val;
Out << "static const ConstantFloatTy FPConstant" << FPCounter++ Out << "static const ConstantFloatTy FPConstant" << FPCounter++
<< " = 0x" << std::hex << FLTUnion.U << std::dec << " = 0x" << std::hex << FloatToBits(Val) << std::dec
<< "U; /* " << Val << " */\n"; << "U; /* " << Val << " */\n";
} else } else
assert(0 && "Unknown float type!"); assert(0 && "Unknown float type!");

View File

@@ -247,22 +247,12 @@ void V8Printer::emitGlobalConstant(const Constant *CV) {
switch (CFP->getType()->getTypeID()) { switch (CFP->getType()->getTypeID()) {
default: assert(0 && "Unknown floating point type!"); default: assert(0 && "Unknown floating point type!");
case Type::FloatTyID: { case Type::FloatTyID: {
union FU { // Abide by C TBAA rules O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n";
float FVal;
unsigned UVal;
} U;
U.FVal = Val;
O << ".long\t" << U.UVal << "\t! float " << Val << "\n";
return; return;
} }
case Type::DoubleTyID: { case Type::DoubleTyID: {
union DU { // Abide by C TBAA rules O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n";
double FVal; O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
uint64_t UVal;
} U;
U.FVal = Val;
O << ".word\t0x" << std::hex << (U.UVal >> 32) << std::dec << "\t! double " << Val << "\n";
O << ".word\t0x" << std::hex << (U.UVal & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
return; return;
} }
} }

View File

@@ -247,22 +247,12 @@ void V8Printer::emitGlobalConstant(const Constant *CV) {
switch (CFP->getType()->getTypeID()) { switch (CFP->getType()->getTypeID()) {
default: assert(0 && "Unknown floating point type!"); default: assert(0 && "Unknown floating point type!");
case Type::FloatTyID: { case Type::FloatTyID: {
union FU { // Abide by C TBAA rules O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n";
float FVal;
unsigned UVal;
} U;
U.FVal = Val;
O << ".long\t" << U.UVal << "\t! float " << Val << "\n";
return; return;
} }
case Type::DoubleTyID: { case Type::DoubleTyID: {
union DU { // Abide by C TBAA rules O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n";
double FVal; O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
uint64_t UVal;
} U;
U.FVal = Val;
O << ".word\t0x" << std::hex << (U.UVal >> 32) << std::dec << "\t! double " << Val << "\n";
O << ".word\t0x" << std::hex << (U.UVal & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
return; return;
} }
} }

View File

@@ -29,6 +29,7 @@
#include "llvm/Support/CFG.h" #include "llvm/Support/CFG.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MathExtras.h"
#include <algorithm> #include <algorithm>
using namespace llvm; using namespace llvm;
@@ -431,18 +432,9 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV,
// Otherwise we could not reparse it to exactly the same value, so we must // Otherwise we could not reparse it to exactly the same value, so we must
// output the string in hexadecimal format! // output the string in hexadecimal format!
//
// Behave nicely in the face of C TBAA rules... see:
// http://www.nullstone.com/htmls/category/aliastyp.htm
//
union {
double D;
uint64_t U;
} V;
V.D = CFP->getValue();
assert(sizeof(double) == sizeof(uint64_t) && assert(sizeof(double) == sizeof(uint64_t) &&
"assuming that double is 64 bits!"); "assuming that double is 64 bits!");
Out << "0x" << utohexstr(V.U); Out << "0x" << utohexstr(DoubleToBits(CFP->getValue()));
} else if (isa<ConstantAggregateZero>(CV)) { } else if (isa<ConstantAggregateZero>(CV)) {
Out << "zeroinitializer"; Out << "zeroinitializer";

View File

@@ -19,6 +19,7 @@
#include "llvm/SymbolTable.h" #include "llvm/SymbolTable.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/Support/MathExtras.h"
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
using namespace llvm; using namespace llvm;
@@ -796,24 +797,14 @@ namespace llvm {
struct ConstantCreator<ConstantFP, Type, uint64_t> { struct ConstantCreator<ConstantFP, Type, uint64_t> {
static ConstantFP *create(const Type *Ty, uint64_t V) { static ConstantFP *create(const Type *Ty, uint64_t V) {
assert(Ty == Type::DoubleTy); assert(Ty == Type::DoubleTy);
union { return new ConstantFP(Ty, BitsToDouble(V));
double F;
uint64_t I;
} T;
T.I = V;
return new ConstantFP(Ty, T.F);
} }
}; };
template<> template<>
struct ConstantCreator<ConstantFP, Type, uint32_t> { struct ConstantCreator<ConstantFP, Type, uint32_t> {
static ConstantFP *create(const Type *Ty, uint32_t V) { static ConstantFP *create(const Type *Ty, uint32_t V) {
assert(Ty == Type::FloatTy); assert(Ty == Type::FloatTy);
union { return new ConstantFP(Ty, BitsToFloat(V));
float F;
uint32_t I;
} T;
T.I = V;
return new ConstantFP(Ty, T.F);
} }
}; };
} }
@@ -824,20 +815,10 @@ static ValueMap<uint32_t, Type, ConstantFP> FloatConstants;
ConstantFP *ConstantFP::get(const Type *Ty, double V) { ConstantFP *ConstantFP::get(const Type *Ty, double V) {
if (Ty == Type::FloatTy) { if (Ty == Type::FloatTy) {
// Force the value through memory to normalize it. // Force the value through memory to normalize it.
union { return FloatConstants.getOrCreate(Ty, FloatToBits(V));
float F;
uint32_t I;
} T;
T.F = (float)V;
return FloatConstants.getOrCreate(Ty, T.I);
} else { } else {
assert(Ty == Type::DoubleTy); assert(Ty == Type::DoubleTy);
union { return DoubleConstants.getOrCreate(Ty, DoubleToBits(V));
double F;
uint64_t I;
} T;
T.F = V;
return DoubleConstants.getOrCreate(Ty, T.I);
} }
} }