1 //===-- NVPTXLowerAlloca.cpp - Make alloca to use local memory =====--===//
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
7 //===----------------------------------------------------------------------===//
9 // For all alloca instructions, and add a pair of cast to local address for
10 // each of them. For example,
13 // store i32 0, i32* %A ; emits st.u32
15 // will be transformed to
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
22 // And we will rely on NVPTXInferAddressSpaces to combine the last two
25 //===----------------------------------------------------------------------===//
28 #include "NVPTXUtilities.h"
29 #include "MCTargetDesc/NVPTXBaseInfo.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"
40 void initializeNVPTXLowerAllocaPass(PassRegistry
&);
44 class NVPTXLowerAlloca
: public FunctionPass
{
45 bool runOnFunction(Function
&F
) override
;
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";
56 char NVPTXLowerAlloca::ID
= 1;
58 INITIALIZE_PASS(NVPTXLowerAlloca
, "nvptx-lower-alloca",
59 "Lower Alloca", false, false)
61 // =============================================================================
62 // Main function for this pass.
63 // =============================================================================
64 bool NVPTXLowerAlloca::runOnFunction(Function
&F
) {
71 if (auto allocaInst
= dyn_cast
<AllocaInst
>(&I
)) {
73 auto PTy
= dyn_cast
<PointerType
>(allocaInst
->getType());
74 auto ETy
= PTy
->getElementType();
75 auto LocalAddrTy
= PointerType::get(ETy
, ADDRESS_SPACE_LOCAL
);
76 auto NewASCToLocal
= new AddrSpaceCastInst(allocaInst
, LocalAddrTy
, "");
77 auto GenericAddrTy
= PointerType::get(ETy
, ADDRESS_SPACE_GENERIC
);
78 auto NewASCToGeneric
=
79 new AddrSpaceCastInst(NewASCToLocal
, GenericAddrTy
, "");
80 NewASCToLocal
->insertAfter(allocaInst
);
81 NewASCToGeneric
->insertAfter(NewASCToLocal
);
82 for (Value::use_iterator UI
= allocaInst
->use_begin(),
83 UE
= allocaInst
->use_end();
85 // Check Load, Store, GEP, and BitCast Uses on alloca and make them
86 // use the converted generic address, in order to expose non-generic
87 // addrspacecast to NVPTXInferAddressSpaces. For other types
88 // of instructions this is unnecessary and may introduce redundant
90 const auto &AllocaUse
= *UI
++;
91 auto LI
= dyn_cast
<LoadInst
>(AllocaUse
.getUser());
92 if (LI
&& LI
->getPointerOperand() == allocaInst
&&
94 LI
->setOperand(LI
->getPointerOperandIndex(), NewASCToGeneric
);
97 auto SI
= dyn_cast
<StoreInst
>(AllocaUse
.getUser());
98 if (SI
&& SI
->getPointerOperand() == allocaInst
&&
100 SI
->setOperand(SI
->getPointerOperandIndex(), NewASCToGeneric
);
103 auto GI
= dyn_cast
<GetElementPtrInst
>(AllocaUse
.getUser());
104 if (GI
&& GI
->getPointerOperand() == allocaInst
) {
105 GI
->setOperand(GI
->getPointerOperandIndex(), NewASCToGeneric
);
108 auto BI
= dyn_cast
<BitCastInst
>(AllocaUse
.getUser());
109 if (BI
&& BI
->getOperand(0) == allocaInst
) {
110 BI
->setOperand(0, NewASCToGeneric
);
119 FunctionPass
*llvm::createNVPTXLowerAllocaPass() {
120 return new NVPTXLowerAlloca();