LLVM: lib/Transforms/Utils/DemoteRegToStack.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
16using namespace llvm;
17
18
19
20
21
22
24 std::optionalBasicBlock::iterator AllocaPoint) {
25 if (I.use_empty()) {
26 I.eraseFromParent();
27 return nullptr;
28 }
29
30 Function *F = I.getParent()->getParent();
32
33
35 if (AllocaPoint) {
36 Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
37 I.getName()+".reg2mem", *AllocaPoint);
38 } else {
39 Slot = new AllocaInst(I.getType(), DL.getAllocaAddrSpace(), nullptr,
40 I.getName() + ".reg2mem", F->getEntryBlock().begin());
41 }
42
43
44
45
47 if (->getNormalDest()->getSinglePredecessor()) {
51 assert(BB && "Unable to split critical edge.");
52 (void)BB;
53 }
54 } else if (CallBrInst *CBI = dyn_cast(&I)) {
55 for (unsigned i = 0; i < CBI->getNumSuccessors(); i++) {
56 auto *Succ = CBI->getSuccessor(i);
57 if (!Succ->getSinglePredecessor()) {
60 assert(BB && "Unable to split critical edge.");
61 }
62 }
63 }
64
65
66 while (.use_empty()) {
67 Instruction *U = cast(I.user_back());
68 if (PHINode *PN = dyn_cast(U)) {
69
70
71
72
73
74
75
76
77
79 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
80 if (PN->getIncomingValue(i) == &I) {
81 Value *&V = Loads[PN->getIncomingBlock(i)];
82 if (!V) {
83
84 V = new LoadInst(I.getType(), Slot, I.getName() + ".reload",
85 VolatileLoads,
86 PN->getIncomingBlock(i)->getTerminator()->getIterator());
87 Loads[PN->getIncomingBlock(i)] = V;
88 }
89 PN->setIncomingValue(i, V);
90 }
91
92 } else {
93
94 Value *V = new LoadInst(I.getType(), Slot, I.getName() + ".reload",
95 VolatileLoads, U->getIterator());
96 U->replaceUsesOfWith(&I, V);
97 }
98 }
99
100
101
102
104 if (.isTerminator()) {
105 InsertPt = ++I.getIterator();
106
107 for (; isa(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
108 if (isa(InsertPt))
109 break;
110 if (isa(InsertPt)) {
112 new StoreInst(&I, Slot, Handler->getFirstInsertionPt());
113 return Slot;
114 }
115 } else if (InvokeInst *II = dyn_cast(&I)) {
116 InsertPt = II->getNormalDest()->getFirstInsertionPt();
117 } else if (CallBrInst *CBI = dyn_cast(&I)) {
119 new StoreInst(CBI, Slot, Succ->getFirstInsertionPt());
120 return Slot;
121 } else {
123 }
124
126 return Slot;
127}
128
129
130
131
133 if (P->use_empty()) {
134 P->eraseFromParent();
135 return nullptr;
136 }
137
139
140
142 if (AllocaPoint) {
143 Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
144 P->getName()+".reg2mem", *AllocaPoint);
145 } else {
146 Function *F = P->getParent()->getParent();
147 Slot = new AllocaInst(P->getType(), DL.getAllocaAddrSpace(), nullptr,
148 P->getName() + ".reg2mem",
149 F->getEntryBlock().begin());
150 }
151
152
153 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
154 if (InvokeInst *II = dyn_cast(P->getIncomingValue(i))) {
155 assert(II->getParent() != P->getIncomingBlock(i) &&
156 "Invoke edge not supported yet"); (void)II;
157 }
158 new StoreInst(P->getIncomingValue(i), Slot,
159 P->getIncomingBlock(i)->getTerminator()->getIterator());
160 }
161
162
164
165 for (; isa(InsertPt) || InsertPt->isEHPad(); ++InsertPt)
166 if (isa(InsertPt))
167 break;
168 if (isa(InsertPt)) {
169
171 for (User *U : P->users()) {
174 }
177 new LoadInst(P->getType(), Slot, P->getName() + ".reload", User->getIterator());
179 }
180 } else {
182 new LoadInst(P->getType(), Slot, P->getName() + ".reload", InsertPt);
183 P->replaceAllUsesWith(V);
184 }
185
186 P->eraseFromParent();
187 return Slot;
188}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the DenseMap class.
iv Induction Variable Users
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
an instruction to allocate memory on the stack
LLVM Basic Block Representation.
InstListType::iterator iterator
Instruction iterators...
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
A parsed version of the target data layout string in and methods for querying it.
An instruction for reading from memory.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
LLVM Value Representation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
unsigned GetSuccessorNumber(const BasicBlock *BB, const BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
auto successors(const MachineBasicBlock *BB)
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...
BasicBlock * SplitCriticalEdge(Instruction *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions(), const Twine &BBName="")
If this edge is a critical edge, insert a new node to split the critical edge.
bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.