LLVM 24.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 "NVPTX.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
18using namespace llvm;
19
21 bool functionModified = false;
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>(BI++);
28 if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) {
29 allocaInst->moveBefore(firstTerminatorInst->getIterator());
30 functionModified = true;
31 }
32 }
33 }
34
35 return functionModified;
36}
37
38namespace {
39// Hoisting the alloca instructions in the non-entry blocks to the entry
40// block.
41class NVPTXAllocaHoistingLegacyPass : public FunctionPass {
42public:
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
61char NVPTXAllocaHoistingLegacyPass::ID = 0;
62
64 NVPTXAllocaHoistingLegacyPass, "alloca-hoisting",
65 "Hoisting alloca instructions in non-entry blocks to the entry block",
66 false, false)
67
69 return new NVPTXAllocaHoistingLegacyPass;
70}
71
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 F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static bool hoistAllocas(Function &function)
FunctionAnalysisManager FAM
#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)"
an instruction to allocate memory on the stack
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.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
BasicBlockListType::iterator iterator
Definition Function.h:70
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
self_iterator getIterator()
Definition ilist_node.h:123
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:643
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:547
FunctionPass * createNVPTXAllocaHoistingLegacyPass()
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.