xref: /freebsd/contrib/llvm-project/llvm/lib/Target/NVPTX/NVPTXLowerAlloca.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===-- NVPTXLowerAlloca.cpp - Make alloca to use local memory =====--===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // For all alloca instructions, and add a pair of cast to local address for
100b57cec5SDimitry Andric // each of them. For example,
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //   %A = alloca i32
130b57cec5SDimitry Andric //   store i32 0, i32* %A ; emits st.u32
140b57cec5SDimitry Andric //
150b57cec5SDimitry Andric // will be transformed to
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric //   %A = alloca i32
180b57cec5SDimitry Andric //   %Local = addrspacecast i32* %A to i32 addrspace(5)*
190b57cec5SDimitry Andric //   %Generic = addrspacecast i32 addrspace(5)* %A to i32*
200b57cec5SDimitry Andric //   store i32 0, i32 addrspace(5)* %Generic ; emits st.local.u32
210b57cec5SDimitry Andric //
220b57cec5SDimitry Andric // And we will rely on NVPTXInferAddressSpaces to combine the last two
230b57cec5SDimitry Andric // instructions.
240b57cec5SDimitry Andric //
250b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric #include "NVPTX.h"
280b57cec5SDimitry Andric #include "NVPTXUtilities.h"
290b57cec5SDimitry Andric #include "MCTargetDesc/NVPTXBaseInfo.h"
300b57cec5SDimitry Andric #include "llvm/IR/Function.h"
310b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
320b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
330b57cec5SDimitry Andric #include "llvm/IR/Module.h"
340b57cec5SDimitry Andric #include "llvm/IR/Type.h"
350b57cec5SDimitry Andric #include "llvm/Pass.h"
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric using namespace llvm;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace llvm {
400b57cec5SDimitry Andric void initializeNVPTXLowerAllocaPass(PassRegistry &);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric namespace {
448bcb0991SDimitry Andric class NVPTXLowerAlloca : public FunctionPass {
458bcb0991SDimitry Andric   bool runOnFunction(Function &F) override;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric public:
480b57cec5SDimitry Andric   static char ID; // Pass identification, replacement for typeid
NVPTXLowerAlloca()498bcb0991SDimitry Andric   NVPTXLowerAlloca() : FunctionPass(ID) {}
getPassName() const500b57cec5SDimitry Andric   StringRef getPassName() const override {
510b57cec5SDimitry Andric     return "convert address space of alloca'ed memory to local";
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric };
540b57cec5SDimitry Andric } // namespace
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric char NVPTXLowerAlloca::ID = 1;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca",
590b57cec5SDimitry Andric                 "Lower Alloca", false, false)
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric // =============================================================================
620b57cec5SDimitry Andric // Main function for this pass.
630b57cec5SDimitry Andric // =============================================================================
runOnFunction(Function & F)648bcb0991SDimitry Andric bool NVPTXLowerAlloca::runOnFunction(Function &F) {
658bcb0991SDimitry Andric   if (skipFunction(F))
660b57cec5SDimitry Andric     return false;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   bool Changed = false;
698bcb0991SDimitry Andric   for (auto &BB : F)
700b57cec5SDimitry Andric     for (auto &I : BB) {
710b57cec5SDimitry Andric       if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
720b57cec5SDimitry Andric         Changed = true;
73fe6060f1SDimitry Andric         auto ETy = allocaInst->getAllocatedType();
740b57cec5SDimitry Andric         auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
750b57cec5SDimitry Andric         auto NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
760b57cec5SDimitry Andric         auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
778bcb0991SDimitry Andric         auto NewASCToGeneric =
788bcb0991SDimitry Andric             new AddrSpaceCastInst(NewASCToLocal, GenericAddrTy, "");
790b57cec5SDimitry Andric         NewASCToLocal->insertAfter(allocaInst);
800b57cec5SDimitry Andric         NewASCToGeneric->insertAfter(NewASCToLocal);
81*349cc55cSDimitry Andric         for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
820b57cec5SDimitry Andric           // Check Load, Store, GEP, and BitCast Uses on alloca and make them
830b57cec5SDimitry Andric           // use the converted generic address, in order to expose non-generic
840b57cec5SDimitry Andric           // addrspacecast to NVPTXInferAddressSpaces. For other types
850b57cec5SDimitry Andric           // of instructions this is unnecessary and may introduce redundant
860b57cec5SDimitry Andric           // address cast.
870b57cec5SDimitry Andric           auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
888bcb0991SDimitry Andric           if (LI && LI->getPointerOperand() == allocaInst &&
898bcb0991SDimitry Andric               !LI->isVolatile()) {
900b57cec5SDimitry Andric             LI->setOperand(LI->getPointerOperandIndex(), NewASCToGeneric);
910b57cec5SDimitry Andric             continue;
920b57cec5SDimitry Andric           }
930b57cec5SDimitry Andric           auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
948bcb0991SDimitry Andric           if (SI && SI->getPointerOperand() == allocaInst &&
958bcb0991SDimitry Andric               !SI->isVolatile()) {
960b57cec5SDimitry Andric             SI->setOperand(SI->getPointerOperandIndex(), NewASCToGeneric);
970b57cec5SDimitry Andric             continue;
980b57cec5SDimitry Andric           }
990b57cec5SDimitry Andric           auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
1000b57cec5SDimitry Andric           if (GI && GI->getPointerOperand() == allocaInst) {
1010b57cec5SDimitry Andric             GI->setOperand(GI->getPointerOperandIndex(), NewASCToGeneric);
1020b57cec5SDimitry Andric             continue;
1030b57cec5SDimitry Andric           }
1040b57cec5SDimitry Andric           auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
1050b57cec5SDimitry Andric           if (BI && BI->getOperand(0) == allocaInst) {
1060b57cec5SDimitry Andric             BI->setOperand(0, NewASCToGeneric);
1070b57cec5SDimitry Andric             continue;
1080b57cec5SDimitry Andric           }
1090b57cec5SDimitry Andric         }
1100b57cec5SDimitry Andric       }
1110b57cec5SDimitry Andric     }
1120b57cec5SDimitry Andric   return Changed;
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
createNVPTXLowerAllocaPass()1158bcb0991SDimitry Andric FunctionPass *llvm::createNVPTXLowerAllocaPass() {
1160b57cec5SDimitry Andric   return new NVPTXLowerAlloca();
1170b57cec5SDimitry Andric }
118