Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceCfgNode.cpp - Basic block (node) implementation -----===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Implements the CfgNode class, including the complexities of |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 12 | /// instruction insertion and in-edge calculation. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 13 | /// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 16 | #include "IceCfgNode.h" |
| 17 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 18 | #include "IceAssembler.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 19 | #include "IceCfg.h" |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 20 | #include "IceGlobalInits.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 21 | #include "IceInst.h" |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 22 | #include "IceInstVarIter.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 23 | #include "IceLiveness.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 24 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 25 | #include "IceTargetLowering.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 26 | |
| 27 | namespace Ice { |
| 28 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 29 | // Adds an instruction to either the Phi list or the regular instruction list. |
| 30 | // Validates that all Phis are added before all regular instructions. |
Eric Holk | 085bdae | 2016-04-18 15:08:19 -0700 | [diff] [blame] | 31 | void CfgNode::appendInst(Inst *Instr) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 32 | ++InstCountEstimate; |
Eric Holk | 16f8061 | 2016-04-04 17:07:42 -0700 | [diff] [blame] | 33 | |
| 34 | if (BuildDefs::wasm()) { |
| 35 | if (llvm::isa<InstSwitch>(Instr) || llvm::isa<InstBr>(Instr)) { |
| 36 | for (auto *N : Instr->getTerminatorEdges()) { |
| 37 | N->addInEdge(this); |
| 38 | addOutEdge(N); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 43 | if (auto *Phi = llvm::dyn_cast<InstPhi>(Instr)) { |
Eric Holk | 085bdae | 2016-04-18 15:08:19 -0700 | [diff] [blame] | 44 | if (!Insts.empty()) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 45 | Func->setError("Phi instruction added to the middle of a block"); |
| 46 | return; |
| 47 | } |
| 48 | Phis.push_back(Phi); |
| 49 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 50 | Insts.push_back(Instr); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 51 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Manasij Mukherjee | 45f51a2 | 2016-06-27 16:12:37 -0700 | [diff] [blame] | 54 | void CfgNode::replaceInEdge(CfgNode *Old, CfgNode *New) { |
| 55 | for (SizeT i = 0; i < InEdges.size(); ++i) { |
| 56 | if (InEdges[i] == Old) { |
| 57 | InEdges[i] = New; |
| 58 | } |
| 59 | } |
| 60 | for (auto &Inst : getPhis()) { |
| 61 | auto &Phi = llvm::cast<InstPhi>(Inst); |
| 62 | for (SizeT i = 0; i < Phi.getSrcSize(); ++i) { |
| 63 | if (Phi.getLabel(i) == Old) { |
| 64 | Phi.setLabel(i, New); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
Jim Stichnoth | 76719b4 | 2016-03-14 08:37:52 -0700 | [diff] [blame] | 70 | namespace { |
| 71 | template <typename List> void removeDeletedAndRenumber(List *L, Cfg *Func) { |
| 72 | const bool DoDelete = |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 73 | BuildDefs::minimal() || !getFlags().getKeepDeletedInsts(); |
Jim Stichnoth | 76719b4 | 2016-03-14 08:37:52 -0700 | [diff] [blame] | 74 | auto I = L->begin(), E = L->end(), Next = I; |
| 75 | for (++Next; I != E; I = Next++) { |
| 76 | if (DoDelete && I->isDeleted()) { |
Nicolas Capens | 038a9b9 | 2016-09-12 15:40:25 -0400 | [diff] [blame] | 77 | L->remove(I); |
Jim Stichnoth | 76719b4 | 2016-03-14 08:37:52 -0700 | [diff] [blame] | 78 | } else { |
| 79 | I->renumber(Func); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } // end of anonymous namespace |
| 84 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 85 | void CfgNode::renumberInstructions() { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 86 | InstNumberT FirstNumber = Func->getNextInstNumber(); |
Jim Stichnoth | 76719b4 | 2016-03-14 08:37:52 -0700 | [diff] [blame] | 87 | removeDeletedAndRenumber(&Phis, Func); |
| 88 | removeDeletedAndRenumber(&Insts, Func); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 89 | InstCountEstimate = Func->getNextInstNumber() - FirstNumber; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 92 | // When a node is created, the OutEdges are immediately known, but the InEdges |
| 93 | // have to be built up incrementally. After the CFG has been constructed, the |
| 94 | // computePredecessors() pass finalizes it by creating the InEdges list. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 95 | void CfgNode::computePredecessors() { |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 96 | for (CfgNode *Succ : OutEdges) |
| 97 | Succ->InEdges.push_back(this); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 100 | void CfgNode::computeSuccessors() { |
Eric Holk | 16f8061 | 2016-04-04 17:07:42 -0700 | [diff] [blame] | 101 | OutEdges.clear(); |
| 102 | InEdges.clear(); |
Eric Holk | 085bdae | 2016-04-18 15:08:19 -0700 | [diff] [blame] | 103 | assert(!Insts.empty()); |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 104 | OutEdges = Insts.rbegin()->getTerminatorEdges(); |
| 105 | } |
| 106 | |
David Sehr | 263ac52 | 2016-04-04 10:11:08 -0700 | [diff] [blame] | 107 | // Ensure each Phi instruction in the node is consistent with respect to control |
| 108 | // flow. For each predecessor, there must be a phi argument with that label. |
| 109 | // If a phi argument's label doesn't appear in the predecessor list (which can |
| 110 | // happen as a result of e.g. unreachable node elimination), its value is |
| 111 | // modified to be zero, to maintain consistency in liveness analysis. This |
| 112 | // allows us to remove some dead control flow without a major rework of the phi |
| 113 | // instructions. We don't check that phi arguments with the same label have the |
| 114 | // same value. |
| 115 | void CfgNode::enforcePhiConsistency() { |
Jim Stichnoth | 1aca230 | 2015-09-16 11:25:22 -0700 | [diff] [blame] | 116 | for (Inst &Instr : Phis) { |
| 117 | auto *Phi = llvm::cast<InstPhi>(&Instr); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 118 | // We do a simple O(N^2) algorithm to check for consistency. Even so, it |
| 119 | // shows up as only about 0.2% of the total translation time. But if |
| 120 | // necessary, we could improve the complexity by using a hash table to |
| 121 | // count how many times each node is referenced in the Phi instruction, and |
| 122 | // how many times each node is referenced in the incoming edge list, and |
| 123 | // compare the two for equality. |
Jim Stichnoth | 1aca230 | 2015-09-16 11:25:22 -0700 | [diff] [blame] | 124 | for (SizeT i = 0; i < Phi->getSrcSize(); ++i) { |
| 125 | CfgNode *Label = Phi->getLabel(i); |
| 126 | bool Found = false; |
| 127 | for (CfgNode *InNode : getInEdges()) { |
| 128 | if (InNode == Label) { |
| 129 | Found = true; |
| 130 | break; |
| 131 | } |
| 132 | } |
David Sehr | 263ac52 | 2016-04-04 10:11:08 -0700 | [diff] [blame] | 133 | if (!Found) { |
| 134 | // Predecessor was unreachable, so if (impossibly) the control flow |
| 135 | // enters from that predecessor, the value should be zero. |
| 136 | Phi->clearOperandForTarget(Label); |
| 137 | } |
Jim Stichnoth | 1aca230 | 2015-09-16 11:25:22 -0700 | [diff] [blame] | 138 | } |
| 139 | for (CfgNode *InNode : getInEdges()) { |
| 140 | bool Found = false; |
| 141 | for (SizeT i = 0; i < Phi->getSrcSize(); ++i) { |
| 142 | CfgNode *Label = Phi->getLabel(i); |
| 143 | if (InNode == Label) { |
| 144 | Found = true; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | if (!Found) |
| 149 | llvm::report_fatal_error("Phi error: missing label for incoming edge"); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 154 | // This does part 1 of Phi lowering, by creating a new dest variable for each |
| 155 | // Phi instruction, replacing the Phi instruction's dest with that variable, |
| 156 | // and adding an explicit assignment of the old dest to the new dest. For |
| 157 | // example, |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 158 | // a=phi(...) |
| 159 | // changes to |
| 160 | // "a_phi=phi(...); a=a_phi". |
| 161 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 162 | // This is in preparation for part 2 which deletes the Phi instructions and |
| 163 | // appends assignment instructions to predecessor blocks. Note that this |
| 164 | // transformation preserves SSA form. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 165 | void CfgNode::placePhiLoads() { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 166 | for (Inst &I : Phis) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 167 | auto *Phi = llvm::dyn_cast<InstPhi>(&I); |
Jim Stichnoth | 1502e59 | 2014-12-11 09:22:45 -0800 | [diff] [blame] | 168 | Insts.insert(Insts.begin(), Phi->lower(Func)); |
| 169 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 172 | // This does part 2 of Phi lowering. For each Phi instruction at each out-edge, |
| 173 | // create a corresponding assignment instruction, and add all the assignments |
| 174 | // near the end of this block. They need to be added before any branch |
| 175 | // instruction, and also if the block ends with a compare instruction followed |
| 176 | // by a branch instruction that we may want to fuse, it's better to insert the |
| 177 | // new assignments before the compare instruction. The |
| 178 | // tryOptimizedCmpxchgCmpBr() method assumes this ordering of instructions. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 179 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 180 | // Note that this transformation takes the Phi dest variables out of SSA form, |
| 181 | // as there may be assignments to the dest variable in multiple blocks. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 182 | void CfgNode::placePhiStores() { |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 183 | // Find the insertion point. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 184 | InstList::iterator InsertionPoint = Insts.end(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 185 | // Every block must end in a terminator instruction, and therefore must have |
| 186 | // at least one instruction, so it's valid to decrement InsertionPoint (but |
| 187 | // assert just in case). |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 188 | assert(InsertionPoint != Insts.begin()); |
| 189 | --InsertionPoint; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 190 | // Confirm that InsertionPoint is a terminator instruction. Calling |
| 191 | // getTerminatorEdges() on a non-terminator instruction will cause an |
| 192 | // llvm_unreachable(). |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 193 | (void)InsertionPoint->getTerminatorEdges(); |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 194 | // SafeInsertionPoint is always immediately before the terminator |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 195 | // instruction. If the block ends in a compare and conditional branch, it's |
| 196 | // better to place the Phi store before the compare so as not to interfere |
| 197 | // with compare/branch fusing. However, if the compare instruction's dest |
| 198 | // operand is the same as the new assignment statement's source operand, this |
| 199 | // can't be done due to data dependences, so we need to fall back to the |
| 200 | // SafeInsertionPoint. To illustrate: |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 201 | // ; <label>:95 |
| 202 | // %97 = load i8* %96, align 1 |
| 203 | // %98 = icmp ne i8 %97, 0 |
| 204 | // br i1 %98, label %99, label %2132 |
| 205 | // ; <label>:99 |
| 206 | // %100 = phi i8 [ %97, %95 ], [ %110, %108 ] |
| 207 | // %101 = phi i1 [ %98, %95 ], [ %111, %108 ] |
| 208 | // would be Phi-lowered as: |
| 209 | // ; <label>:95 |
| 210 | // %97 = load i8* %96, align 1 |
| 211 | // %100_phi = %97 ; can be at InsertionPoint |
| 212 | // %98 = icmp ne i8 %97, 0 |
| 213 | // %101_phi = %98 ; must be at SafeInsertionPoint |
| 214 | // br i1 %98, label %99, label %2132 |
| 215 | // ; <label>:99 |
| 216 | // %100 = %100_phi |
| 217 | // %101 = %101_phi |
| 218 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 219 | // TODO(stichnot): It may be possible to bypass this whole SafeInsertionPoint |
| 220 | // mechanism. If a source basic block ends in a conditional branch: |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 221 | // labelSource: |
| 222 | // ... |
| 223 | // br i1 %foo, label %labelTrue, label %labelFalse |
| 224 | // and a branch target has a Phi involving the branch operand: |
| 225 | // labelTrue: |
| 226 | // %bar = phi i1 [ %foo, %labelSource ], ... |
| 227 | // then we actually know the constant i1 value of the Phi operand: |
| 228 | // labelTrue: |
| 229 | // %bar = phi i1 [ true, %labelSource ], ... |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 230 | // It seems that this optimization should be done by clang or opt, but we |
| 231 | // could also do it here. |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 232 | InstList::iterator SafeInsertionPoint = InsertionPoint; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 233 | // Keep track of the dest variable of a compare instruction, so that we |
| 234 | // insert the new instruction at the SafeInsertionPoint if the compare's dest |
| 235 | // matches the Phi-lowered assignment's source. |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 236 | Variable *CmpInstDest = nullptr; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 237 | // If the current insertion point is at a conditional branch instruction, and |
| 238 | // the previous instruction is a compare instruction, then we move the |
| 239 | // insertion point before the compare instruction so as not to interfere with |
| 240 | // compare/branch fusing. |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 241 | if (auto *Branch = llvm::dyn_cast<InstBr>(InsertionPoint)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 242 | if (!Branch->isUnconditional()) { |
| 243 | if (InsertionPoint != Insts.begin()) { |
| 244 | --InsertionPoint; |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 245 | if (llvm::isa<InstIcmp>(InsertionPoint) || |
| 246 | llvm::isa<InstFcmp>(InsertionPoint)) { |
| 247 | CmpInstDest = InsertionPoint->getDest(); |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 248 | } else { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 249 | ++InsertionPoint; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 254 | |
| 255 | // Consider every out-edge. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 256 | for (CfgNode *Succ : OutEdges) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 257 | // Consider every Phi instruction at the out-edge. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 258 | for (Inst &I : Succ->Phis) { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 259 | auto *Phi = llvm::dyn_cast<InstPhi>(&I); |
Jim Stichnoth | 1502e59 | 2014-12-11 09:22:45 -0800 | [diff] [blame] | 260 | Operand *Operand = Phi->getOperandForTarget(this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 261 | assert(Operand); |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 262 | Variable *Dest = I.getDest(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 263 | assert(Dest); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 264 | auto *NewInst = InstAssign::create(Func, Dest, Operand); |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 265 | if (CmpInstDest == Operand) |
| 266 | Insts.insert(SafeInsertionPoint, NewInst); |
| 267 | else |
| 268 | Insts.insert(InsertionPoint, NewInst); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // Deletes the phi instructions after the loads and stores are placed. |
| 274 | void CfgNode::deletePhis() { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 275 | for (Inst &I : Phis) |
| 276 | I.setDeleted(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 279 | // Splits the edge from Pred to this node by creating a new node and hooking up |
| 280 | // the in and out edges appropriately. (The EdgeIndex parameter is only used to |
| 281 | // make the new node's name unique when there are multiple edges between the |
| 282 | // same pair of nodes.) The new node's instruction list is initialized to the |
| 283 | // empty list, with no terminator instruction. There must not be multiple edges |
| 284 | // from Pred to this node so all Inst::getTerminatorEdges implementations must |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 285 | // not contain duplicates. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 286 | CfgNode *CfgNode::splitIncomingEdge(CfgNode *Pred, SizeT EdgeIndex) { |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 287 | CfgNode *NewNode = Func->makeNode(); |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 288 | // Depth is the minimum as it works if both are the same, but if one is |
| 289 | // outside the loop and the other is inside, the new node should be placed |
| 290 | // outside and not be executed multiple times within the loop. |
| 291 | NewNode->setLoopNestDepth( |
| 292 | std::min(getLoopNestDepth(), Pred->getLoopNestDepth())); |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 293 | if (BuildDefs::dump()) |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 294 | NewNode->setName("split_" + Pred->getName() + "_" + getName() + "_" + |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 295 | std::to_string(EdgeIndex)); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 296 | // The new node is added to the end of the node list, and will later need to |
| 297 | // be sorted into a reasonable topological order. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 298 | NewNode->setNeedsPlacement(true); |
| 299 | // Repoint Pred's out-edge. |
| 300 | bool Found = false; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 301 | for (CfgNode *&I : Pred->OutEdges) { |
| 302 | if (I == this) { |
| 303 | I = NewNode; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 304 | NewNode->InEdges.push_back(Pred); |
| 305 | Found = true; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 306 | break; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | assert(Found); |
Jim Stichnoth | a8d4713 | 2015-09-08 14:43:38 -0700 | [diff] [blame] | 310 | (void)Found; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 311 | // Repoint this node's in-edge. |
| 312 | Found = false; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 313 | for (CfgNode *&I : InEdges) { |
| 314 | if (I == Pred) { |
| 315 | I = NewNode; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 316 | NewNode->OutEdges.push_back(this); |
| 317 | Found = true; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 318 | break; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | assert(Found); |
Jim Stichnoth | a8d4713 | 2015-09-08 14:43:38 -0700 | [diff] [blame] | 322 | (void)Found; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 323 | // Repoint all suitable branch instructions' target and return. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 324 | Found = false; |
Andrew Scull | 87f80c1 | 2015-07-20 10:19:16 -0700 | [diff] [blame] | 325 | for (Inst &I : Pred->getInsts()) |
| 326 | if (!I.isDeleted() && I.repointEdges(this, NewNode)) |
| 327 | Found = true; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 328 | assert(Found); |
Jim Stichnoth | a8d4713 | 2015-09-08 14:43:38 -0700 | [diff] [blame] | 329 | (void)Found; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 330 | return NewNode; |
| 331 | } |
| 332 | |
| 333 | namespace { |
| 334 | |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 335 | // Helpers for advancedPhiLowering(). |
| 336 | |
| 337 | class PhiDesc { |
| 338 | PhiDesc() = delete; |
| 339 | PhiDesc(const PhiDesc &) = delete; |
| 340 | PhiDesc &operator=(const PhiDesc &) = delete; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 341 | |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 342 | public: |
| 343 | PhiDesc(InstPhi *Phi, Variable *Dest) : Phi(Phi), Dest(Dest) {} |
| 344 | PhiDesc(PhiDesc &&) = default; |
| 345 | InstPhi *Phi = nullptr; |
| 346 | Variable *Dest = nullptr; |
| 347 | Operand *Src = nullptr; |
| 348 | bool Processed = false; |
| 349 | size_t NumPred = 0; // number of entries whose Src is this Dest |
| 350 | int32_t Weight = 0; // preference for topological order |
| 351 | }; |
| 352 | using PhiDescList = llvm::SmallVector<PhiDesc, 32>; |
| 353 | |
| 354 | // Always pick NumPred=0 over NumPred>0. |
Jim Stichnoth | bbd449d | 2016-03-30 15:30:30 -0700 | [diff] [blame] | 355 | constexpr int32_t WeightNoPreds = 8; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 356 | // Prefer Src as a register because the register might free up. |
Jim Stichnoth | bbd449d | 2016-03-30 15:30:30 -0700 | [diff] [blame] | 357 | constexpr int32_t WeightSrcIsReg = 4; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 358 | // Prefer Dest not as a register because the register stays free longer. |
Jim Stichnoth | bbd449d | 2016-03-30 15:30:30 -0700 | [diff] [blame] | 359 | constexpr int32_t WeightDestNotReg = 2; |
| 360 | // Prefer NumPred=1 over NumPred>1. This is used as a tiebreaker when a |
| 361 | // dependency cycle must be broken so that hopefully only one temporary |
| 362 | // assignment has to be added to break the cycle. |
| 363 | constexpr int32_t WeightOnePred = 1; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 364 | |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 365 | bool sameVarOrReg(TargetLowering *Target, const Variable *Var1, |
| 366 | const Operand *Opnd) { |
| 367 | if (Var1 == Opnd) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 368 | return true; |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 369 | const auto *Var2 = llvm::dyn_cast<Variable>(Opnd); |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 370 | if (Var2 == nullptr) |
| 371 | return false; |
| 372 | |
| 373 | // If either operand lacks a register, they cannot be the same. |
| 374 | if (!Var1->hasReg()) |
| 375 | return false; |
| 376 | if (!Var2->hasReg()) |
| 377 | return false; |
| 378 | |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 379 | const auto RegNum1 = Var1->getRegNum(); |
| 380 | const auto RegNum2 = Var2->getRegNum(); |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 381 | // Quick common-case check. |
| 382 | if (RegNum1 == RegNum2) |
| 383 | return true; |
| 384 | |
| 385 | assert(Target->getAliasesForRegister(RegNum1)[RegNum2] == |
| 386 | Target->getAliasesForRegister(RegNum2)[RegNum1]); |
| 387 | return Target->getAliasesForRegister(RegNum1)[RegNum2]; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 390 | // Update NumPred for all Phi assignments using Var as their Dest variable. |
Jim Stichnoth | bbd449d | 2016-03-30 15:30:30 -0700 | [diff] [blame] | 391 | // Also update Weight if NumPred dropped from 2 to 1, or 1 to 0. |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 392 | void updatePreds(PhiDescList &Desc, TargetLowering *Target, Variable *Var) { |
| 393 | for (PhiDesc &Item : Desc) { |
| 394 | if (!Item.Processed && sameVarOrReg(Target, Var, Item.Dest)) { |
Jim Stichnoth | bbd449d | 2016-03-30 15:30:30 -0700 | [diff] [blame] | 395 | --Item.NumPred; |
| 396 | if (Item.NumPred == 1) { |
| 397 | // If NumPred changed from 2 to 1, add in WeightOnePred. |
| 398 | Item.Weight += WeightOnePred; |
| 399 | } else if (Item.NumPred == 0) { |
| 400 | // If NumPred changed from 1 to 0, subtract WeightOnePred and add in |
| 401 | // WeightNoPreds. |
| 402 | Item.Weight += (WeightNoPreds - WeightOnePred); |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 408 | } // end of anonymous namespace |
| 409 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 410 | // This the "advanced" version of Phi lowering for a basic block, in contrast |
| 411 | // to the simple version that lowers through assignments involving temporaries. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 412 | // |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 413 | // All Phi instructions in a basic block are conceptually executed in parallel. |
| 414 | // However, if we lower Phis early and commit to a sequential ordering, we may |
| 415 | // end up creating unnecessary interferences which lead to worse register |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 416 | // allocation. Delaying Phi scheduling until after register allocation can help |
| 417 | // unless there are no free registers for shuffling registers or stack slots |
| 418 | // and spilling becomes necessary. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 419 | // |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 420 | // The advanced Phi lowering starts by finding a topological sort of the Phi |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 421 | // instructions, where "A=B" comes before "B=C" due to the anti-dependence on |
| 422 | // B. Preexisting register assignments are considered in the topological sort. |
| 423 | // If a topological sort is not possible due to a cycle, the cycle is broken by |
| 424 | // introducing a non-parallel temporary. For example, a cycle arising from a |
| 425 | // permutation like "A=B;B=C;C=A" can become "T=A;A=B;B=C;C=T". All else being |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 426 | // equal, prefer to schedule assignments with register-allocated Src operands |
| 427 | // earlier, in case that register becomes free afterwards, and prefer to |
| 428 | // schedule assignments with register-allocated Dest variables later, to keep |
| 429 | // that register free for longer. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 430 | // |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 431 | // Once the ordering is determined, the Cfg edge is split and the assignment |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 432 | // list is lowered by the target lowering layer. Since the assignment lowering |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 433 | // may create new infinite-weight temporaries, a follow-on register allocation |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 434 | // pass will be needed. To prepare for this, liveness (including live range |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 435 | // calculation) of the split nodes needs to be calculated, and liveness of the |
| 436 | // original node need to be updated to "undo" the effects of the phi |
| 437 | // assignments. |
| 438 | |
| 439 | // The specific placement of the new node within the Cfg node list is deferred |
| 440 | // until later, including after empty node contraction. |
| 441 | // |
| 442 | // After phi assignments are lowered across all blocks, another register |
| 443 | // allocation pass is run, focusing only on pre-colored and infinite-weight |
| 444 | // variables, similar to Om1 register allocation (except without the need to |
| 445 | // specially compute these variables' live ranges, since they have already been |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 446 | // precisely calculated). The register allocator in this mode needs the ability |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 447 | // to forcibly spill and reload registers in case none are naturally available. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 448 | void CfgNode::advancedPhiLowering() { |
| 449 | if (getPhis().empty()) |
| 450 | return; |
| 451 | |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 452 | PhiDescList Desc; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 453 | |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 454 | for (Inst &I : Phis) { |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 455 | auto *Phi = llvm::dyn_cast<InstPhi>(&I); |
| 456 | if (!Phi->isDeleted()) { |
| 457 | Variable *Dest = Phi->getDest(); |
| 458 | Desc.emplace_back(Phi, Dest); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 459 | // Undo the effect of the phi instruction on this node's live-in set by |
| 460 | // marking the phi dest variable as live on entry. |
| 461 | SizeT VarNum = Func->getLiveness()->getLiveIndex(Dest->getIndex()); |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 462 | auto &LiveIn = Func->getLiveness()->getLiveIn(this); |
| 463 | if (VarNum < LiveIn.size()) { |
| 464 | assert(!LiveIn[VarNum]); |
| 465 | LiveIn[VarNum] = true; |
| 466 | } |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 467 | Phi->setDeleted(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 470 | if (Desc.empty()) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 471 | return; |
| 472 | |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 473 | TargetLowering *Target = Func->getTarget(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 474 | SizeT InEdgeIndex = 0; |
| 475 | for (CfgNode *Pred : InEdges) { |
| 476 | CfgNode *Split = splitIncomingEdge(Pred, InEdgeIndex++); |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 477 | SizeT Remaining = Desc.size(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 478 | |
| 479 | // First pass computes Src and initializes NumPred. |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 480 | for (PhiDesc &Item : Desc) { |
| 481 | Variable *Dest = Item.Dest; |
| 482 | Operand *Src = Item.Phi->getOperandForTarget(Pred); |
| 483 | Item.Src = Src; |
| 484 | Item.Processed = false; |
| 485 | Item.NumPred = 0; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 486 | // Cherry-pick any trivial assignments, so that they don't contribute to |
| 487 | // the running complexity of the topological sort. |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 488 | if (sameVarOrReg(Target, Dest, Src)) { |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 489 | Item.Processed = true; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 490 | --Remaining; |
| 491 | if (Dest != Src) |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 492 | // If Dest and Src are syntactically the same, don't bother adding |
| 493 | // the assignment, because in all respects it would be redundant, and |
| 494 | // if Dest/Src are on the stack, the target lowering may naively |
| 495 | // decide to lower it using a temporary register. |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 496 | Split->appendInst(InstAssign::create(Func, Dest, Src)); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 499 | // Second pass computes NumPred by comparing every pair of Phi instructions. |
| 500 | for (PhiDesc &Item : Desc) { |
| 501 | if (Item.Processed) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 502 | continue; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 503 | const Variable *Dest = Item.Dest; |
| 504 | for (PhiDesc &Item2 : Desc) { |
| 505 | if (Item2.Processed) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 506 | continue; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 507 | // There shouldn't be two different Phis with the same Dest variable or |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 508 | // register. |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 509 | assert((&Item == &Item2) || !sameVarOrReg(Target, Dest, Item2.Dest)); |
| 510 | if (sameVarOrReg(Target, Dest, Item2.Src)) |
| 511 | ++Item.NumPred; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
| 515 | // Another pass to compute initial Weight values. |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 516 | for (PhiDesc &Item : Desc) { |
| 517 | if (Item.Processed) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 518 | continue; |
| 519 | int32_t Weight = 0; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 520 | if (Item.NumPred == 0) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 521 | Weight += WeightNoPreds; |
Jim Stichnoth | bbd449d | 2016-03-30 15:30:30 -0700 | [diff] [blame] | 522 | if (Item.NumPred == 1) |
| 523 | Weight += WeightOnePred; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 524 | if (auto *Var = llvm::dyn_cast<Variable>(Item.Src)) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 525 | if (Var->hasReg()) |
| 526 | Weight += WeightSrcIsReg; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 527 | if (!Item.Dest->hasReg()) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 528 | Weight += WeightDestNotReg; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 529 | Item.Weight = Weight; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 532 | // Repeatedly choose and process the best candidate in the topological sort, |
| 533 | // until no candidates remain. This implementation is O(N^2) where N is the |
| 534 | // number of Phi instructions, but with a small constant factor compared to |
| 535 | // a likely implementation of O(N) topological sort. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 536 | for (; Remaining; --Remaining) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 537 | int32_t BestWeight = -1; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 538 | PhiDesc *BestItem = nullptr; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 539 | // Find the best candidate. |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 540 | for (PhiDesc &Item : Desc) { |
| 541 | if (Item.Processed) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 542 | continue; |
Jim Stichnoth | bbd449d | 2016-03-30 15:30:30 -0700 | [diff] [blame] | 543 | const int32_t Weight = Item.Weight; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 544 | if (Weight > BestWeight) { |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 545 | BestItem = &Item; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 546 | BestWeight = Weight; |
| 547 | } |
| 548 | } |
| 549 | assert(BestWeight >= 0); |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 550 | Variable *Dest = BestItem->Dest; |
| 551 | Operand *Src = BestItem->Src; |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 552 | assert(!sameVarOrReg(Target, Dest, Src)); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 553 | // Break a cycle by introducing a temporary. |
Jim Stichnoth | bbd449d | 2016-03-30 15:30:30 -0700 | [diff] [blame] | 554 | while (BestItem->NumPred > 0) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 555 | bool Found = false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 556 | // If the target instruction "A=B" is part of a cycle, find the "X=A" |
| 557 | // assignment in the cycle because it will have to be rewritten as |
| 558 | // "X=tmp". |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 559 | for (PhiDesc &Item : Desc) { |
| 560 | if (Item.Processed) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 561 | continue; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 562 | Operand *OtherSrc = Item.Src; |
| 563 | if (Item.NumPred && sameVarOrReg(Target, Dest, OtherSrc)) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 564 | SizeT VarNum = Func->getNumVariables(); |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 565 | Variable *Tmp = Func->makeVariable(OtherSrc->getType()); |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 566 | if (BuildDefs::dump()) |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 567 | Tmp->setName(Func, "__split_" + std::to_string(VarNum)); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 568 | Split->appendInst(InstAssign::create(Func, Tmp, OtherSrc)); |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 569 | Item.Src = Tmp; |
| 570 | updatePreds(Desc, Target, llvm::cast<Variable>(OtherSrc)); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 571 | Found = true; |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 572 | break; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | assert(Found); |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 576 | (void)Found; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 577 | } |
| 578 | // Now that a cycle (if any) has been broken, create the actual |
| 579 | // assignment. |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 580 | Split->appendInst(InstAssign::create(Func, Dest, Src)); |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 581 | if (auto *Var = llvm::dyn_cast<Variable>(Src)) |
| 582 | updatePreds(Desc, Target, Var); |
| 583 | BestItem->Processed = true; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 584 | } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 585 | Split->appendInst(InstBr::create(Func, this)); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 586 | |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 587 | Split->genCode(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 588 | Func->getVMetadata()->addNode(Split); |
Jim Stichnoth | ea15bbe | 2015-11-09 11:19:11 -0800 | [diff] [blame] | 589 | // Validate to be safe. All items should be marked as processed, and have |
| 590 | // no predecessors. |
| 591 | if (BuildDefs::asserts()) { |
| 592 | for (PhiDesc &Item : Desc) { |
| 593 | (void)Item; |
| 594 | assert(Item.Processed); |
| 595 | assert(Item.NumPred == 0); |
| 596 | } |
| 597 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 598 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 601 | // Does address mode optimization. Pass each instruction to the TargetLowering |
| 602 | // object. If it returns a new instruction (representing the optimized address |
| 603 | // mode), then insert the new instruction and delete the old. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 604 | void CfgNode::doAddressOpt() { |
| 605 | TargetLowering *Target = Func->getTarget(); |
| 606 | LoweringContext &Context = Target->getContext(); |
| 607 | Context.init(this); |
| 608 | while (!Context.atEnd()) { |
| 609 | Target->doAddressOpt(); |
| 610 | } |
| 611 | } |
| 612 | |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 613 | void CfgNode::doNopInsertion(RandomNumberGenerator &RNG) { |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 614 | TargetLowering *Target = Func->getTarget(); |
| 615 | LoweringContext &Context = Target->getContext(); |
| 616 | Context.init(this); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 617 | Context.setInsertPoint(Context.getCur()); |
| 618 | // Do not insert nop in bundle locked instructions. |
| 619 | bool PauseNopInsertion = false; |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 620 | while (!Context.atEnd()) { |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 621 | if (llvm::isa<InstBundleLock>(Context.getCur())) { |
| 622 | PauseNopInsertion = true; |
| 623 | } else if (llvm::isa<InstBundleUnlock>(Context.getCur())) { |
| 624 | PauseNopInsertion = false; |
| 625 | } |
| 626 | if (!PauseNopInsertion) |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 627 | Target->doNopInsertion(RNG); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 628 | // Ensure Cur=Next, so that the nops are inserted before the current |
| 629 | // instruction rather than after. |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 630 | Context.advanceCur(); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 631 | Context.advanceNext(); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 632 | } |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 635 | // Drives the target lowering. Passes the current instruction and the next |
| 636 | // non-deleted instruction for target lowering. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 637 | void CfgNode::genCode() { |
| 638 | TargetLowering *Target = Func->getTarget(); |
| 639 | LoweringContext &Context = Target->getContext(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 640 | // Lower the regular instructions. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 641 | Context.init(this); |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 642 | Target->initNodeForLowering(this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 643 | while (!Context.atEnd()) { |
| 644 | InstList::iterator Orig = Context.getCur(); |
| 645 | if (llvm::isa<InstRet>(*Orig)) |
| 646 | setHasReturn(); |
| 647 | Target->lower(); |
| 648 | // Ensure target lowering actually moved the cursor. |
| 649 | assert(Context.getCur() != Orig); |
| 650 | } |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 651 | Context.availabilityReset(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 652 | // Do preliminary lowering of the Phi instructions. |
| 653 | Target->prelowerPhis(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 656 | void CfgNode::livenessLightweight() { |
| 657 | SizeT NumVars = Func->getNumVariables(); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 658 | LivenessBV Live(NumVars); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 659 | // Process regular instructions in reverse order. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 660 | for (Inst &I : reverse_range(Insts)) { |
| 661 | if (I.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 662 | continue; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 663 | I.livenessLightweight(Func, Live); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 664 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 665 | for (Inst &I : Phis) { |
| 666 | if (I.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 667 | continue; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 668 | I.livenessLightweight(Func, Live); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 672 | // Performs liveness analysis on the block. Returns true if the incoming |
| 673 | // liveness changed from before, false if it stayed the same. (If it changes, |
| 674 | // the node's predecessors need to be processed again.) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 675 | bool CfgNode::liveness(Liveness *Liveness) { |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 676 | const SizeT NumVars = Liveness->getNumVarsInNode(this); |
| 677 | const SizeT NumGlobalVars = Liveness->getNumGlobalVars(); |
| 678 | LivenessBV &Live = Liveness->getScratchBV(); |
| 679 | Live.clear(); |
| 680 | |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 681 | LiveBeginEndMap *LiveBegin = nullptr; |
| 682 | LiveBeginEndMap *LiveEnd = nullptr; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 683 | // Mark the beginning and ending of each variable's live range with the |
| 684 | // sentinel instruction number 0. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 685 | if (Liveness->getMode() == Liveness_Intervals) { |
| 686 | LiveBegin = Liveness->getLiveBegin(this); |
| 687 | LiveEnd = Liveness->getLiveEnd(this); |
| 688 | LiveBegin->clear(); |
| 689 | LiveEnd->clear(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 690 | // Guess that the number of live ranges beginning is roughly the number of |
| 691 | // instructions, and same for live ranges ending. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 692 | LiveBegin->reserve(getInstCountEstimate()); |
| 693 | LiveEnd->reserve(getInstCountEstimate()); |
| 694 | } |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 695 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 696 | // Initialize Live to be the union of all successors' LiveIn. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 697 | for (CfgNode *Succ : OutEdges) { |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 698 | const LivenessBV &LiveIn = Liveness->getLiveIn(Succ); |
| 699 | assert(LiveIn.empty() || LiveIn.size() == NumGlobalVars); |
| 700 | Live |= LiveIn; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 701 | // Mark corresponding argument of phis in successor as live. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 702 | for (Inst &I : Succ->Phis) { |
Jim Stichnoth | 552490c | 2015-08-05 16:21:42 -0700 | [diff] [blame] | 703 | if (I.isDeleted()) |
| 704 | continue; |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 705 | auto *Phi = llvm::cast<InstPhi>(&I); |
Jim Stichnoth | 1502e59 | 2014-12-11 09:22:45 -0800 | [diff] [blame] | 706 | Phi->livenessPhiOperand(Live, this, Liveness); |
| 707 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 708 | } |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 709 | assert(Live.empty() || Live.size() == NumGlobalVars); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 710 | Liveness->getLiveOut(this) = Live; |
| 711 | |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 712 | // Expand Live so it can hold locals in addition to globals. |
| 713 | Live.resize(NumVars); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 714 | // Process regular instructions in reverse order. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 715 | for (Inst &I : reverse_range(Insts)) { |
| 716 | if (I.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 717 | continue; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 718 | I.liveness(I.getNumber(), Live, Liveness, LiveBegin, LiveEnd); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 719 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 720 | // Process phis in forward order so that we can override the instruction |
| 721 | // number to be that of the earliest phi instruction in the block. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 722 | SizeT NumNonDeadPhis = 0; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 723 | InstNumberT FirstPhiNumber = Inst::NumberSentinel; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 724 | for (Inst &I : Phis) { |
| 725 | if (I.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 726 | continue; |
| 727 | if (FirstPhiNumber == Inst::NumberSentinel) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 728 | FirstPhiNumber = I.getNumber(); |
| 729 | if (I.liveness(FirstPhiNumber, Live, Liveness, LiveBegin, LiveEnd)) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 730 | ++NumNonDeadPhis; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 733 | // When using the sparse representation, after traversing the instructions in |
| 734 | // the block, the Live bitvector should only contain set bits for global |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 735 | // variables upon block entry. We validate this by testing the upper bits of |
| 736 | // the Live bitvector. |
| 737 | if (Live.find_next(NumGlobalVars) != -1) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 738 | if (BuildDefs::dump()) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 739 | // This is a fatal liveness consistency error. Print some diagnostics and |
| 740 | // abort. |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 741 | Ostream &Str = Func->getContext()->getStrDump(); |
| 742 | Func->resetCurrentNode(); |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 743 | Str << "Invalid Live ="; |
| 744 | for (SizeT i = NumGlobalVars; i < Live.size(); ++i) { |
| 745 | if (Live.test(i)) { |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 746 | Str << " "; |
| 747 | Liveness->getVariable(i, this)->dump(Func); |
| 748 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 749 | } |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 750 | Str << "\n"; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 751 | } |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 752 | llvm::report_fatal_error("Fatal inconsistency in liveness analysis"); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 753 | } |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 754 | // Now truncate Live to prevent LiveIn from growing. |
| 755 | Live.resize(NumGlobalVars); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 756 | |
| 757 | bool Changed = false; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 758 | LivenessBV &LiveIn = Liveness->getLiveIn(this); |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 759 | assert(LiveIn.empty() || LiveIn.size() == NumGlobalVars); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 760 | // Add in current LiveIn |
| 761 | Live |= LiveIn; |
| 762 | // Check result, set LiveIn=Live |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 763 | SizeT &PrevNumNonDeadPhis = Liveness->getNumNonDeadPhis(this); |
| 764 | bool LiveInChanged = (Live != LiveIn); |
| 765 | Changed = (NumNonDeadPhis != PrevNumNonDeadPhis || LiveInChanged); |
| 766 | if (LiveInChanged) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 767 | LiveIn = Live; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 768 | PrevNumNonDeadPhis = NumNonDeadPhis; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 769 | return Changed; |
| 770 | } |
| 771 | |
Jim Stichnoth | 230d4101 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 772 | // Validate the integrity of the live ranges in this block. If there are any |
| 773 | // errors, it prints details and returns false. On success, it returns true. |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 774 | bool CfgNode::livenessValidateIntervals(Liveness *Liveness) const { |
Jim Stichnoth | 230d4101 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 775 | if (!BuildDefs::asserts()) |
| 776 | return true; |
| 777 | |
| 778 | // Verify there are no duplicates. |
| 779 | auto ComparePair = |
| 780 | [](const LiveBeginEndMapEntry &A, const LiveBeginEndMapEntry &B) { |
| 781 | return A.first == B.first; |
| 782 | }; |
| 783 | LiveBeginEndMap &MapBegin = *Liveness->getLiveBegin(this); |
| 784 | LiveBeginEndMap &MapEnd = *Liveness->getLiveEnd(this); |
| 785 | if (std::adjacent_find(MapBegin.begin(), MapBegin.end(), ComparePair) == |
| 786 | MapBegin.end() && |
| 787 | std::adjacent_find(MapEnd.begin(), MapEnd.end(), ComparePair) == |
| 788 | MapEnd.end()) |
| 789 | return true; |
| 790 | |
| 791 | // There is definitely a liveness error. All paths from here return false. |
| 792 | if (!BuildDefs::dump()) |
| 793 | return false; |
| 794 | |
| 795 | // Print all the errors. |
| 796 | if (BuildDefs::dump()) { |
| 797 | GlobalContext *Ctx = Func->getContext(); |
| 798 | OstreamLocker L(Ctx); |
| 799 | Ostream &Str = Ctx->getStrDump(); |
| 800 | if (Func->isVerbose()) { |
| 801 | Str << "Live range errors in the following block:\n"; |
| 802 | dump(Func); |
| 803 | } |
| 804 | for (auto Start = MapBegin.begin(); |
| 805 | (Start = std::adjacent_find(Start, MapBegin.end(), ComparePair)) != |
| 806 | MapBegin.end(); |
| 807 | ++Start) { |
| 808 | auto Next = Start + 1; |
| 809 | Str << "Duplicate LR begin, block " << getName() << ", instructions " |
| 810 | << Start->second << " & " << Next->second << ", variable " |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 811 | << Liveness->getVariable(Start->first, this)->getName() << "\n"; |
Jim Stichnoth | 230d4101 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 812 | } |
| 813 | for (auto Start = MapEnd.begin(); |
| 814 | (Start = std::adjacent_find(Start, MapEnd.end(), ComparePair)) != |
| 815 | MapEnd.end(); |
| 816 | ++Start) { |
| 817 | auto Next = Start + 1; |
| 818 | Str << "Duplicate LR end, block " << getName() << ", instructions " |
| 819 | << Start->second << " & " << Next->second << ", variable " |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 820 | << Liveness->getVariable(Start->first, this)->getName() << "\n"; |
Jim Stichnoth | 230d4101 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
| 824 | return false; |
| 825 | } |
| 826 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 827 | // Once basic liveness is complete, compute actual live ranges. It is assumed |
| 828 | // that within a single basic block, a live range begins at most once and ends |
| 829 | // at most once. This is certainly true for pure SSA form. It is also true once |
| 830 | // phis are lowered, since each assignment to the phi-based temporary is in a |
| 831 | // different basic block, and there is a single read that ends the live in the |
| 832 | // basic block that contained the actual phi instruction. |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 833 | void CfgNode::livenessAddIntervals(Liveness *Liveness, InstNumberT FirstInstNum, |
| 834 | InstNumberT LastInstNum) { |
| 835 | TimerMarker T1(TimerStack::TT_liveRange, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 836 | |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 837 | const SizeT NumVars = Liveness->getNumVarsInNode(this); |
| 838 | const LivenessBV &LiveIn = Liveness->getLiveIn(this); |
| 839 | const LivenessBV &LiveOut = Liveness->getLiveOut(this); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 840 | LiveBeginEndMap &MapBegin = *Liveness->getLiveBegin(this); |
| 841 | LiveBeginEndMap &MapEnd = *Liveness->getLiveEnd(this); |
| 842 | std::sort(MapBegin.begin(), MapBegin.end()); |
| 843 | std::sort(MapEnd.begin(), MapEnd.end()); |
Jim Stichnoth | 230d4101 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 844 | |
| 845 | if (!livenessValidateIntervals(Liveness)) { |
| 846 | llvm::report_fatal_error("livenessAddIntervals: Liveness error"); |
| 847 | return; |
| 848 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 849 | |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 850 | LivenessBV &LiveInAndOut = Liveness->getScratchBV(); |
| 851 | LiveInAndOut = LiveIn; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 852 | LiveInAndOut &= LiveOut; |
| 853 | |
| 854 | // Iterate in parallel across the sorted MapBegin[] and MapEnd[]. |
| 855 | auto IBB = MapBegin.begin(), IEB = MapEnd.begin(); |
| 856 | auto IBE = MapBegin.end(), IEE = MapEnd.end(); |
| 857 | while (IBB != IBE || IEB != IEE) { |
| 858 | SizeT i1 = IBB == IBE ? NumVars : IBB->first; |
| 859 | SizeT i2 = IEB == IEE ? NumVars : IEB->first; |
| 860 | SizeT i = std::min(i1, i2); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 861 | // i1 is the Variable number of the next MapBegin entry, and i2 is the |
| 862 | // Variable number of the next MapEnd entry. If i1==i2, then the Variable's |
| 863 | // live range begins and ends in this block. If i1<i2, then i1's live range |
| 864 | // begins at instruction IBB->second and extends through the end of the |
| 865 | // block. If i1>i2, then i2's live range begins at the first instruction of |
| 866 | // the block and ends at IEB->second. In any case, we choose the lesser of |
| 867 | // i1 and i2 and proceed accordingly. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 868 | InstNumberT LB = i == i1 ? IBB->second : FirstInstNum; |
| 869 | InstNumberT LE = i == i2 ? IEB->second : LastInstNum + 1; |
| 870 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 871 | Variable *Var = Liveness->getVariable(i, this); |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 872 | if (LB > LE) { |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 873 | Var->addLiveRange(FirstInstNum, LE, this); |
| 874 | Var->addLiveRange(LB, LastInstNum + 1, this); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 875 | // Assert that Var is a global variable by checking that its liveness |
| 876 | // index is less than the number of globals. This ensures that the |
| 877 | // LiveInAndOut[] access is valid. |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 878 | assert(i < Liveness->getNumGlobalVars()); |
| 879 | LiveInAndOut[i] = false; |
| 880 | } else { |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 881 | Var->addLiveRange(LB, LE, this); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 882 | } |
| 883 | if (i == i1) |
| 884 | ++IBB; |
| 885 | if (i == i2) |
| 886 | ++IEB; |
| 887 | } |
| 888 | // Process the variables that are live across the entire block. |
| 889 | for (int i = LiveInAndOut.find_first(); i != -1; |
| 890 | i = LiveInAndOut.find_next(i)) { |
| 891 | Variable *Var = Liveness->getVariable(i, this); |
Jim Stichnoth | 552490c | 2015-08-05 16:21:42 -0700 | [diff] [blame] | 892 | if (Liveness->getRangeMask(Var->getIndex())) |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 893 | Var->addLiveRange(FirstInstNum, LastInstNum + 1, this); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 897 | // If this node contains only deleted instructions, and ends in an |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 898 | // unconditional branch, contract the node by repointing all its in-edges to |
| 899 | // its successor. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 900 | void CfgNode::contractIfEmpty() { |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 901 | if (InEdges.empty()) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 902 | return; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 903 | Inst *Branch = nullptr; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 904 | for (Inst &I : Insts) { |
| 905 | if (I.isDeleted()) |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 906 | continue; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 907 | if (I.isUnconditionalBranch()) |
| 908 | Branch = &I; |
| 909 | else if (!I.isRedundantAssign()) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 910 | return; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 911 | } |
Jim Stichnoth | 6f7ad6c | 2016-01-15 09:52:54 -0800 | [diff] [blame] | 912 | // Make sure there is actually a successor to repoint in-edges to. |
| 913 | if (OutEdges.empty()) |
| 914 | return; |
Eric Holk | 16f8061 | 2016-04-04 17:07:42 -0700 | [diff] [blame] | 915 | assert(hasSingleOutEdge()); |
Jim Stichnoth | 1921fba | 2015-09-15 10:06:30 -0700 | [diff] [blame] | 916 | // Don't try to delete a self-loop. |
| 917 | if (OutEdges[0] == this) |
| 918 | return; |
Jim Stichnoth | 6f7ad6c | 2016-01-15 09:52:54 -0800 | [diff] [blame] | 919 | // Make sure the node actually contains (ends with) an unconditional branch. |
| 920 | if (Branch == nullptr) |
| 921 | return; |
Jim Stichnoth | 1921fba | 2015-09-15 10:06:30 -0700 | [diff] [blame] | 922 | |
| 923 | Branch->setDeleted(); |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 924 | CfgNode *Successor = OutEdges.front(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 925 | // Repoint all this node's in-edges to this node's successor, unless this |
| 926 | // node's successor is actually itself (in which case the statement |
| 927 | // "OutEdges.front()->InEdges.push_back(Pred)" could invalidate the iterator |
| 928 | // over this->InEdges). |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 929 | if (Successor != this) { |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 930 | for (CfgNode *Pred : InEdges) { |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 931 | for (CfgNode *&I : Pred->OutEdges) { |
| 932 | if (I == this) { |
| 933 | I = Successor; |
| 934 | Successor->InEdges.push_back(Pred); |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 935 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 936 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 937 | for (Inst &I : Pred->getInsts()) { |
| 938 | if (!I.isDeleted()) |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 939 | I.repointEdges(this, Successor); |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 940 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 941 | } |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 942 | |
| 943 | // Remove the in-edge to the successor to allow node reordering to make |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 944 | // better decisions. For example it's more helpful to place a node after a |
| 945 | // reachable predecessor than an unreachable one (like the one we just |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 946 | // contracted). |
| 947 | Successor->InEdges.erase( |
| 948 | std::find(Successor->InEdges.begin(), Successor->InEdges.end(), this)); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 949 | } |
| 950 | InEdges.clear(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 953 | void CfgNode::doBranchOpt(const CfgNode *NextNode) { |
| 954 | TargetLowering *Target = Func->getTarget(); |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 955 | // Find the first opportunity for branch optimization (which will be the last |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 956 | // instruction in the block) and stop. This is sufficient unless there is |
| 957 | // some target lowering where we have the possibility of multiple |
| 958 | // optimizations per block. Take care with switch lowering as there are |
| 959 | // multiple unconditional branches and only the last can be deleted. |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 960 | for (Inst &I : reverse_range(Insts)) { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 961 | if (!I.isDeleted()) { |
| 962 | Target->doBranchOpt(&I, NextNode); |
Andrew Scull | 713278a | 2015-07-22 17:17:02 -0700 | [diff] [blame] | 963 | return; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 968 | // ======================== Dump routines ======================== // |
| 969 | |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 970 | namespace { |
| 971 | |
| 972 | // Helper functions for emit(). |
| 973 | |
| 974 | void emitRegisterUsage(Ostream &Str, const Cfg *Func, const CfgNode *Node, |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 975 | bool IsLiveIn, CfgVector<SizeT> &LiveRegCount) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 976 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 977 | return; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 978 | Liveness *Liveness = Func->getLiveness(); |
| 979 | const LivenessBV *Live; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 980 | const auto StackReg = Func->getTarget()->getStackReg(); |
| 981 | const auto FrameOrStackReg = Func->getTarget()->getFrameOrStackReg(); |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 982 | if (IsLiveIn) { |
| 983 | Live = &Liveness->getLiveIn(Node); |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 984 | Str << "\t\t\t\t/* LiveIn="; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 985 | } else { |
| 986 | Live = &Liveness->getLiveOut(Node); |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 987 | Str << "\t\t\t\t/* LiveOut="; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 988 | } |
| 989 | if (!Live->empty()) { |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 990 | CfgVector<Variable *> LiveRegs; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 991 | for (SizeT i = 0; i < Live->size(); ++i) { |
Jim Stichnoth | e741871 | 2015-10-09 06:54:02 -0700 | [diff] [blame] | 992 | if (!(*Live)[i]) |
| 993 | continue; |
| 994 | Variable *Var = Liveness->getVariable(i, Node); |
| 995 | if (!Var->hasReg()) |
| 996 | continue; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 997 | const auto RegNum = Var->getRegNum(); |
Jim Stichnoth | e741871 | 2015-10-09 06:54:02 -0700 | [diff] [blame] | 998 | if (RegNum == StackReg || RegNum == FrameOrStackReg) |
| 999 | continue; |
| 1000 | if (IsLiveIn) |
| 1001 | ++LiveRegCount[RegNum]; |
| 1002 | LiveRegs.push_back(Var); |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1003 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1004 | // Sort the variables by regnum so they are always printed in a familiar |
| 1005 | // order. |
Jim Stichnoth | 9a05aea | 2015-05-26 16:01:23 -0700 | [diff] [blame] | 1006 | std::sort(LiveRegs.begin(), LiveRegs.end(), |
| 1007 | [](const Variable *V1, const Variable *V2) { |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 1008 | return unsigned(V1->getRegNum()) < unsigned(V2->getRegNum()); |
Jim Stichnoth | 8e6bf6e | 2015-06-03 15:58:12 -0700 | [diff] [blame] | 1009 | }); |
Jim Stichnoth | 9a05aea | 2015-05-26 16:01:23 -0700 | [diff] [blame] | 1010 | bool First = true; |
| 1011 | for (Variable *Var : LiveRegs) { |
| 1012 | if (!First) |
| 1013 | Str << ","; |
| 1014 | First = false; |
| 1015 | Var->emit(Func); |
| 1016 | } |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1017 | } |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 1018 | Str << " */\n"; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1021 | /// Returns true if some text was emitted - in which case the caller definitely |
| 1022 | /// needs to emit a newline character. |
| 1023 | bool emitLiveRangesEnded(Ostream &Str, const Cfg *Func, const Inst *Instr, |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 1024 | CfgVector<SizeT> &LiveRegCount) { |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1025 | bool Printed = false; |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1026 | if (!BuildDefs::dump()) |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1027 | return Printed; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1028 | Variable *Dest = Instr->getDest(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1029 | // Normally we increment the live count for the dest register. But we |
Jim Stichnoth | 230d4101 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 1030 | // shouldn't if the instruction's IsDestRedefined flag is set, because this |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1031 | // means that the target lowering created this instruction as a non-SSA |
| 1032 | // assignment; i.e., a different, previous instruction started the dest |
| 1033 | // variable's live range. |
Jim Stichnoth | 230d4101 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 1034 | if (!Instr->isDestRedefined() && Dest && Dest->hasReg()) |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1035 | ++LiveRegCount[Dest->getRegNum()]; |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 1036 | FOREACH_VAR_IN_INST(Var, *Instr) { |
| 1037 | bool ShouldReport = Instr->isLastUse(Var); |
| 1038 | if (ShouldReport && Var->hasReg()) { |
| 1039 | // Don't report end of live range until the live count reaches 0. |
| 1040 | SizeT NewCount = --LiveRegCount[Var->getRegNum()]; |
| 1041 | if (NewCount) |
| 1042 | ShouldReport = false; |
| 1043 | } |
| 1044 | if (ShouldReport) { |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1045 | if (Printed) |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 1046 | Str << ","; |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1047 | else |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 1048 | Str << " \t/* END="; |
John Porto | ec3f565 | 2015-08-31 15:07:09 -0700 | [diff] [blame] | 1049 | Var->emit(Func); |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1050 | Printed = true; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 1053 | if (Printed) |
| 1054 | Str << " */"; |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1055 | return Printed; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1058 | void updateStats(Cfg *Func, const Inst *I) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1059 | if (!BuildDefs::dump()) |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1060 | return; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1061 | // Update emitted instruction count, plus fill/spill count for Variable |
| 1062 | // operands without a physical register. |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1063 | if (uint32_t Count = I->getEmitInstCount()) { |
| 1064 | Func->getContext()->statsUpdateEmitted(Count); |
| 1065 | if (Variable *Dest = I->getDest()) { |
| 1066 | if (!Dest->hasReg()) |
| 1067 | Func->getContext()->statsUpdateFills(); |
| 1068 | } |
| 1069 | for (SizeT S = 0; S < I->getSrcSize(); ++S) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 1070 | if (auto *Src = llvm::dyn_cast<Variable>(I->getSrc(S))) { |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1071 | if (!Src->hasReg()) |
| 1072 | Func->getContext()->statsUpdateSpills(); |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1078 | } // end of anonymous namespace |
| 1079 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1080 | void CfgNode::emit(Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1081 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 1082 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1083 | Func->setCurrentNode(this); |
| 1084 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1085 | Liveness *Liveness = Func->getLiveness(); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1086 | const bool DecorateAsm = Liveness && getFlags().getDecorateAsm(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1087 | Str << getAsmName() << ":\n"; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1088 | // LiveRegCount keeps track of the number of currently live variables that |
| 1089 | // each register is assigned to. Normally that would be only 0 or 1, but the |
| 1090 | // register allocator's AllowOverlap inference allows it to be greater than 1 |
| 1091 | // for short periods. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 1092 | CfgVector<SizeT> LiveRegCount(Func->getTarget()->getNumRegisters()); |
Jim Stichnoth | 4175b2a | 2015-04-30 12:26:22 -0700 | [diff] [blame] | 1093 | if (DecorateAsm) { |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 1094 | constexpr bool IsLiveIn = true; |
Jim Stichnoth | 4175b2a | 2015-04-30 12:26:22 -0700 | [diff] [blame] | 1095 | emitRegisterUsage(Str, Func, this, IsLiveIn, LiveRegCount); |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 1096 | if (getInEdges().size()) { |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 1097 | Str << "\t\t\t\t/* preds="; |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 1098 | bool First = true; |
| 1099 | for (CfgNode *I : getInEdges()) { |
| 1100 | if (!First) |
| 1101 | Str << ","; |
| 1102 | First = false; |
Jim Stichnoth | 9a63bab | 2015-10-05 11:13:59 -0700 | [diff] [blame] | 1103 | Str << "$" << I->getName(); |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 1104 | } |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 1105 | Str << " */\n"; |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 1106 | } |
| 1107 | if (getLoopNestDepth()) { |
Jim Stichnoth | 751e27e | 2015-12-16 12:40:31 -0800 | [diff] [blame] | 1108 | Str << "\t\t\t\t/* loop depth=" << getLoopNestDepth() << " */\n"; |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 1109 | } |
Jim Stichnoth | 4175b2a | 2015-04-30 12:26:22 -0700 | [diff] [blame] | 1110 | } |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1111 | |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1112 | for (const Inst &I : Phis) { |
| 1113 | if (I.isDeleted()) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1114 | continue; |
| 1115 | // Emitting a Phi instruction should cause an error. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1116 | I.emit(Func); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1117 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1118 | for (const Inst &I : Insts) { |
| 1119 | if (I.isDeleted()) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1120 | continue; |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 1121 | if (I.isRedundantAssign()) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1122 | // Usually, redundant assignments end the live range of the src variable |
| 1123 | // and begin the live range of the dest variable, with no net effect on |
| 1124 | // the liveness of their register. However, if the register allocator |
| 1125 | // infers the AllowOverlap condition, then this may be a redundant |
| 1126 | // assignment that does not end the src variable's live range, in which |
| 1127 | // case the active variable count for that register needs to be bumped. |
| 1128 | // That normally would have happened as part of emitLiveRangesEnded(), |
| 1129 | // but that isn't called for redundant assignments. |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 1130 | Variable *Dest = I.getDest(); |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 1131 | if (DecorateAsm && Dest->hasReg()) { |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 1132 | ++LiveRegCount[Dest->getRegNum()]; |
Jim Stichnoth | 1fb030c | 2015-10-15 11:10:38 -0700 | [diff] [blame] | 1133 | if (I.isLastUse(I.getSrc(0))) |
| 1134 | --LiveRegCount[llvm::cast<Variable>(I.getSrc(0))->getRegNum()]; |
| 1135 | } |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 1136 | continue; |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 1137 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1138 | I.emit(Func); |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1139 | bool Printed = false; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1140 | if (DecorateAsm) |
Jim Stichnoth | 3e859b7 | 2015-11-10 14:39:51 -0800 | [diff] [blame] | 1141 | Printed = emitLiveRangesEnded(Str, Func, &I, LiveRegCount); |
| 1142 | if (Printed || llvm::isa<InstTarget>(&I)) |
| 1143 | Str << "\n"; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1144 | updateStats(Func, &I); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1145 | } |
Jim Stichnoth | 4175b2a | 2015-04-30 12:26:22 -0700 | [diff] [blame] | 1146 | if (DecorateAsm) { |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 1147 | constexpr bool IsLiveIn = false; |
Jim Stichnoth | 4175b2a | 2015-04-30 12:26:22 -0700 | [diff] [blame] | 1148 | emitRegisterUsage(Str, Func, this, IsLiveIn, LiveRegCount); |
| 1149 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1152 | // Helper class for emitIAS(). |
| 1153 | namespace { |
| 1154 | class BundleEmitHelper { |
| 1155 | BundleEmitHelper() = delete; |
| 1156 | BundleEmitHelper(const BundleEmitHelper &) = delete; |
| 1157 | BundleEmitHelper &operator=(const BundleEmitHelper &) = delete; |
| 1158 | |
| 1159 | public: |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1160 | BundleEmitHelper(Assembler *Asm, const InstList &Insts) |
| 1161 | : Asm(Asm), End(Insts.end()), BundleLockStart(End), |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1162 | BundleSize(1 << Asm->getBundleAlignLog2Bytes()), |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1163 | BundleMaskLo(BundleSize - 1), BundleMaskHi(~BundleMaskLo) {} |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1164 | // Check whether we're currently within a bundle_lock region. |
| 1165 | bool isInBundleLockRegion() const { return BundleLockStart != End; } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1166 | // Check whether the current bundle_lock region has the align_to_end option. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1167 | bool isAlignToEnd() const { |
| 1168 | assert(isInBundleLockRegion()); |
| 1169 | return llvm::cast<InstBundleLock>(getBundleLockStart())->getOption() == |
| 1170 | InstBundleLock::Opt_AlignToEnd; |
| 1171 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1172 | bool isPadToEnd() const { |
| 1173 | assert(isInBundleLockRegion()); |
| 1174 | return llvm::cast<InstBundleLock>(getBundleLockStart())->getOption() == |
| 1175 | InstBundleLock::Opt_PadToEnd; |
| 1176 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1177 | // Check whether the entire bundle_lock region falls within the same bundle. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1178 | bool isSameBundle() const { |
| 1179 | assert(isInBundleLockRegion()); |
| 1180 | return SizeSnapshotPre == SizeSnapshotPost || |
| 1181 | (SizeSnapshotPre & BundleMaskHi) == |
| 1182 | ((SizeSnapshotPost - 1) & BundleMaskHi); |
| 1183 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1184 | // Get the bundle alignment of the first instruction of the bundle_lock |
| 1185 | // region. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1186 | intptr_t getPreAlignment() const { |
| 1187 | assert(isInBundleLockRegion()); |
| 1188 | return SizeSnapshotPre & BundleMaskLo; |
| 1189 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1190 | // Get the bundle alignment of the first instruction past the bundle_lock |
| 1191 | // region. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1192 | intptr_t getPostAlignment() const { |
| 1193 | assert(isInBundleLockRegion()); |
| 1194 | return SizeSnapshotPost & BundleMaskLo; |
| 1195 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1196 | // Get the iterator pointing to the bundle_lock instruction, e.g. to roll |
| 1197 | // back the instruction iteration to that point. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1198 | InstList::const_iterator getBundleLockStart() const { |
| 1199 | assert(isInBundleLockRegion()); |
| 1200 | return BundleLockStart; |
| 1201 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1202 | // Set up bookkeeping when the bundle_lock instruction is first processed. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1203 | void enterBundleLock(InstList::const_iterator I) { |
| 1204 | assert(!isInBundleLockRegion()); |
| 1205 | BundleLockStart = I; |
| 1206 | SizeSnapshotPre = Asm->getBufferSize(); |
| 1207 | Asm->setPreliminary(true); |
| 1208 | assert(isInBundleLockRegion()); |
| 1209 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1210 | // Update bookkeeping when the bundle_unlock instruction is processed. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1211 | void enterBundleUnlock() { |
| 1212 | assert(isInBundleLockRegion()); |
| 1213 | SizeSnapshotPost = Asm->getBufferSize(); |
| 1214 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1215 | // Update bookkeeping when we are completely finished with the bundle_lock |
| 1216 | // region. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1217 | void leaveBundleLockRegion() { BundleLockStart = End; } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1218 | // Check whether the instruction sequence fits within the current bundle, and |
| 1219 | // if not, add nop padding to the end of the current bundle. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1220 | void padToNextBundle() { |
| 1221 | assert(isInBundleLockRegion()); |
| 1222 | if (!isSameBundle()) { |
| 1223 | intptr_t PadToNextBundle = BundleSize - getPreAlignment(); |
| 1224 | Asm->padWithNop(PadToNextBundle); |
| 1225 | SizeSnapshotPre += PadToNextBundle; |
| 1226 | SizeSnapshotPost += PadToNextBundle; |
| 1227 | assert((Asm->getBufferSize() & BundleMaskLo) == 0); |
| 1228 | assert(Asm->getBufferSize() == SizeSnapshotPre); |
| 1229 | } |
| 1230 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1231 | // If align_to_end is specified, add padding such that the instruction |
| 1232 | // sequences ends precisely at a bundle boundary. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1233 | void padForAlignToEnd() { |
| 1234 | assert(isInBundleLockRegion()); |
| 1235 | if (isAlignToEnd()) { |
| 1236 | if (intptr_t Offset = getPostAlignment()) { |
| 1237 | Asm->padWithNop(BundleSize - Offset); |
| 1238 | SizeSnapshotPre = Asm->getBufferSize(); |
| 1239 | } |
| 1240 | } |
| 1241 | } |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1242 | // If pad_to_end is specified, add padding such that the first instruction |
| 1243 | // after the instruction sequence starts at a bundle boundary. |
| 1244 | void padForPadToEnd() { |
| 1245 | assert(isInBundleLockRegion()); |
| 1246 | if (isPadToEnd()) { |
| 1247 | if (intptr_t Offset = getPostAlignment()) { |
| 1248 | Asm->padWithNop(BundleSize - Offset); |
| 1249 | SizeSnapshotPre = Asm->getBufferSize(); |
| 1250 | } |
| 1251 | } |
| 1252 | } // Update bookkeeping when rolling back for the second pass. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1253 | void rollback() { |
| 1254 | assert(isInBundleLockRegion()); |
| 1255 | Asm->setBufferSize(SizeSnapshotPre); |
| 1256 | Asm->setPreliminary(false); |
| 1257 | } |
| 1258 | |
| 1259 | private: |
| 1260 | Assembler *const Asm; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1261 | // End is a sentinel value such that BundleLockStart==End implies that we are |
| 1262 | // not in a bundle_lock region. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1263 | const InstList::const_iterator End; |
| 1264 | InstList::const_iterator BundleLockStart; |
| 1265 | const intptr_t BundleSize; |
| 1266 | // Masking with BundleMaskLo identifies an address's bundle offset. |
| 1267 | const intptr_t BundleMaskLo; |
| 1268 | // Masking with BundleMaskHi identifies an address's bundle. |
| 1269 | const intptr_t BundleMaskHi; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1270 | intptr_t SizeSnapshotPre = 0; |
| 1271 | intptr_t SizeSnapshotPost = 0; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1272 | }; |
| 1273 | |
| 1274 | } // end of anonymous namespace |
| 1275 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1276 | void CfgNode::emitIAS(Cfg *Func) const { |
| 1277 | Func->setCurrentNode(this); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 1278 | Assembler *Asm = Func->getAssembler<>(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1279 | // TODO(stichnot): When sandboxing, defer binding the node label until just |
| 1280 | // before the first instruction is emitted, to reduce the chance that a |
| 1281 | // padding nop is a branch target. |
Karl Schimpf | 50a3331 | 2015-10-23 09:19:48 -0700 | [diff] [blame] | 1282 | Asm->bindCfgNodeLabel(this); |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1283 | for (const Inst &I : Phis) { |
| 1284 | if (I.isDeleted()) |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1285 | continue; |
| 1286 | // Emitting a Phi instruction should cause an error. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1287 | I.emitIAS(Func); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1288 | } |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1289 | |
| 1290 | // Do the simple emission if not sandboxed. |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 1291 | if (!getFlags().getUseSandboxing()) { |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1292 | for (const Inst &I : Insts) { |
| 1293 | if (!I.isDeleted() && !I.isRedundantAssign()) { |
| 1294 | I.emitIAS(Func); |
| 1295 | updateStats(Func, &I); |
| 1296 | } |
| 1297 | } |
| 1298 | return; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1299 | } |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1300 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1301 | // The remainder of the function handles emission with sandboxing. There are |
| 1302 | // explicit bundle_lock regions delimited by bundle_lock and bundle_unlock |
| 1303 | // instructions. All other instructions are treated as an implicit |
| 1304 | // one-instruction bundle_lock region. Emission is done twice for each |
| 1305 | // bundle_lock region. The first pass is a preliminary pass, after which we |
| 1306 | // can figure out what nop padding is needed, then roll back, and make the |
| 1307 | // final pass. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1308 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1309 | // Ideally, the first pass would be speculative and the second pass would |
| 1310 | // only be done if nop padding were needed, but the structure of the |
| 1311 | // integrated assembler makes it hard to roll back the state of label |
| 1312 | // bindings, label links, and relocation fixups. Instead, the first pass just |
| 1313 | // disables all mutation of that state. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1314 | |
David Sehr | 26217e3 | 2015-11-26 13:03:50 -0800 | [diff] [blame] | 1315 | BundleEmitHelper Helper(Asm, Insts); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1316 | InstList::const_iterator End = Insts.end(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1317 | // Retrying indicates that we had to roll back to the bundle_lock instruction |
| 1318 | // to apply padding before the bundle_lock sequence. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1319 | bool Retrying = false; |
| 1320 | for (InstList::const_iterator I = Insts.begin(); I != End; ++I) { |
| 1321 | if (I->isDeleted() || I->isRedundantAssign()) |
| 1322 | continue; |
| 1323 | |
| 1324 | if (llvm::isa<InstBundleLock>(I)) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1325 | // Set up the initial bundle_lock state. This should not happen while |
| 1326 | // retrying, because the retry rolls back to the instruction following |
| 1327 | // the bundle_lock instruction. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1328 | assert(!Retrying); |
| 1329 | Helper.enterBundleLock(I); |
| 1330 | continue; |
| 1331 | } |
| 1332 | |
| 1333 | if (llvm::isa<InstBundleUnlock>(I)) { |
| 1334 | Helper.enterBundleUnlock(); |
| 1335 | if (Retrying) { |
| 1336 | // Make sure all instructions are in the same bundle. |
| 1337 | assert(Helper.isSameBundle()); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1338 | // If align_to_end is specified, make sure the next instruction begins |
| 1339 | // the bundle. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1340 | assert(!Helper.isAlignToEnd() || Helper.getPostAlignment() == 0); |
John Porto | 56958cb | 2016-01-14 09:18:18 -0800 | [diff] [blame] | 1341 | Helper.padForPadToEnd(); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1342 | Helper.leaveBundleLockRegion(); |
| 1343 | Retrying = false; |
| 1344 | } else { |
| 1345 | // This is the first pass, so roll back for the retry pass. |
| 1346 | Helper.rollback(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1347 | // Pad to the next bundle if the instruction sequence crossed a bundle |
| 1348 | // boundary. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1349 | Helper.padToNextBundle(); |
| 1350 | // Insert additional padding to make AlignToEnd work. |
| 1351 | Helper.padForAlignToEnd(); |
| 1352 | // Prepare for the retry pass after padding is done. |
| 1353 | Retrying = true; |
| 1354 | I = Helper.getBundleLockStart(); |
| 1355 | } |
| 1356 | continue; |
| 1357 | } |
| 1358 | |
| 1359 | // I points to a non bundle_lock/bundle_unlock instruction. |
| 1360 | if (Helper.isInBundleLockRegion()) { |
| 1361 | I->emitIAS(Func); |
| 1362 | // Only update stats during the final pass. |
| 1363 | if (Retrying) |
Jim Stichnoth | f5fdd23 | 2016-05-09 12:24:36 -0700 | [diff] [blame] | 1364 | updateStats(Func, iteratorToInst(I)); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1365 | } else { |
| 1366 | // Treat it as though there were an implicit bundle_lock and |
| 1367 | // bundle_unlock wrapping the instruction. |
| 1368 | Helper.enterBundleLock(I); |
| 1369 | I->emitIAS(Func); |
| 1370 | Helper.enterBundleUnlock(); |
| 1371 | Helper.rollback(); |
| 1372 | Helper.padToNextBundle(); |
| 1373 | I->emitIAS(Func); |
Jim Stichnoth | f5fdd23 | 2016-05-09 12:24:36 -0700 | [diff] [blame] | 1374 | updateStats(Func, iteratorToInst(I)); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1375 | Helper.leaveBundleLockRegion(); |
| 1376 | } |
| 1377 | } |
| 1378 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1379 | // Don't allow bundle locking across basic blocks, to keep the backtracking |
| 1380 | // mechanism simple. |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1381 | assert(!Helper.isInBundleLockRegion()); |
| 1382 | assert(!Retrying); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1383 | } |
| 1384 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1385 | void CfgNode::dump(Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1386 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 1387 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1388 | Func->setCurrentNode(this); |
| 1389 | Ostream &Str = Func->getContext()->getStrDump(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1390 | Liveness *Liveness = Func->getLiveness(); |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1391 | if (Func->isVerbose(IceV_Instructions) || Func->isVerbose(IceV_Loop)) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1392 | Str << getName() << ":\n"; |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1393 | // Dump the loop nest depth |
| 1394 | if (Func->isVerbose(IceV_Loop)) |
| 1395 | Str << " // LoopNestDepth = " << getLoopNestDepth() << "\n"; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1396 | // Dump list of predecessor nodes. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1397 | if (Func->isVerbose(IceV_Preds) && !InEdges.empty()) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1398 | Str << " // preds = "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1399 | bool First = true; |
| 1400 | for (CfgNode *I : InEdges) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1401 | if (!First) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1402 | Str << ", "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1403 | First = false; |
| 1404 | Str << "%" << I->getName(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1405 | } |
| 1406 | Str << "\n"; |
| 1407 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1408 | // Dump the live-in variables. |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 1409 | if (Func->isVerbose(IceV_Liveness)) { |
| 1410 | if (Liveness != nullptr && !Liveness->getLiveIn(this).empty()) { |
| 1411 | const LivenessBV &LiveIn = Liveness->getLiveIn(this); |
| 1412 | Str << " // LiveIn:"; |
| 1413 | for (SizeT i = 0; i < LiveIn.size(); ++i) { |
| 1414 | if (LiveIn[i]) { |
| 1415 | Variable *Var = Liveness->getVariable(i, this); |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 1416 | Str << " %" << Var->getName(); |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 1417 | if (Func->isVerbose(IceV_RegOrigins) && Var->hasReg()) { |
| 1418 | Str << ":" |
| 1419 | << Func->getTarget()->getRegName(Var->getRegNum(), |
| 1420 | Var->getType()); |
| 1421 | } |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 1422 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1423 | } |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 1424 | Str << "\n"; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1425 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1426 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1427 | // Dump each instruction. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1428 | if (Func->isVerbose(IceV_Instructions)) { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1429 | for (const Inst &I : Phis) |
| 1430 | I.dumpDecorated(Func); |
| 1431 | for (const Inst &I : Insts) |
| 1432 | I.dumpDecorated(Func); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1433 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1434 | // Dump the live-out variables. |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 1435 | if (Func->isVerbose(IceV_Liveness)) { |
| 1436 | if (Liveness != nullptr && !Liveness->getLiveOut(this).empty()) { |
| 1437 | const LivenessBV &LiveOut = Liveness->getLiveOut(this); |
| 1438 | Str << " // LiveOut:"; |
| 1439 | for (SizeT i = 0; i < LiveOut.size(); ++i) { |
| 1440 | if (LiveOut[i]) { |
| 1441 | Variable *Var = Liveness->getVariable(i, this); |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 1442 | Str << " %" << Var->getName(); |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 1443 | if (Func->isVerbose(IceV_RegOrigins) && Var->hasReg()) { |
| 1444 | Str << ":" |
| 1445 | << Func->getTarget()->getRegName(Var->getRegNum(), |
| 1446 | Var->getType()); |
| 1447 | } |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 1448 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1449 | } |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 1450 | Str << "\n"; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1451 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1452 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1453 | // Dump list of successor nodes. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1454 | if (Func->isVerbose(IceV_Succs)) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1455 | Str << " // succs = "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1456 | bool First = true; |
| 1457 | for (CfgNode *I : OutEdges) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1458 | if (!First) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1459 | Str << ", "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1460 | First = false; |
| 1461 | Str << "%" << I->getName(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1462 | } |
| 1463 | Str << "\n"; |
| 1464 | } |
| 1465 | } |
| 1466 | |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1467 | void CfgNode::profileExecutionCount(VariableDeclaration *Var) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 1468 | GlobalContext *Ctx = Func->getContext(); |
| 1469 | GlobalString RMW_I64 = Ctx->getGlobalString("llvm.nacl.atomic.rmw.i64"); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1470 | |
| 1471 | bool BadIntrinsic = false; |
| 1472 | const Intrinsics::FullIntrinsicInfo *Info = |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 1473 | Ctx->getIntrinsicsInfo().find(RMW_I64, BadIntrinsic); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1474 | assert(!BadIntrinsic); |
| 1475 | assert(Info != nullptr); |
| 1476 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 1477 | Operand *RMWI64Name = Ctx->getConstantExternSym(RMW_I64); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 1478 | constexpr RelocOffsetT Offset = 0; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 1479 | Constant *Counter = Ctx->getConstantSym(Offset, Var->getName()); |
| 1480 | Constant *AtomicRMWOp = Ctx->getConstantInt32(Intrinsics::AtomicAdd); |
| 1481 | Constant *One = Ctx->getConstantInt64(1); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1482 | Constant *OrderAcquireRelease = |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 1483 | Ctx->getConstantInt32(Intrinsics::MemoryOrderAcquireRelease); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1484 | |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1485 | auto *Instr = InstIntrinsicCall::create( |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1486 | Func, 5, Func->makeVariable(IceType_i64), RMWI64Name, Info->Info); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 1487 | Instr->addArg(AtomicRMWOp); |
| 1488 | Instr->addArg(Counter); |
| 1489 | Instr->addArg(One); |
| 1490 | Instr->addArg(OrderAcquireRelease); |
| 1491 | Insts.push_front(Instr); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Manasij Mukherjee | 45f51a2 | 2016-06-27 16:12:37 -0700 | [diff] [blame] | 1494 | void CfgNode::removeInEdge(CfgNode *In) { |
| 1495 | InEdges.erase(std::find(InEdges.begin(), InEdges.end(), In)); |
| 1496 | } |
| 1497 | |
| 1498 | CfgNode *CfgNode::shortCircuit() { |
| 1499 | auto *Func = getCfg(); |
| 1500 | auto *Last = &getInsts().back(); |
| 1501 | Variable *Condition = nullptr; |
| 1502 | InstBr *Br = nullptr; |
| 1503 | if ((Br = llvm::dyn_cast<InstBr>(Last))) { |
| 1504 | if (!Br->isUnconditional()) { |
| 1505 | Condition = llvm::dyn_cast<Variable>(Br->getCondition()); |
| 1506 | } |
| 1507 | } |
| 1508 | if (Condition == nullptr) |
| 1509 | return nullptr; |
| 1510 | |
| 1511 | auto *JumpOnTrue = Br->getTargetTrue(); |
| 1512 | auto *JumpOnFalse = Br->getTargetFalse(); |
| 1513 | |
| 1514 | bool FoundOr = false; |
| 1515 | bool FoundAnd = false; |
| 1516 | |
| 1517 | InstArithmetic *TopLevelBoolOp = nullptr; |
| 1518 | |
| 1519 | for (auto &Inst : reverse_range(getInsts())) { |
| 1520 | if (Inst.isDeleted()) |
| 1521 | continue; |
| 1522 | if (Inst.getDest() == Condition) { |
| 1523 | if (auto *Arith = llvm::dyn_cast<InstArithmetic>(&Inst)) { |
| 1524 | |
| 1525 | FoundOr = (Arith->getOp() == InstArithmetic::OpKind::Or); |
| 1526 | FoundAnd = (Arith->getOp() == InstArithmetic::OpKind::And); |
| 1527 | |
| 1528 | if (FoundOr || FoundAnd) { |
| 1529 | TopLevelBoolOp = Arith; |
| 1530 | break; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | if (!TopLevelBoolOp) |
| 1537 | return nullptr; |
| 1538 | |
| 1539 | auto IsOperand = [](Inst *Instr, Operand *Opr) -> bool { |
| 1540 | for (SizeT i = 0; i < Instr->getSrcSize(); ++i) { |
| 1541 | if (Instr->getSrc(i) == Opr) |
| 1542 | return true; |
| 1543 | } |
| 1544 | return false; |
| 1545 | }; |
| 1546 | Inst *FirstOperandDef = nullptr; |
| 1547 | for (auto &Inst : getInsts()) { |
| 1548 | if (IsOperand(TopLevelBoolOp, Inst.getDest())) { |
| 1549 | FirstOperandDef = &Inst; |
| 1550 | break; |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | if (FirstOperandDef == nullptr) { |
| 1555 | return nullptr; |
| 1556 | } |
| 1557 | |
| 1558 | // Check for side effects |
| 1559 | auto It = Ice::instToIterator(FirstOperandDef); |
| 1560 | while (It != getInsts().end()) { |
| 1561 | if (It->isDeleted()) { |
| 1562 | ++It; |
| 1563 | continue; |
| 1564 | } |
| 1565 | if (llvm::isa<InstBr>(It) || llvm::isa<InstRet>(It)) { |
| 1566 | break; |
| 1567 | } |
| 1568 | auto *Dest = It->getDest(); |
| 1569 | if (It->getDest() == nullptr || It->hasSideEffects() || |
| 1570 | !Func->getVMetadata()->isSingleBlock(Dest)) { |
| 1571 | // Relying on short cicuit eval here. |
| 1572 | // getVMetadata()->isSingleBlock(Dest) |
| 1573 | // will segfault if It->getDest() == nullptr |
| 1574 | return nullptr; |
| 1575 | } |
| 1576 | It++; |
| 1577 | } |
| 1578 | |
| 1579 | auto *NewNode = Func->makeNode(); |
| 1580 | NewNode->setLoopNestDepth(getLoopNestDepth()); |
| 1581 | It = Ice::instToIterator(FirstOperandDef); |
| 1582 | It++; // Have to split after the def |
| 1583 | |
| 1584 | NewNode->getInsts().splice(NewNode->getInsts().begin(), getInsts(), It, |
| 1585 | getInsts().end()); |
| 1586 | |
| 1587 | if (BuildDefs::dump()) { |
| 1588 | NewNode->setName(getName().append("_2")); |
| 1589 | setName(getName().append("_1")); |
| 1590 | } |
| 1591 | |
| 1592 | // Point edges properly |
| 1593 | NewNode->addInEdge(this); |
| 1594 | for (auto *Out : getOutEdges()) { |
| 1595 | NewNode->addOutEdge(Out); |
| 1596 | Out->addInEdge(NewNode); |
| 1597 | } |
| 1598 | removeAllOutEdges(); |
| 1599 | addOutEdge(NewNode); |
| 1600 | |
| 1601 | // Manage Phi instructions of successors |
| 1602 | for (auto *Succ : NewNode->getOutEdges()) { |
| 1603 | for (auto &Inst : Succ->getPhis()) { |
| 1604 | auto *Phi = llvm::cast<InstPhi>(&Inst); |
| 1605 | for (SizeT i = 0; i < Phi->getSrcSize(); ++i) { |
| 1606 | if (Phi->getLabel(i) == this) { |
| 1607 | Phi->addArgument(Phi->getSrc(i), NewNode); |
| 1608 | } |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | // Create new Br instruction |
| 1614 | InstBr *NewInst = nullptr; |
| 1615 | if (FoundOr) { |
| 1616 | addOutEdge(JumpOnTrue); |
| 1617 | JumpOnFalse->removeInEdge(this); |
| 1618 | NewInst = |
| 1619 | InstBr::create(Func, FirstOperandDef->getDest(), JumpOnTrue, NewNode); |
| 1620 | } else if (FoundAnd) { |
| 1621 | addOutEdge(JumpOnFalse); |
| 1622 | JumpOnTrue->removeInEdge(this); |
| 1623 | NewInst = |
| 1624 | InstBr::create(Func, FirstOperandDef->getDest(), NewNode, JumpOnFalse); |
| 1625 | } else { |
| 1626 | return nullptr; |
| 1627 | } |
| 1628 | |
| 1629 | assert(NewInst != nullptr); |
| 1630 | appendInst(NewInst); |
| 1631 | |
| 1632 | Operand *UnusedOperand = nullptr; |
| 1633 | assert(TopLevelBoolOp->getSrcSize() == 2); |
| 1634 | if (TopLevelBoolOp->getSrc(0) == FirstOperandDef->getDest()) |
| 1635 | UnusedOperand = TopLevelBoolOp->getSrc(1); |
| 1636 | else if (TopLevelBoolOp->getSrc(1) == FirstOperandDef->getDest()) |
| 1637 | UnusedOperand = TopLevelBoolOp->getSrc(0); |
| 1638 | assert(UnusedOperand); |
| 1639 | |
| 1640 | Br->replaceSource(0, UnusedOperand); // Index 0 has the condition of the Br |
| 1641 | |
| 1642 | TopLevelBoolOp->setDeleted(); |
| 1643 | return NewNode; |
| 1644 | } |
| 1645 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1646 | } // end of namespace Ice |