LLVM  10.0.0svn
RISCVTargetTransformInfo.cpp
Go to the documentation of this file.
1 //===-- RISCVTargetTransformInfo.cpp - RISC-V specific TTI ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "Utils/RISCVMatInt.h"
14 using namespace llvm;
15 
16 #define DEBUG_TYPE "riscvtti"
17 
18 int RISCVTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
19  assert(Ty->isIntegerTy() &&
20  "getIntImmCost can only estimate cost of materialising integers");
21 
22  // We have a Zero register, so 0 is always free.
23  if (Imm == 0)
24  return TTI::TCC_Free;
25 
26  // Otherwise, we check how many instructions it will take to materialise.
27  const DataLayout &DL = getDataLayout();
29  getST()->is64Bit());
30 }
31 
32 int RISCVTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
33  Type *Ty) {
34  assert(Ty->isIntegerTy() &&
35  "getIntImmCost can only estimate cost of materialising integers");
36 
37  // We have a Zero register, so 0 is always free.
38  if (Imm == 0)
39  return TTI::TCC_Free;
40 
41  // Some instructions in RISC-V can take a 12-bit immediate. Some of these are
42  // commutative, in others the immediate comes from a specific argument index.
43  bool Takes12BitImm = false;
44  unsigned ImmArgIdx = ~0U;
45 
46  switch (Opcode) {
47  case Instruction::GetElementPtr:
48  // Never hoist any arguments to a GetElementPtr. CodeGenPrepare will
49  // split up large offsets in GEP into better parts than ConstantHoisting
50  // can.
51  return TTI::TCC_Free;
52  case Instruction::Add:
53  case Instruction::And:
54  case Instruction::Or:
55  case Instruction::Xor:
56  case Instruction::Mul:
57  Takes12BitImm = true;
58  break;
59  case Instruction::Sub:
60  case Instruction::Shl:
61  case Instruction::LShr:
62  case Instruction::AShr:
63  Takes12BitImm = true;
64  ImmArgIdx = 1;
65  break;
66  default:
67  break;
68  }
69 
70  if (Takes12BitImm) {
71  // Check immediate is the correct argument...
72  if (Instruction::isCommutative(Opcode) || Idx == ImmArgIdx) {
73  // ... and fits into the 12-bit immediate.
74  if (Imm.getMinSignedBits() <= 64 &&
75  getTLI()->isLegalAddImmediate(Imm.getSExtValue())) {
76  return TTI::TCC_Free;
77  }
78  }
79 
80  // Otherwise, use the full materialisation cost.
81  return getIntImmCost(Imm, Ty);
82  }
83 
84  // By default, prevent hoisting.
85  return TTI::TCC_Free;
86 }
87 
89  const APInt &Imm, Type *Ty) {
90  // Prevent hoisting in unknown cases.
91  return TTI::TCC_Free;
92 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:196
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1581
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
Expected to fold away in lowering.
This file provides a helper that implements much of the TTI interface in terms of the target-independ...
bool isLegalAddImmediate(int64_t Imm) const override
Return true if the specified immediate is legal add immediate, that is the target has add instruction...
bool isCommutative() const
Return true if the instruction is commutative:
Definition: Instruction.h:488
Class for arbitrary precision integers.
Definition: APInt.h:69
bool is64Bit() const
int getIntMatCost(const APInt &Val, unsigned Size, bool IsRV64)
Definition: RISCVMatInt.cpp:78
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:602
This file defines a TargetTransformInfo::Concept conforming object specific to the RISC-V target mach...
int getIntImmCost(const APInt &Imm, Type *Ty)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getMinSignedBits() const
Get the minimum bit size for this signed APInt.
Definition: APInt.h:1558
const DataLayout & getDataLayout() const
This pass exposes codegen information to IR-level passes.
This file describes how to lower LLVM code to machine code.