1 //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file demotes all registers to memory references. It is intended to be 10 // the inverse of PromoteMemoryToRegister. By converting to loads, the only 11 // values live across basic blocks are allocas and loads before phi nodes. 12 // It is intended that this should make CFG hacking much easier. 13 // To make later hacking easier, the entry block is split into two, such that 14 // all introduced allocas and nothing else are in the entry block. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/Transforms/Scalar/Reg2Mem.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/Analysis/LoopInfo.h" 21 #include "llvm/IR/BasicBlock.h" 22 #include "llvm/IR/CFG.h" 23 #include "llvm/IR/Dominators.h" 24 #include "llvm/IR/Function.h" 25 #include "llvm/IR/InstIterator.h" 26 #include "llvm/IR/Instructions.h" 27 #include "llvm/IR/PassManager.h" 28 #include "llvm/Transforms/Scalar.h" 29 #include "llvm/Transforms/Utils.h" 30 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 31 #include "llvm/Transforms/Utils/Local.h" 32 #include <list> 33 using namespace llvm; 34 35 #define DEBUG_TYPE "reg2mem" 36 37 STATISTIC(NumRegsDemoted, "Number of registers demoted"); 38 STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted"); 39 40 static bool valueEscapes(const Instruction &Inst) { 41 if (!Inst.getType()->isSized()) 42 return false; 43 44 const BasicBlock *BB = Inst.getParent(); 45 for (const User *U : Inst.users()) { 46 const Instruction *UI = cast<Instruction>(U); 47 if (UI->getParent() != BB || isa<PHINode>(UI)) 48 return true; 49 } 50 return false; 51 } 52 53 static bool runPass(Function &F) { 54 // Insert all new allocas into entry block. 55 BasicBlock *BBEntry = &F.getEntryBlock(); 56 assert(pred_empty(BBEntry) && 57 "Entry block to function must not have predecessors!"); 58 59 // Find first non-alloca instruction and create insertion point. This is 60 // safe if block is well-formed: it always have terminator, otherwise 61 // we'll get and assertion. 62 BasicBlock::iterator I = BBEntry->begin(); 63 while (isa<AllocaInst>(I)) ++I; 64 65 CastInst *AllocaInsertionPoint = new BitCastInst( 66 Constant::getNullValue(Type::getInt32Ty(F.getContext())), 67 Type::getInt32Ty(F.getContext()), "reg2mem alloca point", I); 68 69 // Find the escaped instructions. But don't create stack slots for 70 // allocas in entry block. 71 std::list<Instruction*> WorkList; 72 for (Instruction &I : instructions(F)) 73 if (!(isa<AllocaInst>(I) && I.getParent() == BBEntry) && valueEscapes(I)) 74 WorkList.push_front(&I); 75 76 // Demote escaped instructions 77 NumRegsDemoted += WorkList.size(); 78 for (Instruction *I : WorkList) 79 DemoteRegToStack(*I, false, AllocaInsertionPoint->getIterator()); 80 81 WorkList.clear(); 82 83 // Find all phi's 84 for (BasicBlock &BB : F) 85 for (auto &Phi : BB.phis()) 86 WorkList.push_front(&Phi); 87 88 // Demote phi nodes 89 NumPhisDemoted += WorkList.size(); 90 for (Instruction *I : WorkList) 91 DemotePHIToStack(cast<PHINode>(I), AllocaInsertionPoint->getIterator()); 92 93 return true; 94 } 95 96 PreservedAnalyses RegToMemPass::run(Function &F, FunctionAnalysisManager &AM) { 97 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F); 98 auto *LI = &AM.getResult<LoopAnalysis>(F); 99 unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI)); 100 bool Changed = runPass(F); 101 if (N == 0 && !Changed) 102 return PreservedAnalyses::all(); 103 PreservedAnalyses PA; 104 PA.preserve<DominatorTreeAnalysis>(); 105 PA.preserve<LoopAnalysis>(); 106 return PA; 107 } 108