1703360f93cf88aeab3d602a454c554622eba63e9Joel Jones//===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===//
249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski//
349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski//                     The LLVM Compiler Infrastructure
449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski//
549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski// This file is distributed under the University of Illinois Open Source
649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski// License. See LICENSE.TXT for details.
749683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski//
849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski//===----------------------------------------------------------------------===//
949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski//
1049683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski// Hoist the alloca instructions in the non-entry blocks to the entry blocks.
1149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski//
1249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski//===----------------------------------------------------------------------===//
1349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
14d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "NVPTXAllocaHoisting.h"
150b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h"
170b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instructions.h"
1849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
1949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskinamespace llvm {
2049683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
2149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskibool NVPTXAllocaHoisting::runOnFunction(Function &function) {
223639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  bool functionModified = false;
233639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  Function::iterator I = function.begin();
243639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  TerminatorInst *firstTerminatorInst = (I++)->getTerminator();
2549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
2649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  for (Function::iterator E = function.end(); I != E; ++I) {
2749683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski    for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {
2849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski      AllocaInst *allocaInst = dyn_cast<AllocaInst>(BI++);
2949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski      if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) {
3049683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski        allocaInst->moveBefore(firstTerminatorInst);
3149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski        functionModified = true;
3249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski      }
3349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski    }
3449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  }
3549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
3649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  return functionModified;
3749683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski}
3849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
3949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskichar NVPTXAllocaHoisting::ID = 1;
402f438131f115a3860ee344a827a091790d6dc13dTim Northoverstatic RegisterPass<NVPTXAllocaHoisting>
413639ce2575660a0e6938d2e84e8bd9a738fd7051Justin HolewinskiX("alloca-hoisting", "Hoisting alloca instructions in non-entry "
423639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski                     "blocks to the entry block");
4349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
443639ce2575660a0e6938d2e84e8bd9a738fd7051Justin HolewinskiFunctionPass *createAllocaHoisting() { return new NVPTXAllocaHoisting(); }
4549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
4649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski} // end namespace llvm
47