1 //===- HexagonTargetTransformInfo.cpp - Hexagon specific TTI pass ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 /// \file
9 /// This file implements a TargetTransformInfo analysis pass specific to the
10 /// Hexagon target machine. It uses the target's detailed information to provide
11 /// more precise answers to certain TTI queries, while letting the target
12 /// independent and default TTI implementations handle the rest.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "HexagonTargetTransformInfo.h"
17 #include "HexagonSubtarget.h"
18 #include "llvm/Analysis/TargetTransformInfo.h"
19 #include "llvm/IR/InstrTypes.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/User.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/CommandLine.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "hexagontti"
28 
29 static cl::opt<bool> EmitLookupTables("hexagon-emit-lookup-tables",
30   cl::init(true), cl::Hidden,
31   cl::desc("Control lookup table emission on Hexagon target"));
32 
33 TargetTransformInfo::PopcntSupportKind
34 HexagonTTIImpl::getPopcntSupport(unsigned IntTyWidthInBit) const {
35   // Return Fast Hardware support as every input  < 64 bits will be promoted
36   // to 64 bits.
37   return TargetTransformInfo::PSK_FastHardware;
38 }
39 
40 // The Hexagon target can unroll loops with run-time trip counts.
41 void HexagonTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
42                                              TTI::UnrollingPreferences &UP) {
43   UP.Runtime = UP.Partial = true;
44 }
45 
46 unsigned HexagonTTIImpl::getNumberOfRegisters(bool vector) const {
47   return vector ? 0 : 32;
48 }
49 
50 unsigned HexagonTTIImpl::getPrefetchDistance() const {
51   return getST()->getL1PrefetchDistance();
52 }
53 
54 unsigned HexagonTTIImpl::getCacheLineSize() const {
55   return getST()->getL1CacheLineSize();
56 }
57 
58 int HexagonTTIImpl::getUserCost(const User *U,
59                                 ArrayRef<const Value *> Operands) {
60   auto isCastFoldedIntoLoad = [](const CastInst *CI) -> bool {
61     if (!CI->isIntegerCast())
62       return false;
63     const LoadInst *LI = dyn_cast<const LoadInst>(CI->getOperand(0));
64     // Technically, this code could allow multiple uses of the load, and
65     // check if all the uses are the same extension operation, but this
66     // should be sufficient for most cases.
67     if (!LI || !LI->hasOneUse())
68       return false;
69 
70     // Only extensions from an integer type shorter than 32-bit to i32
71     // can be folded into the load.
72     unsigned SBW = CI->getSrcTy()->getIntegerBitWidth();
73     unsigned DBW = CI->getDestTy()->getIntegerBitWidth();
74     return DBW == 32 && (SBW < DBW);
75   };
76 
77   if (const CastInst *CI = dyn_cast<const CastInst>(U))
78     if (isCastFoldedIntoLoad(CI))
79       return TargetTransformInfo::TCC_Free;
80   return BaseT::getUserCost(U, Operands);
81 }
82 
83 bool HexagonTTIImpl::shouldBuildLookupTables() const {
84    return EmitLookupTables;
85 }
86