Disable indvar widening if arithmetics on the wider type are more expensive
[oota-llvm.git] / lib / Target / NVPTX / NVPTXTargetTransformInfo.cpp
1 //===-- NVPTXTargetTransformInfo.cpp - NVPTX 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 //===----------------------------------------------------------------------===//
9 //
10 // \file
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.
15 //
16 //===----------------------------------------------------------------------===//
17
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/Support/Debug.h"
23 #include "llvm/Target/CostTable.h"
24 #include "llvm/Target/TargetLowering.h"
25 using namespace llvm;
26
27 #define DEBUG_TYPE "NVPTXtti"
28
29 // Declare the pass initialization routine locally as target-specific passes
30 // don't have a target-wide initialization entry point, and so we rely on the
31 // pass constructor initialization.
32 namespace llvm {
33 void initializeNVPTXTTIPass(PassRegistry &);
34 }
35
36 namespace {
37
38 class NVPTXTTI final : public ImmutablePass, public TargetTransformInfo {
39   const NVPTXTargetLowering *TLI;
40 public:
41   NVPTXTTI() : ImmutablePass(ID), TLI(nullptr) {
42     llvm_unreachable("This pass cannot be directly constructed");
43   }
44
45   NVPTXTTI(const NVPTXTargetMachine *TM)
46       : ImmutablePass(ID), TLI(TM->getSubtargetImpl()->getTargetLowering()) {
47     initializeNVPTXTTIPass(*PassRegistry::getPassRegistry());
48   }
49
50   void initializePass() override { pushTTIStack(this); }
51
52   void getAnalysisUsage(AnalysisUsage &AU) const override {
53     TargetTransformInfo::getAnalysisUsage(AU);
54   }
55
56   /// Pass identification.
57   static char ID;
58
59   /// Provide necessary pointer adjustments for the two base classes.
60   void *getAdjustedAnalysisPointer(const void *ID) override {
61     if (ID == &TargetTransformInfo::ID)
62       return (TargetTransformInfo *)this;
63     return this;
64   }
65
66   bool hasBranchDivergence() const override;
67
68   unsigned getArithmeticInstrCost(
69       unsigned Opcode, Type *Ty, OperandValueKind Opd1Info = OK_AnyValue,
70       OperandValueKind Opd2Info = OK_AnyValue,
71       OperandValueProperties Opd1PropInfo = OP_None,
72       OperandValueProperties Opd2PropInfo = OP_None) const override;
73
74   /// @}
75 };
76
77 } // end anonymous namespace
78
79 INITIALIZE_AG_PASS(NVPTXTTI, TargetTransformInfo, "NVPTXtti",
80                    "NVPTX Target Transform Info", true, true, false)
81 char NVPTXTTI::ID = 0;
82
83 ImmutablePass *
84 llvm::createNVPTXTargetTransformInfoPass(const NVPTXTargetMachine *TM) {
85   return new NVPTXTTI(TM);
86 }
87
88 bool NVPTXTTI::hasBranchDivergence() const { return true; }
89
90 unsigned NVPTXTTI::getArithmeticInstrCost(
91     unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
92     OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
93     OperandValueProperties Opd2PropInfo) const {
94   // Legalize the type.
95   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
96
97   int ISD = TLI->InstructionOpcodeToISD(Opcode);
98
99   switch (ISD) {
100   default:
101     return TargetTransformInfo::getArithmeticInstrCost(
102         Opcode, Ty, Opd1Info, Opd2Info, Opd1PropInfo, Opd2PropInfo);
103   case ISD::ADD:
104   case ISD::MUL:
105   case ISD::XOR:
106   case ISD::OR:
107   case ISD::AND:
108     // The machine code (SASS) simulates an i64 with two i32. Therefore, we
109     // estimate that arithmetic operations on i64 are twice as expensive as
110     // those on types that can fit into one machine register.
111     if (LT.second.SimpleTy == MVT::i64)
112       return 2 * LT.first;
113     // Delegate other cases to the basic TTI.
114     return TargetTransformInfo::getArithmeticInstrCost(
115         Opcode, Ty, Opd1Info, Opd2Info, Opd1PropInfo, Opd2PropInfo);
116   }
117 }