LLVM  3.7.0
NVPTXAllocaHoisting.cpp
Go to the documentation of this file.
1 //===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Hoist the alloca instructions in the non-entry blocks to the entry blocks.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "NVPTXAllocaHoisting.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/Instructions.h"
20 using namespace llvm;
21 
22 namespace {
23 // Hoisting the alloca instructions in the non-entry blocks to the entry
24 // block.
25 class NVPTXAllocaHoisting : public FunctionPass {
26 public:
27  static char ID; // Pass ID
28  NVPTXAllocaHoisting() : FunctionPass(ID) {}
29 
30  void getAnalysisUsage(AnalysisUsage &AU) const override {
33  }
34 
35  const char *getPassName() const override {
36  return "NVPTX specific alloca hoisting";
37  }
38 
39  bool runOnFunction(Function &function) override;
40 };
41 } // namespace
42 
43 bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
44  bool functionModified = false;
45  Function::iterator I = function.begin();
46  TerminatorInst *firstTerminatorInst = (I++)->getTerminator();
47 
48  for (Function::iterator E = function.end(); I != E; ++I) {
49  for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {
50  AllocaInst *allocaInst = dyn_cast<AllocaInst>(BI++);
51  if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) {
52  allocaInst->moveBefore(firstTerminatorInst);
53  functionModified = true;
54  }
55  }
56  }
57 
58  return functionModified;
59 }
60 
62 
63 namespace llvm {
65 }
66 
68  NVPTXAllocaHoisting, "alloca-hoisting",
69  "Hoisting alloca instructions in non-entry blocks to the entry block",
70  false, false)
71 
72 FunctionPass *llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting; }
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:240
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
FunctionPass * createAllocaHoisting()
MachineFunctionAnalysis - This class is a Pass that manages a MachineFunction object.
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:35
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
void initializeNVPTXAllocaHoistingPass(PassRegistry &)
INITIALIZE_PASS(NVPTXAllocaHoisting,"alloca-hoisting","Hoisting alloca instructions in non-entry blocks to the entry block", false, false) FunctionPass *llvm
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
#define I(x, y, z)
Definition: MD5.cpp:54
const Value * getArraySize() const
getArraySize - Get the number of elements allocated.
Definition: Instructions.h:110
void moveBefore(Instruction *MovePos)
moveBefore - Unlink this instruction from its current basic block and insert it into the basic block ...
Definition: Instruction.cpp:89
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:41
AllocaInst - an instruction to allocate memory on the stack.
Definition: Instructions.h:76