NVPTXAllocaHoisting.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "NVPTXAllocaHoisting.h"
  13. #include "llvm/CodeGen/StackProtector.h"
  14. #include "llvm/IR/Constants.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/Instructions.h"
  17. using namespace llvm;
  18. namespace {
  19. // Hoisting the alloca instructions in the non-entry blocks to the entry
  20. // block.
  21. class NVPTXAllocaHoisting : public FunctionPass {
  22. public:
  23. static char ID; // Pass ID
  24. NVPTXAllocaHoisting() : FunctionPass(ID) {}
  25. void getAnalysisUsage(AnalysisUsage &AU) const override {
  26. AU.addPreserved<StackProtector>();
  27. }
  28. StringRef getPassName() const override {
  29. return "NVPTX specific alloca hoisting";
  30. }
  31. bool runOnFunction(Function &function) override;
  32. };
  33. } // namespace
  34. bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
  35. bool functionModified = false;
  36. Function::iterator I = function.begin();
  37. Instruction *firstTerminatorInst = (I++)->getTerminator();
  38. for (Function::iterator E = function.end(); I != E; ++I) {
  39. for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {
  40. AllocaInst *allocaInst = dyn_cast<AllocaInst>(BI++);
  41. if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) {
  42. allocaInst->moveBefore(firstTerminatorInst);
  43. functionModified = true;
  44. }
  45. }
  46. }
  47. return functionModified;
  48. }
  49. char NVPTXAllocaHoisting::ID = 0;
  50. namespace llvm {
  51. void initializeNVPTXAllocaHoistingPass(PassRegistry &);
  52. }
  53. INITIALIZE_PASS(
  54. NVPTXAllocaHoisting, "alloca-hoisting",
  55. "Hoisting alloca instructions in non-entry blocks to the entry block",
  56. false, false)
  57. FunctionPass *llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting; }