blob: 7c5a54162d777421af6d32a2323c68fa7f814afd [file] [log] [blame]
Eli Benderskybbef1722014-04-03 21:18:25 +00001//===-- NVPTXFavorNonGenericAddrSpace.cpp - ---------------------*- C++ -*-===//
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//
Jingyue Wu13755602016-03-20 20:59:20 +000010// FIXME: This pass is deprecated in favor of NVPTXInferAddressSpaces, which
11// uses a new algorithm that handles pointer induction variables.
12//
Eli Benderskybbef1722014-04-03 21:18:25 +000013// When a load/store accesses the generic address space, checks whether the
14// address is casted from a non-generic address space. If so, remove this
15// addrspacecast because accessing non-generic address spaces is typically
Jingyue Wu995dde22015-05-29 17:00:27 +000016// faster. Besides removing addrspacecasts directly used by loads/stores, this
17// optimization also recursively traces into a GEP's pointer operand and a
18// bitcast's source to find more eliminable addrspacecasts.
Eli Benderskybbef1722014-04-03 21:18:25 +000019//
20// For instance, the code below loads a float from an array allocated in
21// addrspace(3).
22//
Jingyue Wu995dde22015-05-29 17:00:27 +000023// %0 = addrspacecast [10 x float] addrspace(3)* @a to [10 x float]*
24// %1 = gep [10 x float]* %0, i64 0, i64 %i
25// %2 = bitcast float* %1 to i32*
26// %3 = load i32* %2 ; emits ld.u32
Eli Benderskybbef1722014-04-03 21:18:25 +000027//
Jingyue Wu995dde22015-05-29 17:00:27 +000028// First, function hoistAddrSpaceCastFrom reorders the addrspacecast, the GEP,
29// and the bitcast to expose more optimization opportunities to function
Eli Benderskybbef1722014-04-03 21:18:25 +000030// optimizeMemoryInst. The intermediate code looks like:
31//
Jingyue Wu995dde22015-05-29 17:00:27 +000032// %0 = gep [10 x float] addrspace(3)* @a, i64 0, i64 %i
33// %1 = bitcast float addrspace(3)* %0 to i32 addrspace(3)*
34// %2 = addrspacecast i32 addrspace(3)* %1 to i32*
35// %3 = load i32* %2 ; still emits ld.u32, but will be optimized shortly
Eli Benderskybbef1722014-04-03 21:18:25 +000036//
37// Then, function optimizeMemoryInstruction detects a load from addrspacecast'ed
38// generic pointers, and folds the load and the addrspacecast into a load from
39// the original address space. The final code looks like:
40//
Jingyue Wu995dde22015-05-29 17:00:27 +000041// %0 = gep [10 x float] addrspace(3)* @a, i64 0, i64 %i
42// %1 = bitcast float addrspace(3)* %0 to i32 addrspace(3)*
43// %3 = load i32 addrspace(3)* %1 ; emits ld.shared.f32
Eli Benderskybbef1722014-04-03 21:18:25 +000044//
45// This pass may remove an addrspacecast in a different BB. Therefore, we
46// implement it as a FunctionPass.
47//
Jingyue Wu995dde22015-05-29 17:00:27 +000048// TODO:
49// The current implementation doesn't handle PHINodes. Eliminating
50// addrspacecasts used by PHINodes is trickier because PHINodes can introduce
51// loops in data flow. For example,
52//
53// %generic.input = addrspacecast float addrspace(3)* %input to float*
54// loop:
55// %y = phi [ %generic.input, %y2 ]
56// %y2 = getelementptr %y, 1
57// %v = load %y2
58// br ..., label %loop, ...
59//
60// Marking %y2 shared depends on marking %y shared, but %y also data-flow
61// depends on %y2. We probably need an iterative fix-point algorithm on handle
62// this case.
63//
Eli Benderskybbef1722014-04-03 21:18:25 +000064//===----------------------------------------------------------------------===//
65
66#include "NVPTX.h"
67#include "llvm/IR/Function.h"
68#include "llvm/IR/Instructions.h"
69#include "llvm/IR/Operator.h"
70#include "llvm/Support/CommandLine.h"
71
72using namespace llvm;
73
74// An option to disable this optimization. Enable it by default.
75static cl::opt<bool> DisableFavorNonGeneric(
76 "disable-nvptx-favor-non-generic",
77 cl::init(false),
78 cl::desc("Do not convert generic address space usage "
79 "to non-generic address space usage"),
80 cl::Hidden);
81
82namespace {
83/// \brief NVPTXFavorNonGenericAddrSpaces
84class NVPTXFavorNonGenericAddrSpaces : public FunctionPass {
85public:
86 static char ID;
87 NVPTXFavorNonGenericAddrSpaces() : FunctionPass(ID) {}
Jingyue Wu69a66852014-05-23 06:30:12 +000088 bool runOnFunction(Function &F) override;
Eli Benderskybbef1722014-04-03 21:18:25 +000089
Jingyue Wu995dde22015-05-29 17:00:27 +000090private:
Eli Benderskybbef1722014-04-03 21:18:25 +000091 /// Optimizes load/store instructions. Idx is the index of the pointer operand
92 /// (0 for load, and 1 for store). Returns true if it changes anything.
93 bool optimizeMemoryInstruction(Instruction *I, unsigned Idx);
Jingyue Wu995dde22015-05-29 17:00:27 +000094 /// Recursively traces into a GEP's pointer operand or a bitcast's source to
95 /// find an eliminable addrspacecast, and hoists that addrspacecast to the
96 /// outermost level. For example, this function transforms
97 /// bitcast(gep(gep(addrspacecast(X))))
98 /// to
99 /// addrspacecast(bitcast(gep(gep(X)))).
100 ///
101 /// This reordering exposes to optimizeMemoryInstruction more
102 /// optimization opportunities on loads and stores.
103 ///
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000104 /// If this function successfully hoists an eliminable addrspacecast or V is
Jingyue Wu75589ff2015-06-09 21:50:32 +0000105 /// already such an addrspacecast, it returns the transformed value (which is
106 /// guaranteed to be an addrspacecast); otherwise, it returns nullptr.
107 Value *hoistAddrSpaceCastFrom(Value *V, int Depth = 0);
Jingyue Wu995dde22015-05-29 17:00:27 +0000108 /// Helper function for GEPs.
Jingyue Wu75589ff2015-06-09 21:50:32 +0000109 Value *hoistAddrSpaceCastFromGEP(GEPOperator *GEP, int Depth);
Jingyue Wu995dde22015-05-29 17:00:27 +0000110 /// Helper function for bitcasts.
Jingyue Wu75589ff2015-06-09 21:50:32 +0000111 Value *hoistAddrSpaceCastFromBitCast(BitCastOperator *BC, int Depth);
Eli Benderskybbef1722014-04-03 21:18:25 +0000112};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000113}
Eli Benderskybbef1722014-04-03 21:18:25 +0000114
115char NVPTXFavorNonGenericAddrSpaces::ID = 0;
116
117namespace llvm {
118void initializeNVPTXFavorNonGenericAddrSpacesPass(PassRegistry &);
119}
120INITIALIZE_PASS(NVPTXFavorNonGenericAddrSpaces, "nvptx-favor-non-generic",
121 "Remove unnecessary non-generic-to-generic addrspacecasts",
122 false, false)
123
Jingyue Wu995dde22015-05-29 17:00:27 +0000124// Decides whether V is an addrspacecast and shortcutting V in load/store is
125// valid and beneficial.
126static bool isEliminableAddrSpaceCast(Value *V) {
127 // Returns false if V is not even an addrspacecast.
128 Operator *Cast = dyn_cast<Operator>(V);
129 if (Cast == nullptr || Cast->getOpcode() != Instruction::AddrSpaceCast)
Eli Benderskybbef1722014-04-03 21:18:25 +0000130 return false;
131
132 Value *Src = Cast->getOperand(0);
133 PointerType *SrcTy = cast<PointerType>(Src->getType());
134 PointerType *DestTy = cast<PointerType>(Cast->getType());
135 // TODO: For now, we only handle the case where the addrspacecast only changes
136 // the address space but not the type. If the type also changes, we could
137 // still get rid of the addrspacecast by adding an extra bitcast, but we
138 // rarely see such scenarios.
139 if (SrcTy->getElementType() != DestTy->getElementType())
140 return false;
141
142 // Checks whether the addrspacecast is from a non-generic address space to the
143 // generic address space.
144 return (SrcTy->getAddressSpace() != AddressSpace::ADDRESS_SPACE_GENERIC &&
145 DestTy->getAddressSpace() == AddressSpace::ADDRESS_SPACE_GENERIC);
146}
147
Jingyue Wu75589ff2015-06-09 21:50:32 +0000148Value *NVPTXFavorNonGenericAddrSpaces::hoistAddrSpaceCastFromGEP(
149 GEPOperator *GEP, int Depth) {
150 Value *NewOperand =
151 hoistAddrSpaceCastFrom(GEP->getPointerOperand(), Depth + 1);
152 if (NewOperand == nullptr)
153 return nullptr;
Eli Benderskybbef1722014-04-03 21:18:25 +0000154
Jingyue Wu75589ff2015-06-09 21:50:32 +0000155 // hoistAddrSpaceCastFrom returns an eliminable addrspacecast or nullptr.
156 assert(isEliminableAddrSpaceCast(NewOperand));
157 Operator *Cast = cast<Operator>(NewOperand);
Eli Benderskybbef1722014-04-03 21:18:25 +0000158
159 SmallVector<Value *, 8> Indices(GEP->idx_begin(), GEP->idx_end());
Jingyue Wu75589ff2015-06-09 21:50:32 +0000160 Value *NewASC;
Eli Benderskybbef1722014-04-03 21:18:25 +0000161 if (Instruction *GEPI = dyn_cast<Instruction>(GEP)) {
Jingyue Wu995dde22015-05-29 17:00:27 +0000162 // GEP = gep (addrspacecast X), indices
Eli Benderskybbef1722014-04-03 21:18:25 +0000163 // =>
Jingyue Wu995dde22015-05-29 17:00:27 +0000164 // NewGEP = gep X, indices
165 // NewASC = addrspacecast NewGEP
166 GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
David Blaikie096b1da2015-03-14 19:53:33 +0000167 GEP->getSourceElementType(), Cast->getOperand(0), Indices,
Jingyue Wu995dde22015-05-29 17:00:27 +0000168 "", GEPI);
169 NewGEP->setIsInBounds(GEP->isInBounds());
Jingyue Wu13755602016-03-20 20:59:20 +0000170 NewGEP->takeName(GEP);
Jingyue Wu75589ff2015-06-09 21:50:32 +0000171 NewASC = new AddrSpaceCastInst(NewGEP, GEP->getType(), "", GEPI);
Jingyue Wu75589ff2015-06-09 21:50:32 +0000172 // Without RAUWing GEP, the compiler would visit GEP again and emit
173 // redundant instructions. This is exercised in test @rauw in
174 // access-non-generic.ll.
Jingyue Wu995dde22015-05-29 17:00:27 +0000175 GEP->replaceAllUsesWith(NewASC);
Eli Benderskybbef1722014-04-03 21:18:25 +0000176 } else {
177 // GEP is a constant expression.
Jingyue Wu995dde22015-05-29 17:00:27 +0000178 Constant *NewGEP = ConstantExpr::getGetElementPtr(
David Blaikie4a2e73b2015-04-02 18:55:32 +0000179 GEP->getSourceElementType(), cast<Constant>(Cast->getOperand(0)),
180 Indices, GEP->isInBounds());
Jingyue Wu75589ff2015-06-09 21:50:32 +0000181 NewASC = ConstantExpr::getAddrSpaceCast(NewGEP, GEP->getType());
Eli Benderskybbef1722014-04-03 21:18:25 +0000182 }
Jingyue Wu75589ff2015-06-09 21:50:32 +0000183 return NewASC;
Eli Benderskybbef1722014-04-03 21:18:25 +0000184}
185
Jingyue Wu75589ff2015-06-09 21:50:32 +0000186Value *NVPTXFavorNonGenericAddrSpaces::hoistAddrSpaceCastFromBitCast(
Jingyue Wu995dde22015-05-29 17:00:27 +0000187 BitCastOperator *BC, int Depth) {
Jingyue Wu75589ff2015-06-09 21:50:32 +0000188 Value *NewOperand = hoistAddrSpaceCastFrom(BC->getOperand(0), Depth + 1);
189 if (NewOperand == nullptr)
190 return nullptr;
Jingyue Wu995dde22015-05-29 17:00:27 +0000191
Jingyue Wu75589ff2015-06-09 21:50:32 +0000192 // hoistAddrSpaceCastFrom returns an eliminable addrspacecast or nullptr.
193 assert(isEliminableAddrSpaceCast(NewOperand));
194 Operator *Cast = cast<Operator>(NewOperand);
Jingyue Wu995dde22015-05-29 17:00:27 +0000195
196 // Cast = addrspacecast Src
197 // BC = bitcast Cast
198 // =>
199 // Cast' = bitcast Src
200 // BC' = addrspacecast Cast'
201 Value *Src = Cast->getOperand(0);
202 Type *TypeOfNewCast =
203 PointerType::get(BC->getType()->getPointerElementType(),
204 Src->getType()->getPointerAddressSpace());
Jingyue Wu75589ff2015-06-09 21:50:32 +0000205 Value *NewBC;
Jingyue Wu995dde22015-05-29 17:00:27 +0000206 if (BitCastInst *BCI = dyn_cast<BitCastInst>(BC)) {
207 Value *NewCast = new BitCastInst(Src, TypeOfNewCast, "", BCI);
Jingyue Wu75589ff2015-06-09 21:50:32 +0000208 NewBC = new AddrSpaceCastInst(NewCast, BC->getType(), "", BCI);
Jingyue Wu995dde22015-05-29 17:00:27 +0000209 NewBC->takeName(BC);
Jingyue Wu75589ff2015-06-09 21:50:32 +0000210 // Without RAUWing BC, the compiler would visit BC again and emit
211 // redundant instructions. This is exercised in test @rauw in
212 // access-non-generic.ll.
Jingyue Wu995dde22015-05-29 17:00:27 +0000213 BC->replaceAllUsesWith(NewBC);
214 } else {
215 // BC is a constant expression.
216 Constant *NewCast =
217 ConstantExpr::getBitCast(cast<Constant>(Src), TypeOfNewCast);
Jingyue Wu75589ff2015-06-09 21:50:32 +0000218 NewBC = ConstantExpr::getAddrSpaceCast(NewCast, BC->getType());
Jingyue Wu995dde22015-05-29 17:00:27 +0000219 }
Jingyue Wu75589ff2015-06-09 21:50:32 +0000220 return NewBC;
Jingyue Wu995dde22015-05-29 17:00:27 +0000221}
222
Jingyue Wu75589ff2015-06-09 21:50:32 +0000223Value *NVPTXFavorNonGenericAddrSpaces::hoistAddrSpaceCastFrom(Value *V,
224 int Depth) {
225 // Returns V if V is already an eliminable addrspacecast.
Jingyue Wu995dde22015-05-29 17:00:27 +0000226 if (isEliminableAddrSpaceCast(V))
Jingyue Wu75589ff2015-06-09 21:50:32 +0000227 return V;
Jingyue Wu995dde22015-05-29 17:00:27 +0000228
229 // Limit the depth to prevent this recursive function from running too long.
230 const int MaxDepth = 20;
231 if (Depth >= MaxDepth)
Jingyue Wu75589ff2015-06-09 21:50:32 +0000232 return nullptr;
Jingyue Wu995dde22015-05-29 17:00:27 +0000233
234 // If V is a GEP or bitcast, hoist the addrspacecast if any from its pointer
235 // operand. This enables optimizeMemoryInstruction to shortcut addrspacecasts
236 // that are not directly used by the load/store.
237 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
238 return hoistAddrSpaceCastFromGEP(GEP, Depth);
239
240 if (BitCastOperator *BC = dyn_cast<BitCastOperator>(V))
241 return hoistAddrSpaceCastFromBitCast(BC, Depth);
242
Jingyue Wu75589ff2015-06-09 21:50:32 +0000243 return nullptr;
Jingyue Wu995dde22015-05-29 17:00:27 +0000244}
245
Eli Benderskybbef1722014-04-03 21:18:25 +0000246bool NVPTXFavorNonGenericAddrSpaces::optimizeMemoryInstruction(Instruction *MI,
247 unsigned Idx) {
Jingyue Wu75589ff2015-06-09 21:50:32 +0000248 Value *NewOperand = hoistAddrSpaceCastFrom(MI->getOperand(Idx));
249 if (NewOperand == nullptr)
250 return false;
251
252 // load/store (addrspacecast X) => load/store X if shortcutting the
253 // addrspacecast is valid and can improve performance.
254 //
255 // e.g.,
256 // %1 = addrspacecast float addrspace(3)* %0 to float*
257 // %2 = load float* %1
258 // ->
259 // %2 = load float addrspace(3)* %0
260 //
261 // Note: the addrspacecast can also be a constant expression.
262 assert(isEliminableAddrSpaceCast(NewOperand));
263 Operator *ASC = dyn_cast<Operator>(NewOperand);
264 MI->setOperand(Idx, ASC->getOperand(0));
265 return true;
Eli Benderskybbef1722014-04-03 21:18:25 +0000266}
267
268bool NVPTXFavorNonGenericAddrSpaces::runOnFunction(Function &F) {
Andrew Kaylor87b10dd2016-04-26 23:44:31 +0000269 if (DisableFavorNonGeneric || skipFunction(F))
Eli Benderskybbef1722014-04-03 21:18:25 +0000270 return false;
271
272 bool Changed = false;
Duncan P. N. Exon Smith61149b82015-10-20 00:54:09 +0000273 for (BasicBlock &B : F) {
274 for (Instruction &I : B) {
Eli Benderskybbef1722014-04-03 21:18:25 +0000275 if (isa<LoadInst>(I)) {
276 // V = load P
Duncan P. N. Exon Smith61149b82015-10-20 00:54:09 +0000277 Changed |= optimizeMemoryInstruction(&I, 0);
Eli Benderskybbef1722014-04-03 21:18:25 +0000278 } else if (isa<StoreInst>(I)) {
279 // store V, P
Duncan P. N. Exon Smith61149b82015-10-20 00:54:09 +0000280 Changed |= optimizeMemoryInstruction(&I, 1);
Eli Benderskybbef1722014-04-03 21:18:25 +0000281 }
282 }
283 }
284 return Changed;
285}
286
287FunctionPass *llvm::createNVPTXFavorNonGenericAddrSpacesPass() {
288 return new NVPTXFavorNonGenericAddrSpaces();
289}