LLVM  14.0.0git
NVPTXLowerAlloca.cpp
Go to the documentation of this file.
1 //===-- NVPTXLowerAlloca.cpp - Make alloca to use local memory =====--===//
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 //
9 // For all alloca instructions, and add a pair of cast to local address for
10 // each of them. For example,
11 //
12 // %A = alloca i32
13 // store i32 0, i32* %A ; emits st.u32
14 //
15 // will be transformed to
16 //
17 // %A = alloca i32
18 // %Local = addrspacecast i32* %A to i32 addrspace(5)*
19 // %Generic = addrspacecast i32 addrspace(5)* %A to i32*
20 // store i32 0, i32 addrspace(5)* %Generic ; emits st.local.u32
21 //
22 // And we will rely on NVPTXInferAddressSpaces to combine the last two
23 // instructions.
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #include "NVPTX.h"
28 #include "NVPTXUtilities.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/Pass.h"
36 
37 using namespace llvm;
38 
39 namespace llvm {
41 }
42 
43 namespace {
44 class NVPTXLowerAlloca : public FunctionPass {
45  bool runOnFunction(Function &F) override;
46 
47 public:
48  static char ID; // Pass identification, replacement for typeid
49  NVPTXLowerAlloca() : FunctionPass(ID) {}
50  StringRef getPassName() const override {
51  return "convert address space of alloca'ed memory to local";
52  }
53 };
54 } // namespace
55 
56 char NVPTXLowerAlloca::ID = 1;
57 
58 INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca",
59  "Lower Alloca", false, false)
60 
61 // =============================================================================
62 // Main function for this pass.
63 // =============================================================================
64 bool NVPTXLowerAlloca::runOnFunction(Function &F) {
65  if (skipFunction(F))
66  return false;
67 
68  bool Changed = false;
69  for (auto &BB : F)
70  for (auto &I : BB) {
71  if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
72  Changed = true;
73  auto ETy = allocaInst->getAllocatedType();
74  auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
75  auto NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
76  auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
77  auto NewASCToGeneric =
78  new AddrSpaceCastInst(NewASCToLocal, GenericAddrTy, "");
79  NewASCToLocal->insertAfter(allocaInst);
80  NewASCToGeneric->insertAfter(NewASCToLocal);
81  for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
82  // Check Load, Store, GEP, and BitCast Uses on alloca and make them
83  // use the converted generic address, in order to expose non-generic
84  // addrspacecast to NVPTXInferAddressSpaces. For other types
85  // of instructions this is unnecessary and may introduce redundant
86  // address cast.
87  auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
88  if (LI && LI->getPointerOperand() == allocaInst &&
89  !LI->isVolatile()) {
90  LI->setOperand(LI->getPointerOperandIndex(), NewASCToGeneric);
91  continue;
92  }
93  auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
94  if (SI && SI->getPointerOperand() == allocaInst &&
95  !SI->isVolatile()) {
96  SI->setOperand(SI->getPointerOperandIndex(), NewASCToGeneric);
97  continue;
98  }
99  auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
100  if (GI && GI->getPointerOperand() == allocaInst) {
101  GI->setOperand(GI->getPointerOperandIndex(), NewASCToGeneric);
102  continue;
103  }
104  auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
105  if (BI && BI->getOperand(0) == allocaInst) {
106  BI->setOperand(0, NewASCToGeneric);
107  continue;
108  }
109  }
110  }
111  }
112  return Changed;
113 }
114 
116  return new NVPTXLowerAlloca();
117 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
IntrinsicInst.h
llvm::Function
Definition: Function.h:62
Pass.h
llvm::PointerType::get
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
Definition: Type.cpp:729
llvm::ADDRESS_SPACE_LOCAL
@ ADDRESS_SPACE_LOCAL
Definition: NVPTXBaseInfo.h:26
Module.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
NVPTX.h
NVPTXUtilities.h
llvm::initializeNVPTXLowerAllocaPass
void initializeNVPTXLowerAllocaPass(PassRegistry &)
llvm::AddrSpaceCastInst
This class represents a conversion between pointers from one address space to another.
Definition: Instructions.h:5249
INITIALIZE_PASS
INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca", "Lower Alloca", false, false) bool NVPTXLowerAlloca
Definition: NVPTXLowerAlloca.cpp:58
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
Type.h
NVPTXBaseInfo.h
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::make_early_inc_range
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:593
SI
StandardInstrumentations SI(Debug, VerifyEach)
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::ADDRESS_SPACE_GENERIC
@ ADDRESS_SPACE_GENERIC
Definition: NVPTXBaseInfo.h:22
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
Function.h
Instructions.h
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::createNVPTXLowerAllocaPass
FunctionPass * createNVPTXLowerAllocaPass()
Definition: NVPTXLowerAlloca.cpp:115
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38