Moving Cost Tables up to share with other targets

llvm-svn: 173382
This commit is contained in:
Renato Golin 2013-01-24 23:01:00 +00:00
parent fd9cebe07d
commit efde585fc3
2 changed files with 75 additions and 48 deletions

View File

@ -0,0 +1,64 @@
//===-- CostTable.h - Instruction Cost Table handling -----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief Cost tables and simple lookup functions
///
//===----------------------------------------------------------------------===//
#ifndef LLVM_TARGET_COSTTABLE_H_
#define LLVM_TARGET_COSTTABLE_H_
namespace llvm {
/// Cost Table Entry
template <class TypeTy>
struct CostTblEntry {
int ISD;
TypeTy Type;
unsigned Cost;
};
/// Find in cost table, TypeTy must be comparable by ==
template <class TypeTy>
int CostTableLookup(const CostTblEntry<TypeTy> *Tbl,
unsigned len, int ISD, TypeTy Ty) {
for (unsigned int i = 0; i < len; ++i)
if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
return i;
// Could not find an entry.
return -1;
}
/// Type Conversion Cost Table
template <class TypeTy>
struct TypeConversionCostTblEntry {
int ISD;
TypeTy Dst;
TypeTy Src;
unsigned Cost;
};
/// Find in type conversion cost table, TypeTy must be comparable by ==
template <class TypeTy>
int ConvertCostTableLookup(const TypeConversionCostTblEntry<TypeTy> *Tbl,
unsigned len, int ISD, TypeTy Dst, TypeTy Src) {
for (unsigned int i = 0; i < len; ++i)
if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
return i;
// Could not find an entry.
return -1;
}
} // namespace llvm
#endif /* LLVM_TARGET_COSTTABLE_H_ */

View File

@ -20,6 +20,7 @@
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/CostTable.h"
using namespace llvm;
// Declare the pass initialization routine locally as target-specific passes
@ -119,44 +120,6 @@ llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) {
//
//===----------------------------------------------------------------------===//
namespace {
struct X86CostTblEntry {
int ISD;
MVT Type;
unsigned Cost;
};
}
static int
FindInTable(const X86CostTblEntry *Tbl, unsigned len, int ISD, MVT Ty) {
for (unsigned int i = 0; i < len; ++i)
if (Tbl[i].ISD == ISD && Tbl[i].Type == Ty)
return i;
// Could not find an entry.
return -1;
}
namespace {
struct X86TypeConversionCostTblEntry {
int ISD;
MVT Dst;
MVT Src;
unsigned Cost;
};
}
static int
FindInConvertTable(const X86TypeConversionCostTblEntry *Tbl, unsigned len,
int ISD, MVT Dst, MVT Src) {
for (unsigned int i = 0; i < len; ++i)
if (Tbl[i].ISD == ISD && Tbl[i].Src == Src && Tbl[i].Dst == Dst)
return i;
// Could not find an entry.
return -1;
}
X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const {
assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
// TODO: Currently the __builtin_popcount() implementation using SSE3
@ -206,7 +169,7 @@ unsigned X86TTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
int ISD = TLI->InstructionOpcodeToISD(Opcode);
assert(ISD && "Invalid opcode");
static const X86CostTblEntry AVX1CostTable[] = {
static const CostTblEntry<MVT> AVX1CostTable[] = {
// We don't have to scalarize unsupported ops. We can issue two half-sized
// operations and we only need to extract the upper YMM half.
// Two ops + 1 extract + 1 insert = 4.
@ -220,7 +183,7 @@ unsigned X86TTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
// Look for AVX1 lowering tricks.
if (ST->hasAVX()) {
int Idx = FindInTable(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
int Idx = CostTableLookup<MVT>(AVX1CostTable, array_lengthof(AVX1CostTable), ISD,
LT.second);
if (Idx != -1)
return LT.first * AVX1CostTable[Idx].Cost;
@ -254,7 +217,7 @@ unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
if (!SrcTy.isSimple() || !DstTy.isSimple())
return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
static const X86TypeConversionCostTblEntry AVXConversionTbl[] = {
static const TypeConversionCostTblEntry<MVT> AVXConversionTbl[] = {
{ ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
{ ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
{ ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
@ -273,7 +236,7 @@ unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
};
if (ST->hasAVX()) {
int Idx = FindInConvertTable(AVXConversionTbl,
int Idx = ConvertCostTableLookup<MVT>(AVXConversionTbl,
array_lengthof(AVXConversionTbl),
ISD, DstTy.getSimpleVT(), SrcTy.getSimpleVT());
if (Idx != -1)
@ -293,7 +256,7 @@ unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
int ISD = TLI->InstructionOpcodeToISD(Opcode);
assert(ISD && "Invalid opcode");
static const X86CostTblEntry SSE42CostTbl[] = {
static const CostTblEntry<MVT> SSE42CostTbl[] = {
{ ISD::SETCC, MVT::v2f64, 1 },
{ ISD::SETCC, MVT::v4f32, 1 },
{ ISD::SETCC, MVT::v2i64, 1 },
@ -302,7 +265,7 @@ unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
{ ISD::SETCC, MVT::v16i8, 1 },
};
static const X86CostTblEntry AVX1CostTbl[] = {
static const CostTblEntry<MVT> AVX1CostTbl[] = {
{ ISD::SETCC, MVT::v4f64, 1 },
{ ISD::SETCC, MVT::v8f32, 1 },
// AVX1 does not support 8-wide integer compare.
@ -312,7 +275,7 @@ unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
{ ISD::SETCC, MVT::v32i8, 4 },
};
static const X86CostTblEntry AVX2CostTbl[] = {
static const CostTblEntry<MVT> AVX2CostTbl[] = {
{ ISD::SETCC, MVT::v4i64, 1 },
{ ISD::SETCC, MVT::v8i32, 1 },
{ ISD::SETCC, MVT::v16i16, 1 },
@ -320,19 +283,19 @@ unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
};
if (ST->hasAVX2()) {
int Idx = FindInTable(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
int Idx = CostTableLookup<MVT>(AVX2CostTbl, array_lengthof(AVX2CostTbl), ISD, MTy);
if (Idx != -1)
return LT.first * AVX2CostTbl[Idx].Cost;
}
if (ST->hasAVX()) {
int Idx = FindInTable(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
int Idx = CostTableLookup<MVT>(AVX1CostTbl, array_lengthof(AVX1CostTbl), ISD, MTy);
if (Idx != -1)
return LT.first * AVX1CostTbl[Idx].Cost;
}
if (ST->hasSSE42()) {
int Idx = FindInTable(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
int Idx = CostTableLookup<MVT>(SSE42CostTbl, array_lengthof(SSE42CostTbl), ISD, MTy);
if (Idx != -1)
return LT.first * SSE42CostTbl[Idx].Cost;
}