LLVM  9.0.0svn
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"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/Pass.h"
35 
36 using namespace llvm;
37 
38 namespace llvm {
40 }
41 
42 namespace {
43 class NVPTXLowerAlloca : public BasicBlockPass {
44  bool runOnBasicBlock(BasicBlock &BB) override;
45 
46 public:
47  static char ID; // Pass identification, replacement for typeid
48  NVPTXLowerAlloca() : BasicBlockPass(ID) {}
49  StringRef getPassName() const override {
50  return "convert address space of alloca'ed memory to local";
51  }
52 };
53 } // namespace
54 
55 char NVPTXLowerAlloca::ID = 1;
56 
57 INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca",
58  "Lower Alloca", false, false)
59 
60 // =============================================================================
61 // Main function for this pass.
62 // =============================================================================
63 bool NVPTXLowerAlloca::runOnBasicBlock(BasicBlock &BB) {
64  if (skipBasicBlock(BB))
65  return false;
66 
67  bool Changed = false;
68  for (auto &I : BB) {
69  if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
70  Changed = true;
71  auto PTy = dyn_cast<PointerType>(allocaInst->getType());
72  auto ETy = PTy->getElementType();
73  auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
74  auto NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
75  auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
76  auto NewASCToGeneric = new AddrSpaceCastInst(NewASCToLocal,
77  GenericAddrTy, "");
78  NewASCToLocal->insertAfter(allocaInst);
79  NewASCToGeneric->insertAfter(NewASCToLocal);
80  for (Value::use_iterator UI = allocaInst->use_begin(),
81  UE = allocaInst->use_end();
82  UI != UE; ) {
83  // Check Load, Store, GEP, and BitCast Uses on alloca and make them
84  // use the converted generic address, in order to expose non-generic
85  // addrspacecast to NVPTXInferAddressSpaces. For other types
86  // of instructions this is unnecessary and may introduce redundant
87  // address cast.
88  const auto &AllocaUse = *UI++;
89  auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
90  if (LI && LI->getPointerOperand() == allocaInst && !LI->isVolatile()) {
91  LI->setOperand(LI->getPointerOperandIndex(), NewASCToGeneric);
92  continue;
93  }
94  auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
95  if (SI && SI->getPointerOperand() == allocaInst && !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 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:629
An instruction for reading from memory.
Definition: Instructions.h:167
This class represents a conversion between pointers from one address space to another.
INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca", "Lower Alloca", false, false) bool NVPTXLowerAlloca
This class represents a no-op cast from one type to another.
An instruction for storing to memory.
Definition: Instructions.h:320
Class to represent pointers.
Definition: DerivedTypes.h:498
use_iterator_impl< Use > use_iterator
Definition: Value.h:331
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:873
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
BasicBlockPass class - This class is used to implement most local optimizations.
Definition: Pass.h:318
void initializeNVPTXLowerAllocaPass(PassRegistry &)
BasicBlockPass * createNVPTXLowerAllocaPass()
Module.h This file contains the declarations for the Module class.
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
static bool runOnBasicBlock(MachineBasicBlock *MBB, std::vector< StringRef > &bbNames, std::vector< unsigned > &renamedInOtherBB, unsigned &basicBlockNum, unsigned &VRegGapIndex, NamedVRegCursor &NVC)
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
Type * getElementType() const
Definition: DerivedTypes.h:517