Path: blob/main/contrib/llvm-project/llvm/lib/Target/NVPTX/NVPTXAllocaHoisting.cpp
35271 views
//===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Hoist the alloca instructions in the non-entry blocks to the entry blocks.9//10//===----------------------------------------------------------------------===//1112#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"17using namespace llvm;1819namespace {20// Hoisting the alloca instructions in the non-entry blocks to the entry21// block.22class NVPTXAllocaHoisting : public FunctionPass {23public:24static char ID; // Pass ID25NVPTXAllocaHoisting() : FunctionPass(ID) {}2627void getAnalysisUsage(AnalysisUsage &AU) const override {28AU.addPreserved<StackProtector>();29}3031StringRef getPassName() const override {32return "NVPTX specific alloca hoisting";33}3435bool runOnFunction(Function &function) override;36};37} // namespace3839bool NVPTXAllocaHoisting::runOnFunction(Function &function) {40bool functionModified = false;41Function::iterator I = function.begin();42Instruction *firstTerminatorInst = (I++)->getTerminator();4344for (Function::iterator E = function.end(); I != E; ++I) {45for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {46AllocaInst *allocaInst = dyn_cast<AllocaInst>(BI++);47if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) {48allocaInst->moveBefore(firstTerminatorInst);49functionModified = true;50}51}52}5354return functionModified;55}5657char NVPTXAllocaHoisting::ID = 0;5859namespace llvm {60void initializeNVPTXAllocaHoistingPass(PassRegistry &);61}6263INITIALIZE_PASS(64NVPTXAllocaHoisting, "alloca-hoisting",65"Hoisting alloca instructions in non-entry blocks to the entry block",66false, false)6768FunctionPass *llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting; }697071