blob: 668c39308f71890a01384ae419c270538eba7270 [file] [log] [blame]
Joel Jones703360f2012-05-31 17:11:25 +00001//===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===//
Justin Holewinski49683f32012-05-04 20:18:50 +00002//
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 "llvm/Function.h"
15#include "llvm/Instructions.h"
16#include "llvm/Constants.h"
17#include "NVPTXAllocaHoisting.h"
18
19namespace llvm {
20
21bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
22 bool functionModified = false;
23 Function::iterator I = function.begin();
24 TerminatorInst *firstTerminatorInst = (I++)->getTerminator();
25
26 for (Function::iterator E = function.end(); I != E; ++I) {
27 for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {
28 AllocaInst *allocaInst = dyn_cast<AllocaInst>(BI++);
29 if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) {
30 allocaInst->moveBefore(firstTerminatorInst);
31 functionModified = true;
32 }
33 }
34 }
35
36 return functionModified;
37}
38
39char NVPTXAllocaHoisting::ID = 1;
40RegisterPass<NVPTXAllocaHoisting> X("alloca-hoisting",
Joel Jones703360f2012-05-31 17:11:25 +000041 "Hoisting alloca instructions in non-entry "
Justin Holewinski49683f32012-05-04 20:18:50 +000042 "blocks to the entry block");
43
44FunctionPass *createAllocaHoisting() {
45 return new NVPTXAllocaHoisting();
46}
47
48} // end namespace llvm