1*03ce13f7SAndroid Build Coastguard Worker //===- subzero/src/IceVariableSplitting.cpp - Local variable splitting ----===//
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // The Subzero Code Generator
4*03ce13f7SAndroid Build Coastguard Worker //
5*03ce13f7SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*03ce13f7SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*03ce13f7SAndroid Build Coastguard Worker //
8*03ce13f7SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*03ce13f7SAndroid Build Coastguard Worker ///
10*03ce13f7SAndroid Build Coastguard Worker /// \file
11*03ce13f7SAndroid Build Coastguard Worker /// \brief Aggressive block-local variable splitting to improve linear-scan
12*03ce13f7SAndroid Build Coastguard Worker /// register allocation.
13*03ce13f7SAndroid Build Coastguard Worker ///
14*03ce13f7SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*03ce13f7SAndroid Build Coastguard Worker
16*03ce13f7SAndroid Build Coastguard Worker #include "IceVariableSplitting.h"
17*03ce13f7SAndroid Build Coastguard Worker
18*03ce13f7SAndroid Build Coastguard Worker #include "IceCfg.h"
19*03ce13f7SAndroid Build Coastguard Worker #include "IceCfgNode.h"
20*03ce13f7SAndroid Build Coastguard Worker #include "IceClFlags.h"
21*03ce13f7SAndroid Build Coastguard Worker #include "IceInst.h"
22*03ce13f7SAndroid Build Coastguard Worker #include "IceOperand.h"
23*03ce13f7SAndroid Build Coastguard Worker #include "IceTargetLowering.h"
24*03ce13f7SAndroid Build Coastguard Worker
25*03ce13f7SAndroid Build Coastguard Worker namespace Ice {
26*03ce13f7SAndroid Build Coastguard Worker
27*03ce13f7SAndroid Build Coastguard Worker namespace {
28*03ce13f7SAndroid Build Coastguard Worker
29*03ce13f7SAndroid Build Coastguard Worker /// A Variable is "allocable" if it is a register allocation candidate but
30*03ce13f7SAndroid Build Coastguard Worker /// doesn't already have a register.
isAllocable(const Variable * Var)31*03ce13f7SAndroid Build Coastguard Worker bool isAllocable(const Variable *Var) {
32*03ce13f7SAndroid Build Coastguard Worker if (Var == nullptr)
33*03ce13f7SAndroid Build Coastguard Worker return false;
34*03ce13f7SAndroid Build Coastguard Worker return !Var->hasReg() && Var->mayHaveReg();
35*03ce13f7SAndroid Build Coastguard Worker }
36*03ce13f7SAndroid Build Coastguard Worker
37*03ce13f7SAndroid Build Coastguard Worker /// A Variable is "inf" if it already has a register or is infinite-weight.
isInf(const Variable * Var)38*03ce13f7SAndroid Build Coastguard Worker bool isInf(const Variable *Var) {
39*03ce13f7SAndroid Build Coastguard Worker if (Var == nullptr)
40*03ce13f7SAndroid Build Coastguard Worker return false;
41*03ce13f7SAndroid Build Coastguard Worker return Var->hasReg() || Var->mustHaveReg();
42*03ce13f7SAndroid Build Coastguard Worker }
43*03ce13f7SAndroid Build Coastguard Worker
44*03ce13f7SAndroid Build Coastguard Worker /// VariableMap is a simple helper class that keeps track of the latest split
45*03ce13f7SAndroid Build Coastguard Worker /// version of the original Variables, as well as the instruction containing the
46*03ce13f7SAndroid Build Coastguard Worker /// last use of the Variable within the current block. For each entry, the
47*03ce13f7SAndroid Build Coastguard Worker /// Variable is tagged with the CfgNode that it is valid in, so that we don't
48*03ce13f7SAndroid Build Coastguard Worker /// need to clear the entire Map[] vector for each block.
49*03ce13f7SAndroid Build Coastguard Worker class VariableMap {
50*03ce13f7SAndroid Build Coastguard Worker private:
51*03ce13f7SAndroid Build Coastguard Worker VariableMap() = delete;
52*03ce13f7SAndroid Build Coastguard Worker VariableMap(const VariableMap &) = delete;
53*03ce13f7SAndroid Build Coastguard Worker VariableMap &operator=(const VariableMap &) = delete;
54*03ce13f7SAndroid Build Coastguard Worker
55*03ce13f7SAndroid Build Coastguard Worker struct VarInfo {
56*03ce13f7SAndroid Build Coastguard Worker /// MappedVar is the latest mapped/split version of the Variable.
57*03ce13f7SAndroid Build Coastguard Worker Variable *MappedVar = nullptr;
58*03ce13f7SAndroid Build Coastguard Worker /// MappedVarNode is the block in which MappedVar is valid.
59*03ce13f7SAndroid Build Coastguard Worker const CfgNode *MappedVarNode = nullptr;
60*03ce13f7SAndroid Build Coastguard Worker /// LastUseInst is the last instruction in the block that uses the Variable
61*03ce13f7SAndroid Build Coastguard Worker /// as a source operand.
62*03ce13f7SAndroid Build Coastguard Worker const Inst *LastUseInst = nullptr;
63*03ce13f7SAndroid Build Coastguard Worker /// LastUseNode is the block in which LastUseInst is valid.
64*03ce13f7SAndroid Build Coastguard Worker const CfgNode *LastUseNode = nullptr;
65*03ce13f7SAndroid Build Coastguard Worker VarInfo() = default;
66*03ce13f7SAndroid Build Coastguard Worker
67*03ce13f7SAndroid Build Coastguard Worker private:
68*03ce13f7SAndroid Build Coastguard Worker VarInfo(const VarInfo &) = delete;
69*03ce13f7SAndroid Build Coastguard Worker VarInfo &operator=(const VarInfo &) = delete;
70*03ce13f7SAndroid Build Coastguard Worker };
71*03ce13f7SAndroid Build Coastguard Worker
72*03ce13f7SAndroid Build Coastguard Worker public:
VariableMap(Cfg * Func)73*03ce13f7SAndroid Build Coastguard Worker explicit VariableMap(Cfg *Func)
74*03ce13f7SAndroid Build Coastguard Worker : Func(Func), NumVars(Func->getNumVariables()), Map(NumVars) {}
75*03ce13f7SAndroid Build Coastguard Worker /// Reset the mappings at the start of a block.
reset(const CfgNode * CurNode)76*03ce13f7SAndroid Build Coastguard Worker void reset(const CfgNode *CurNode) {
77*03ce13f7SAndroid Build Coastguard Worker Node = CurNode;
78*03ce13f7SAndroid Build Coastguard Worker // Do a prepass through all the instructions, marking which instruction is
79*03ce13f7SAndroid Build Coastguard Worker // the last use of each Variable within the block.
80*03ce13f7SAndroid Build Coastguard Worker for (const Inst &Instr : Node->getInsts()) {
81*03ce13f7SAndroid Build Coastguard Worker if (Instr.isDeleted())
82*03ce13f7SAndroid Build Coastguard Worker continue;
83*03ce13f7SAndroid Build Coastguard Worker for (SizeT i = 0; i < Instr.getSrcSize(); ++i) {
84*03ce13f7SAndroid Build Coastguard Worker if (auto *SrcVar = llvm::dyn_cast<Variable>(Instr.getSrc(i))) {
85*03ce13f7SAndroid Build Coastguard Worker const SizeT VarNum = getVarNum(SrcVar);
86*03ce13f7SAndroid Build Coastguard Worker Map[VarNum].LastUseInst = &Instr;
87*03ce13f7SAndroid Build Coastguard Worker Map[VarNum].LastUseNode = Node;
88*03ce13f7SAndroid Build Coastguard Worker }
89*03ce13f7SAndroid Build Coastguard Worker }
90*03ce13f7SAndroid Build Coastguard Worker }
91*03ce13f7SAndroid Build Coastguard Worker }
92*03ce13f7SAndroid Build Coastguard Worker /// Get Var's current mapping (or Var itself if it has no mapping yet).
get(Variable * Var) const93*03ce13f7SAndroid Build Coastguard Worker Variable *get(Variable *Var) const {
94*03ce13f7SAndroid Build Coastguard Worker const SizeT VarNum = getVarNum(Var);
95*03ce13f7SAndroid Build Coastguard Worker Variable *MappedVar = Map[VarNum].MappedVar;
96*03ce13f7SAndroid Build Coastguard Worker if (MappedVar == nullptr)
97*03ce13f7SAndroid Build Coastguard Worker return Var;
98*03ce13f7SAndroid Build Coastguard Worker if (Map[VarNum].MappedVarNode != Node)
99*03ce13f7SAndroid Build Coastguard Worker return Var;
100*03ce13f7SAndroid Build Coastguard Worker return MappedVar;
101*03ce13f7SAndroid Build Coastguard Worker }
102*03ce13f7SAndroid Build Coastguard Worker /// Create a new linked Variable in the LinkedTo chain, and set it as Var's
103*03ce13f7SAndroid Build Coastguard Worker /// latest mapping.
makeLinked(Variable * Var)104*03ce13f7SAndroid Build Coastguard Worker Variable *makeLinked(Variable *Var) {
105*03ce13f7SAndroid Build Coastguard Worker Variable *NewVar = Func->makeVariable(Var->getType());
106*03ce13f7SAndroid Build Coastguard Worker NewVar->setRegClass(Var->getRegClass());
107*03ce13f7SAndroid Build Coastguard Worker NewVar->setLinkedTo(get(Var));
108*03ce13f7SAndroid Build Coastguard Worker const SizeT VarNum = getVarNum(Var);
109*03ce13f7SAndroid Build Coastguard Worker Map[VarNum].MappedVar = NewVar;
110*03ce13f7SAndroid Build Coastguard Worker Map[VarNum].MappedVarNode = Node;
111*03ce13f7SAndroid Build Coastguard Worker return NewVar;
112*03ce13f7SAndroid Build Coastguard Worker }
113*03ce13f7SAndroid Build Coastguard Worker /// Given Var that is LinkedTo some other variable, re-splice it into the
114*03ce13f7SAndroid Build Coastguard Worker /// LinkedTo chain so that the chain is ordered by Variable::getIndex().
spliceBlockLocalLinkedToChain(Variable * Var)115*03ce13f7SAndroid Build Coastguard Worker void spliceBlockLocalLinkedToChain(Variable *Var) {
116*03ce13f7SAndroid Build Coastguard Worker Variable *LinkedTo = Var->getLinkedTo();
117*03ce13f7SAndroid Build Coastguard Worker assert(LinkedTo != nullptr);
118*03ce13f7SAndroid Build Coastguard Worker assert(Var->getIndex() > LinkedTo->getIndex());
119*03ce13f7SAndroid Build Coastguard Worker const SizeT VarNum = getVarNum(LinkedTo);
120*03ce13f7SAndroid Build Coastguard Worker Variable *Link = Map[VarNum].MappedVar;
121*03ce13f7SAndroid Build Coastguard Worker if (Link == nullptr || Map[VarNum].MappedVarNode != Node)
122*03ce13f7SAndroid Build Coastguard Worker return;
123*03ce13f7SAndroid Build Coastguard Worker Variable *LinkParent = Link->getLinkedTo();
124*03ce13f7SAndroid Build Coastguard Worker while (LinkParent != nullptr && LinkParent->getIndex() >= Var->getIndex()) {
125*03ce13f7SAndroid Build Coastguard Worker Link = LinkParent;
126*03ce13f7SAndroid Build Coastguard Worker LinkParent = Link->getLinkedTo();
127*03ce13f7SAndroid Build Coastguard Worker }
128*03ce13f7SAndroid Build Coastguard Worker Var->setLinkedTo(LinkParent);
129*03ce13f7SAndroid Build Coastguard Worker Link->setLinkedTo(Var);
130*03ce13f7SAndroid Build Coastguard Worker }
131*03ce13f7SAndroid Build Coastguard Worker /// Return whether the given Variable has any uses as a source operand within
132*03ce13f7SAndroid Build Coastguard Worker /// the current block. If it has no source operand uses, but is assigned as a
133*03ce13f7SAndroid Build Coastguard Worker /// dest variable in some instruction in the block, then we needn't bother
134*03ce13f7SAndroid Build Coastguard Worker /// splitting it.
isDestUsedInBlock(const Variable * Dest) const135*03ce13f7SAndroid Build Coastguard Worker bool isDestUsedInBlock(const Variable *Dest) const {
136*03ce13f7SAndroid Build Coastguard Worker return Map[getVarNum(Dest)].LastUseNode == Node;
137*03ce13f7SAndroid Build Coastguard Worker }
138*03ce13f7SAndroid Build Coastguard Worker /// Return whether the given instruction is the last use of the given Variable
139*03ce13f7SAndroid Build Coastguard Worker /// within the current block. If it is, then we needn't bother splitting the
140*03ce13f7SAndroid Build Coastguard Worker /// Variable at this instruction.
isInstLastUseOfVar(const Variable * Var,const Inst * Instr)141*03ce13f7SAndroid Build Coastguard Worker bool isInstLastUseOfVar(const Variable *Var, const Inst *Instr) {
142*03ce13f7SAndroid Build Coastguard Worker return Map[getVarNum(Var)].LastUseInst == Instr;
143*03ce13f7SAndroid Build Coastguard Worker }
144*03ce13f7SAndroid Build Coastguard Worker
145*03ce13f7SAndroid Build Coastguard Worker private:
146*03ce13f7SAndroid Build Coastguard Worker Cfg *const Func;
147*03ce13f7SAndroid Build Coastguard Worker // NumVars is for the size of the Map array. It can be const because any new
148*03ce13f7SAndroid Build Coastguard Worker // Variables created during the splitting pass don't need to be mapped.
149*03ce13f7SAndroid Build Coastguard Worker const SizeT NumVars;
150*03ce13f7SAndroid Build Coastguard Worker CfgVector<VarInfo> Map;
151*03ce13f7SAndroid Build Coastguard Worker const CfgNode *Node = nullptr;
152*03ce13f7SAndroid Build Coastguard Worker /// Get Var's VarNum, and do some validation.
getVarNum(const Variable * Var) const153*03ce13f7SAndroid Build Coastguard Worker SizeT getVarNum(const Variable *Var) const {
154*03ce13f7SAndroid Build Coastguard Worker const SizeT VarNum = Var->getIndex();
155*03ce13f7SAndroid Build Coastguard Worker assert(VarNum < NumVars);
156*03ce13f7SAndroid Build Coastguard Worker return VarNum;
157*03ce13f7SAndroid Build Coastguard Worker }
158*03ce13f7SAndroid Build Coastguard Worker };
159*03ce13f7SAndroid Build Coastguard Worker
160*03ce13f7SAndroid Build Coastguard Worker /// LocalVariableSplitter tracks the necessary splitting state across
161*03ce13f7SAndroid Build Coastguard Worker /// instructions.
162*03ce13f7SAndroid Build Coastguard Worker class LocalVariableSplitter {
163*03ce13f7SAndroid Build Coastguard Worker LocalVariableSplitter() = delete;
164*03ce13f7SAndroid Build Coastguard Worker LocalVariableSplitter(const LocalVariableSplitter &) = delete;
165*03ce13f7SAndroid Build Coastguard Worker LocalVariableSplitter &operator=(const LocalVariableSplitter &) = delete;
166*03ce13f7SAndroid Build Coastguard Worker
167*03ce13f7SAndroid Build Coastguard Worker public:
LocalVariableSplitter(Cfg * Func)168*03ce13f7SAndroid Build Coastguard Worker explicit LocalVariableSplitter(Cfg *Func)
169*03ce13f7SAndroid Build Coastguard Worker : Target(Func->getTarget()), VarMap(Func) {}
170*03ce13f7SAndroid Build Coastguard Worker /// setNode() is called before processing the instructions of a block.
setNode(CfgNode * CurNode)171*03ce13f7SAndroid Build Coastguard Worker void setNode(CfgNode *CurNode) {
172*03ce13f7SAndroid Build Coastguard Worker Node = CurNode;
173*03ce13f7SAndroid Build Coastguard Worker VarMap.reset(Node);
174*03ce13f7SAndroid Build Coastguard Worker LinkedToFixups.clear();
175*03ce13f7SAndroid Build Coastguard Worker }
176*03ce13f7SAndroid Build Coastguard Worker /// finalizeNode() is called after all instructions in the block are
177*03ce13f7SAndroid Build Coastguard Worker /// processed.
finalizeNode()178*03ce13f7SAndroid Build Coastguard Worker void finalizeNode() {
179*03ce13f7SAndroid Build Coastguard Worker // Splice in any preexisting LinkedTo links into the single chain. These
180*03ce13f7SAndroid Build Coastguard Worker // are the ones that were recorded during setInst().
181*03ce13f7SAndroid Build Coastguard Worker for (Variable *Var : LinkedToFixups) {
182*03ce13f7SAndroid Build Coastguard Worker VarMap.spliceBlockLocalLinkedToChain(Var);
183*03ce13f7SAndroid Build Coastguard Worker }
184*03ce13f7SAndroid Build Coastguard Worker }
185*03ce13f7SAndroid Build Coastguard Worker /// setInst() is called before processing the next instruction. The iterators
186*03ce13f7SAndroid Build Coastguard Worker /// are the insertion points for a new instructions, depending on whether the
187*03ce13f7SAndroid Build Coastguard Worker /// new instruction should be inserted before or after the current
188*03ce13f7SAndroid Build Coastguard Worker /// instruction.
setInst(Inst * CurInst,InstList::iterator Cur,InstList::iterator Next)189*03ce13f7SAndroid Build Coastguard Worker void setInst(Inst *CurInst, InstList::iterator Cur, InstList::iterator Next) {
190*03ce13f7SAndroid Build Coastguard Worker Instr = CurInst;
191*03ce13f7SAndroid Build Coastguard Worker Dest = Instr->getDest();
192*03ce13f7SAndroid Build Coastguard Worker IterCur = Cur;
193*03ce13f7SAndroid Build Coastguard Worker IterNext = Next;
194*03ce13f7SAndroid Build Coastguard Worker ShouldSkipRemainingInstructions = false;
195*03ce13f7SAndroid Build Coastguard Worker // Note any preexisting LinkedTo relationships that were created during
196*03ce13f7SAndroid Build Coastguard Worker // target lowering. Record them in LinkedToFixups which is then processed
197*03ce13f7SAndroid Build Coastguard Worker // in finalizeNode().
198*03ce13f7SAndroid Build Coastguard Worker if (Dest != nullptr && Dest->getLinkedTo() != nullptr) {
199*03ce13f7SAndroid Build Coastguard Worker LinkedToFixups.emplace_back(Dest);
200*03ce13f7SAndroid Build Coastguard Worker }
201*03ce13f7SAndroid Build Coastguard Worker }
shouldSkipRemainingInstructions() const202*03ce13f7SAndroid Build Coastguard Worker bool shouldSkipRemainingInstructions() const {
203*03ce13f7SAndroid Build Coastguard Worker return ShouldSkipRemainingInstructions;
204*03ce13f7SAndroid Build Coastguard Worker }
isUnconditionallyExecuted() const205*03ce13f7SAndroid Build Coastguard Worker bool isUnconditionallyExecuted() const { return WaitingForLabel == nullptr; }
206*03ce13f7SAndroid Build Coastguard Worker
207*03ce13f7SAndroid Build Coastguard Worker /// Note: the handle*() functions return true to indicate that the instruction
208*03ce13f7SAndroid Build Coastguard Worker /// has now been handled and that the instruction loop should continue to the
209*03ce13f7SAndroid Build Coastguard Worker /// next instruction in the block (and return false otherwise). In addition,
210*03ce13f7SAndroid Build Coastguard Worker /// they set the ShouldSkipRemainingInstructions flag to indicate that no more
211*03ce13f7SAndroid Build Coastguard Worker /// instructions in the block should be processed.
212*03ce13f7SAndroid Build Coastguard Worker
213*03ce13f7SAndroid Build Coastguard Worker /// Handle an "unwanted" instruction by returning true;
handleUnwantedInstruction()214*03ce13f7SAndroid Build Coastguard Worker bool handleUnwantedInstruction() {
215*03ce13f7SAndroid Build Coastguard Worker // We can limit the splitting to an arbitrary subset of the instructions,
216*03ce13f7SAndroid Build Coastguard Worker // and still expect correct code. As such, we can do instruction-subset
217*03ce13f7SAndroid Build Coastguard Worker // bisection to help debug any problems in this pass.
218*03ce13f7SAndroid Build Coastguard Worker static constexpr char AnInstructionHasNoName[] = "";
219*03ce13f7SAndroid Build Coastguard Worker if (!BuildDefs::minimal() &&
220*03ce13f7SAndroid Build Coastguard Worker !getFlags().matchSplitInsts(AnInstructionHasNoName,
221*03ce13f7SAndroid Build Coastguard Worker Instr->getNumber())) {
222*03ce13f7SAndroid Build Coastguard Worker return true;
223*03ce13f7SAndroid Build Coastguard Worker }
224*03ce13f7SAndroid Build Coastguard Worker if (!llvm::isa<InstTarget>(Instr)) {
225*03ce13f7SAndroid Build Coastguard Worker // Ignore non-lowered instructions like FakeDef/FakeUse.
226*03ce13f7SAndroid Build Coastguard Worker return true;
227*03ce13f7SAndroid Build Coastguard Worker }
228*03ce13f7SAndroid Build Coastguard Worker return false;
229*03ce13f7SAndroid Build Coastguard Worker }
230*03ce13f7SAndroid Build Coastguard Worker
231*03ce13f7SAndroid Build Coastguard Worker /// Process a potential label instruction.
handleLabel()232*03ce13f7SAndroid Build Coastguard Worker bool handleLabel() {
233*03ce13f7SAndroid Build Coastguard Worker if (!Instr->isLabel())
234*03ce13f7SAndroid Build Coastguard Worker return false;
235*03ce13f7SAndroid Build Coastguard Worker // A Label instruction shouldn't have any operands, so it can be handled
236*03ce13f7SAndroid Build Coastguard Worker // right here and then move on.
237*03ce13f7SAndroid Build Coastguard Worker assert(Dest == nullptr);
238*03ce13f7SAndroid Build Coastguard Worker assert(Instr->getSrcSize() == 0);
239*03ce13f7SAndroid Build Coastguard Worker if (Instr == WaitingForLabel) {
240*03ce13f7SAndroid Build Coastguard Worker // If we found the forward-branch-target Label instruction we're waiting
241*03ce13f7SAndroid Build Coastguard Worker // for, then clear the WaitingForLabel state.
242*03ce13f7SAndroid Build Coastguard Worker WaitingForLabel = nullptr;
243*03ce13f7SAndroid Build Coastguard Worker } else if (WaitingForLabel == nullptr && WaitingForBranchTo == nullptr) {
244*03ce13f7SAndroid Build Coastguard Worker // If we found a new Label instruction while the WaitingFor* state is
245*03ce13f7SAndroid Build Coastguard Worker // clear, then set things up for this being a backward branch target.
246*03ce13f7SAndroid Build Coastguard Worker WaitingForBranchTo = Instr;
247*03ce13f7SAndroid Build Coastguard Worker } else {
248*03ce13f7SAndroid Build Coastguard Worker // We see something we don't understand, so skip to the next block.
249*03ce13f7SAndroid Build Coastguard Worker ShouldSkipRemainingInstructions = true;
250*03ce13f7SAndroid Build Coastguard Worker }
251*03ce13f7SAndroid Build Coastguard Worker return true;
252*03ce13f7SAndroid Build Coastguard Worker }
253*03ce13f7SAndroid Build Coastguard Worker
254*03ce13f7SAndroid Build Coastguard Worker /// Process a potential intra-block branch instruction.
handleIntraBlockBranch()255*03ce13f7SAndroid Build Coastguard Worker bool handleIntraBlockBranch() {
256*03ce13f7SAndroid Build Coastguard Worker const Inst *Label = Instr->getIntraBlockBranchTarget();
257*03ce13f7SAndroid Build Coastguard Worker if (Label == nullptr)
258*03ce13f7SAndroid Build Coastguard Worker return false;
259*03ce13f7SAndroid Build Coastguard Worker // An intra-block branch instruction shouldn't have any operands, so it can
260*03ce13f7SAndroid Build Coastguard Worker // be handled right here and then move on.
261*03ce13f7SAndroid Build Coastguard Worker assert(Dest == nullptr);
262*03ce13f7SAndroid Build Coastguard Worker assert(Instr->getSrcSize() == 0);
263*03ce13f7SAndroid Build Coastguard Worker if (WaitingForBranchTo == Label && WaitingForLabel == nullptr) {
264*03ce13f7SAndroid Build Coastguard Worker WaitingForBranchTo = nullptr;
265*03ce13f7SAndroid Build Coastguard Worker } else if (WaitingForBranchTo == nullptr &&
266*03ce13f7SAndroid Build Coastguard Worker (WaitingForLabel == nullptr || WaitingForLabel == Label)) {
267*03ce13f7SAndroid Build Coastguard Worker WaitingForLabel = Label;
268*03ce13f7SAndroid Build Coastguard Worker } else {
269*03ce13f7SAndroid Build Coastguard Worker // We see something we don't understand, so skip to the next block.
270*03ce13f7SAndroid Build Coastguard Worker ShouldSkipRemainingInstructions = true;
271*03ce13f7SAndroid Build Coastguard Worker }
272*03ce13f7SAndroid Build Coastguard Worker return true;
273*03ce13f7SAndroid Build Coastguard Worker }
274*03ce13f7SAndroid Build Coastguard Worker
275*03ce13f7SAndroid Build Coastguard Worker /// Specially process a potential "Variable=Variable" assignment instruction,
276*03ce13f7SAndroid Build Coastguard Worker /// when it conforms to certain patterns.
handleSimpleVarAssign()277*03ce13f7SAndroid Build Coastguard Worker bool handleSimpleVarAssign() {
278*03ce13f7SAndroid Build Coastguard Worker if (!Instr->isVarAssign())
279*03ce13f7SAndroid Build Coastguard Worker return false;
280*03ce13f7SAndroid Build Coastguard Worker const bool DestIsInf = isInf(Dest);
281*03ce13f7SAndroid Build Coastguard Worker const bool DestIsAllocable = isAllocable(Dest);
282*03ce13f7SAndroid Build Coastguard Worker auto *SrcVar = llvm::cast<Variable>(Instr->getSrc(0));
283*03ce13f7SAndroid Build Coastguard Worker const bool SrcIsInf = isInf(SrcVar);
284*03ce13f7SAndroid Build Coastguard Worker const bool SrcIsAllocable = isAllocable(SrcVar);
285*03ce13f7SAndroid Build Coastguard Worker if (DestIsInf && SrcIsInf) {
286*03ce13f7SAndroid Build Coastguard Worker // The instruction:
287*03ce13f7SAndroid Build Coastguard Worker // t:inf = u:inf
288*03ce13f7SAndroid Build Coastguard Worker // No transformation is needed.
289*03ce13f7SAndroid Build Coastguard Worker return true;
290*03ce13f7SAndroid Build Coastguard Worker }
291*03ce13f7SAndroid Build Coastguard Worker if (DestIsInf && SrcIsAllocable && Dest->getType() == SrcVar->getType()) {
292*03ce13f7SAndroid Build Coastguard Worker // The instruction:
293*03ce13f7SAndroid Build Coastguard Worker // t:inf = v
294*03ce13f7SAndroid Build Coastguard Worker // gets transformed to:
295*03ce13f7SAndroid Build Coastguard Worker // t:inf = v1
296*03ce13f7SAndroid Build Coastguard Worker // v2 = t:inf
297*03ce13f7SAndroid Build Coastguard Worker // where:
298*03ce13f7SAndroid Build Coastguard Worker // v1 := map[v]
299*03ce13f7SAndroid Build Coastguard Worker // v2 := linkTo(v)
300*03ce13f7SAndroid Build Coastguard Worker // map[v] := v2
301*03ce13f7SAndroid Build Coastguard Worker //
302*03ce13f7SAndroid Build Coastguard Worker // If both v2 and its linkedToStackRoot get a stack slot, then "v2=t:inf"
303*03ce13f7SAndroid Build Coastguard Worker // is recognized as a redundant assignment and elided.
304*03ce13f7SAndroid Build Coastguard Worker //
305*03ce13f7SAndroid Build Coastguard Worker // Note that if the dest and src types are different, then this is
306*03ce13f7SAndroid Build Coastguard Worker // actually a truncation operation, which would make "v2=t:inf" an invalid
307*03ce13f7SAndroid Build Coastguard Worker // instruction. In this case, the type test will make it fall through to
308*03ce13f7SAndroid Build Coastguard Worker // the general case below.
309*03ce13f7SAndroid Build Coastguard Worker Variable *OldMapped = VarMap.get(SrcVar);
310*03ce13f7SAndroid Build Coastguard Worker Instr->replaceSource(0, OldMapped);
311*03ce13f7SAndroid Build Coastguard Worker if (isUnconditionallyExecuted()) {
312*03ce13f7SAndroid Build Coastguard Worker // Only create new mapping state if the instruction is unconditionally
313*03ce13f7SAndroid Build Coastguard Worker // executed.
314*03ce13f7SAndroid Build Coastguard Worker if (!VarMap.isInstLastUseOfVar(SrcVar, Instr)) {
315*03ce13f7SAndroid Build Coastguard Worker Variable *NewMapped = VarMap.makeLinked(SrcVar);
316*03ce13f7SAndroid Build Coastguard Worker Inst *Mov = Target->createLoweredMove(NewMapped, Dest);
317*03ce13f7SAndroid Build Coastguard Worker Node->getInsts().insert(IterNext, Mov);
318*03ce13f7SAndroid Build Coastguard Worker }
319*03ce13f7SAndroid Build Coastguard Worker }
320*03ce13f7SAndroid Build Coastguard Worker return true;
321*03ce13f7SAndroid Build Coastguard Worker }
322*03ce13f7SAndroid Build Coastguard Worker if (DestIsAllocable && SrcIsInf) {
323*03ce13f7SAndroid Build Coastguard Worker if (!VarMap.isDestUsedInBlock(Dest)) {
324*03ce13f7SAndroid Build Coastguard Worker return true;
325*03ce13f7SAndroid Build Coastguard Worker }
326*03ce13f7SAndroid Build Coastguard Worker // The instruction:
327*03ce13f7SAndroid Build Coastguard Worker // v = t:inf
328*03ce13f7SAndroid Build Coastguard Worker // gets transformed to:
329*03ce13f7SAndroid Build Coastguard Worker // v = t:inf
330*03ce13f7SAndroid Build Coastguard Worker // v2 = t:inf
331*03ce13f7SAndroid Build Coastguard Worker // where:
332*03ce13f7SAndroid Build Coastguard Worker // v2 := linkTo(v)
333*03ce13f7SAndroid Build Coastguard Worker // map[v] := v2
334*03ce13f7SAndroid Build Coastguard Worker //
335*03ce13f7SAndroid Build Coastguard Worker // If both v2 and v get a stack slot, then "v2=t:inf" is recognized as a
336*03ce13f7SAndroid Build Coastguard Worker // redundant assignment and elided.
337*03ce13f7SAndroid Build Coastguard Worker if (isUnconditionallyExecuted()) {
338*03ce13f7SAndroid Build Coastguard Worker // Only create new mapping state if the instruction is unconditionally
339*03ce13f7SAndroid Build Coastguard Worker // executed.
340*03ce13f7SAndroid Build Coastguard Worker Variable *NewMapped = VarMap.makeLinked(Dest);
341*03ce13f7SAndroid Build Coastguard Worker Inst *Mov = Target->createLoweredMove(NewMapped, SrcVar);
342*03ce13f7SAndroid Build Coastguard Worker Node->getInsts().insert(IterNext, Mov);
343*03ce13f7SAndroid Build Coastguard Worker } else {
344*03ce13f7SAndroid Build Coastguard Worker // For a conditionally executed instruction, add a redefinition of the
345*03ce13f7SAndroid Build Coastguard Worker // original Dest mapping, without creating a new linked variable.
346*03ce13f7SAndroid Build Coastguard Worker Variable *OldMapped = VarMap.get(Dest);
347*03ce13f7SAndroid Build Coastguard Worker Inst *Mov = Target->createLoweredMove(OldMapped, SrcVar);
348*03ce13f7SAndroid Build Coastguard Worker Mov->setDestRedefined();
349*03ce13f7SAndroid Build Coastguard Worker Node->getInsts().insert(IterNext, Mov);
350*03ce13f7SAndroid Build Coastguard Worker }
351*03ce13f7SAndroid Build Coastguard Worker return true;
352*03ce13f7SAndroid Build Coastguard Worker }
353*03ce13f7SAndroid Build Coastguard Worker assert(!ShouldSkipRemainingInstructions);
354*03ce13f7SAndroid Build Coastguard Worker return false;
355*03ce13f7SAndroid Build Coastguard Worker }
356*03ce13f7SAndroid Build Coastguard Worker
357*03ce13f7SAndroid Build Coastguard Worker /// Process the dest Variable of a Phi instruction.
handlePhi()358*03ce13f7SAndroid Build Coastguard Worker bool handlePhi() {
359*03ce13f7SAndroid Build Coastguard Worker assert(llvm::isa<InstPhi>(Instr));
360*03ce13f7SAndroid Build Coastguard Worker const bool DestIsAllocable = isAllocable(Dest);
361*03ce13f7SAndroid Build Coastguard Worker if (!DestIsAllocable)
362*03ce13f7SAndroid Build Coastguard Worker return true;
363*03ce13f7SAndroid Build Coastguard Worker if (!VarMap.isDestUsedInBlock(Dest))
364*03ce13f7SAndroid Build Coastguard Worker return true;
365*03ce13f7SAndroid Build Coastguard Worker Variable *NewMapped = VarMap.makeLinked(Dest);
366*03ce13f7SAndroid Build Coastguard Worker Inst *Mov = Target->createLoweredMove(NewMapped, Dest);
367*03ce13f7SAndroid Build Coastguard Worker Node->getInsts().insert(IterCur, Mov);
368*03ce13f7SAndroid Build Coastguard Worker return true;
369*03ce13f7SAndroid Build Coastguard Worker }
370*03ce13f7SAndroid Build Coastguard Worker
371*03ce13f7SAndroid Build Coastguard Worker /// Process an arbitrary instruction.
handleGeneralInst()372*03ce13f7SAndroid Build Coastguard Worker bool handleGeneralInst() {
373*03ce13f7SAndroid Build Coastguard Worker const bool DestIsAllocable = isAllocable(Dest);
374*03ce13f7SAndroid Build Coastguard Worker // The (non-variable-assignment) instruction:
375*03ce13f7SAndroid Build Coastguard Worker // ... = F(v)
376*03ce13f7SAndroid Build Coastguard Worker // where v is not infinite-weight, gets transformed to:
377*03ce13f7SAndroid Build Coastguard Worker // v2 = v1
378*03ce13f7SAndroid Build Coastguard Worker // ... = F(v1)
379*03ce13f7SAndroid Build Coastguard Worker // where:
380*03ce13f7SAndroid Build Coastguard Worker // v1 := map[v]
381*03ce13f7SAndroid Build Coastguard Worker // v2 := linkTo(v)
382*03ce13f7SAndroid Build Coastguard Worker // map[v] := v2
383*03ce13f7SAndroid Build Coastguard Worker // After that, if the "..." dest=u is not infinite-weight, append:
384*03ce13f7SAndroid Build Coastguard Worker // u2 = u
385*03ce13f7SAndroid Build Coastguard Worker // where:
386*03ce13f7SAndroid Build Coastguard Worker // u2 := linkTo(u)
387*03ce13f7SAndroid Build Coastguard Worker // map[u] := u2
388*03ce13f7SAndroid Build Coastguard Worker for (SizeT i = 0; i < Instr->getSrcSize(); ++i) {
389*03ce13f7SAndroid Build Coastguard Worker // Iterate over the top-level src vars. Don't bother to dig into
390*03ce13f7SAndroid Build Coastguard Worker // e.g. MemOperands because their vars should all be infinite-weight.
391*03ce13f7SAndroid Build Coastguard Worker // (This assumption would need to change if the pass were done
392*03ce13f7SAndroid Build Coastguard Worker // pre-lowering.)
393*03ce13f7SAndroid Build Coastguard Worker if (auto *SrcVar = llvm::dyn_cast<Variable>(Instr->getSrc(i))) {
394*03ce13f7SAndroid Build Coastguard Worker const bool SrcIsAllocable = isAllocable(SrcVar);
395*03ce13f7SAndroid Build Coastguard Worker if (SrcIsAllocable) {
396*03ce13f7SAndroid Build Coastguard Worker Variable *OldMapped = VarMap.get(SrcVar);
397*03ce13f7SAndroid Build Coastguard Worker if (isUnconditionallyExecuted()) {
398*03ce13f7SAndroid Build Coastguard Worker if (!VarMap.isInstLastUseOfVar(SrcVar, Instr)) {
399*03ce13f7SAndroid Build Coastguard Worker Variable *NewMapped = VarMap.makeLinked(SrcVar);
400*03ce13f7SAndroid Build Coastguard Worker Inst *Mov = Target->createLoweredMove(NewMapped, OldMapped);
401*03ce13f7SAndroid Build Coastguard Worker Node->getInsts().insert(IterCur, Mov);
402*03ce13f7SAndroid Build Coastguard Worker }
403*03ce13f7SAndroid Build Coastguard Worker }
404*03ce13f7SAndroid Build Coastguard Worker Instr->replaceSource(i, OldMapped);
405*03ce13f7SAndroid Build Coastguard Worker }
406*03ce13f7SAndroid Build Coastguard Worker }
407*03ce13f7SAndroid Build Coastguard Worker }
408*03ce13f7SAndroid Build Coastguard Worker // Transformation of Dest is the same as the "v=t:inf" case above.
409*03ce13f7SAndroid Build Coastguard Worker if (DestIsAllocable && VarMap.isDestUsedInBlock(Dest)) {
410*03ce13f7SAndroid Build Coastguard Worker if (isUnconditionallyExecuted()) {
411*03ce13f7SAndroid Build Coastguard Worker Variable *NewMapped = VarMap.makeLinked(Dest);
412*03ce13f7SAndroid Build Coastguard Worker Inst *Mov = Target->createLoweredMove(NewMapped, Dest);
413*03ce13f7SAndroid Build Coastguard Worker Node->getInsts().insert(IterNext, Mov);
414*03ce13f7SAndroid Build Coastguard Worker } else {
415*03ce13f7SAndroid Build Coastguard Worker Variable *OldMapped = VarMap.get(Dest);
416*03ce13f7SAndroid Build Coastguard Worker Inst *Mov = Target->createLoweredMove(OldMapped, Dest);
417*03ce13f7SAndroid Build Coastguard Worker Mov->setDestRedefined();
418*03ce13f7SAndroid Build Coastguard Worker Node->getInsts().insert(IterNext, Mov);
419*03ce13f7SAndroid Build Coastguard Worker }
420*03ce13f7SAndroid Build Coastguard Worker }
421*03ce13f7SAndroid Build Coastguard Worker return true;
422*03ce13f7SAndroid Build Coastguard Worker }
423*03ce13f7SAndroid Build Coastguard Worker
424*03ce13f7SAndroid Build Coastguard Worker private:
425*03ce13f7SAndroid Build Coastguard Worker TargetLowering *Target;
426*03ce13f7SAndroid Build Coastguard Worker CfgNode *Node = nullptr;
427*03ce13f7SAndroid Build Coastguard Worker Inst *Instr = nullptr;
428*03ce13f7SAndroid Build Coastguard Worker Variable *Dest = nullptr;
429*03ce13f7SAndroid Build Coastguard Worker InstList::iterator IterCur;
430*03ce13f7SAndroid Build Coastguard Worker InstList::iterator IterNext;
431*03ce13f7SAndroid Build Coastguard Worker bool ShouldSkipRemainingInstructions = false;
432*03ce13f7SAndroid Build Coastguard Worker VariableMap VarMap;
433*03ce13f7SAndroid Build Coastguard Worker CfgVector<Variable *> LinkedToFixups;
434*03ce13f7SAndroid Build Coastguard Worker /// WaitingForLabel and WaitingForBranchTo are for tracking intra-block
435*03ce13f7SAndroid Build Coastguard Worker /// control flow.
436*03ce13f7SAndroid Build Coastguard Worker const Inst *WaitingForLabel = nullptr;
437*03ce13f7SAndroid Build Coastguard Worker const Inst *WaitingForBranchTo = nullptr;
438*03ce13f7SAndroid Build Coastguard Worker };
439*03ce13f7SAndroid Build Coastguard Worker
440*03ce13f7SAndroid Build Coastguard Worker } // end of anonymous namespace
441*03ce13f7SAndroid Build Coastguard Worker
442*03ce13f7SAndroid Build Coastguard Worker /// Within each basic block, rewrite Variable references in terms of chained
443*03ce13f7SAndroid Build Coastguard Worker /// copies of the original Variable. For example:
444*03ce13f7SAndroid Build Coastguard Worker /// A = B + C
445*03ce13f7SAndroid Build Coastguard Worker /// might be rewritten as:
446*03ce13f7SAndroid Build Coastguard Worker /// B1 = B
447*03ce13f7SAndroid Build Coastguard Worker /// C1 = C
448*03ce13f7SAndroid Build Coastguard Worker /// A = B + C
449*03ce13f7SAndroid Build Coastguard Worker /// A1 = A
450*03ce13f7SAndroid Build Coastguard Worker /// and then:
451*03ce13f7SAndroid Build Coastguard Worker /// D = A + B
452*03ce13f7SAndroid Build Coastguard Worker /// might be rewritten as:
453*03ce13f7SAndroid Build Coastguard Worker /// A2 = A1
454*03ce13f7SAndroid Build Coastguard Worker /// B2 = B1
455*03ce13f7SAndroid Build Coastguard Worker /// D = A1 + B1
456*03ce13f7SAndroid Build Coastguard Worker /// D1 = D
457*03ce13f7SAndroid Build Coastguard Worker ///
458*03ce13f7SAndroid Build Coastguard Worker /// The purpose is to present the linear-scan register allocator with smaller
459*03ce13f7SAndroid Build Coastguard Worker /// live ranges, to help mitigate its "all or nothing" allocation strategy,
460*03ce13f7SAndroid Build Coastguard Worker /// while counting on its preference mechanism to keep the split versions in the
461*03ce13f7SAndroid Build Coastguard Worker /// same register when possible.
462*03ce13f7SAndroid Build Coastguard Worker ///
463*03ce13f7SAndroid Build Coastguard Worker /// When creating new Variables, A2 is linked to A1 which is linked to A, and
464*03ce13f7SAndroid Build Coastguard Worker /// similar for the other Variable linked-to chains. Rewrites apply only to
465*03ce13f7SAndroid Build Coastguard Worker /// Variables where mayHaveReg() is true.
466*03ce13f7SAndroid Build Coastguard Worker ///
467*03ce13f7SAndroid Build Coastguard Worker /// At code emission time, redundant linked-to stack assignments will be
468*03ce13f7SAndroid Build Coastguard Worker /// recognized and elided. To illustrate using the above example, if A1 gets a
469*03ce13f7SAndroid Build Coastguard Worker /// register but A and A2 are on the stack, the "A2=A1" store instruction is
470*03ce13f7SAndroid Build Coastguard Worker /// redundant since A and A2 share the same stack slot and A1 originated from A.
471*03ce13f7SAndroid Build Coastguard Worker ///
472*03ce13f7SAndroid Build Coastguard Worker /// Simple assignment instructions are rewritten slightly differently, to take
473*03ce13f7SAndroid Build Coastguard Worker /// maximal advantage of Variables known to have registers.
474*03ce13f7SAndroid Build Coastguard Worker ///
475*03ce13f7SAndroid Build Coastguard Worker /// In general, there may be several valid ways to rewrite an instruction: add
476*03ce13f7SAndroid Build Coastguard Worker /// the new assignment instruction either before or after the original
477*03ce13f7SAndroid Build Coastguard Worker /// instruction, and rewrite the original instruction with either the old or the
478*03ce13f7SAndroid Build Coastguard Worker /// new variable mapping. We try to pick a strategy most likely to avoid
479*03ce13f7SAndroid Build Coastguard Worker /// potential performance problems. For example, try to avoid storing to the
480*03ce13f7SAndroid Build Coastguard Worker /// stack and then immediately reloading from the same location. One
481*03ce13f7SAndroid Build Coastguard Worker /// consequence is that code might be generated that loads a register from a
482*03ce13f7SAndroid Build Coastguard Worker /// stack location, followed almost immediately by another use of the same stack
483*03ce13f7SAndroid Build Coastguard Worker /// location, despite its value already being available in a register as a
484*03ce13f7SAndroid Build Coastguard Worker /// result of the first instruction. However, the performance impact here is
485*03ce13f7SAndroid Build Coastguard Worker /// likely to be negligible, and a simple availability peephole optimization
486*03ce13f7SAndroid Build Coastguard Worker /// could clean it up.
487*03ce13f7SAndroid Build Coastguard Worker ///
488*03ce13f7SAndroid Build Coastguard Worker /// This pass potentially adds a lot of new instructions and variables, and as
489*03ce13f7SAndroid Build Coastguard Worker /// such there are compile-time performance concerns, particularly with liveness
490*03ce13f7SAndroid Build Coastguard Worker /// analysis and register allocation. Note that for liveness analysis, the new
491*03ce13f7SAndroid Build Coastguard Worker /// variables have single-block liveness, so they don't increase the size of the
492*03ce13f7SAndroid Build Coastguard Worker /// liveness bit vectors that need to be merged across blocks. As a result, the
493*03ce13f7SAndroid Build Coastguard Worker /// performance impact is likely to be linearly related to the number of new
494*03ce13f7SAndroid Build Coastguard Worker /// instructions, rather than number of new variables times number of blocks
495*03ce13f7SAndroid Build Coastguard Worker /// which would be the case if they were multi-block variables.
splitBlockLocalVariables(Cfg * Func)496*03ce13f7SAndroid Build Coastguard Worker void splitBlockLocalVariables(Cfg *Func) {
497*03ce13f7SAndroid Build Coastguard Worker if (!getFlags().getSplitLocalVars())
498*03ce13f7SAndroid Build Coastguard Worker return;
499*03ce13f7SAndroid Build Coastguard Worker TimerMarker _(TimerStack::TT_splitLocalVars, Func);
500*03ce13f7SAndroid Build Coastguard Worker LocalVariableSplitter Splitter(Func);
501*03ce13f7SAndroid Build Coastguard Worker // TODO(stichnot): Fix this mechanism for LinkedTo variables and stack slot
502*03ce13f7SAndroid Build Coastguard Worker // assignment.
503*03ce13f7SAndroid Build Coastguard Worker //
504*03ce13f7SAndroid Build Coastguard Worker // To work around shortcomings with stack frame mapping, we want to arrange
505*03ce13f7SAndroid Build Coastguard Worker // LinkedTo structure such that within one block, the LinkedTo structure
506*03ce13f7SAndroid Build Coastguard Worker // leading to a root forms a list, not a tree. A LinkedTo root can have
507*03ce13f7SAndroid Build Coastguard Worker // multiple children linking to it, but only one per block. Furthermore,
508*03ce13f7SAndroid Build Coastguard Worker // because stack slot mapping processes variables in numerical order, the
509*03ce13f7SAndroid Build Coastguard Worker // LinkedTo chain needs to be ordered such that when A->getLinkedTo() == B,
510*03ce13f7SAndroid Build Coastguard Worker // then A->getIndex() > B->getIndex().
511*03ce13f7SAndroid Build Coastguard Worker //
512*03ce13f7SAndroid Build Coastguard Worker // To effect this, while processing a block we keep track of preexisting
513*03ce13f7SAndroid Build Coastguard Worker // LinkedTo relationships via the LinkedToFixups vector, and at the end of the
514*03ce13f7SAndroid Build Coastguard Worker // block we splice them in such that the block has a single chain for each
515*03ce13f7SAndroid Build Coastguard Worker // root, ordered by getIndex() value.
516*03ce13f7SAndroid Build Coastguard Worker CfgVector<Variable *> LinkedToFixups;
517*03ce13f7SAndroid Build Coastguard Worker for (CfgNode *Node : Func->getNodes()) {
518*03ce13f7SAndroid Build Coastguard Worker // Clear the VarMap and LinkedToFixups at the start of every block.
519*03ce13f7SAndroid Build Coastguard Worker LinkedToFixups.clear();
520*03ce13f7SAndroid Build Coastguard Worker Splitter.setNode(Node);
521*03ce13f7SAndroid Build Coastguard Worker auto &Insts = Node->getInsts();
522*03ce13f7SAndroid Build Coastguard Worker auto Iter = Insts.begin();
523*03ce13f7SAndroid Build Coastguard Worker auto IterEnd = Insts.end();
524*03ce13f7SAndroid Build Coastguard Worker // TODO(stichnot): Figure out why Phi processing usually degrades
525*03ce13f7SAndroid Build Coastguard Worker // performance. Disable for now.
526*03ce13f7SAndroid Build Coastguard Worker static constexpr bool ProcessPhis = false;
527*03ce13f7SAndroid Build Coastguard Worker if (ProcessPhis) {
528*03ce13f7SAndroid Build Coastguard Worker for (Inst &Instr : Node->getPhis()) {
529*03ce13f7SAndroid Build Coastguard Worker if (Instr.isDeleted())
530*03ce13f7SAndroid Build Coastguard Worker continue;
531*03ce13f7SAndroid Build Coastguard Worker Splitter.setInst(&Instr, Iter, Iter);
532*03ce13f7SAndroid Build Coastguard Worker Splitter.handlePhi();
533*03ce13f7SAndroid Build Coastguard Worker }
534*03ce13f7SAndroid Build Coastguard Worker }
535*03ce13f7SAndroid Build Coastguard Worker InstList::iterator NextIter;
536*03ce13f7SAndroid Build Coastguard Worker for (; Iter != IterEnd && !Splitter.shouldSkipRemainingInstructions();
537*03ce13f7SAndroid Build Coastguard Worker Iter = NextIter) {
538*03ce13f7SAndroid Build Coastguard Worker NextIter = Iter;
539*03ce13f7SAndroid Build Coastguard Worker ++NextIter;
540*03ce13f7SAndroid Build Coastguard Worker Inst *Instr = iteratorToInst(Iter);
541*03ce13f7SAndroid Build Coastguard Worker if (Instr->isDeleted())
542*03ce13f7SAndroid Build Coastguard Worker continue;
543*03ce13f7SAndroid Build Coastguard Worker Splitter.setInst(Instr, Iter, NextIter);
544*03ce13f7SAndroid Build Coastguard Worker
545*03ce13f7SAndroid Build Coastguard Worker // Before doing any transformations, take care of the bookkeeping for
546*03ce13f7SAndroid Build Coastguard Worker // intra-block branching.
547*03ce13f7SAndroid Build Coastguard Worker //
548*03ce13f7SAndroid Build Coastguard Worker // This is tricky because the transformation for one instruction may
549*03ce13f7SAndroid Build Coastguard Worker // depend on a transformation for a previous instruction, but if that
550*03ce13f7SAndroid Build Coastguard Worker // previous instruction is not dynamically executed due to intra-block
551*03ce13f7SAndroid Build Coastguard Worker // control flow, it may lead to an inconsistent state and incorrect code.
552*03ce13f7SAndroid Build Coastguard Worker //
553*03ce13f7SAndroid Build Coastguard Worker // We want to handle some simple cases, and reject some others:
554*03ce13f7SAndroid Build Coastguard Worker //
555*03ce13f7SAndroid Build Coastguard Worker // 1. For something like a select instruction, we could have:
556*03ce13f7SAndroid Build Coastguard Worker // test cond
557*03ce13f7SAndroid Build Coastguard Worker // dest = src_false
558*03ce13f7SAndroid Build Coastguard Worker // branch conditionally to label
559*03ce13f7SAndroid Build Coastguard Worker // dest = src_true
560*03ce13f7SAndroid Build Coastguard Worker // label:
561*03ce13f7SAndroid Build Coastguard Worker //
562*03ce13f7SAndroid Build Coastguard Worker // Between the conditional branch and the label, we need to treat dest and
563*03ce13f7SAndroid Build Coastguard Worker // src variables specially, specifically not creating any new state.
564*03ce13f7SAndroid Build Coastguard Worker //
565*03ce13f7SAndroid Build Coastguard Worker // 2. Some 64-bit atomic instructions may be lowered to a loop:
566*03ce13f7SAndroid Build Coastguard Worker // label:
567*03ce13f7SAndroid Build Coastguard Worker // ...
568*03ce13f7SAndroid Build Coastguard Worker // branch conditionally to label
569*03ce13f7SAndroid Build Coastguard Worker //
570*03ce13f7SAndroid Build Coastguard Worker // No special treatment is needed, but it's worth tracking so that case #1
571*03ce13f7SAndroid Build Coastguard Worker // above can also be handled.
572*03ce13f7SAndroid Build Coastguard Worker //
573*03ce13f7SAndroid Build Coastguard Worker // 3. Advanced switch lowering can create really complex intra-block
574*03ce13f7SAndroid Build Coastguard Worker // control flow, so when we recognize this, we should just stop splitting
575*03ce13f7SAndroid Build Coastguard Worker // for the remainder of the block (which isn't much since a switch
576*03ce13f7SAndroid Build Coastguard Worker // instruction is a terminator).
577*03ce13f7SAndroid Build Coastguard Worker //
578*03ce13f7SAndroid Build Coastguard Worker // 4. Other complex lowering, e.g. an i64 icmp on a 32-bit architecture,
579*03ce13f7SAndroid Build Coastguard Worker // can result in an if/then/else like structure with two labels. One
580*03ce13f7SAndroid Build Coastguard Worker // possibility would be to suspect splitting for the remainder of the
581*03ce13f7SAndroid Build Coastguard Worker // lowered instruction, and then resume for the remainder of the block,
582*03ce13f7SAndroid Build Coastguard Worker // but since we don't have high-level instruction markers, we might as
583*03ce13f7SAndroid Build Coastguard Worker // well just stop splitting for the remainder of the block.
584*03ce13f7SAndroid Build Coastguard Worker if (Splitter.handleLabel())
585*03ce13f7SAndroid Build Coastguard Worker continue;
586*03ce13f7SAndroid Build Coastguard Worker if (Splitter.handleIntraBlockBranch())
587*03ce13f7SAndroid Build Coastguard Worker continue;
588*03ce13f7SAndroid Build Coastguard Worker if (Splitter.handleUnwantedInstruction())
589*03ce13f7SAndroid Build Coastguard Worker continue;
590*03ce13f7SAndroid Build Coastguard Worker
591*03ce13f7SAndroid Build Coastguard Worker // Intra-block bookkeeping is complete, now do the transformations.
592*03ce13f7SAndroid Build Coastguard Worker
593*03ce13f7SAndroid Build Coastguard Worker // Determine the transformation based on the kind of instruction, and
594*03ce13f7SAndroid Build Coastguard Worker // whether its Variables are infinite-weight. New instructions can be
595*03ce13f7SAndroid Build Coastguard Worker // inserted before the current instruction via Iter, or after the current
596*03ce13f7SAndroid Build Coastguard Worker // instruction via NextIter.
597*03ce13f7SAndroid Build Coastguard Worker if (Splitter.handleSimpleVarAssign())
598*03ce13f7SAndroid Build Coastguard Worker continue;
599*03ce13f7SAndroid Build Coastguard Worker if (Splitter.handleGeneralInst())
600*03ce13f7SAndroid Build Coastguard Worker continue;
601*03ce13f7SAndroid Build Coastguard Worker }
602*03ce13f7SAndroid Build Coastguard Worker Splitter.finalizeNode();
603*03ce13f7SAndroid Build Coastguard Worker }
604*03ce13f7SAndroid Build Coastguard Worker
605*03ce13f7SAndroid Build Coastguard Worker Func->dump("After splitting local variables");
606*03ce13f7SAndroid Build Coastguard Worker }
607*03ce13f7SAndroid Build Coastguard Worker
608*03ce13f7SAndroid Build Coastguard Worker } // end of namespace Ice
609