blob: 132765ab9b0d7641aaaa82d949952d5968bc9f0f [file] [log] [blame]
Tom Stellard8b1e0212013-07-27 00:01:07 +00001//===-- AMDGPUTargetTransformInfo.cpp - AMDGPU 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// AMDGPU 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
Tom Stellard8b1e0212013-07-27 00:01:07 +000018#include "AMDGPU.h"
19#include "AMDGPUTargetMachine.h"
Tom Stellard8cce9bd2014-01-23 18:49:28 +000020#include "llvm/Analysis/LoopInfo.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000021#include "llvm/Analysis/TargetTransformInfo.h"
Tom Stellard8cce9bd2014-01-23 18:49:28 +000022#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000023#include "llvm/CodeGen/BasicTTIImpl.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000024#include "llvm/Support/Debug.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000025#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000026#include "llvm/Target/TargetLowering.h"
Tom Stellard8b1e0212013-07-27 00:01:07 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "AMDGPUtti"
30
Tom Stellard8b1e0212013-07-27 00:01:07 +000031namespace {
32
Chandler Carruth705b1852015-01-31 03:43:40 +000033class AMDGPUTTIImpl : public BasicTTIImplBase<AMDGPUTTIImpl> {
34 typedef BasicTTIImplBase<AMDGPUTTIImpl> BaseT;
35 typedef TargetTransformInfo TTI;
Tom Stellard8b1e0212013-07-27 00:01:07 +000036
Chandler Carruth705b1852015-01-31 03:43:40 +000037 const AMDGPUSubtarget *ST;
Tom Stellard8b1e0212013-07-27 00:01:07 +000038
39public:
Chandler Carruth705b1852015-01-31 03:43:40 +000040 explicit AMDGPUTTIImpl(const AMDGPUTargetMachine *TM = nullptr)
41 : BaseT(TM), ST(TM->getSubtargetImpl()) {}
42
43 // Provide value semantics. MSVC requires that we spell all of these out.
44 AMDGPUTTIImpl(const AMDGPUTTIImpl &Arg)
45 : BaseT(static_cast<const BaseT &>(Arg)), ST(Arg.ST) {}
46 AMDGPUTTIImpl(AMDGPUTTIImpl &&Arg)
47 : BaseT(std::move(static_cast<BaseT &>(Arg))), ST(std::move(Arg.ST)) {}
48 AMDGPUTTIImpl &operator=(const AMDGPUTTIImpl &RHS) {
49 BaseT::operator=(static_cast<const BaseT &>(RHS));
50 ST = RHS.ST;
51 return *this;
52 }
53 AMDGPUTTIImpl &operator=(AMDGPUTTIImpl &&RHS) {
54 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
55 ST = std::move(RHS.ST);
56 return *this;
Tom Stellard8b1e0212013-07-27 00:01:07 +000057 }
58
Chandler Carruth705b1852015-01-31 03:43:40 +000059 bool hasBranchDivergence() { return true; }
Tom Stellard8b1e0212013-07-27 00:01:07 +000060
Eric Christopherd85ffb12014-09-18 00:34:14 +000061 void getUnrollingPreferences(const Function *F, Loop *L,
Chandler Carruth705b1852015-01-31 03:43:40 +000062 TTI::UnrollingPreferences &UP);
Tom Stellard8cce9bd2014-01-23 18:49:28 +000063
Chandler Carruth705b1852015-01-31 03:43:40 +000064 TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth) {
65 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
66 return ST->hasBCNT(TyWidth) ? TTI::PSK_FastHardware : TTI::PSK_Software;
67 }
Matt Arsenault3dd43fc2014-07-18 06:07:13 +000068
Chandler Carruth705b1852015-01-31 03:43:40 +000069 unsigned getNumberOfRegisters(bool Vector);
70 unsigned getRegisterBitWidth(bool Vector);
71 unsigned getMaxInterleaveFactor();
Tom Stellard8b1e0212013-07-27 00:01:07 +000072};
73
74} // end anonymous namespace
75
Tom Stellard8b1e0212013-07-27 00:01:07 +000076ImmutablePass *
77llvm::createAMDGPUTargetTransformInfoPass(const AMDGPUTargetMachine *TM) {
Chandler Carruth705b1852015-01-31 03:43:40 +000078 return new TargetTransformInfoWrapperPass(AMDGPUTTIImpl(TM));
Tom Stellard8b1e0212013-07-27 00:01:07 +000079}
80
Chandler Carruth705b1852015-01-31 03:43:40 +000081void AMDGPUTTIImpl::getUnrollingPreferences(const Function *, Loop *L,
82 TTI::UnrollingPreferences &UP) {
Matt Arsenaultc8244582014-07-25 23:02:42 +000083 UP.Threshold = 300; // Twice the default.
84 UP.Count = UINT_MAX;
85 UP.Partial = true;
86
87 // TODO: Do we want runtime unrolling?
88
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000089 for (const BasicBlock *BB : L->getBlocks()) {
90 for (const Instruction &I : *BB) {
91 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I);
Matt Arsenault5e2b0f52014-07-17 06:13:41 +000092 if (!GEP || GEP->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
Tom Stellard8cce9bd2014-01-23 18:49:28 +000093 continue;
Matt Arsenaultac6e39c2014-07-17 06:19:06 +000094
Tom Stellard8cce9bd2014-01-23 18:49:28 +000095 const Value *Ptr = GEP->getPointerOperand();
96 const AllocaInst *Alloca = dyn_cast<AllocaInst>(GetUnderlyingObject(Ptr));
97 if (Alloca) {
98 // We want to do whatever we can to limit the number of alloca
99 // instructions that make it through to the code generator. allocas
100 // require us to use indirect addressing, which is slow and prone to
101 // compiler bugs. If this loop does an address calculation on an
Tom Stellardfd0d86c2014-02-25 21:36:21 +0000102 // alloca ptr, then we want to use a higher than normal loop unroll
Matt Arsenault5e1e4312014-04-04 20:13:08 +0000103 // threshold. This will give SROA a better chance to eliminate these
104 // allocas.
105 //
106 // Don't use the maximum allowed value here as it will make some
107 // programs way too big.
Matt Arsenaultc8244582014-07-25 23:02:42 +0000108 UP.Threshold = 800;
Tom Stellard8cce9bd2014-01-23 18:49:28 +0000109 }
110 }
111 }
112}
Matt Arsenault3dd43fc2014-07-18 06:07:13 +0000113
Chandler Carruth705b1852015-01-31 03:43:40 +0000114unsigned AMDGPUTTIImpl::getNumberOfRegisters(bool Vec) {
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000115 if (Vec)
116 return 0;
117
118 // Number of VGPRs on SI.
119 if (ST->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS)
120 return 256;
121
122 return 4 * 128; // XXX - 4 channels. Should these count as vector instead?
123}
124
Chandler Carruth705b1852015-01-31 03:43:40 +0000125unsigned AMDGPUTTIImpl::getRegisterBitWidth(bool) { return 32; }
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000126
Chandler Carruth705b1852015-01-31 03:43:40 +0000127unsigned AMDGPUTTIImpl::getMaxInterleaveFactor() {
Matt Arsenaulta93441f2014-07-19 18:15:16 +0000128 // Semi-arbitrary large amount.
129 return 64;
130}