LLVM  3.7.0
NVPTXLowerAlloca.cpp
Go to the documentation of this file.
1 //===-- NVPTXLowerAlloca.cpp - Make alloca to use local memory =====--===//
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 // For all alloca instructions, and add a pair of cast to local address for
11 // each of them. For example,
12 //
13 // %A = alloca i32
14 // store i32 0, i32* %A ; emits st.u32
15 //
16 // will be transformed to
17 //
18 // %A = alloca i32
19 // %Local = addrspacecast i32* %A to i32 addrspace(5)*
20 // %Generic = addrspacecast i32 addrspace(5)* %A to i32*
21 // store i32 0, i32 addrspace(5)* %Generic ; emits st.local.u32
22 //
23 // And we will rely on NVPTXFavorNonGenericAddrSpace to combine the last
24 // two instructions.
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #include "NVPTX.h"
29 #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 BasicBlockPass {
45  bool runOnBasicBlock(BasicBlock &BB) override;
46 
47 public:
48  static char ID; // Pass identification, replacement for typeid
49  NVPTXLowerAlloca() : BasicBlockPass(ID) {}
50  const char *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::runOnBasicBlock(BasicBlock &BB) {
65  bool Changed = false;
66  for (auto &I : BB) {
67  if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
68  Changed = true;
69  auto PTy = dyn_cast<PointerType>(allocaInst->getType());
70  auto ETy = PTy->getElementType();
71  auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
72  auto NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
73  auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
74  auto NewASCToGeneric = new AddrSpaceCastInst(NewASCToLocal,
75  GenericAddrTy, "");
76  NewASCToLocal->insertAfter(allocaInst);
77  NewASCToGeneric->insertAfter(NewASCToLocal);
78  for (Value::use_iterator UI = allocaInst->use_begin(),
79  UE = allocaInst->use_end();
80  UI != UE; ) {
81  // Check Load, Store, GEP, and BitCast Uses on alloca and make them
82  // use the converted generic address, in order to expose non-generic
83  // addrspacecast to NVPTXFavorNonGenericAddrSpace. For other types
84  // of instructions this is unecessary and may introduce redudant
85  // address cast.
86  const auto &AllocaUse = *UI++;
87  auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
88  if (LI && LI->getPointerOperand() == allocaInst && !LI->isVolatile()) {
89  LI->setOperand(LI->getPointerOperandIndex(), NewASCToGeneric);
90  continue;
91  }
92  auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
93  if (SI && SI->getPointerOperand() == allocaInst && !SI->isVolatile()) {
94  SI->setOperand(SI->getPointerOperandIndex(), NewASCToGeneric);
95  continue;
96  }
97  auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
98  if (GI && GI->getPointerOperand() == allocaInst) {
99  GI->setOperand(GI->getPointerOperandIndex(), NewASCToGeneric);
100  continue;
101  }
102  auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
103  if (BI && BI->getOperand(0) == allocaInst) {
104  BI->setOperand(0, NewASCToGeneric);
105  continue;
106  }
107  }
108  }
109  }
110  return Changed;
111 }
112 
114  return new NVPTXLowerAlloca();
115 }
use_iterator_impl< Use > use_iterator
Definition: Value.h:277
static PointerType * get(Type *ElementType, unsigned AddressSpace)
PointerType::get - This constructs a pointer to an object of the specified type in a numbered address...
Definition: Type.cpp:738
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
This class represents a conversion between pointers from one address space to another.
This class represents a no-op cast from one type to another.
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
Type * getElementType() const
Definition: DerivedTypes.h:323
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
Definition: Instructions.h:830
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
INITIALIZE_PASS(NVPTXLowerAlloca,"nvptx-lower-alloca","Lower Alloca", false, false) bool NVPTXLowerAlloca
BasicBlockPass class - This class is used to implement most local optimizations.
Definition: Pass.h:330
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:122
LLVM_ATTRIBUTE_UNUSED_RESULT 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:285
#define I(x, y, z)
Definition: MD5.cpp:54
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:41