| 1 | //===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===// |
|---|---|
| 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 | // Hoist the alloca instructions in the non-entry blocks to the entry blocks. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "NVPTX.h" |
| 14 | #include "llvm/CodeGen/StackProtector.h" |
| 15 | #include "llvm/IR/Constants.h" |
| 16 | #include "llvm/IR/Function.h" |
| 17 | #include "llvm/IR/Instructions.h" |
| 18 | using namespace llvm; |
| 19 | |
| 20 | static bool hoistAllocas(Function &function) { |
| 21 | bool functionModified = false; |
| 22 | Function::iterator I = function.begin(); |
| 23 | Instruction *firstTerminatorInst = (I++)->getTerminator(); |
| 24 | |
| 25 | for (Function::iterator E = function.end(); I != E; ++I) { |
| 26 | for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) { |
| 27 | AllocaInst *allocaInst = dyn_cast<AllocaInst>(Val: BI++); |
| 28 | if (allocaInst && isa<ConstantInt>(Val: allocaInst->getArraySize())) { |
| 29 | allocaInst->moveBefore(InsertPos: firstTerminatorInst->getIterator()); |
| 30 | functionModified = true; |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return functionModified; |
| 36 | } |
| 37 | |
| 38 | namespace { |
| 39 | // Hoisting the alloca instructions in the non-entry blocks to the entry |
| 40 | // block. |
| 41 | class NVPTXAllocaHoistingLegacyPass : public FunctionPass { |
| 42 | public: |
| 43 | static char ID; // Pass ID |
| 44 | NVPTXAllocaHoistingLegacyPass() : FunctionPass(ID) {} |
| 45 | |
| 46 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 47 | AU.setPreservesCFG(); |
| 48 | AU.addPreserved<StackProtector>(); |
| 49 | } |
| 50 | |
| 51 | StringRef getPassName() const override { |
| 52 | return "NVPTX specific alloca hoisting"; |
| 53 | } |
| 54 | |
| 55 | bool runOnFunction(Function &function) override { |
| 56 | return hoistAllocas(function); |
| 57 | } |
| 58 | }; |
| 59 | } // namespace |
| 60 | |
| 61 | char NVPTXAllocaHoistingLegacyPass::ID = 0; |
| 62 | |
| 63 | INITIALIZE_PASS( |
| 64 | NVPTXAllocaHoistingLegacyPass, "alloca-hoisting", |
| 65 | "Hoisting alloca instructions in non-entry blocks to the entry block", |
| 66 | false, false) |
| 67 | |
| 68 | FunctionPass *llvm::createNVPTXAllocaHoistingLegacyPass() { |
| 69 | return new NVPTXAllocaHoistingLegacyPass; |
| 70 | } |
| 71 | |
| 72 | PreservedAnalyses NVPTXAllocaHoistingPass::run(Function &F, |
| 73 | FunctionAnalysisManager &FAM) { |
| 74 | if (!hoistAllocas(function&: F)) |
| 75 | return PreservedAnalyses::all(); |
| 76 | PreservedAnalyses PA; |
| 77 | PA.preserveSet<CFGAnalyses>(); |
| 78 | PA.preserve<SSPLayoutAnalysis>(); |
| 79 | return PA; |
| 80 | } |
| 81 |