Rewrite CanShareConstantPoolEntry to be implemented in terms of the

mid-level constant folding APIs instead of doing its own analysis.
This makes it more general (e.g. can now share a <2 x i64> with a
<4 x i32>) and avoid duplicating a bunch of logic.

llvm-svn: 149111
This commit is contained in:
Chris Lattner 2012-01-27 01:46:00 +00:00
parent 111d6ee655
commit 7ba81830ff
1 changed files with 25 additions and 25 deletions

View File

@ -27,6 +27,7 @@
#include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/Passes.h"
#include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h" #include "llvm/MC/MCContext.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/DebugInfo.h" #include "llvm/Analysis/DebugInfo.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
@ -654,38 +655,37 @@ static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
// reject them. // reject them.
if (A->getType() == B->getType()) return false; if (A->getType() == B->getType()) return false;
// We can't handle structs or arrays.
if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
return false;
// For now, only support constants with the same size. // For now, only support constants with the same size.
uint64_t StoreSize = TD->getTypeStoreSize(A->getType()); uint64_t StoreSize = TD->getTypeStoreSize(A->getType());
if (StoreSize != TD->getTypeStoreSize(B->getType()) || if (StoreSize != TD->getTypeStoreSize(B->getType()) ||
StoreSize > 128) StoreSize > 128)
return false; return false;
Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
// If a floating-point value and an integer value have the same encoding,
// they can share a constant-pool entry.
if (const ConstantFP *AFP = dyn_cast<ConstantFP>(A))
if (const ConstantInt *BI = dyn_cast<ConstantInt>(B))
return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
if (const ConstantFP *BFP = dyn_cast<ConstantFP>(B))
if (const ConstantInt *AI = dyn_cast<ConstantInt>(A))
return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
// Two vectors can share an entry if each pair of corresponding // Try constant folding a bitcast of both instructions to an integer. If we
// elements could. // get two identical ConstantInt's, then we are good to share them. We use
if (const ConstantVector *AV = dyn_cast<ConstantVector>(A)) // the constant folding APIs to do this so that we get the benefit of
if (const ConstantVector *BV = dyn_cast<ConstantVector>(B)) { // TargetData.
if (AV->getType()->getNumElements() != BV->getType()->getNumElements()) if (isa<PointerType>(A->getType()))
return false; A = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i) const_cast<Constant*>(A), TD);
if (!CanShareConstantPoolEntry(AV->getOperand(i), else if (A->getType() != IntTy)
BV->getOperand(i), TD)) A = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
return false; const_cast<Constant*>(A), TD);
return true; if (isa<PointerType>(B->getType()))
} B = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
const_cast<Constant*>(B), TD);
// TODO: Handle other cases. else if (B->getType() != IntTy)
B = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
return false; const_cast<Constant*>(B), TD);
return A == B;
} }
/// getConstantPoolIndex - Create a new entry in the constant pool or return /// getConstantPoolIndex - Create a new entry in the constant pool or return