1*0b57cec5SDimitry Andric //===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // Hoist the alloca instructions in the non-entry blocks to the entry blocks. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "NVPTXAllocaHoisting.h" 14*0b57cec5SDimitry Andric #include "llvm/CodeGen/StackProtector.h" 15*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 16*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 17*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 18*0b57cec5SDimitry Andric using namespace llvm; 19*0b57cec5SDimitry Andric 20*0b57cec5SDimitry Andric namespace { 21*0b57cec5SDimitry Andric // Hoisting the alloca instructions in the non-entry blocks to the entry 22*0b57cec5SDimitry Andric // block. 23*0b57cec5SDimitry Andric class NVPTXAllocaHoisting : public FunctionPass { 24*0b57cec5SDimitry Andric public: 25*0b57cec5SDimitry Andric static char ID; // Pass ID NVPTXAllocaHoisting()26*0b57cec5SDimitry Andric NVPTXAllocaHoisting() : FunctionPass(ID) {} 27*0b57cec5SDimitry Andric getAnalysisUsage(AnalysisUsage & AU) const28*0b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 29*0b57cec5SDimitry Andric AU.addPreserved<StackProtector>(); 30*0b57cec5SDimitry Andric } 31*0b57cec5SDimitry Andric getPassName() const32*0b57cec5SDimitry Andric StringRef getPassName() const override { 33*0b57cec5SDimitry Andric return "NVPTX specific alloca hoisting"; 34*0b57cec5SDimitry Andric } 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric bool runOnFunction(Function &function) override; 37*0b57cec5SDimitry Andric }; 38*0b57cec5SDimitry Andric } // namespace 39*0b57cec5SDimitry Andric runOnFunction(Function & function)40*0b57cec5SDimitry Andricbool NVPTXAllocaHoisting::runOnFunction(Function &function) { 41*0b57cec5SDimitry Andric bool functionModified = false; 42*0b57cec5SDimitry Andric Function::iterator I = function.begin(); 43*0b57cec5SDimitry Andric Instruction *firstTerminatorInst = (I++)->getTerminator(); 44*0b57cec5SDimitry Andric 45*0b57cec5SDimitry Andric for (Function::iterator E = function.end(); I != E; ++I) { 46*0b57cec5SDimitry Andric for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) { 47*0b57cec5SDimitry Andric AllocaInst *allocaInst = dyn_cast<AllocaInst>(BI++); 48*0b57cec5SDimitry Andric if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) { 49*0b57cec5SDimitry Andric allocaInst->moveBefore(firstTerminatorInst); 50*0b57cec5SDimitry Andric functionModified = true; 51*0b57cec5SDimitry Andric } 52*0b57cec5SDimitry Andric } 53*0b57cec5SDimitry Andric } 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric return functionModified; 56*0b57cec5SDimitry Andric } 57*0b57cec5SDimitry Andric 58*0b57cec5SDimitry Andric char NVPTXAllocaHoisting::ID = 0; 59*0b57cec5SDimitry Andric 60*0b57cec5SDimitry Andric namespace llvm { 61*0b57cec5SDimitry Andric void initializeNVPTXAllocaHoistingPass(PassRegistry &); 62*0b57cec5SDimitry Andric } 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric INITIALIZE_PASS( 65*0b57cec5SDimitry Andric NVPTXAllocaHoisting, "alloca-hoisting", 66*0b57cec5SDimitry Andric "Hoisting alloca instructions in non-entry blocks to the entry block", 67*0b57cec5SDimitry Andric false, false) 68*0b57cec5SDimitry Andric createAllocaHoisting()69*0b57cec5SDimitry AndricFunctionPass *llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting; } 70