1 //===-- NVPTXTargetTransformInfo.cpp - NVPTX specific TTI pass ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // This file implements a TargetTransformInfo analysis pass specific to the
12 // NVPTX target machine. It uses the target's detailed information to provide
13 // more precise answers to certain TTI queries, while letting the target
14 // independent and default TTI implementations handle the rest.
16 //===----------------------------------------------------------------------===//
18 #include "NVPTXTargetMachine.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/CodeGen/BasicTTIImpl.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Target/CostTable.h"
25 #include "llvm/Target/TargetLowering.h"
28 #define DEBUG_TYPE "NVPTXtti"
32 class NVPTXTTIImpl : public BasicTTIImplBase<NVPTXTTIImpl> {
33 typedef BasicTTIImplBase<NVPTXTTIImpl> BaseT;
34 typedef TargetTransformInfo TTI;
36 const NVPTXTargetLowering *TLI;
39 explicit NVPTXTTIImpl(const NVPTXTargetMachine *TM = nullptr)
41 TLI(TM ? TM->getSubtargetImpl()->getTargetLowering() : nullptr) {}
43 // Provide value semantics. MSVC requires that we spell all of these out.
44 NVPTXTTIImpl(const NVPTXTTIImpl &Arg)
45 : BaseT(static_cast<const BaseT &>(Arg)), TLI(Arg.TLI) {}
46 NVPTXTTIImpl(NVPTXTTIImpl &&Arg)
47 : BaseT(std::move(static_cast<BaseT &>(Arg))), TLI(std::move(Arg.TLI)) {}
48 NVPTXTTIImpl &operator=(const NVPTXTTIImpl &RHS) {
49 BaseT::operator=(static_cast<const BaseT &>(RHS));
53 NVPTXTTIImpl &operator=(NVPTXTTIImpl &&RHS) {
54 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
55 TLI = std::move(RHS.TLI);
59 bool hasBranchDivergence() { return true; }
61 unsigned getArithmeticInstrCost(
62 unsigned Opcode, Type *Ty,
63 TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue,
64 TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue,
65 TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None,
66 TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None);
69 } // end anonymous namespace
72 llvm::createNVPTXTargetTransformInfoPass(const NVPTXTargetMachine *TM) {
73 return new TargetTransformInfoWrapperPass(NVPTXTTIImpl(TM));
76 unsigned NVPTXTTIImpl::getArithmeticInstrCost(
77 unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info,
78 TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo,
79 TTI::OperandValueProperties Opd2PropInfo) {
81 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
83 int ISD = TLI->InstructionOpcodeToISD(Opcode);
87 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
88 Opd1PropInfo, Opd2PropInfo);
94 // The machine code (SASS) simulates an i64 with two i32. Therefore, we
95 // estimate that arithmetic operations on i64 are twice as expensive as
96 // those on types that can fit into one machine register.
97 if (LT.second.SimpleTy == MVT::i64)
99 // Delegate other cases to the basic TTI.
100 return BaseT::getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
101 Opd1PropInfo, Opd2PropInfo);