1 //===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Hoist the alloca instructions in the non-entry blocks to the entry blocks.
12 //===----------------------------------------------------------------------===//
14 #include "NVPTXAllocaHoisting.h"
15 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
16 #include "llvm/CodeGen/StackProtector.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/Instructions.h"
23 // Hoisting the alloca instructions in the non-entry blocks to the entry
25 class NVPTXAllocaHoisting : public FunctionPass {
27 static char ID; // Pass ID
28 NVPTXAllocaHoisting() : FunctionPass(ID) {}
30 void getAnalysisUsage(AnalysisUsage &AU) const override {
31 AU.addPreserved<MachineFunctionAnalysis>();
32 AU.addPreserved<StackProtector>();
35 const char *getPassName() const override {
36 return "NVPTX specific alloca hoisting";
39 bool runOnFunction(Function &function) override;
43 bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
44 bool functionModified = false;
45 Function::iterator I = function.begin();
46 TerminatorInst *firstTerminatorInst = (I++)->getTerminator();
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;
58 return functionModified;
61 char NVPTXAllocaHoisting::ID = 0;
64 void initializeNVPTXAllocaHoistingPass(PassRegistry &);
68 NVPTXAllocaHoisting, "alloca-hoisting",
69 "Hoisting alloca instructions in non-entry blocks to the entry block",
72 FunctionPass *llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting; }