blob: 1f3769601788506e4ae7167e3c4e95c71333cb4e [file] [log] [blame]
Joel Jones585bc822012-05-31 17:11:25 +00001//===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===//
Justin Holewinskiae556d32012-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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "NVPTXAllocaHoisting.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/Instructions.h"
Justin Holewinskiae556d32012-05-04 20:18:50 +000018
19namespace llvm {
20
21bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
Justin Holewinski0497ab12013-03-30 14:29:21 +000022 bool functionModified = false;
23 Function::iterator I = function.begin();
24 TerminatorInst *firstTerminatorInst = (I++)->getTerminator();
Justin Holewinskiae556d32012-05-04 20:18:50 +000025
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;
Tim Northovera7ecd242013-07-16 09:46:55 +000040static RegisterPass<NVPTXAllocaHoisting>
Justin Holewinski0497ab12013-03-30 14:29:21 +000041X("alloca-hoisting", "Hoisting alloca instructions in non-entry "
42 "blocks to the entry block");
Justin Holewinskiae556d32012-05-04 20:18:50 +000043
Justin Holewinski0497ab12013-03-30 14:29:21 +000044FunctionPass *createAllocaHoisting() { return new NVPTXAllocaHoisting(); }
Justin Holewinskiae556d32012-05-04 20:18:50 +000045
46} // end namespace llvm