10b57cec5SDimitry Andric //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file demotes all registers to memory references. It is intended to be
100b57cec5SDimitry Andric // the inverse of PromoteMemoryToRegister. By converting to loads, the only
110b57cec5SDimitry Andric // values live across basic blocks are allocas and loads before phi nodes.
120b57cec5SDimitry Andric // It is intended that this should make CFG hacking much easier.
130b57cec5SDimitry Andric // To make later hacking easier, the entry block is split into two, such that
140b57cec5SDimitry Andric // all introduced allocas and nothing else are in the entry block.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric
18e8d8bef9SDimitry Andric #include "llvm/Transforms/Scalar/Reg2Mem.h"
190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
20e8d8bef9SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
210b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
220b57cec5SDimitry Andric #include "llvm/IR/CFG.h"
23e8d8bef9SDimitry Andric #include "llvm/IR/Dominators.h"
240b57cec5SDimitry Andric #include "llvm/IR/Function.h"
25e8d8bef9SDimitry Andric #include "llvm/IR/InstIterator.h"
260b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
27e8d8bef9SDimitry Andric #include "llvm/IR/PassManager.h"
280b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
290b57cec5SDimitry Andric #include "llvm/Transforms/Utils.h"
30e8d8bef9SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
31480093f4SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
320b57cec5SDimitry Andric #include <list>
330b57cec5SDimitry Andric using namespace llvm;
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric #define DEBUG_TYPE "reg2mem"
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric STATISTIC(NumRegsDemoted, "Number of registers demoted");
380b57cec5SDimitry Andric STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
390b57cec5SDimitry Andric
valueEscapes(const Instruction & Inst)40e8d8bef9SDimitry Andric static bool valueEscapes(const Instruction &Inst) {
41bdd1243dSDimitry Andric if (!Inst.getType()->isSized())
42bdd1243dSDimitry Andric return false;
43bdd1243dSDimitry Andric
44e8d8bef9SDimitry Andric const BasicBlock *BB = Inst.getParent();
45e8d8bef9SDimitry Andric for (const User *U : Inst.users()) {
460b57cec5SDimitry Andric const Instruction *UI = cast<Instruction>(U);
470b57cec5SDimitry Andric if (UI->getParent() != BB || isa<PHINode>(UI))
480b57cec5SDimitry Andric return true;
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric return false;
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
runPass(Function & F)53e8d8bef9SDimitry Andric static bool runPass(Function &F) {
540b57cec5SDimitry Andric // Insert all new allocas into entry block.
550b57cec5SDimitry Andric BasicBlock *BBEntry = &F.getEntryBlock();
560b57cec5SDimitry Andric assert(pred_empty(BBEntry) &&
570b57cec5SDimitry Andric "Entry block to function must not have predecessors!");
580b57cec5SDimitry Andric
590b57cec5SDimitry Andric // Find first non-alloca instruction and create insertion point. This is
600b57cec5SDimitry Andric // safe if block is well-formed: it always have terminator, otherwise
610b57cec5SDimitry Andric // we'll get and assertion.
620b57cec5SDimitry Andric BasicBlock::iterator I = BBEntry->begin();
630b57cec5SDimitry Andric while (isa<AllocaInst>(I)) ++I;
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric CastInst *AllocaInsertionPoint = new BitCastInst(
660b57cec5SDimitry Andric Constant::getNullValue(Type::getInt32Ty(F.getContext())),
67*0fca6ea1SDimitry Andric Type::getInt32Ty(F.getContext()), "reg2mem alloca point", I);
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric // Find the escaped instructions. But don't create stack slots for
700b57cec5SDimitry Andric // allocas in entry block.
710b57cec5SDimitry Andric std::list<Instruction*> WorkList;
72e8d8bef9SDimitry Andric for (Instruction &I : instructions(F))
73e8d8bef9SDimitry Andric if (!(isa<AllocaInst>(I) && I.getParent() == BBEntry) && valueEscapes(I))
74e8d8bef9SDimitry Andric WorkList.push_front(&I);
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric // Demote escaped instructions
770b57cec5SDimitry Andric NumRegsDemoted += WorkList.size();
78e8d8bef9SDimitry Andric for (Instruction *I : WorkList)
79*0fca6ea1SDimitry Andric DemoteRegToStack(*I, false, AllocaInsertionPoint->getIterator());
800b57cec5SDimitry Andric
810b57cec5SDimitry Andric WorkList.clear();
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric // Find all phi's
84e8d8bef9SDimitry Andric for (BasicBlock &BB : F)
85e8d8bef9SDimitry Andric for (auto &Phi : BB.phis())
86e8d8bef9SDimitry Andric WorkList.push_front(&Phi);
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric // Demote phi nodes
890b57cec5SDimitry Andric NumPhisDemoted += WorkList.size();
90e8d8bef9SDimitry Andric for (Instruction *I : WorkList)
91*0fca6ea1SDimitry Andric DemotePHIToStack(cast<PHINode>(I), AllocaInsertionPoint->getIterator());
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric return true;
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)96e8d8bef9SDimitry Andric PreservedAnalyses RegToMemPass::run(Function &F, FunctionAnalysisManager &AM) {
97e8d8bef9SDimitry Andric auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
98e8d8bef9SDimitry Andric auto *LI = &AM.getResult<LoopAnalysis>(F);
99e8d8bef9SDimitry Andric unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));
100e8d8bef9SDimitry Andric bool Changed = runPass(F);
101e8d8bef9SDimitry Andric if (N == 0 && !Changed)
102e8d8bef9SDimitry Andric return PreservedAnalyses::all();
103e8d8bef9SDimitry Andric PreservedAnalyses PA;
104e8d8bef9SDimitry Andric PA.preserve<DominatorTreeAnalysis>();
105e8d8bef9SDimitry Andric PA.preserve<LoopAnalysis>();
106e8d8bef9SDimitry Andric return PA;
107e8d8bef9SDimitry Andric }
108