blob: bf922eb8a19554fb7f086f26e6a7f9facf723165 [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"
Benjamin Kramer414c0962015-03-10 19:20:52 +000015#include "llvm/CodeGen/StackProtector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/Instructions.h"
Benjamin Kramer414c0962015-03-10 19:20:52 +000019using namespace llvm;
Justin Holewinskiae556d32012-05-04 20:18:50 +000020
Benjamin Kramer414c0962015-03-10 19:20:52 +000021namespace {
22// Hoisting the alloca instructions in the non-entry blocks to the entry
23// block.
24class NVPTXAllocaHoisting : public FunctionPass {
25public:
26 static char ID; // Pass ID
27 NVPTXAllocaHoisting() : FunctionPass(ID) {}
28
29 void getAnalysisUsage(AnalysisUsage &AU) const override {
Benjamin Kramer414c0962015-03-10 19:20:52 +000030 AU.addPreserved<StackProtector>();
31 }
32
Mehdi Amini117296c2016-10-01 02:56:57 +000033 StringRef getPassName() const override {
Benjamin Kramer414c0962015-03-10 19:20:52 +000034 return "NVPTX specific alloca hoisting";
35 }
36
37 bool runOnFunction(Function &function) override;
38};
39} // namespace
Justin Holewinskiae556d32012-05-04 20:18:50 +000040
41bool NVPTXAllocaHoisting::runOnFunction(Function &function) {
Justin Holewinski0497ab12013-03-30 14:29:21 +000042 bool functionModified = false;
43 Function::iterator I = function.begin();
Chandler Carruthedb12a82018-10-15 10:04:59 +000044 Instruction *firstTerminatorInst = (I++)->getTerminator();
Justin Holewinskiae556d32012-05-04 20:18:50 +000045
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;
52 }
53 }
54 }
55
56 return functionModified;
57}
58
Benjamin Kramer414c0962015-03-10 19:20:52 +000059char NVPTXAllocaHoisting::ID = 0;
Justin Holewinskiae556d32012-05-04 20:18:50 +000060
Benjamin Kramer414c0962015-03-10 19:20:52 +000061namespace llvm {
62void initializeNVPTXAllocaHoistingPass(PassRegistry &);
63}
Justin Holewinskiae556d32012-05-04 20:18:50 +000064
Benjamin Kramer414c0962015-03-10 19:20:52 +000065INITIALIZE_PASS(
66 NVPTXAllocaHoisting, "alloca-hoisting",
67 "Hoisting alloca instructions in non-entry blocks to the entry block",
68 false, false)
69
70FunctionPass *llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting; }