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/StackProtector.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Instructions.h"
22 // Hoisting the alloca instructions in the non-entry blocks to the entry
24 class NVPTXAllocaHoisting : public FunctionPass {
26 static char ID; // Pass ID
27 NVPTXAllocaHoisting() : FunctionPass(ID) {}
29 void getAnalysisUsage(AnalysisUsage &AU) const override {
30 AU.addPreserved<StackProtector>();
33 StringRef getPassName() const override {
34 return "NVPTX specific alloca hoisting";
37 bool runOnFunction(Function &function) override;
41 bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
42 bool functionModified = false;
43 Function::iterator I = function.begin();
44 TerminatorInst *firstTerminatorInst = (I++)->getTerminator();
46 for (Function::iterator E = function.end(); I != E; ++I) {
47 for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) {
48 AllocaInst *allocaInst = dyn_cast<AllocaInst>(BI++);
49 if (allocaInst && isa<ConstantInt>(allocaInst->getArraySize())) {
50 allocaInst->moveBefore(firstTerminatorInst);
51 functionModified = true;
56 return functionModified;
59 char NVPTXAllocaHoisting::ID = 0;
62 void initializeNVPTXAllocaHoistingPass(PassRegistry &);
66 NVPTXAllocaHoisting, "alloca-hoisting",
67 "Hoisting alloca instructions in non-entry blocks to the entry block",
70 FunctionPass *llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting; }