1*9880d681SAndroid Build Coastguard Worker //===-- NVPTXLowerAlloca.cpp - Make alloca to use local memory =====--===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // For all alloca instructions, and add a pair of cast to local address for
11*9880d681SAndroid Build Coastguard Worker // each of them. For example,
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // %A = alloca i32
14*9880d681SAndroid Build Coastguard Worker // store i32 0, i32* %A ; emits st.u32
15*9880d681SAndroid Build Coastguard Worker //
16*9880d681SAndroid Build Coastguard Worker // will be transformed to
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker // %A = alloca i32
19*9880d681SAndroid Build Coastguard Worker // %Local = addrspacecast i32* %A to i32 addrspace(5)*
20*9880d681SAndroid Build Coastguard Worker // %Generic = addrspacecast i32 addrspace(5)* %A to i32*
21*9880d681SAndroid Build Coastguard Worker // store i32 0, i32 addrspace(5)* %Generic ; emits st.local.u32
22*9880d681SAndroid Build Coastguard Worker //
23*9880d681SAndroid Build Coastguard Worker // And we will rely on NVPTXFavorNonGenericAddrSpace to combine the last
24*9880d681SAndroid Build Coastguard Worker // two instructions.
25*9880d681SAndroid Build Coastguard Worker //
26*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker #include "NVPTX.h"
29*9880d681SAndroid Build Coastguard Worker #include "NVPTXUtilities.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker using namespace llvm;
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker namespace llvm {
40*9880d681SAndroid Build Coastguard Worker void initializeNVPTXLowerAllocaPass(PassRegistry &);
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker namespace {
44*9880d681SAndroid Build Coastguard Worker class NVPTXLowerAlloca : public BasicBlockPass {
45*9880d681SAndroid Build Coastguard Worker bool runOnBasicBlock(BasicBlock &BB) override;
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker public:
48*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
NVPTXLowerAlloca()49*9880d681SAndroid Build Coastguard Worker NVPTXLowerAlloca() : BasicBlockPass(ID) {}
getPassName() const50*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
51*9880d681SAndroid Build Coastguard Worker return "convert address space of alloca'ed memory to local";
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker };
54*9880d681SAndroid Build Coastguard Worker } // namespace
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker char NVPTXLowerAlloca::ID = 1;
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca",
59*9880d681SAndroid Build Coastguard Worker "Lower Alloca", false, false)
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker // =============================================================================
62*9880d681SAndroid Build Coastguard Worker // Main function for this pass.
63*9880d681SAndroid Build Coastguard Worker // =============================================================================
runOnBasicBlock(BasicBlock & BB)64*9880d681SAndroid Build Coastguard Worker bool NVPTXLowerAlloca::runOnBasicBlock(BasicBlock &BB) {
65*9880d681SAndroid Build Coastguard Worker if (skipBasicBlock(BB))
66*9880d681SAndroid Build Coastguard Worker return false;
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard Worker bool Changed = false;
69*9880d681SAndroid Build Coastguard Worker for (auto &I : BB) {
70*9880d681SAndroid Build Coastguard Worker if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
71*9880d681SAndroid Build Coastguard Worker Changed = true;
72*9880d681SAndroid Build Coastguard Worker auto PTy = dyn_cast<PointerType>(allocaInst->getType());
73*9880d681SAndroid Build Coastguard Worker auto ETy = PTy->getElementType();
74*9880d681SAndroid Build Coastguard Worker auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
75*9880d681SAndroid Build Coastguard Worker auto NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
76*9880d681SAndroid Build Coastguard Worker auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
77*9880d681SAndroid Build Coastguard Worker auto NewASCToGeneric = new AddrSpaceCastInst(NewASCToLocal,
78*9880d681SAndroid Build Coastguard Worker GenericAddrTy, "");
79*9880d681SAndroid Build Coastguard Worker NewASCToLocal->insertAfter(allocaInst);
80*9880d681SAndroid Build Coastguard Worker NewASCToGeneric->insertAfter(NewASCToLocal);
81*9880d681SAndroid Build Coastguard Worker for (Value::use_iterator UI = allocaInst->use_begin(),
82*9880d681SAndroid Build Coastguard Worker UE = allocaInst->use_end();
83*9880d681SAndroid Build Coastguard Worker UI != UE; ) {
84*9880d681SAndroid Build Coastguard Worker // Check Load, Store, GEP, and BitCast Uses on alloca and make them
85*9880d681SAndroid Build Coastguard Worker // use the converted generic address, in order to expose non-generic
86*9880d681SAndroid Build Coastguard Worker // addrspacecast to NVPTXFavorNonGenericAddrSpace. For other types
87*9880d681SAndroid Build Coastguard Worker // of instructions this is unnecessary and may introduce redundant
88*9880d681SAndroid Build Coastguard Worker // address cast.
89*9880d681SAndroid Build Coastguard Worker const auto &AllocaUse = *UI++;
90*9880d681SAndroid Build Coastguard Worker auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
91*9880d681SAndroid Build Coastguard Worker if (LI && LI->getPointerOperand() == allocaInst && !LI->isVolatile()) {
92*9880d681SAndroid Build Coastguard Worker LI->setOperand(LI->getPointerOperandIndex(), NewASCToGeneric);
93*9880d681SAndroid Build Coastguard Worker continue;
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
96*9880d681SAndroid Build Coastguard Worker if (SI && SI->getPointerOperand() == allocaInst && !SI->isVolatile()) {
97*9880d681SAndroid Build Coastguard Worker SI->setOperand(SI->getPointerOperandIndex(), NewASCToGeneric);
98*9880d681SAndroid Build Coastguard Worker continue;
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
101*9880d681SAndroid Build Coastguard Worker if (GI && GI->getPointerOperand() == allocaInst) {
102*9880d681SAndroid Build Coastguard Worker GI->setOperand(GI->getPointerOperandIndex(), NewASCToGeneric);
103*9880d681SAndroid Build Coastguard Worker continue;
104*9880d681SAndroid Build Coastguard Worker }
105*9880d681SAndroid Build Coastguard Worker auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
106*9880d681SAndroid Build Coastguard Worker if (BI && BI->getOperand(0) == allocaInst) {
107*9880d681SAndroid Build Coastguard Worker BI->setOperand(0, NewASCToGeneric);
108*9880d681SAndroid Build Coastguard Worker continue;
109*9880d681SAndroid Build Coastguard Worker }
110*9880d681SAndroid Build Coastguard Worker }
111*9880d681SAndroid Build Coastguard Worker }
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker return Changed;
114*9880d681SAndroid Build Coastguard Worker }
115*9880d681SAndroid Build Coastguard Worker
createNVPTXLowerAllocaPass()116*9880d681SAndroid Build Coastguard Worker BasicBlockPass *llvm::createNVPTXLowerAllocaPass() {
117*9880d681SAndroid Build Coastguard Worker return new NVPTXLowerAlloca();
118*9880d681SAndroid Build Coastguard Worker }
119