1 //===-- AllocaHoisting.cpp - Hoist allocas to the entry block --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Hoist the alloca instructions in the non-entry blocks to the entry blocks.
11 //===----------------------------------------------------------------------===//
13 #include "NVPTXAllocaHoisting.h"
14 #include "llvm/CodeGen/StackProtector.h"
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Instructions.h"
21 // Hoisting the alloca instructions in the non-entry blocks to the entry
23 class NVPTXAllocaHoisting
: public FunctionPass
{
25 static char ID
; // Pass ID
26 NVPTXAllocaHoisting() : FunctionPass(ID
) {}
28 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
29 AU
.addPreserved
<StackProtector
>();
32 StringRef
getPassName() const override
{
33 return "NVPTX specific alloca hoisting";
36 bool runOnFunction(Function
&function
) override
;
40 bool NVPTXAllocaHoisting::runOnFunction(Function
&function
) {
41 bool functionModified
= false;
42 Function::iterator I
= function
.begin();
43 Instruction
*firstTerminatorInst
= (I
++)->getTerminator();
45 for (Function::iterator E
= function
.end(); I
!= E
; ++I
) {
46 for (BasicBlock::iterator BI
= I
->begin(), BE
= I
->end(); BI
!= BE
;) {
47 AllocaInst
*allocaInst
= dyn_cast
<AllocaInst
>(BI
++);
48 if (allocaInst
&& isa
<ConstantInt
>(allocaInst
->getArraySize())) {
49 allocaInst
->moveBefore(firstTerminatorInst
);
50 functionModified
= true;
55 return functionModified
;
58 char NVPTXAllocaHoisting::ID
= 0;
61 void initializeNVPTXAllocaHoistingPass(PassRegistry
&);
65 NVPTXAllocaHoisting
, "alloca-hoisting",
66 "Hoisting alloca instructions in non-entry blocks to the entry block",
69 FunctionPass
*llvm::createAllocaHoisting() { return new NVPTXAllocaHoisting
; }