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 | //===----------------------------------------------------------------------===// |
| 9 | // |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 10 | // This file implements the CfgNode class, including the complexities |
| 11 | // of instruction insertion and in-edge calculation. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 15 | #include "IceAssembler.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 16 | #include "IceCfg.h" |
| 17 | #include "IceCfgNode.h" |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 18 | #include "IceGlobalInits.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 19 | #include "IceInst.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 20 | #include "IceLiveness.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 21 | #include "IceOperand.h" |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 22 | #include "IceTargetLowering.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 23 | |
| 24 | namespace Ice { |
| 25 | |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 26 | CfgNode::CfgNode(Cfg *Func, SizeT LabelNumber) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 27 | : Func(Func), Number(LabelNumber) {} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 28 | |
| 29 | // Returns the name the node was created with. If no name was given, |
| 30 | // it synthesizes a (hopefully) unique name. |
| 31 | IceString CfgNode::getName() const { |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 32 | if (NameIndex >= 0) |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 33 | return Func->getIdentifierName(NameIndex); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 34 | return "__" + std::to_string(getIndex()); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Adds an instruction to either the Phi list or the regular |
| 38 | // instruction list. Validates that all Phis are added before all |
| 39 | // regular instructions. |
| 40 | void CfgNode::appendInst(Inst *Inst) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 41 | ++InstCountEstimate; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 42 | if (InstPhi *Phi = llvm::dyn_cast<InstPhi>(Inst)) { |
| 43 | if (!Insts.empty()) { |
| 44 | Func->setError("Phi instruction added to the middle of a block"); |
| 45 | return; |
| 46 | } |
| 47 | Phis.push_back(Phi); |
| 48 | } else { |
| 49 | Insts.push_back(Inst); |
| 50 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 53 | // Renumbers the non-deleted instructions in the node. This needs to |
| 54 | // be done in preparation for live range analysis. The instruction |
| 55 | // numbers in a block must be monotonically increasing. The range of |
| 56 | // instruction numbers in a block, from lowest to highest, must not |
| 57 | // overlap with the range of any other block. |
| 58 | void CfgNode::renumberInstructions() { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 59 | InstNumberT FirstNumber = Func->getNextInstNumber(); |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 60 | for (Inst &I : Phis) |
| 61 | I.renumber(Func); |
| 62 | for (Inst &I : Insts) |
| 63 | I.renumber(Func); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 64 | InstCountEstimate = Func->getNextInstNumber() - FirstNumber; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 67 | // When a node is created, the OutEdges are immediately known, but the |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 68 | // InEdges have to be built up incrementally. After the CFG has been |
| 69 | // constructed, the computePredecessors() pass finalizes it by |
| 70 | // creating the InEdges list. |
| 71 | void CfgNode::computePredecessors() { |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 72 | for (CfgNode *Succ : OutEdges) |
| 73 | Succ->InEdges.push_back(this); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 76 | void CfgNode::computeSuccessors() { |
| 77 | OutEdges = Insts.rbegin()->getTerminatorEdges(); |
| 78 | } |
| 79 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 80 | // This does part 1 of Phi lowering, by creating a new dest variable |
| 81 | // for each Phi instruction, replacing the Phi instruction's dest with |
| 82 | // that variable, and adding an explicit assignment of the old dest to |
| 83 | // the new dest. For example, |
| 84 | // a=phi(...) |
| 85 | // changes to |
| 86 | // "a_phi=phi(...); a=a_phi". |
| 87 | // |
| 88 | // This is in preparation for part 2 which deletes the Phi |
| 89 | // instructions and appends assignment instructions to predecessor |
| 90 | // blocks. Note that this transformation preserves SSA form. |
| 91 | void CfgNode::placePhiLoads() { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 92 | for (Inst &I : Phis) { |
| 93 | auto Phi = llvm::dyn_cast<InstPhi>(&I); |
Jim Stichnoth | 1502e59 | 2014-12-11 09:22:45 -0800 | [diff] [blame] | 94 | Insts.insert(Insts.begin(), Phi->lower(Func)); |
| 95 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // This does part 2 of Phi lowering. For each Phi instruction at each |
| 99 | // out-edge, create a corresponding assignment instruction, and add |
| 100 | // all the assignments near the end of this block. They need to be |
| 101 | // added before any branch instruction, and also if the block ends |
| 102 | // with a compare instruction followed by a branch instruction that we |
| 103 | // may want to fuse, it's better to insert the new assignments before |
Jan Voung | c820ddf | 2014-07-29 14:38:51 -0700 | [diff] [blame] | 104 | // the compare instruction. The tryOptimizedCmpxchgCmpBr() method |
| 105 | // assumes this ordering of instructions. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 106 | // |
| 107 | // Note that this transformation takes the Phi dest variables out of |
| 108 | // SSA form, as there may be assignments to the dest variable in |
| 109 | // multiple blocks. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 110 | void CfgNode::placePhiStores() { |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 111 | // Find the insertion point. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 112 | InstList::iterator InsertionPoint = Insts.end(); |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 113 | // Every block must end in a terminator instruction, and therefore |
| 114 | // must have at least one instruction, so it's valid to decrement |
| 115 | // InsertionPoint (but assert just in case). |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 116 | assert(InsertionPoint != Insts.begin()); |
| 117 | --InsertionPoint; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 118 | // Confirm that InsertionPoint is a terminator instruction. Calling |
| 119 | // getTerminatorEdges() on a non-terminator instruction will cause |
| 120 | // an llvm_unreachable(). |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 121 | (void)InsertionPoint->getTerminatorEdges(); |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 122 | // SafeInsertionPoint is always immediately before the terminator |
| 123 | // instruction. If the block ends in a compare and conditional |
| 124 | // branch, it's better to place the Phi store before the compare so |
| 125 | // as not to interfere with compare/branch fusing. However, if the |
| 126 | // compare instruction's dest operand is the same as the new |
| 127 | // assignment statement's source operand, this can't be done due to |
| 128 | // data dependences, so we need to fall back to the |
| 129 | // SafeInsertionPoint. To illustrate: |
| 130 | // ; <label>:95 |
| 131 | // %97 = load i8* %96, align 1 |
| 132 | // %98 = icmp ne i8 %97, 0 |
| 133 | // br i1 %98, label %99, label %2132 |
| 134 | // ; <label>:99 |
| 135 | // %100 = phi i8 [ %97, %95 ], [ %110, %108 ] |
| 136 | // %101 = phi i1 [ %98, %95 ], [ %111, %108 ] |
| 137 | // would be Phi-lowered as: |
| 138 | // ; <label>:95 |
| 139 | // %97 = load i8* %96, align 1 |
| 140 | // %100_phi = %97 ; can be at InsertionPoint |
| 141 | // %98 = icmp ne i8 %97, 0 |
| 142 | // %101_phi = %98 ; must be at SafeInsertionPoint |
| 143 | // br i1 %98, label %99, label %2132 |
| 144 | // ; <label>:99 |
| 145 | // %100 = %100_phi |
| 146 | // %101 = %101_phi |
| 147 | // |
| 148 | // TODO(stichnot): It may be possible to bypass this whole |
| 149 | // SafeInsertionPoint mechanism. If a source basic block ends in a |
| 150 | // conditional branch: |
| 151 | // labelSource: |
| 152 | // ... |
| 153 | // br i1 %foo, label %labelTrue, label %labelFalse |
| 154 | // and a branch target has a Phi involving the branch operand: |
| 155 | // labelTrue: |
| 156 | // %bar = phi i1 [ %foo, %labelSource ], ... |
| 157 | // then we actually know the constant i1 value of the Phi operand: |
| 158 | // labelTrue: |
| 159 | // %bar = phi i1 [ true, %labelSource ], ... |
| 160 | // It seems that this optimization should be done by clang or opt, |
| 161 | // but we could also do it here. |
| 162 | InstList::iterator SafeInsertionPoint = InsertionPoint; |
| 163 | // Keep track of the dest variable of a compare instruction, so that |
| 164 | // we insert the new instruction at the SafeInsertionPoint if the |
| 165 | // compare's dest matches the Phi-lowered assignment's source. |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 166 | Variable *CmpInstDest = nullptr; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 167 | // If the current insertion point is at a conditional branch |
| 168 | // instruction, and the previous instruction is a compare |
| 169 | // instruction, then we move the insertion point before the compare |
| 170 | // instruction so as not to interfere with compare/branch fusing. |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 171 | if (InstBr *Branch = llvm::dyn_cast<InstBr>(InsertionPoint)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 172 | if (!Branch->isUnconditional()) { |
| 173 | if (InsertionPoint != Insts.begin()) { |
| 174 | --InsertionPoint; |
Jim Stichnoth | 607e9f0 | 2014-11-06 13:32:05 -0800 | [diff] [blame] | 175 | if (llvm::isa<InstIcmp>(InsertionPoint) || |
| 176 | llvm::isa<InstFcmp>(InsertionPoint)) { |
| 177 | CmpInstDest = InsertionPoint->getDest(); |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 178 | } else { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 179 | ++InsertionPoint; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 184 | |
| 185 | // Consider every out-edge. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 186 | for (CfgNode *Succ : OutEdges) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 187 | // Consider every Phi instruction at the out-edge. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 188 | for (Inst &I : Succ->Phis) { |
| 189 | auto Phi = llvm::dyn_cast<InstPhi>(&I); |
Jim Stichnoth | 1502e59 | 2014-12-11 09:22:45 -0800 | [diff] [blame] | 190 | Operand *Operand = Phi->getOperandForTarget(this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 191 | assert(Operand); |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 192 | Variable *Dest = I.getDest(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 193 | assert(Dest); |
| 194 | InstAssign *NewInst = InstAssign::create(Func, Dest, Operand); |
Jim Stichnoth | e5a5be7 | 2014-09-10 11:51:38 -0700 | [diff] [blame] | 195 | if (CmpInstDest == Operand) |
| 196 | Insts.insert(SafeInsertionPoint, NewInst); |
| 197 | else |
| 198 | Insts.insert(InsertionPoint, NewInst); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Deletes the phi instructions after the loads and stores are placed. |
| 204 | void CfgNode::deletePhis() { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 205 | for (Inst &I : Phis) |
| 206 | I.setDeleted(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 209 | // Splits the edge from Pred to this node by creating a new node and |
| 210 | // hooking up the in and out edges appropriately. (The EdgeIndex |
| 211 | // parameter is only used to make the new node's name unique when |
| 212 | // there are multiple edges between the same pair of nodes.) The new |
| 213 | // node's instruction list is initialized to the empty list, with no |
| 214 | // terminator instruction. If there are multiple edges from Pred to |
| 215 | // this node, only one edge is split, and the particular choice of |
| 216 | // edge is undefined. This could happen with a switch instruction, or |
| 217 | // a conditional branch that weirdly has both branches to the same |
| 218 | // place. TODO(stichnot,kschimpf): Figure out whether this is legal |
| 219 | // in the LLVM IR or the PNaCl bitcode, and if so, we need to |
| 220 | // establish a strong relationship among the ordering of Pred's |
| 221 | // out-edge list, this node's in-edge list, and the Phi instruction's |
| 222 | // operand list. |
| 223 | CfgNode *CfgNode::splitIncomingEdge(CfgNode *Pred, SizeT EdgeIndex) { |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 224 | CfgNode *NewNode = Func->makeNode(); |
| 225 | if (ALLOW_DUMP) |
| 226 | NewNode->setName("split_" + Pred->getName() + "_" + getName() + "_" + |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 227 | std::to_string(EdgeIndex)); |
| 228 | // The new node is added to the end of the node list, and will later |
| 229 | // need to be sorted into a reasonable topological order. |
| 230 | NewNode->setNeedsPlacement(true); |
| 231 | // Repoint Pred's out-edge. |
| 232 | bool Found = false; |
| 233 | for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); |
| 234 | !Found && I != E; ++I) { |
| 235 | if (*I == this) { |
| 236 | *I = NewNode; |
| 237 | NewNode->InEdges.push_back(Pred); |
| 238 | Found = true; |
| 239 | } |
| 240 | } |
| 241 | assert(Found); |
| 242 | // Repoint this node's in-edge. |
| 243 | Found = false; |
| 244 | for (auto I = InEdges.begin(), E = InEdges.end(); !Found && I != E; ++I) { |
| 245 | if (*I == Pred) { |
| 246 | *I = NewNode; |
| 247 | NewNode->OutEdges.push_back(this); |
| 248 | Found = true; |
| 249 | } |
| 250 | } |
| 251 | assert(Found); |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 252 | // Repoint a suitable branch instruction's target and return. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 253 | Found = false; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 254 | for (Inst &I : reverse_range(Pred->getInsts())) { |
| 255 | if (!I.isDeleted() && I.repointEdge(this, NewNode)) |
| 256 | return NewNode; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 257 | } |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 258 | // This should be unreachable, so the assert will fail. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 259 | assert(Found); |
| 260 | return NewNode; |
| 261 | } |
| 262 | |
| 263 | namespace { |
| 264 | |
| 265 | // Helper function used by advancedPhiLowering(). |
| 266 | bool sameVarOrReg(const Variable *Var, const Operand *Opnd) { |
| 267 | if (Var == Opnd) |
| 268 | return true; |
| 269 | if (const auto Var2 = llvm::dyn_cast<Variable>(Opnd)) { |
| 270 | if (Var->hasReg() && Var->getRegNum() == Var2->getRegNum()) |
| 271 | return true; |
| 272 | } |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | } // end of anonymous namespace |
| 277 | |
| 278 | // This the "advanced" version of Phi lowering for a basic block, in |
| 279 | // contrast to the simple version that lowers through assignments |
| 280 | // involving temporaries. |
| 281 | // |
| 282 | // All Phi instructions in a basic block are conceptually executed in |
| 283 | // parallel. However, if we lower Phis early and commit to a |
| 284 | // sequential ordering, we may end up creating unnecessary |
| 285 | // interferences which lead to worse register allocation. Delaying |
| 286 | // Phi scheduling until after register allocation can help unless |
| 287 | // there are no free registers for shuffling registers or stack slots |
| 288 | // and spilling becomes necessary. |
| 289 | // |
| 290 | // The advanced Phi lowering starts by finding a topological sort of |
| 291 | // the Phi instructions, where "A=B" comes before "B=C" due to the |
| 292 | // anti-dependence on B. If a topological sort is not possible due to |
| 293 | // a cycle, the cycle is broken by introducing a non-parallel |
| 294 | // temporary. For example, a cycle arising from a permutation like |
| 295 | // "A=B;B=C;C=A" can become "T=A;A=B;B=C;C=T". All else being equal, |
| 296 | // prefer to schedule assignments with register-allocated Src operands |
| 297 | // earlier, in case that register becomes free afterwards, and prefer |
| 298 | // to schedule assignments with register-allocated Dest variables |
| 299 | // later, to keep that register free for longer. |
| 300 | // |
| 301 | // Once the ordering is determined, the Cfg edge is split and the |
| 302 | // assignment list is lowered by the target lowering layer. The |
| 303 | // specific placement of the new node within the Cfg node list is |
| 304 | // deferred until later, including after empty node contraction. |
| 305 | void CfgNode::advancedPhiLowering() { |
| 306 | if (getPhis().empty()) |
| 307 | return; |
| 308 | |
| 309 | // Count the number of non-deleted Phi instructions. |
JF Bastien | f2e93b6 | 2015-01-22 14:30:57 -0800 | [diff] [blame] | 310 | struct PhiDesc { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 311 | InstPhi *Phi; |
| 312 | Variable *Dest; |
| 313 | Operand *Src; |
| 314 | bool Processed; |
| 315 | size_t NumPred; // number of entries whose Src is this Dest |
| 316 | int32_t Weight; // preference for topological order |
JF Bastien | f2e93b6 | 2015-01-22 14:30:57 -0800 | [diff] [blame] | 317 | }; |
| 318 | llvm::SmallVector<PhiDesc, 32> Desc(getPhis().size()); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 319 | |
| 320 | size_t NumPhis = 0; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 321 | for (Inst &I : Phis) { |
| 322 | auto Inst = llvm::dyn_cast<InstPhi>(&I); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 323 | if (!Inst->isDeleted()) { |
| 324 | Desc[NumPhis].Phi = Inst; |
| 325 | Desc[NumPhis].Dest = Inst->getDest(); |
| 326 | ++NumPhis; |
| 327 | } |
| 328 | } |
| 329 | if (NumPhis == 0) |
| 330 | return; |
| 331 | |
| 332 | SizeT InEdgeIndex = 0; |
| 333 | for (CfgNode *Pred : InEdges) { |
| 334 | CfgNode *Split = splitIncomingEdge(Pred, InEdgeIndex++); |
| 335 | AssignList Assignments; |
| 336 | SizeT Remaining = NumPhis; |
| 337 | |
| 338 | // First pass computes Src and initializes NumPred. |
| 339 | for (size_t I = 0; I < NumPhis; ++I) { |
| 340 | Variable *Dest = Desc[I].Dest; |
| 341 | Operand *Src = Desc[I].Phi->getOperandForTarget(Pred); |
| 342 | Desc[I].Src = Src; |
| 343 | Desc[I].Processed = false; |
| 344 | Desc[I].NumPred = 0; |
| 345 | // Cherry-pick any trivial assignments, so that they don't |
| 346 | // contribute to the running complexity of the topological sort. |
| 347 | if (sameVarOrReg(Dest, Src)) { |
| 348 | Desc[I].Processed = true; |
| 349 | --Remaining; |
| 350 | if (Dest != Src) |
| 351 | // If Dest and Src are syntactically the same, don't bother |
| 352 | // adding the assignment, because in all respects it would |
| 353 | // be redundant, and if Dest/Src are on the stack, the |
| 354 | // target lowering may naively decide to lower it using a |
| 355 | // temporary register. |
| 356 | Assignments.push_back(InstAssign::create(Func, Dest, Src)); |
| 357 | } |
| 358 | } |
| 359 | // Second pass computes NumPred by comparing every pair of Phi |
| 360 | // instructions. |
| 361 | for (size_t I = 0; I < NumPhis; ++I) { |
| 362 | if (Desc[I].Processed) |
| 363 | continue; |
| 364 | const Variable *Dest = Desc[I].Dest; |
| 365 | for (size_t J = 0; J < NumPhis; ++J) { |
| 366 | if (Desc[J].Processed) |
| 367 | continue; |
| 368 | if (I != J) { |
| 369 | // There shouldn't be two Phis with the same Dest variable |
| 370 | // or register. |
| 371 | assert(!sameVarOrReg(Dest, Desc[J].Dest)); |
| 372 | } |
| 373 | const Operand *Src = Desc[J].Src; |
| 374 | if (sameVarOrReg(Dest, Src)) |
| 375 | ++Desc[I].NumPred; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Another pass to compute initial Weight values. |
| 380 | |
| 381 | // Always pick NumPred=0 over NumPred>0. |
| 382 | const int32_t WeightNoPreds = 4; |
| 383 | // Prefer Src as a register because the register might free up. |
| 384 | const int32_t WeightSrcIsReg = 2; |
| 385 | // Prefer Dest not as a register because the register stays free |
| 386 | // longer. |
| 387 | const int32_t WeightDestNotReg = 1; |
| 388 | |
| 389 | for (size_t I = 0; I < NumPhis; ++I) { |
| 390 | if (Desc[I].Processed) |
| 391 | continue; |
| 392 | int32_t Weight = 0; |
| 393 | if (Desc[I].NumPred == 0) |
| 394 | Weight += WeightNoPreds; |
| 395 | if (auto Var = llvm::dyn_cast<Variable>(Desc[I].Src)) |
| 396 | if (Var->hasReg()) |
| 397 | Weight += WeightSrcIsReg; |
| 398 | if (!Desc[I].Dest->hasReg()) |
| 399 | Weight += WeightDestNotReg; |
| 400 | Desc[I].Weight = Weight; |
| 401 | } |
| 402 | |
| 403 | // Repeatedly choose and process the best candidate in the |
| 404 | // topological sort, until no candidates remain. This |
| 405 | // implementation is O(N^2) where N is the number of Phi |
| 406 | // instructions, but with a small constant factor compared to a |
| 407 | // likely implementation of O(N) topological sort. |
| 408 | for (; Remaining; --Remaining) { |
| 409 | size_t BestIndex = 0; |
| 410 | int32_t BestWeight = -1; |
| 411 | // Find the best candidate. |
| 412 | for (size_t I = 0; I < NumPhis; ++I) { |
| 413 | if (Desc[I].Processed) |
| 414 | continue; |
| 415 | int32_t Weight = 0; |
| 416 | Weight = Desc[I].Weight; |
| 417 | if (Weight > BestWeight) { |
| 418 | BestIndex = I; |
| 419 | BestWeight = Weight; |
| 420 | } |
| 421 | } |
| 422 | assert(BestWeight >= 0); |
| 423 | assert(Desc[BestIndex].NumPred <= 1); |
| 424 | Variable *Dest = Desc[BestIndex].Dest; |
| 425 | Operand *Src = Desc[BestIndex].Src; |
| 426 | assert(!sameVarOrReg(Dest, Src)); |
| 427 | // Break a cycle by introducing a temporary. |
| 428 | if (Desc[BestIndex].NumPred) { |
| 429 | bool Found = false; |
| 430 | // If the target instruction "A=B" is part of a cycle, find |
| 431 | // the "X=A" assignment in the cycle because it will have to |
| 432 | // be rewritten as "X=tmp". |
| 433 | for (size_t J = 0; !Found && J < NumPhis; ++J) { |
| 434 | if (Desc[J].Processed) |
| 435 | continue; |
| 436 | Operand *OtherSrc = Desc[J].Src; |
| 437 | if (Desc[J].NumPred && sameVarOrReg(Dest, OtherSrc)) { |
| 438 | SizeT VarNum = Func->getNumVariables(); |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 439 | Variable *Tmp = Func->makeVariable(OtherSrc->getType()); |
| 440 | if (ALLOW_DUMP) |
| 441 | Tmp->setName(Func, "__split_" + std::to_string(VarNum)); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 442 | Assignments.push_back(InstAssign::create(Func, Tmp, OtherSrc)); |
| 443 | Desc[J].Src = Tmp; |
| 444 | Found = true; |
| 445 | } |
| 446 | } |
| 447 | assert(Found); |
| 448 | } |
| 449 | // Now that a cycle (if any) has been broken, create the actual |
| 450 | // assignment. |
| 451 | Assignments.push_back(InstAssign::create(Func, Dest, Src)); |
| 452 | // Update NumPred for all Phi assignments using this Phi's Src |
| 453 | // as their Dest variable. Also update Weight if NumPred |
| 454 | // dropped from 1 to 0. |
| 455 | if (auto Var = llvm::dyn_cast<Variable>(Src)) { |
| 456 | for (size_t I = 0; I < NumPhis; ++I) { |
| 457 | if (Desc[I].Processed) |
| 458 | continue; |
| 459 | if (sameVarOrReg(Var, Desc[I].Dest)) { |
| 460 | if (--Desc[I].NumPred == 0) |
| 461 | Desc[I].Weight += WeightNoPreds; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | Desc[BestIndex].Processed = true; |
| 466 | } |
| 467 | |
| 468 | Func->getTarget()->lowerPhiAssignments(Split, Assignments); |
| 469 | |
| 470 | // Renumber the instructions to be monotonically increasing so |
| 471 | // that addNode() doesn't assert when multi-definitions are added |
| 472 | // out of order. |
| 473 | Split->renumberInstructions(); |
| 474 | Func->getVMetadata()->addNode(Split); |
| 475 | } |
| 476 | |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 477 | for (Inst &I : Phis) |
| 478 | I.setDeleted(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 481 | // Does address mode optimization. Pass each instruction to the |
| 482 | // TargetLowering object. If it returns a new instruction |
| 483 | // (representing the optimized address mode), then insert the new |
| 484 | // instruction and delete the old. |
| 485 | void CfgNode::doAddressOpt() { |
| 486 | TargetLowering *Target = Func->getTarget(); |
| 487 | LoweringContext &Context = Target->getContext(); |
| 488 | Context.init(this); |
| 489 | while (!Context.atEnd()) { |
| 490 | Target->doAddressOpt(); |
| 491 | } |
| 492 | } |
| 493 | |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 494 | void CfgNode::doNopInsertion() { |
| 495 | TargetLowering *Target = Func->getTarget(); |
| 496 | LoweringContext &Context = Target->getContext(); |
| 497 | Context.init(this); |
| 498 | while (!Context.atEnd()) { |
| 499 | Target->doNopInsertion(); |
| 500 | // Ensure Cur=Next, so that the nops are inserted before the current |
| 501 | // instruction rather than after. |
| 502 | Context.advanceNext(); |
| 503 | Context.advanceCur(); |
| 504 | } |
| 505 | // Insert before all instructions. |
| 506 | Context.setInsertPoint(getInsts().begin()); |
| 507 | Context.advanceNext(); |
| 508 | Context.advanceCur(); |
| 509 | Target->doNopInsertion(); |
| 510 | } |
| 511 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 512 | // Drives the target lowering. Passes the current instruction and the |
| 513 | // next non-deleted instruction for target lowering. |
| 514 | void CfgNode::genCode() { |
| 515 | TargetLowering *Target = Func->getTarget(); |
| 516 | LoweringContext &Context = Target->getContext(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 517 | // Lower the regular instructions. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 518 | Context.init(this); |
Jim Stichnoth | a59ae6f | 2015-05-17 10:11:41 -0700 | [diff] [blame] | 519 | Target->initNodeForLowering(this); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 520 | while (!Context.atEnd()) { |
| 521 | InstList::iterator Orig = Context.getCur(); |
| 522 | if (llvm::isa<InstRet>(*Orig)) |
| 523 | setHasReturn(); |
| 524 | Target->lower(); |
| 525 | // Ensure target lowering actually moved the cursor. |
| 526 | assert(Context.getCur() != Orig); |
| 527 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 528 | // Do preliminary lowering of the Phi instructions. |
| 529 | Target->prelowerPhis(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 532 | void CfgNode::livenessLightweight() { |
| 533 | SizeT NumVars = Func->getNumVariables(); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 534 | LivenessBV Live(NumVars); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 535 | // Process regular instructions in reverse order. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 536 | for (Inst &I : reverse_range(Insts)) { |
| 537 | if (I.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 538 | continue; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 539 | I.livenessLightweight(Func, Live); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 540 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 541 | for (Inst &I : Phis) { |
| 542 | if (I.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 543 | continue; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 544 | I.livenessLightweight(Func, Live); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | // Performs liveness analysis on the block. Returns true if the |
| 549 | // incoming liveness changed from before, false if it stayed the same. |
| 550 | // (If it changes, the node's predecessors need to be processed |
| 551 | // again.) |
| 552 | bool CfgNode::liveness(Liveness *Liveness) { |
| 553 | SizeT NumVars = Liveness->getNumVarsInNode(this); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 554 | LivenessBV Live(NumVars); |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 555 | LiveBeginEndMap *LiveBegin = nullptr; |
| 556 | LiveBeginEndMap *LiveEnd = nullptr; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 557 | // Mark the beginning and ending of each variable's live range |
| 558 | // with the sentinel instruction number 0. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 559 | if (Liveness->getMode() == Liveness_Intervals) { |
| 560 | LiveBegin = Liveness->getLiveBegin(this); |
| 561 | LiveEnd = Liveness->getLiveEnd(this); |
| 562 | LiveBegin->clear(); |
| 563 | LiveEnd->clear(); |
| 564 | // Guess that the number of live ranges beginning is roughly the |
| 565 | // number of instructions, and same for live ranges ending. |
| 566 | LiveBegin->reserve(getInstCountEstimate()); |
| 567 | LiveEnd->reserve(getInstCountEstimate()); |
| 568 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 569 | // Initialize Live to be the union of all successors' LiveIn. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 570 | for (CfgNode *Succ : OutEdges) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 571 | Live |= Liveness->getLiveIn(Succ); |
| 572 | // Mark corresponding argument of phis in successor as live. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 573 | for (Inst &I : Succ->Phis) { |
| 574 | auto Phi = llvm::dyn_cast<InstPhi>(&I); |
Jim Stichnoth | 1502e59 | 2014-12-11 09:22:45 -0800 | [diff] [blame] | 575 | Phi->livenessPhiOperand(Live, this, Liveness); |
| 576 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 577 | } |
| 578 | Liveness->getLiveOut(this) = Live; |
| 579 | |
| 580 | // Process regular instructions in reverse order. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 581 | for (Inst &I : reverse_range(Insts)) { |
| 582 | if (I.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 583 | continue; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 584 | I.liveness(I.getNumber(), Live, Liveness, LiveBegin, LiveEnd); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 585 | } |
| 586 | // Process phis in forward order so that we can override the |
| 587 | // instruction number to be that of the earliest phi instruction in |
| 588 | // the block. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 589 | SizeT NumNonDeadPhis = 0; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 590 | InstNumberT FirstPhiNumber = Inst::NumberSentinel; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 591 | for (Inst &I : Phis) { |
| 592 | if (I.isDeleted()) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 593 | continue; |
| 594 | if (FirstPhiNumber == Inst::NumberSentinel) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 595 | FirstPhiNumber = I.getNumber(); |
| 596 | if (I.liveness(FirstPhiNumber, Live, Liveness, LiveBegin, LiveEnd)) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 597 | ++NumNonDeadPhis; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | // When using the sparse representation, after traversing the |
| 601 | // instructions in the block, the Live bitvector should only contain |
| 602 | // set bits for global variables upon block entry. We validate this |
| 603 | // by shrinking the Live vector and then testing it against the |
| 604 | // pre-shrunk version. (The shrinking is required, but the |
| 605 | // validation is not.) |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 606 | LivenessBV LiveOrig = Live; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 607 | Live.resize(Liveness->getNumGlobalVars()); |
| 608 | // Non-global arguments in the entry node are allowed to be live on |
| 609 | // entry. |
| 610 | bool IsEntry = (Func->getEntryNode() == this); |
| 611 | if (!(IsEntry || Live == LiveOrig)) { |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 612 | if (ALLOW_DUMP) { |
| 613 | // This is a fatal liveness consistency error. Print some |
| 614 | // diagnostics and abort. |
| 615 | Ostream &Str = Func->getContext()->getStrDump(); |
| 616 | Func->resetCurrentNode(); |
| 617 | Str << "LiveOrig-Live ="; |
| 618 | for (SizeT i = Live.size(); i < LiveOrig.size(); ++i) { |
| 619 | if (LiveOrig.test(i)) { |
| 620 | Str << " "; |
| 621 | Liveness->getVariable(i, this)->dump(Func); |
| 622 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 623 | } |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 624 | Str << "\n"; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 625 | } |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 626 | llvm::report_fatal_error("Fatal inconsistency in liveness analysis"); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | bool Changed = false; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 630 | LivenessBV &LiveIn = Liveness->getLiveIn(this); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 631 | // Add in current LiveIn |
| 632 | Live |= LiveIn; |
| 633 | // Check result, set LiveIn=Live |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 634 | SizeT &PrevNumNonDeadPhis = Liveness->getNumNonDeadPhis(this); |
| 635 | bool LiveInChanged = (Live != LiveIn); |
| 636 | Changed = (NumNonDeadPhis != PrevNumNonDeadPhis || LiveInChanged); |
| 637 | if (LiveInChanged) |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 638 | LiveIn = Live; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 639 | PrevNumNonDeadPhis = NumNonDeadPhis; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 640 | return Changed; |
| 641 | } |
| 642 | |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 643 | // Once basic liveness is complete, compute actual live ranges. It is |
| 644 | // assumed that within a single basic block, a live range begins at |
| 645 | // most once and ends at most once. This is certainly true for pure |
| 646 | // SSA form. It is also true once phis are lowered, since each |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 647 | // assignment to the phi-based temporary is in a different basic |
| 648 | // block, and there is a single read that ends the live in the basic |
| 649 | // block that contained the actual phi instruction. |
Jim Stichnoth | e5b73e6 | 2014-12-15 09:58:51 -0800 | [diff] [blame] | 650 | void CfgNode::livenessAddIntervals(Liveness *Liveness, InstNumberT FirstInstNum, |
| 651 | InstNumberT LastInstNum) { |
| 652 | TimerMarker T1(TimerStack::TT_liveRange, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 653 | |
| 654 | SizeT NumVars = Liveness->getNumVarsInNode(this); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 655 | LivenessBV &LiveIn = Liveness->getLiveIn(this); |
| 656 | LivenessBV &LiveOut = Liveness->getLiveOut(this); |
| 657 | LiveBeginEndMap &MapBegin = *Liveness->getLiveBegin(this); |
| 658 | LiveBeginEndMap &MapEnd = *Liveness->getLiveEnd(this); |
| 659 | std::sort(MapBegin.begin(), MapBegin.end()); |
| 660 | std::sort(MapEnd.begin(), MapEnd.end()); |
| 661 | // Verify there are no duplicates. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 662 | struct ComparePair { |
| 663 | bool operator()(const LiveBeginEndMapEntry &A, |
| 664 | const LiveBeginEndMapEntry &B) { |
| 665 | return A.first == B.first; |
| 666 | } |
| 667 | }; |
| 668 | assert(std::adjacent_find(MapBegin.begin(), MapBegin.end(), ComparePair()) == |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 669 | MapBegin.end()); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 670 | assert(std::adjacent_find(MapEnd.begin(), MapEnd.end(), ComparePair()) == |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 671 | MapEnd.end()); |
| 672 | |
| 673 | LivenessBV LiveInAndOut = LiveIn; |
| 674 | LiveInAndOut &= LiveOut; |
| 675 | |
| 676 | // Iterate in parallel across the sorted MapBegin[] and MapEnd[]. |
| 677 | auto IBB = MapBegin.begin(), IEB = MapEnd.begin(); |
| 678 | auto IBE = MapBegin.end(), IEE = MapEnd.end(); |
| 679 | while (IBB != IBE || IEB != IEE) { |
| 680 | SizeT i1 = IBB == IBE ? NumVars : IBB->first; |
| 681 | SizeT i2 = IEB == IEE ? NumVars : IEB->first; |
| 682 | SizeT i = std::min(i1, i2); |
| 683 | // i1 is the Variable number of the next MapBegin entry, and i2 is |
| 684 | // the Variable number of the next MapEnd entry. If i1==i2, then |
| 685 | // the Variable's live range begins and ends in this block. If |
| 686 | // i1<i2, then i1's live range begins at instruction IBB->second |
| 687 | // and extends through the end of the block. If i1>i2, then i2's |
| 688 | // live range begins at the first instruction of the block and |
| 689 | // ends at IEB->second. In any case, we choose the lesser of i1 |
| 690 | // and i2 and proceed accordingly. |
| 691 | InstNumberT LB = i == i1 ? IBB->second : FirstInstNum; |
| 692 | InstNumberT LE = i == i2 ? IEB->second : LastInstNum + 1; |
| 693 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 694 | Variable *Var = Liveness->getVariable(i, this); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 695 | if (!Var->getIgnoreLiveness()) { |
| 696 | if (LB > LE) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 697 | Var->addLiveRange(FirstInstNum, LE, 1); |
| 698 | Var->addLiveRange(LB, LastInstNum + 1, 1); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 699 | // Assert that Var is a global variable by checking that its |
| 700 | // liveness index is less than the number of globals. This |
| 701 | // ensures that the LiveInAndOut[] access is valid. |
| 702 | assert(i < Liveness->getNumGlobalVars()); |
| 703 | LiveInAndOut[i] = false; |
| 704 | } else { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 705 | Var->addLiveRange(LB, LE, 1); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | if (i == i1) |
| 709 | ++IBB; |
| 710 | if (i == i2) |
| 711 | ++IEB; |
| 712 | } |
| 713 | // Process the variables that are live across the entire block. |
| 714 | for (int i = LiveInAndOut.find_first(); i != -1; |
| 715 | i = LiveInAndOut.find_next(i)) { |
| 716 | Variable *Var = Liveness->getVariable(i, this); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 717 | Var->addLiveRange(FirstInstNum, LastInstNum + 1, 1); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 721 | // If this node contains only deleted instructions, and ends in an |
| 722 | // unconditional branch, contract the node by repointing all its |
| 723 | // in-edges to its successor. |
| 724 | void CfgNode::contractIfEmpty() { |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 725 | if (InEdges.empty()) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 726 | return; |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 727 | Inst *Branch = nullptr; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 728 | for (Inst &I : Insts) { |
| 729 | if (I.isDeleted()) |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 730 | continue; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 731 | if (I.isUnconditionalBranch()) |
| 732 | Branch = &I; |
| 733 | else if (!I.isRedundantAssign()) |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 734 | return; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 735 | } |
| 736 | Branch->setDeleted(); |
| 737 | assert(OutEdges.size() == 1); |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 738 | // Repoint all this node's in-edges to this node's successor, unless |
| 739 | // this node's successor is actually itself (in which case the |
| 740 | // statement "OutEdges.front()->InEdges.push_back(Pred)" could |
| 741 | // invalidate the iterator over this->InEdges). |
| 742 | if (OutEdges.front() != this) { |
| 743 | for (CfgNode *Pred : InEdges) { |
| 744 | for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); I != E; |
| 745 | ++I) { |
| 746 | if (*I == this) { |
| 747 | *I = OutEdges.front(); |
| 748 | OutEdges.front()->InEdges.push_back(Pred); |
| 749 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 750 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 751 | for (Inst &I : Pred->getInsts()) { |
| 752 | if (!I.isDeleted()) |
| 753 | I.repointEdge(this, OutEdges.front()); |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 754 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | InEdges.clear(); |
| 758 | // Don't bother removing the single out-edge, which would also |
| 759 | // require finding the corresponding in-edge in the successor and |
| 760 | // removing it. |
| 761 | } |
| 762 | |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 763 | void CfgNode::doBranchOpt(const CfgNode *NextNode) { |
| 764 | TargetLowering *Target = Func->getTarget(); |
| 765 | // Check every instruction for a branch optimization opportunity. |
| 766 | // It may be more efficient to iterate in reverse and stop after the |
| 767 | // first opportunity, unless there is some target lowering where we |
| 768 | // have the possibility of multiple such optimizations per block |
| 769 | // (currently not the case for x86 lowering). |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 770 | for (Inst &I : Insts) { |
| 771 | if (!I.isDeleted()) { |
| 772 | Target->doBranchOpt(&I, NextNode); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 777 | // ======================== Dump routines ======================== // |
| 778 | |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 779 | namespace { |
| 780 | |
| 781 | // Helper functions for emit(). |
| 782 | |
| 783 | void emitRegisterUsage(Ostream &Str, const Cfg *Func, const CfgNode *Node, |
| 784 | bool IsLiveIn, std::vector<SizeT> &LiveRegCount) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 785 | if (!ALLOW_DUMP) |
| 786 | return; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 787 | Liveness *Liveness = Func->getLiveness(); |
| 788 | const LivenessBV *Live; |
| 789 | if (IsLiveIn) { |
| 790 | Live = &Liveness->getLiveIn(Node); |
| 791 | Str << "\t\t\t\t# LiveIn="; |
| 792 | } else { |
| 793 | Live = &Liveness->getLiveOut(Node); |
| 794 | Str << "\t\t\t\t# LiveOut="; |
| 795 | } |
| 796 | if (!Live->empty()) { |
Jim Stichnoth | 9a05aea | 2015-05-26 16:01:23 -0700 | [diff] [blame] | 797 | std::vector<Variable *> LiveRegs; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 798 | for (SizeT i = 0; i < Live->size(); ++i) { |
| 799 | if ((*Live)[i]) { |
| 800 | Variable *Var = Liveness->getVariable(i, Node); |
| 801 | if (Var->hasReg()) { |
| 802 | if (IsLiveIn) |
| 803 | ++LiveRegCount[Var->getRegNum()]; |
Jim Stichnoth | 9a05aea | 2015-05-26 16:01:23 -0700 | [diff] [blame] | 804 | LiveRegs.push_back(Var); |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | } |
Jim Stichnoth | 9a05aea | 2015-05-26 16:01:23 -0700 | [diff] [blame] | 808 | // Sort the variables by regnum so they are always printed in a |
| 809 | // familiar order. |
| 810 | std::sort(LiveRegs.begin(), LiveRegs.end(), |
| 811 | [](const Variable *V1, const Variable *V2) { |
Jim Stichnoth | 8e6bf6e | 2015-06-03 15:58:12 -0700 | [diff] [blame] | 812 | return V1->getRegNum() < V2->getRegNum(); |
| 813 | }); |
Jim Stichnoth | 9a05aea | 2015-05-26 16:01:23 -0700 | [diff] [blame] | 814 | bool First = true; |
| 815 | for (Variable *Var : LiveRegs) { |
| 816 | if (!First) |
| 817 | Str << ","; |
| 818 | First = false; |
| 819 | Var->emit(Func); |
| 820 | } |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 821 | } |
| 822 | Str << "\n"; |
| 823 | } |
| 824 | |
| 825 | void emitLiveRangesEnded(Ostream &Str, const Cfg *Func, const Inst *Instr, |
| 826 | std::vector<SizeT> &LiveRegCount) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 827 | if (!ALLOW_DUMP) |
| 828 | return; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 829 | bool First = true; |
| 830 | Variable *Dest = Instr->getDest(); |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 831 | // Normally we increment the live count for the dest register. But |
| 832 | // we shouldn't if the instruction's IsDestNonKillable flag is set, |
| 833 | // because this means that the target lowering created this |
| 834 | // instruction as a non-SSA assignment; i.e., a different, previous |
| 835 | // instruction started the dest variable's live range. |
| 836 | if (!Instr->isDestNonKillable() && Dest && Dest->hasReg()) |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 837 | ++LiveRegCount[Dest->getRegNum()]; |
| 838 | for (SizeT I = 0; I < Instr->getSrcSize(); ++I) { |
| 839 | Operand *Src = Instr->getSrc(I); |
| 840 | SizeT NumVars = Src->getNumVars(); |
| 841 | for (SizeT J = 0; J < NumVars; ++J) { |
| 842 | const Variable *Var = Src->getVar(J); |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 843 | bool ShouldReport = Instr->isLastUse(Var); |
| 844 | if (ShouldReport && Var->hasReg()) { |
Jim Stichnoth | 9a05aea | 2015-05-26 16:01:23 -0700 | [diff] [blame] | 845 | // Don't report end of live range until the live count reaches 0. |
| 846 | SizeT NewCount = --LiveRegCount[Var->getRegNum()]; |
| 847 | if (NewCount) |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 848 | ShouldReport = false; |
Jim Stichnoth | 9a05aea | 2015-05-26 16:01:23 -0700 | [diff] [blame] | 849 | } |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 850 | if (ShouldReport) { |
Jim Stichnoth | 4175b2a | 2015-04-30 12:26:22 -0700 | [diff] [blame] | 851 | if (First) |
| 852 | Str << " \t# END="; |
| 853 | else |
| 854 | Str << ","; |
| 855 | Var->emit(Func); |
| 856 | First = false; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 862 | void updateStats(Cfg *Func, const Inst *I) { |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 863 | if (!ALLOW_DUMP) |
| 864 | return; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 865 | // Update emitted instruction count, plus fill/spill count for |
| 866 | // Variable operands without a physical register. |
| 867 | if (uint32_t Count = I->getEmitInstCount()) { |
| 868 | Func->getContext()->statsUpdateEmitted(Count); |
| 869 | if (Variable *Dest = I->getDest()) { |
| 870 | if (!Dest->hasReg()) |
| 871 | Func->getContext()->statsUpdateFills(); |
| 872 | } |
| 873 | for (SizeT S = 0; S < I->getSrcSize(); ++S) { |
| 874 | if (Variable *Src = llvm::dyn_cast<Variable>(I->getSrc(S))) { |
| 875 | if (!Src->hasReg()) |
| 876 | Func->getContext()->statsUpdateSpills(); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 882 | } // end of anonymous namespace |
| 883 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 884 | void CfgNode::emit(Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 885 | if (!ALLOW_DUMP) |
| 886 | return; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 887 | Func->setCurrentNode(this); |
| 888 | Ostream &Str = Func->getContext()->getStrEmit(); |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 889 | Liveness *Liveness = Func->getLiveness(); |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 890 | bool DecorateAsm = |
| 891 | Liveness && Func->getContext()->getFlags().getDecorateAsm(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 892 | Str << getAsmName() << ":\n"; |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 893 | // LiveRegCount keeps track of the number of currently live |
| 894 | // variables that each register is assigned to. Normally that would |
| 895 | // be only 0 or 1, but the register allocator's AllowOverlap |
| 896 | // inference allows it to be greater than 1 for short periods. |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 897 | std::vector<SizeT> LiveRegCount(Func->getTarget()->getNumRegisters()); |
Jim Stichnoth | 4175b2a | 2015-04-30 12:26:22 -0700 | [diff] [blame] | 898 | if (DecorateAsm) { |
| 899 | const bool IsLiveIn = true; |
| 900 | emitRegisterUsage(Str, Func, this, IsLiveIn, LiveRegCount); |
| 901 | } |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 902 | |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 903 | for (const Inst &I : Phis) { |
| 904 | if (I.isDeleted()) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 905 | continue; |
| 906 | // Emitting a Phi instruction should cause an error. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 907 | I.emit(Func); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 908 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 909 | for (const Inst &I : Insts) { |
| 910 | if (I.isDeleted()) |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 911 | continue; |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 912 | if (I.isRedundantAssign()) { |
| 913 | // Usually, redundant assignments end the live range of the src |
| 914 | // variable and begin the live range of the dest variable, with |
| 915 | // no net effect on the liveness of their register. However, if |
| 916 | // the register allocator infers the AllowOverlap condition, |
| 917 | // then this may be a redundant assignment that does not end the |
| 918 | // src variable's live range, in which case the active variable |
| 919 | // count for that register needs to be bumped. That normally |
| 920 | // would have happened as part of emitLiveRangesEnded(), but |
| 921 | // that isn't called for redundant assignments. |
| 922 | Variable *Dest = I.getDest(); |
| 923 | if (DecorateAsm && Dest->hasReg() && !I.isLastUse(I.getSrc(0))) |
| 924 | ++LiveRegCount[Dest->getRegNum()]; |
Jim Stichnoth | 3d44fe8 | 2014-11-01 10:10:18 -0700 | [diff] [blame] | 925 | continue; |
Jim Stichnoth | b82baf2 | 2015-05-27 10:41:07 -0700 | [diff] [blame] | 926 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 927 | I.emit(Func); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 928 | if (DecorateAsm) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 929 | emitLiveRangesEnded(Str, Func, &I, LiveRegCount); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 930 | Str << "\n"; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 931 | updateStats(Func, &I); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 932 | } |
Jim Stichnoth | 4175b2a | 2015-04-30 12:26:22 -0700 | [diff] [blame] | 933 | if (DecorateAsm) { |
| 934 | const bool IsLiveIn = false; |
| 935 | emitRegisterUsage(Str, Func, this, IsLiveIn, LiveRegCount); |
| 936 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 939 | // Helper class for emitIAS(). |
| 940 | namespace { |
| 941 | class BundleEmitHelper { |
| 942 | BundleEmitHelper() = delete; |
| 943 | BundleEmitHelper(const BundleEmitHelper &) = delete; |
| 944 | BundleEmitHelper &operator=(const BundleEmitHelper &) = delete; |
| 945 | |
| 946 | public: |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 947 | BundleEmitHelper(Assembler *Asm, TargetLowering *Target, |
| 948 | const InstList &Insts) |
| 949 | : Asm(Asm), Target(Target), End(Insts.end()), BundleLockStart(End), |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 950 | BundleSize(1 << Asm->getBundleAlignLog2Bytes()), |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 951 | BundleMaskLo(BundleSize - 1), BundleMaskHi(~BundleMaskLo) {} |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 952 | // Check whether we're currently within a bundle_lock region. |
| 953 | bool isInBundleLockRegion() const { return BundleLockStart != End; } |
| 954 | // Check whether the current bundle_lock region has the align_to_end |
| 955 | // option. |
| 956 | bool isAlignToEnd() const { |
| 957 | assert(isInBundleLockRegion()); |
| 958 | return llvm::cast<InstBundleLock>(getBundleLockStart())->getOption() == |
| 959 | InstBundleLock::Opt_AlignToEnd; |
| 960 | } |
| 961 | // Check whether the entire bundle_lock region falls within the same |
| 962 | // bundle. |
| 963 | bool isSameBundle() const { |
| 964 | assert(isInBundleLockRegion()); |
| 965 | return SizeSnapshotPre == SizeSnapshotPost || |
| 966 | (SizeSnapshotPre & BundleMaskHi) == |
| 967 | ((SizeSnapshotPost - 1) & BundleMaskHi); |
| 968 | } |
| 969 | // Get the bundle alignment of the first instruction of the |
| 970 | // bundle_lock region. |
| 971 | intptr_t getPreAlignment() const { |
| 972 | assert(isInBundleLockRegion()); |
| 973 | return SizeSnapshotPre & BundleMaskLo; |
| 974 | } |
| 975 | // Get the bundle alignment of the first instruction past the |
| 976 | // bundle_lock region. |
| 977 | intptr_t getPostAlignment() const { |
| 978 | assert(isInBundleLockRegion()); |
| 979 | return SizeSnapshotPost & BundleMaskLo; |
| 980 | } |
| 981 | // Get the iterator pointing to the bundle_lock instruction, e.g. to |
| 982 | // roll back the instruction iteration to that point. |
| 983 | InstList::const_iterator getBundleLockStart() const { |
| 984 | assert(isInBundleLockRegion()); |
| 985 | return BundleLockStart; |
| 986 | } |
| 987 | // Set up bookkeeping when the bundle_lock instruction is first |
| 988 | // processed. |
| 989 | void enterBundleLock(InstList::const_iterator I) { |
| 990 | assert(!isInBundleLockRegion()); |
| 991 | BundleLockStart = I; |
| 992 | SizeSnapshotPre = Asm->getBufferSize(); |
| 993 | Asm->setPreliminary(true); |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 994 | Target->snapshotEmitState(); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 995 | assert(isInBundleLockRegion()); |
| 996 | } |
| 997 | // Update bookkeeping when the bundle_unlock instruction is |
| 998 | // processed. |
| 999 | void enterBundleUnlock() { |
| 1000 | assert(isInBundleLockRegion()); |
| 1001 | SizeSnapshotPost = Asm->getBufferSize(); |
| 1002 | } |
| 1003 | // Update bookkeeping when we are completely finished with the |
| 1004 | // bundle_lock region. |
| 1005 | void leaveBundleLockRegion() { BundleLockStart = End; } |
| 1006 | // Check whether the instruction sequence fits within the current |
| 1007 | // bundle, and if not, add nop padding to the end of the current |
| 1008 | // bundle. |
| 1009 | void padToNextBundle() { |
| 1010 | assert(isInBundleLockRegion()); |
| 1011 | if (!isSameBundle()) { |
| 1012 | intptr_t PadToNextBundle = BundleSize - getPreAlignment(); |
| 1013 | Asm->padWithNop(PadToNextBundle); |
| 1014 | SizeSnapshotPre += PadToNextBundle; |
| 1015 | SizeSnapshotPost += PadToNextBundle; |
| 1016 | assert((Asm->getBufferSize() & BundleMaskLo) == 0); |
| 1017 | assert(Asm->getBufferSize() == SizeSnapshotPre); |
| 1018 | } |
| 1019 | } |
| 1020 | // If align_to_end is specified, add padding such that the |
| 1021 | // instruction sequences ends precisely at a bundle boundary. |
| 1022 | void padForAlignToEnd() { |
| 1023 | assert(isInBundleLockRegion()); |
| 1024 | if (isAlignToEnd()) { |
| 1025 | if (intptr_t Offset = getPostAlignment()) { |
| 1026 | Asm->padWithNop(BundleSize - Offset); |
| 1027 | SizeSnapshotPre = Asm->getBufferSize(); |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | // Update bookkeeping when rolling back for the second pass. |
| 1032 | void rollback() { |
| 1033 | assert(isInBundleLockRegion()); |
| 1034 | Asm->setBufferSize(SizeSnapshotPre); |
| 1035 | Asm->setPreliminary(false); |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 1036 | Target->rollbackEmitState(); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | private: |
| 1040 | Assembler *const Asm; |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 1041 | TargetLowering *const Target; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1042 | // End is a sentinel value such that BundleLockStart==End implies |
| 1043 | // that we are not in a bundle_lock region. |
| 1044 | const InstList::const_iterator End; |
| 1045 | InstList::const_iterator BundleLockStart; |
| 1046 | const intptr_t BundleSize; |
| 1047 | // Masking with BundleMaskLo identifies an address's bundle offset. |
| 1048 | const intptr_t BundleMaskLo; |
| 1049 | // Masking with BundleMaskHi identifies an address's bundle. |
| 1050 | const intptr_t BundleMaskHi; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1051 | intptr_t SizeSnapshotPre = 0; |
| 1052 | intptr_t SizeSnapshotPost = 0; |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1053 | }; |
| 1054 | |
| 1055 | } // end of anonymous namespace |
| 1056 | |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1057 | void CfgNode::emitIAS(Cfg *Func) const { |
| 1058 | Func->setCurrentNode(this); |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 1059 | Assembler *Asm = Func->getAssembler<>(); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1060 | // TODO(stichnot): When sandboxing, defer binding the node label |
| 1061 | // until just before the first instruction is emitted, to reduce the |
| 1062 | // chance that a padding nop is a branch target. |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 1063 | Asm->bindCfgNodeLabel(getIndex()); |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1064 | for (const Inst &I : Phis) { |
| 1065 | if (I.isDeleted()) |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1066 | continue; |
| 1067 | // Emitting a Phi instruction should cause an error. |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1068 | I.emitIAS(Func); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1069 | } |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1070 | |
| 1071 | // Do the simple emission if not sandboxed. |
| 1072 | if (!Func->getContext()->getFlags().getUseSandboxing()) { |
| 1073 | for (const Inst &I : Insts) { |
| 1074 | if (!I.isDeleted() && !I.isRedundantAssign()) { |
| 1075 | I.emitIAS(Func); |
| 1076 | updateStats(Func, &I); |
| 1077 | } |
| 1078 | } |
| 1079 | return; |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1080 | } |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1081 | |
| 1082 | // The remainder of the function handles emission with sandboxing. |
| 1083 | // There are explicit bundle_lock regions delimited by bundle_lock |
| 1084 | // and bundle_unlock instructions. All other instructions are |
| 1085 | // treated as an implicit one-instruction bundle_lock region. |
| 1086 | // Emission is done twice for each bundle_lock region. The first |
| 1087 | // pass is a preliminary pass, after which we can figure out what |
| 1088 | // nop padding is needed, then roll back, and make the final pass. |
| 1089 | // |
| 1090 | // Ideally, the first pass would be speculative and the second pass |
| 1091 | // would only be done if nop padding were needed, but the structure |
| 1092 | // of the integrated assembler makes it hard to roll back the state |
| 1093 | // of label bindings, label links, and relocation fixups. Instead, |
| 1094 | // the first pass just disables all mutation of that state. |
| 1095 | |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 1096 | BundleEmitHelper Helper(Asm, Func->getTarget(), Insts); |
Jim Stichnoth | 9f42d8c | 2015-02-20 09:20:14 -0800 | [diff] [blame] | 1097 | InstList::const_iterator End = Insts.end(); |
| 1098 | // Retrying indicates that we had to roll back to the bundle_lock |
| 1099 | // instruction to apply padding before the bundle_lock sequence. |
| 1100 | bool Retrying = false; |
| 1101 | for (InstList::const_iterator I = Insts.begin(); I != End; ++I) { |
| 1102 | if (I->isDeleted() || I->isRedundantAssign()) |
| 1103 | continue; |
| 1104 | |
| 1105 | if (llvm::isa<InstBundleLock>(I)) { |
| 1106 | // Set up the initial bundle_lock state. This should not happen |
| 1107 | // while retrying, because the retry rolls back to the |
| 1108 | // instruction following the bundle_lock instruction. |
| 1109 | assert(!Retrying); |
| 1110 | Helper.enterBundleLock(I); |
| 1111 | continue; |
| 1112 | } |
| 1113 | |
| 1114 | if (llvm::isa<InstBundleUnlock>(I)) { |
| 1115 | Helper.enterBundleUnlock(); |
| 1116 | if (Retrying) { |
| 1117 | // Make sure all instructions are in the same bundle. |
| 1118 | assert(Helper.isSameBundle()); |
| 1119 | // If align_to_end is specified, make sure the next |
| 1120 | // instruction begins the bundle. |
| 1121 | assert(!Helper.isAlignToEnd() || Helper.getPostAlignment() == 0); |
| 1122 | Helper.leaveBundleLockRegion(); |
| 1123 | Retrying = false; |
| 1124 | } else { |
| 1125 | // This is the first pass, so roll back for the retry pass. |
| 1126 | Helper.rollback(); |
| 1127 | // Pad to the next bundle if the instruction sequence crossed |
| 1128 | // a bundle boundary. |
| 1129 | Helper.padToNextBundle(); |
| 1130 | // Insert additional padding to make AlignToEnd work. |
| 1131 | Helper.padForAlignToEnd(); |
| 1132 | // Prepare for the retry pass after padding is done. |
| 1133 | Retrying = true; |
| 1134 | I = Helper.getBundleLockStart(); |
| 1135 | } |
| 1136 | continue; |
| 1137 | } |
| 1138 | |
| 1139 | // I points to a non bundle_lock/bundle_unlock instruction. |
| 1140 | if (Helper.isInBundleLockRegion()) { |
| 1141 | I->emitIAS(Func); |
| 1142 | // Only update stats during the final pass. |
| 1143 | if (Retrying) |
| 1144 | updateStats(Func, I); |
| 1145 | } else { |
| 1146 | // Treat it as though there were an implicit bundle_lock and |
| 1147 | // bundle_unlock wrapping the instruction. |
| 1148 | Helper.enterBundleLock(I); |
| 1149 | I->emitIAS(Func); |
| 1150 | Helper.enterBundleUnlock(); |
| 1151 | Helper.rollback(); |
| 1152 | Helper.padToNextBundle(); |
| 1153 | I->emitIAS(Func); |
| 1154 | updateStats(Func, I); |
| 1155 | Helper.leaveBundleLockRegion(); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | // Don't allow bundle locking across basic blocks, to keep the |
| 1160 | // backtracking mechanism simple. |
| 1161 | assert(!Helper.isInBundleLockRegion()); |
| 1162 | assert(!Retrying); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1165 | void CfgNode::dump(Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 1166 | if (!ALLOW_DUMP) |
| 1167 | return; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1168 | Func->setCurrentNode(this); |
| 1169 | Ostream &Str = Func->getContext()->getStrDump(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1170 | Liveness *Liveness = Func->getLiveness(); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1171 | if (Func->isVerbose(IceV_Instructions)) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1172 | Str << getName() << ":\n"; |
| 1173 | } |
| 1174 | // Dump list of predecessor nodes. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1175 | if (Func->isVerbose(IceV_Preds) && !InEdges.empty()) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1176 | Str << " // preds = "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1177 | bool First = true; |
| 1178 | for (CfgNode *I : InEdges) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1179 | if (!First) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1180 | Str << ", "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1181 | First = false; |
| 1182 | Str << "%" << I->getName(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1183 | } |
| 1184 | Str << "\n"; |
| 1185 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1186 | // Dump the live-in variables. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 1187 | LivenessBV LiveIn; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1188 | if (Liveness) |
| 1189 | LiveIn = Liveness->getLiveIn(this); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1190 | if (Func->isVerbose(IceV_Liveness) && !LiveIn.empty()) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1191 | Str << " // LiveIn:"; |
| 1192 | for (SizeT i = 0; i < LiveIn.size(); ++i) { |
| 1193 | if (LiveIn[i]) { |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 1194 | Variable *Var = Liveness->getVariable(i, this); |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 1195 | Str << " %" << Var->getName(Func); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1196 | if (Func->isVerbose(IceV_RegOrigins) && Var->hasReg()) { |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 1197 | Str << ":" |
| 1198 | << Func->getTarget()->getRegName(Var->getRegNum(), |
| 1199 | Var->getType()); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 1200 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | Str << "\n"; |
| 1204 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1205 | // Dump each instruction. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1206 | if (Func->isVerbose(IceV_Instructions)) { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 1207 | for (const Inst &I : Phis) |
| 1208 | I.dumpDecorated(Func); |
| 1209 | for (const Inst &I : Insts) |
| 1210 | I.dumpDecorated(Func); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1211 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1212 | // Dump the live-out variables. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 1213 | LivenessBV LiveOut; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1214 | if (Liveness) |
| 1215 | LiveOut = Liveness->getLiveOut(this); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1216 | if (Func->isVerbose(IceV_Liveness) && !LiveOut.empty()) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1217 | Str << " // LiveOut:"; |
| 1218 | for (SizeT i = 0; i < LiveOut.size(); ++i) { |
| 1219 | if (LiveOut[i]) { |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 1220 | Variable *Var = Liveness->getVariable(i, this); |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 1221 | Str << " %" << Var->getName(Func); |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1222 | if (Func->isVerbose(IceV_RegOrigins) && Var->hasReg()) { |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 1223 | Str << ":" |
| 1224 | << Func->getTarget()->getRegName(Var->getRegNum(), |
| 1225 | Var->getType()); |
Jim Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 1226 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | Str << "\n"; |
| 1230 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1231 | // Dump list of successor nodes. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 1232 | if (Func->isVerbose(IceV_Succs)) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1233 | Str << " // succs = "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1234 | bool First = true; |
| 1235 | for (CfgNode *I : OutEdges) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 1236 | if (!First) |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1237 | Str << ", "; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 1238 | First = false; |
| 1239 | Str << "%" << I->getName(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1240 | } |
| 1241 | Str << "\n"; |
| 1242 | } |
| 1243 | } |
| 1244 | |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1245 | void CfgNode::profileExecutionCount(VariableDeclaration *Var) { |
| 1246 | constexpr char RMW_I64[] = "llvm.nacl.atomic.rmw.i64"; |
| 1247 | |
| 1248 | GlobalContext *Context = Func->getContext(); |
| 1249 | |
| 1250 | bool BadIntrinsic = false; |
| 1251 | const Intrinsics::FullIntrinsicInfo *Info = |
| 1252 | Context->getIntrinsicsInfo().find(RMW_I64, BadIntrinsic); |
| 1253 | assert(!BadIntrinsic); |
| 1254 | assert(Info != nullptr); |
| 1255 | |
| 1256 | Operand *RMWI64Name = Context->getConstantExternSym(RMW_I64); |
John Porto | 8b1a705 | 2015-06-17 13:20:08 -0700 | [diff] [blame] | 1257 | constexpr RelocOffsetT Offset = 0; |
| 1258 | constexpr bool SuppressMangling = true; |
| 1259 | Constant *Counter = |
| 1260 | Context->getConstantSym(Offset, Var->getName(), SuppressMangling); |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 1261 | Constant *AtomicRMWOp = Context->getConstantInt32(Intrinsics::AtomicAdd); |
| 1262 | Constant *One = Context->getConstantInt64(1); |
| 1263 | Constant *OrderAcquireRelease = |
| 1264 | Context->getConstantInt32(Intrinsics::MemoryOrderAcquireRelease); |
| 1265 | |
| 1266 | InstIntrinsicCall *Inst = InstIntrinsicCall::create( |
| 1267 | Func, 5, Func->makeVariable(IceType_i64), RMWI64Name, Info->Info); |
| 1268 | Inst->addArg(AtomicRMWOp); |
| 1269 | Inst->addArg(Counter); |
| 1270 | Inst->addArg(One); |
| 1271 | Inst->addArg(OrderAcquireRelease); |
| 1272 | Insts.push_front(Inst); |
| 1273 | } |
| 1274 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1275 | } // end of namespace Ice |