LLVM 22.0.0git
NVPTXAllocaHoisting.cpp
Go to the documentation of this file.
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 "NVPTXAllocaHoisting.h"
14#include "NVPTX.h"
16#include "llvm/IR/Constants.h"
17#include "llvm/IR/Function.h"
19using namespace llvm;
20
21namespace {
22// Hoisting the alloca instructions in the non-entry blocks to the entry
23// block.
24class NVPTXAllocaHoisting : public FunctionPass {
25public:
26 static char ID; // Pass ID
27 NVPTXAllocaHoisting() : FunctionPass(ID) {}
28
29 void getAnalysisUsage(AnalysisUsage &AU) const override {
30 AU.addPreserved<StackProtector>();
31 }
32
33 StringRef getPassName() const override {
34 return "NVPTX specific alloca hoisting";
35 }
36
37 bool runOnFunction(Function &function) override;
38};
39} // namespace
40
41bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
42 bool functionModified = false;
44 Instruction *firstTerminatorInst = (I++)->getTerminator();
45
46 for (Function::iterator E = function.end(); I != E; ++I) {
47 for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {
48 AllocaInst *allocaInst = dyn_cast<AllocaInst>(BI++);
49 if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) {
50 allocaInst->moveBefore(firstTerminatorInst->getIterator());
51 functionModified = true;
52 }
53 }
54 }
55
56 return functionModified;
57}
58
59char NVPTXAllocaHoisting::ID = 0;
60
62 NVPTXAllocaHoisting, "alloca-hoisting",
63 "Hoisting alloca instructions in non-entry blocks to the entry block",
64 false, false)
65
66FunctionPass *llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting; }
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
#define I(x, y, z)
Definition MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
dot regions Print regions of function to dot true view regions View regions of function(with no function bodies)"
const Value * getArraySize() const
Get the number of elements allocated.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
BasicBlockListType::iterator iterator
Definition Function.h:69
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
self_iterator getIterator()
Definition ilist_node.h:134
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
LLVM_ABI Instruction * getTerminator() const
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
FunctionPass * createAllocaHoisting()
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548