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