Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceCfgNode.cpp - Basic block (node) implementation -----===// | 1 //===- subzero/src/IceCfgNode.cpp - Basic block (node) implementation -----===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the CfgNode class, including the complexities | 10 // This file implements the CfgNode class, including the complexities |
| 11 // of instruction insertion and in-edge calculation. | 11 // of instruction insertion and in-edge calculation. |
| 12 // | 12 // |
| 13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
| 14 | 14 |
| 15 #include "assembler.h" | 15 #include "assembler.h" |
| 16 #include "IceCfg.h" | 16 #include "IceCfg.h" |
| 17 #include "IceCfgNode.h" | 17 #include "IceCfgNode.h" |
| 18 #include "IceInst.h" | 18 #include "IceInst.h" |
| 19 #include "IceLiveness.h" | 19 #include "IceLiveness.h" |
| 20 #include "IceOperand.h" | 20 #include "IceOperand.h" |
| 21 #include "IceTargetLowering.h" | 21 #include "IceTargetLowering.h" |
| 22 | 22 |
| 23 namespace Ice { | 23 namespace Ice { |
| 24 | 24 |
| 25 CfgNode::CfgNode(Cfg *Func, SizeT LabelNumber, IceString Name) | 25 CfgNode::CfgNode(Cfg *Func, SizeT LabelNumber, IceString Name) |
| 26 : Func(Func), Number(LabelNumber), Name(Name), HasReturn(false), | 26 : Func(Func), Number(LabelNumber), Name(Name), HasReturn(false), |
| 27 InstCountEstimate(0) {} | 27 NeedsPlacement(false), InstCountEstimate(0) {} |
| 28 | 28 |
| 29 // Returns the name the node was created with. If no name was given, | 29 // Returns the name the node was created with. If no name was given, |
| 30 // it synthesizes a (hopefully) unique name. | 30 // it synthesizes a (hopefully) unique name. |
| 31 IceString CfgNode::getName() const { | 31 IceString CfgNode::getName() const { |
| 32 if (!Name.empty()) | 32 if (!Name.empty()) |
| 33 return Name; | 33 return Name; |
| 34 return "__" + std::to_string(getIndex()); | 34 return "__" + std::to_string(getIndex()); |
| 35 } | 35 } |
| 36 | 36 |
| 37 // Adds an instruction to either the Phi list or the regular | 37 // Adds an instruction to either the Phi list or the regular |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Deletes the phi instructions after the loads and stores are placed. | 201 // Deletes the phi instructions after the loads and stores are placed. |
| 202 void CfgNode::deletePhis() { | 202 void CfgNode::deletePhis() { |
| 203 for (InstPhi *I : Phis) | 203 for (InstPhi *I : Phis) |
| 204 I->setDeleted(); | 204 I->setDeleted(); |
| 205 } | 205 } |
| 206 | 206 |
| 207 // Splits the edge from Pred to this node by creating a new node and | |
| 208 // hooking up the in and out edges appropriately. (The EdgeIndex | |
| 209 // parameter is only used to make the new node's name unique when | |
| 210 // there are multiple edges between the same pair of nodes.) The new | |
| 211 // node's instruction list is initialized to the empty list, with no | |
| 212 // terminator instruction. If there are multiple edges from Pred to | |
| 213 // this node, only one edge is split, and the particular choice of | |
| 214 // edge is undefined. This could happen with a switch instruction, or | |
| 215 // a conditional branch that weirdly has both branches to the same | |
| 216 // place. TODO(stichnot,kschimpf): Figure out whether this is legal | |
| 217 // in the LLVM IR or the PNaCl bitcode, and if so, we need to | |
| 218 // establish a strong relationship among the ordering of Pred's | |
| 219 // out-edge list, this node's in-edge list, and the Phi instruction's | |
|
jvoung (off chromium)
2014/10/27 22:12:20
So if this happens, the phi lowering might be wron
Jim Stichnoth
2014/10/28 01:20:14
After thinking about this, my belief is that if th
| |
| 220 // operand list. | |
| 221 CfgNode *CfgNode::splitIncomingEdge(CfgNode *Pred, SizeT EdgeIndex) { | |
| 222 CfgNode *NewNode = | |
| 223 Func->makeNode("split_" + Pred->getName() + "_" + getName() + "_" + | |
| 224 std::to_string(EdgeIndex)); | |
| 225 // The new node is added to the end of the node list, and will later | |
| 226 // need to be sorted into a reasonable topological order. | |
| 227 NewNode->setNeedsPlacement(true); | |
| 228 // Repoint Pred's out-edge. | |
| 229 bool Found = false; | |
| 230 for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); | |
| 231 !Found && I != E; ++I) { | |
| 232 if (*I == this) { | |
| 233 *I = NewNode; | |
| 234 NewNode->InEdges.push_back(Pred); | |
| 235 Found = true; | |
| 236 } | |
| 237 } | |
| 238 assert(Found); | |
| 239 // Repoint this node's in-edge. | |
| 240 Found = false; | |
| 241 for (auto I = InEdges.begin(), E = InEdges.end(); !Found && I != E; ++I) { | |
| 242 if (*I == Pred) { | |
| 243 *I = NewNode; | |
| 244 NewNode->OutEdges.push_back(this); | |
| 245 Found = true; | |
| 246 } | |
| 247 } | |
| 248 assert(Found); | |
| 249 // Repoint a suitable branch instruction's target. | |
| 250 Found = false; | |
| 251 for (auto I = Pred->getInsts().rbegin(), E = Pred->getInsts().rend(); | |
| 252 !Found && I != E; ++I) { | |
| 253 if (!(*I)->isDeleted()) { | |
| 254 Found = (*I)->repointEdge(this, NewNode); | |
| 255 } | |
| 256 } | |
| 257 assert(Found); | |
| 258 return NewNode; | |
| 259 } | |
| 260 | |
| 261 namespace { | |
| 262 | |
| 263 // Helper function used by advancedPhiLowering(). | |
| 264 bool sameVarOrReg(const Variable *Var, const Operand *Opnd) { | |
| 265 if (Var == Opnd) | |
| 266 return true; | |
| 267 if (const auto Var2 = llvm::dyn_cast<Variable>(Opnd)) { | |
| 268 if (Var->hasReg() && Var->getRegNum() == Var2->getRegNum()) | |
| 269 return true; | |
| 270 } | |
| 271 return false; | |
| 272 } | |
| 273 | |
| 274 } // end of anonymous namespace | |
| 275 | |
| 276 // This the "advanced" version of Phi lowering for a basic block, in | |
| 277 // contrast to the simple version that lowers through assignments | |
| 278 // involving temporaries. | |
| 279 // | |
| 280 // All Phi instructions in a basic block are conceptually executed in | |
| 281 // parallel. However, if we lower Phis early and commit to a | |
| 282 // sequential ordering, we may end up creating unnecessary | |
| 283 // interferences which lead to worse register allocation. Delaying | |
| 284 // Phi scheduling until after register allocation can helps unless | |
|
jvoung (off chromium)
2014/10/27 22:12:21
"can helps" -> "can help"
Jim Stichnoth
2014/10/28 01:20:14
Done.
| |
| 285 // there are no free registers for shuffling registers or stack slots | |
| 286 // and spilling becomes necessary. | |
| 287 // | |
| 288 // The advanced Phi lowering starts by finding a topological sort of | |
| 289 // the Phi instructions, where "A=B" comes before "B=C" due to the | |
| 290 // anti-dependence on B. If a topological sort is not possible due to | |
| 291 // a cycle, the cycle is broken by introducing a non-parallel | |
| 292 // temporary. For example, a cycle arising from a permutation like | |
| 293 // "A=B;B=C;C=A" can become "T=A;A=B;B=C;C=T". All else being equal, | |
| 294 // prefer to schedule assignments with register-allocated Src operands | |
| 295 // earlier, in case that register becomes free afterwards; and prefer | |
|
jvoung (off chromium)
2014/10/27 22:12:21
", and" instead of "; and" ?
Jim Stichnoth
2014/10/28 01:20:14
Done.
| |
| 296 // to schedule assignments with register-allocated Dest variables | |
| 297 // later, to keep that register free for longer. | |
| 298 // | |
| 299 // Once the ordering is determined, the Cfg edge is split and the | |
| 300 // assignment list is lowered by the target lowering layer. The | |
| 301 // specific placement of the new node within the Cfg node list is | |
| 302 // deferred until later, including after empty node contraction. | |
| 303 void CfgNode::advancedPhiLowering() { | |
| 304 if (getPhis().empty()) | |
| 305 return; | |
| 306 | |
| 307 // Count the number of non-deleted Phi instructions. | |
| 308 struct { | |
| 309 InstPhi *Phi; | |
| 310 Variable *Dest; | |
| 311 Operand *Src; | |
| 312 bool Processed; | |
| 313 size_t NumPred; // number of entries whose Src is this Dest | |
| 314 int32_t Weight; // preference for topological order | |
| 315 } Desc[getPhis().size()]; | |
| 316 | |
| 317 size_t NumPhis = 0; | |
| 318 for (InstPhi *Inst : getPhis()) { | |
| 319 if (!Inst->isDeleted()) { | |
| 320 Desc[NumPhis].Phi = Inst; | |
| 321 Desc[NumPhis].Dest = Inst->getDest(); | |
| 322 ++NumPhis; | |
| 323 } | |
| 324 } | |
| 325 if (NumPhis == 0) | |
| 326 return; | |
| 327 | |
| 328 SizeT InEdgeIndex = 0; | |
| 329 for (CfgNode *Pred : InEdges) { | |
| 330 CfgNode *Split = splitIncomingEdge(Pred, InEdgeIndex++); | |
| 331 AssignList Assignments; | |
| 332 | |
| 333 // First pass computes Src and initializes NumPred. | |
| 334 for (size_t I = 0; I < NumPhis; ++I) { | |
| 335 Desc[I].Src = Desc[I].Phi->getOperandForTarget(Pred); | |
| 336 Desc[I].Processed = false; | |
| 337 Desc[I].NumPred = 0; | |
| 338 } | |
| 339 // Second pass computes NumPred by comparing every pair of Phi | |
| 340 // instructions. | |
| 341 for (size_t I = 0; I < NumPhis; ++I) { | |
| 342 if (Desc[I].Processed) | |
|
jvoung (off chromium)
2014/10/27 22:12:21
It wasn't clear to me why Processed would be true
Jim Stichnoth
2014/10/28 01:20:14
You're right - I think I over-conservatively added
Jim Stichnoth
2014/10/30 00:25:25
BTW, these checks are now back after moving the re
| |
| 343 continue; | |
| 344 const Variable *Dest = Desc[I].Dest; | |
| 345 for (size_t J = 0; J < NumPhis; ++J) { | |
| 346 if (Desc[J].Processed) | |
| 347 continue; | |
| 348 if (I != J) { | |
| 349 // There shouldn't be two Phis with the same Dest variable | |
| 350 // or register. | |
| 351 assert(!sameVarOrReg(Dest, Desc[J].Dest)); | |
| 352 } | |
| 353 const Operand *Src = Desc[J].Src; | |
| 354 if (sameVarOrReg(Dest, Src)) | |
| 355 ++Desc[I].NumPred; | |
| 356 } | |
| 357 } | |
| 358 | |
| 359 // Another pass to compute initial Weight values. | |
| 360 | |
| 361 // Always pick NumPred=0 over NumPred>0. | |
| 362 const int32_t WeightNoPreds = 4; | |
| 363 // Prefer Src as a register because the register might free up. | |
| 364 const int32_t WeightSrcIsReg = 2; | |
| 365 // Prefer Dest not as a register because the register stays free | |
| 366 // longer. | |
| 367 const int32_t WeightDestNotReg = 1; | |
| 368 | |
| 369 for (size_t I = 0; I < NumPhis; ++I) { | |
| 370 if (Desc[I].Processed) | |
| 371 continue; | |
| 372 int32_t Weight = 0; | |
| 373 if (Desc[I].NumPred == 0) | |
| 374 Weight += WeightNoPreds; | |
| 375 if (auto Var = llvm::dyn_cast<Variable>(Desc[I].Src)) | |
| 376 if (Var->hasReg()) | |
| 377 Weight += WeightSrcIsReg; | |
| 378 if (!Desc[I].Dest->hasReg()) | |
| 379 Weight += WeightDestNotReg; | |
| 380 Desc[I].Weight = Weight; | |
| 381 } | |
| 382 | |
| 383 // Repeatedly choose and process the best candidate in the | |
| 384 // topological sort, until no candidates remain. This | |
| 385 // implementation is O(N^2) where N is the number of Phi | |
| 386 // instructions, but with a small constant factor compared to a | |
| 387 // likely implementation of O(N) topological sort. | |
| 388 for (size_t Remaining = NumPhis; Remaining; --Remaining) { | |
| 389 size_t BestIndex = 0; | |
| 390 int32_t BestWeight = -1; | |
| 391 // Find the best candidate. | |
| 392 for (size_t I = 0; I < NumPhis; ++I) { | |
| 393 if (Desc[I].Processed) | |
| 394 continue; | |
| 395 int32_t Weight = 0; | |
| 396 Weight = Desc[I].Weight; | |
| 397 if (Weight > BestWeight) { | |
| 398 BestIndex = I; | |
| 399 BestWeight = Weight; | |
| 400 } | |
| 401 } | |
| 402 assert(BestWeight >= 0); | |
| 403 assert(Desc[BestIndex].NumPred <= 1); | |
| 404 Variable *Dest = Desc[BestIndex].Dest; | |
| 405 Operand *Src = Desc[BestIndex].Src; | |
| 406 // Break a cycle by introducing a temporary. Except don't | |
| 407 // bother for a self-assignment, which looks like a self-cycle | |
| 408 // with NumPred=1, since it can be left alone and will | |
| 409 // eventually be removed as a redundant assignment. | |
| 410 bool IsSelfAssignment = sameVarOrReg(Dest, Src); | |
| 411 if (!IsSelfAssignment && Desc[BestIndex].NumPred) { | |
| 412 bool Found = false; | |
| 413 // If the target instruction "A=B" is part of a cycle, find | |
| 414 // the "X=A" assignment in the cycle because it will have to | |
| 415 // be rewritten as "X=tmp". | |
| 416 for (size_t J = 0; !Found && J < NumPhis; ++J) { | |
| 417 if (Desc[J].Processed) | |
| 418 continue; | |
| 419 Operand *OtherSrc = Desc[J].Src; | |
| 420 if (Desc[J].NumPred && sameVarOrReg(Dest, OtherSrc)) { | |
| 421 // Set Tmp's optional Name parameter to something like | |
|
jvoung (off chromium)
2014/10/27 22:12:21
Did you want to do the name-setting here, or mark
Jim Stichnoth
2014/10/28 01:20:14
I meant it sort of as a weak TODO. But thinking a
| |
| 422 // "__split" + std::to_string(Func->getNumVariables()) | |
| 423 // for easier identification during debugging if desired. | |
| 424 Variable *Tmp = Func->makeVariable(OtherSrc->getType()); | |
| 425 Tmp->setNeedsStackSlot(); | |
| 426 Assignments.push_back(InstAssign::create(Func, Tmp, OtherSrc)); | |
| 427 Desc[J].Src = Tmp; | |
| 428 Found = true; | |
| 429 } | |
| 430 } | |
| 431 assert(Found); | |
| 432 } | |
| 433 // Now that a cycle (if any) has been broken, create the actual | |
| 434 // assignment. Except if Dest and Src are syntactically the | |
| 435 // same, don't bother adding the assignment, because in all | |
| 436 // respects it would be redundant, and if Dest/Src are on the | |
| 437 // stack, the target lowering may naively decide to lower it | |
| 438 // using a temporary register. | |
| 439 if (Dest != Src) | |
| 440 Assignments.push_back(InstAssign::create(Func, Dest, Src)); | |
| 441 // Update NumPred for all Phi assignments using this Phi's Src | |
| 442 // as their Dest variable. Also update Weight if NumPred | |
| 443 // dropped from 1 to 0. | |
| 444 if (Variable *Var = llvm::dyn_cast<Variable>(Src)) { | |
|
jvoung (off chromium)
2014/10/27 22:12:21
nit: could auto this too
Jim Stichnoth
2014/10/28 01:20:14
Done.
| |
| 445 for (size_t I = 0; I < NumPhis; ++I) { | |
| 446 if (Desc[I].Processed) | |
| 447 continue; | |
| 448 if (sameVarOrReg(Var, Desc[I].Dest)) { | |
| 449 if (--Desc[I].NumPred == 0) | |
| 450 Desc[I].Weight += WeightNoPreds; | |
| 451 } | |
| 452 } | |
| 453 } | |
| 454 Desc[BestIndex].Processed = true; | |
| 455 } | |
| 456 | |
| 457 Func->getTarget()->lowerPhiAssignments(Split, Assignments); | |
| 458 | |
| 459 // Renumber the instructions to be monotonically increasing so | |
| 460 // that addNode() doesn't assert when multi-definitions are added | |
| 461 // out of order. | |
| 462 Split->renumberInstructions(); | |
| 463 Func->getVMetadata()->addNode(Split); | |
| 464 } | |
| 465 | |
| 466 for (InstPhi *Inst : getPhis()) | |
| 467 Inst->setDeleted(); | |
| 468 } | |
| 469 | |
| 207 // Does address mode optimization. Pass each instruction to the | 470 // Does address mode optimization. Pass each instruction to the |
| 208 // TargetLowering object. If it returns a new instruction | 471 // TargetLowering object. If it returns a new instruction |
| 209 // (representing the optimized address mode), then insert the new | 472 // (representing the optimized address mode), then insert the new |
| 210 // instruction and delete the old. | 473 // instruction and delete the old. |
| 211 void CfgNode::doAddressOpt() { | 474 void CfgNode::doAddressOpt() { |
| 212 TargetLowering *Target = Func->getTarget(); | 475 TargetLowering *Target = Func->getTarget(); |
| 213 LoweringContext &Context = Target->getContext(); | 476 LoweringContext &Context = Target->getContext(); |
| 214 Context.init(this); | 477 Context.init(this); |
| 215 while (!Context.atEnd()) { | 478 while (!Context.atEnd()) { |
| 216 Target->doAddressOpt(); | 479 Target->doAddressOpt(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 233 Context.advanceNext(); | 496 Context.advanceNext(); |
| 234 Context.advanceCur(); | 497 Context.advanceCur(); |
| 235 Target->doNopInsertion(); | 498 Target->doNopInsertion(); |
| 236 } | 499 } |
| 237 | 500 |
| 238 // Drives the target lowering. Passes the current instruction and the | 501 // Drives the target lowering. Passes the current instruction and the |
| 239 // next non-deleted instruction for target lowering. | 502 // next non-deleted instruction for target lowering. |
| 240 void CfgNode::genCode() { | 503 void CfgNode::genCode() { |
| 241 TargetLowering *Target = Func->getTarget(); | 504 TargetLowering *Target = Func->getTarget(); |
| 242 LoweringContext &Context = Target->getContext(); | 505 LoweringContext &Context = Target->getContext(); |
| 243 // Lower only the regular instructions. Defer the Phi instructions. | 506 // Lower the regular instructions. |
| 244 Context.init(this); | 507 Context.init(this); |
| 245 while (!Context.atEnd()) { | 508 while (!Context.atEnd()) { |
| 246 InstList::iterator Orig = Context.getCur(); | 509 InstList::iterator Orig = Context.getCur(); |
| 247 if (llvm::isa<InstRet>(*Orig)) | 510 if (llvm::isa<InstRet>(*Orig)) |
| 248 setHasReturn(); | 511 setHasReturn(); |
| 249 Target->lower(); | 512 Target->lower(); |
| 250 // Ensure target lowering actually moved the cursor. | 513 // Ensure target lowering actually moved the cursor. |
| 251 assert(Context.getCur() != Orig); | 514 assert(Context.getCur() != Orig); |
| 252 } | 515 } |
| 516 // Do preliminary lowering of the Phi instructions. | |
| 517 Target->prelowerPhis(); | |
| 253 } | 518 } |
| 254 | 519 |
| 255 void CfgNode::livenessLightweight() { | 520 void CfgNode::livenessLightweight() { |
| 256 SizeT NumVars = Func->getNumVariables(); | 521 SizeT NumVars = Func->getNumVariables(); |
| 257 LivenessBV Live(NumVars); | 522 LivenessBV Live(NumVars); |
| 258 // Process regular instructions in reverse order. | 523 // Process regular instructions in reverse order. |
| 259 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. | 524 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. |
| 260 for (auto I = Insts.rbegin(), E = Insts.rend(); I != E; ++I) { | 525 for (auto I = Insts.rbegin(), E = Insts.rend(); I != E; ++I) { |
| 261 if ((*I)->isDeleted()) | 526 if ((*I)->isDeleted()) |
| 262 continue; | 527 continue; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 293 // Initialize Live to be the union of all successors' LiveIn. | 558 // Initialize Live to be the union of all successors' LiveIn. |
| 294 for (CfgNode *Succ : OutEdges) { | 559 for (CfgNode *Succ : OutEdges) { |
| 295 Live |= Liveness->getLiveIn(Succ); | 560 Live |= Liveness->getLiveIn(Succ); |
| 296 // Mark corresponding argument of phis in successor as live. | 561 // Mark corresponding argument of phis in successor as live. |
| 297 for (InstPhi *I : Succ->Phis) | 562 for (InstPhi *I : Succ->Phis) |
| 298 I->livenessPhiOperand(Live, this, Liveness); | 563 I->livenessPhiOperand(Live, this, Liveness); |
| 299 } | 564 } |
| 300 Liveness->getLiveOut(this) = Live; | 565 Liveness->getLiveOut(this) = Live; |
| 301 | 566 |
| 302 // Process regular instructions in reverse order. | 567 // Process regular instructions in reverse order. |
| 303 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. | |
| 304 for (auto I = Insts.rbegin(), E = Insts.rend(); I != E; ++I) { | 568 for (auto I = Insts.rbegin(), E = Insts.rend(); I != E; ++I) { |
| 305 if ((*I)->isDeleted()) | 569 if ((*I)->isDeleted()) |
| 306 continue; | 570 continue; |
| 307 (*I)->liveness((*I)->getNumber(), Live, Liveness, LiveBegin, LiveEnd); | 571 (*I)->liveness((*I)->getNumber(), Live, Liveness, LiveBegin, LiveEnd); |
| 308 } | 572 } |
| 309 // Process phis in forward order so that we can override the | 573 // Process phis in forward order so that we can override the |
| 310 // instruction number to be that of the earliest phi instruction in | 574 // instruction number to be that of the earliest phi instruction in |
| 311 // the block. | 575 // the block. |
| 576 SizeT NumNonDeadPhis = 0; | |
| 312 InstNumberT FirstPhiNumber = Inst::NumberSentinel; | 577 InstNumberT FirstPhiNumber = Inst::NumberSentinel; |
| 313 for (InstPhi *I : Phis) { | 578 for (InstPhi *I : Phis) { |
| 314 if (I->isDeleted()) | 579 if (I->isDeleted()) |
| 315 continue; | 580 continue; |
| 316 if (FirstPhiNumber == Inst::NumberSentinel) | 581 if (FirstPhiNumber == Inst::NumberSentinel) |
| 317 FirstPhiNumber = I->getNumber(); | 582 FirstPhiNumber = I->getNumber(); |
| 318 I->liveness(FirstPhiNumber, Live, Liveness, LiveBegin, LiveEnd); | 583 if (I->liveness(FirstPhiNumber, Live, Liveness, LiveBegin, LiveEnd)) |
| 584 ++NumNonDeadPhis; | |
| 319 } | 585 } |
| 320 | 586 |
| 321 // When using the sparse representation, after traversing the | 587 // When using the sparse representation, after traversing the |
| 322 // instructions in the block, the Live bitvector should only contain | 588 // instructions in the block, the Live bitvector should only contain |
| 323 // set bits for global variables upon block entry. We validate this | 589 // set bits for global variables upon block entry. We validate this |
| 324 // by shrinking the Live vector and then testing it against the | 590 // by shrinking the Live vector and then testing it against the |
| 325 // pre-shrunk version. (The shrinking is required, but the | 591 // pre-shrunk version. (The shrinking is required, but the |
| 326 // validation is not.) | 592 // validation is not.) |
| 327 LivenessBV LiveOrig = Live; | 593 LivenessBV LiveOrig = Live; |
| 328 Live.resize(Liveness->getNumGlobalVars()); | 594 Live.resize(Liveness->getNumGlobalVars()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 343 } | 609 } |
| 344 Str << "\n"; | 610 Str << "\n"; |
| 345 llvm_unreachable("Fatal inconsistency in liveness analysis"); | 611 llvm_unreachable("Fatal inconsistency in liveness analysis"); |
| 346 } | 612 } |
| 347 | 613 |
| 348 bool Changed = false; | 614 bool Changed = false; |
| 349 LivenessBV &LiveIn = Liveness->getLiveIn(this); | 615 LivenessBV &LiveIn = Liveness->getLiveIn(this); |
| 350 // Add in current LiveIn | 616 // Add in current LiveIn |
| 351 Live |= LiveIn; | 617 Live |= LiveIn; |
| 352 // Check result, set LiveIn=Live | 618 // Check result, set LiveIn=Live |
| 353 Changed = (Live != LiveIn); | 619 SizeT &PrevNumNonDeadPhis = Liveness->getNumNonDeadPhis(this); |
| 354 if (Changed) | 620 bool LiveInChanged = (Live != LiveIn); |
| 621 Changed = (NumNonDeadPhis != PrevNumNonDeadPhis || LiveInChanged); | |
| 622 if (LiveInChanged) | |
| 355 LiveIn = Live; | 623 LiveIn = Live; |
| 624 PrevNumNonDeadPhis = NumNonDeadPhis; | |
| 356 return Changed; | 625 return Changed; |
| 357 } | 626 } |
| 358 | 627 |
| 359 // Now that basic liveness is complete, remove dead instructions that | 628 // Now that basic liveness is complete, remove dead instructions that |
| 360 // were tentatively marked as dead, and compute actual live ranges. | 629 // were tentatively marked as dead, and compute actual live ranges. |
| 361 // It is assumed that within a single basic block, a live range begins | 630 // It is assumed that within a single basic block, a live range begins |
| 362 // at most once and ends at most once. This is certainly true for | 631 // at most once and ends at most once. This is certainly true for |
| 363 // pure SSA form. It is also true once phis are lowered, since each | 632 // pure SSA form. It is also true once phis are lowered, since each |
| 364 // assignment to the phi-based temporary is in a different basic | 633 // assignment to the phi-based temporary is in a different basic |
| 365 // block, and there is a single read that ends the live in the basic | 634 // block, and there is a single read that ends the live in the basic |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 ++IEB; | 734 ++IEB; |
| 466 } | 735 } |
| 467 // Process the variables that are live across the entire block. | 736 // Process the variables that are live across the entire block. |
| 468 for (int i = LiveInAndOut.find_first(); i != -1; | 737 for (int i = LiveInAndOut.find_first(); i != -1; |
| 469 i = LiveInAndOut.find_next(i)) { | 738 i = LiveInAndOut.find_next(i)) { |
| 470 Variable *Var = Liveness->getVariable(i, this); | 739 Variable *Var = Liveness->getVariable(i, this); |
| 471 Var->addLiveRange(FirstInstNum, LastInstNum + 1, 1); | 740 Var->addLiveRange(FirstInstNum, LastInstNum + 1, 1); |
| 472 } | 741 } |
| 473 } | 742 } |
| 474 | 743 |
| 744 // If this node contains only deleted instructions, and ends in an | |
| 745 // unconditional branch, contract the node by repointing all its | |
| 746 // in-edges to its successor. | |
| 747 void CfgNode::contractIfEmpty() { | |
| 748 if (InEdges.size() == 0) | |
| 749 return; | |
| 750 Inst *Branch = NULL; | |
| 751 for (Inst *I : Insts) { | |
| 752 if (!I->isDeleted() && !I->isUnconditionalBranch()) | |
| 753 return; | |
| 754 Branch = I; | |
| 755 } | |
| 756 Branch->setDeleted(); | |
| 757 assert(OutEdges.size() == 1); | |
| 758 // Repoint all this node's in-edges to this node's successor. | |
| 759 for (CfgNode *Pred : InEdges) { | |
| 760 for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); I != E; | |
| 761 ++I) { | |
| 762 if (*I == this) { | |
| 763 *I = OutEdges[0]; | |
| 764 OutEdges[0]->InEdges.push_back(Pred); | |
| 765 } | |
| 766 } | |
| 767 for (Inst *I : Pred->getInsts()) { | |
| 768 if (!I->isDeleted()) | |
| 769 I->repointEdge(this, OutEdges[0]); | |
|
jvoung (off chromium)
2014/10/27 22:12:20
Could break out of the loop here too, once the edg
Jim Stichnoth
2014/10/28 01:20:14
E.g., there could be a lowered Switch instruction
jvoung (off chromium)
2014/10/29 13:46:50
Ah okay... I was looking at the way splitIncomingE
| |
| 770 } | |
| 771 } | |
| 772 InEdges.clear(); | |
| 773 // Don't bother removing the single out-edge, which would also | |
| 774 // require finding the corresponding in-edge in the successor and | |
| 775 // removing it. | |
| 776 } | |
| 777 | |
| 475 void CfgNode::doBranchOpt(const CfgNode *NextNode) { | 778 void CfgNode::doBranchOpt(const CfgNode *NextNode) { |
| 476 TargetLowering *Target = Func->getTarget(); | 779 TargetLowering *Target = Func->getTarget(); |
| 477 // Check every instruction for a branch optimization opportunity. | 780 // Check every instruction for a branch optimization opportunity. |
| 478 // It may be more efficient to iterate in reverse and stop after the | 781 // It may be more efficient to iterate in reverse and stop after the |
| 479 // first opportunity, unless there is some target lowering where we | 782 // first opportunity, unless there is some target lowering where we |
| 480 // have the possibility of multiple such optimizations per block | 783 // have the possibility of multiple such optimizations per block |
| 481 // (currently not the case for x86 lowering). | 784 // (currently not the case for x86 lowering). |
| 482 for (Inst *I : Insts) | 785 for (Inst *I : Insts) { |
| 483 Target->doBranchOpt(I, NextNode); | 786 if (!I->isDeleted()) { |
| 787 Target->doBranchOpt(I, NextNode); | |
| 788 } | |
| 789 } | |
| 484 } | 790 } |
| 485 | 791 |
| 486 // ======================== Dump routines ======================== // | 792 // ======================== Dump routines ======================== // |
| 487 | 793 |
| 488 void CfgNode::emit(Cfg *Func) const { | 794 void CfgNode::emit(Cfg *Func) const { |
| 489 Func->setCurrentNode(this); | 795 Func->setCurrentNode(this); |
| 490 Ostream &Str = Func->getContext()->getStrEmit(); | 796 Ostream &Str = Func->getContext()->getStrEmit(); |
| 491 if (Func->getEntryNode() == this) { | 797 if (Func->getEntryNode() == this) { |
| 492 Str << Func->getContext()->mangleName(Func->getFunctionName()) << ":\n"; | 798 Str << Func->getContext()->mangleName(Func->getFunctionName()) << ":\n"; |
| 493 } | 799 } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 599 if (!First) | 905 if (!First) |
| 600 Str << ", "; | 906 Str << ", "; |
| 601 First = false; | 907 First = false; |
| 602 Str << "%" << I->getName(); | 908 Str << "%" << I->getName(); |
| 603 } | 909 } |
| 604 Str << "\n"; | 910 Str << "\n"; |
| 605 } | 911 } |
| 606 } | 912 } |
| 607 | 913 |
| 608 } // end of namespace Ice | 914 } // end of namespace Ice |
| OLD | NEW |