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/ADT/Statistic.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/CFG.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/InstIterator.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/InitializePasses.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Transforms/Scalar.h"
29 #include "llvm/Transforms/Utils.h"
30 #include "llvm/Transforms/Utils/Local.h"
31 #include <list>
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "reg2mem"
35 
36 STATISTIC(NumRegsDemoted, "Number of registers demoted");
37 STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
38 
39 namespace {
40   struct RegToMem : public FunctionPass {
41     static char ID; // Pass identification, replacement for typeid
42     RegToMem() : FunctionPass(ID) {
43       initializeRegToMemPass(*PassRegistry::getPassRegistry());
44     }
45 
46     void getAnalysisUsage(AnalysisUsage &AU) const override {
47       AU.addRequiredID(BreakCriticalEdgesID);
48       AU.addPreservedID(BreakCriticalEdgesID);
49     }
50 
51     bool valueEscapes(const Instruction &Inst) const {
52       const BasicBlock *BB = Inst.getParent();
53       for (const User *U : Inst.users()) {
54         const Instruction *UI = cast<Instruction>(U);
55         if (UI->getParent() != BB || isa<PHINode>(UI))
56           return true;
57       }
58       return false;
59     }
60 
61     bool runOnFunction(Function &F) override;
62   };
63 }
64 
65 char RegToMem::ID = 0;
66 INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots",
67                 false, false)
68 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
69 INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots",
70                 false, false)
71 
72 bool RegToMem::runOnFunction(Function &F) {
73   if (F.isDeclaration() || skipFunction(F))
74     return false;
75 
76   // Insert all new allocas into entry block.
77   BasicBlock *BBEntry = &F.getEntryBlock();
78   assert(pred_empty(BBEntry) &&
79          "Entry block to function must not have predecessors!");
80 
81   // Find first non-alloca instruction and create insertion point. This is
82   // safe if block is well-formed: it always have terminator, otherwise
83   // we'll get and assertion.
84   BasicBlock::iterator I = BBEntry->begin();
85   while (isa<AllocaInst>(I)) ++I;
86 
87   CastInst *AllocaInsertionPoint = new BitCastInst(
88       Constant::getNullValue(Type::getInt32Ty(F.getContext())),
89       Type::getInt32Ty(F.getContext()), "reg2mem alloca point", &*I);
90 
91   // Find the escaped instructions. But don't create stack slots for
92   // allocas in entry block.
93   std::list<Instruction*> WorkList;
94   for (Instruction &I : instructions(F))
95     if (!(isa<AllocaInst>(I) && I.getParent() == BBEntry) && valueEscapes(I))
96       WorkList.push_front(&I);
97 
98   // Demote escaped instructions
99   NumRegsDemoted += WorkList.size();
100   for (Instruction *I : WorkList)
101     DemoteRegToStack(*I, false, AllocaInsertionPoint);
102 
103   WorkList.clear();
104 
105   // Find all phi's
106   for (BasicBlock &BB : F)
107     for (auto &Phi : BB.phis())
108       WorkList.push_front(&Phi);
109 
110   // Demote phi nodes
111   NumPhisDemoted += WorkList.size();
112   for (Instruction *I : WorkList)
113     DemotePHIToStack(cast<PHINode>(I), AllocaInsertionPoint);
114 
115   return true;
116 }
117 
118 
119 // createDemoteRegisterToMemory - Provide an entry point to create this pass.
120 char &llvm::DemoteRegisterToMemoryID = RegToMem::ID;
121 FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
122   return new RegToMem();
123 }
124