LLVM 19.0.0git
Go to the documentation of this file.
1//===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
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
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.
19#include "llvm/ADT/Statistic.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"
27#include "llvm/IR/PassManager.h"
32#include <list>
33using namespace llvm;
35#define DEBUG_TYPE "reg2mem"
37STATISTIC(NumRegsDemoted, "Number of registers demoted");
38STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
40static bool valueEscapes(const Instruction &Inst) {
41 if (!Inst.getType()->isSized())
42 return false;
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;
53static 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!");
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;
65 CastInst *AllocaInsertionPoint = new BitCastInst(
67 Type::getInt32Ty(F.getContext()), "reg2mem alloca point", I);
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);
76 // Demote escaped instructions
77 NumRegsDemoted += WorkList.size();
78 for (Instruction *I : WorkList)
79 DemoteRegToStack(*I, false, AllocaInsertionPoint->getIterator());
81 WorkList.clear();
83 // Find all phi's
84 for (BasicBlock &BB : F)
85 for (auto &Phi : BB.phis())
86 WorkList.push_front(&Phi);
88 // Demote phi nodes
89 NumPhisDemoted += WorkList.size();
90 for (Instruction *I : WorkList)
91 DemotePHIToStack(cast<PHINode>(I), AllocaInsertionPoint->getIterator());
93 return true;
97 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
98 auto *LI = &AM.getResult<LoopAnalysis>(F);
100 bool Changed = runPass(F);
101 if (N == 0 && !Changed)
102 return PreservedAnalyses::all();
106 return PA;
Expand Atomic instructions
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static cl::opt< bool > SplitAllCriticalEdges("phi-elim-split-all-critical-edges", cl::init(false), cl::Hidden, cl::desc("Split all critical edges during " "PHI elimination"))
This header defines various interfaces for pass management in LLVM.
static bool valueEscapes(const Instruction &Inst)
Definition: Reg2Mem.cpp:40
static bool runPass(Function &F)
Definition: Reg2Mem.cpp:53
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
Definition: Statistic.h:167
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:405
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:438
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:167
This class represents a no-op cast from one type to another.
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:530
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:571
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: Reg2Mem.cpp:96
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
static IntegerType * getInt32Ty(LLVMContext &C)
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
iterator_range< user_iterator > users()
Definition: Value.h:421
const ParentTy * getParent() const
Definition: ilist_node.h:32
self_iterator getIterator()
Definition: ilist_node.h:132
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
AllocaInst * DemoteRegToStack(Instruction &X, bool VolatileLoads=false, std::optional< BasicBlock::iterator > AllocaPoint=std::nullopt)
This function takes a virtual register computed by an Instruction and replaces it with a slot in the ...
AllocaInst * DemotePHIToStack(PHINode *P, std::optional< BasicBlock::iterator > AllocaPoint=std::nullopt)
This function takes a virtual register computed by a phi node and replaces it with a slot in the stac...
bool pred_empty(const BasicBlock *BB)
Definition: CFG.h:118
#define N
Option class for critical edge splitting.