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 | |
| 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 help unless | |
| 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 | |
| 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 const Variable *Dest = Desc[I].Dest; | |
| 343 for (size_t J = 0; J < NumPhis; ++J) { | |
| 344 if (I != J) { | |
| 345 // There shouldn't be two Phis with the same Dest variable | |
| 346 // or register. | |
| 347 assert(!sameVarOrReg(Dest, Desc[J].Dest)); | |
| 348 } | |
| 349 const Operand *Src = Desc[J].Src; | |
| 350 if (sameVarOrReg(Dest, Src)) | |
| 351 ++Desc[I].NumPred; | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 // Another pass to compute initial Weight values. | |
| 356 | |
| 357 // Always pick NumPred=0 over NumPred>0. | |
| 358 const int32_t WeightNoPreds = 4; | |
| 359 // Prefer Src as a register because the register might free up. | |
| 360 const int32_t WeightSrcIsReg = 2; | |
| 361 // Prefer Dest not as a register because the register stays free | |
| 362 // longer. | |
| 363 const int32_t WeightDestNotReg = 1; | |
| 364 | |
| 365 for (size_t I = 0; I < NumPhis; ++I) { | |
| 366 int32_t Weight = 0; | |
| 367 if (Desc[I].NumPred == 0) | |
| 368 Weight += WeightNoPreds; | |
| 369 if (auto Var = llvm::dyn_cast<Variable>(Desc[I].Src)) | |
| 370 if (Var->hasReg()) | |
| 371 Weight += WeightSrcIsReg; | |
| 372 if (!Desc[I].Dest->hasReg()) | |
| 373 Weight += WeightDestNotReg; | |
| 374 Desc[I].Weight = Weight; | |
| 375 } | |
| 376 | |
| 377 // Repeatedly choose and process the best candidate in the | |
| 378 // topological sort, until no candidates remain. This | |
| 379 // implementation is O(N^2) where N is the number of Phi | |
|
jvoung (off chromium)
2014/10/29 13:46:50
Any idea the "usual" # of Phis? (Would the N^2 be
Jim Stichnoth
2014/10/30 00:25:25
OK, I ran some numbers across spec2k (is that repr
| |
| 380 // instructions, but with a small constant factor compared to a | |
| 381 // likely implementation of O(N) topological sort. | |
| 382 for (size_t Remaining = NumPhis; Remaining; --Remaining) { | |
| 383 size_t BestIndex = 0; | |
| 384 int32_t BestWeight = -1; | |
| 385 // Find the best candidate. | |
| 386 for (size_t I = 0; I < NumPhis; ++I) { | |
| 387 if (Desc[I].Processed) | |
| 388 continue; | |
| 389 int32_t Weight = 0; | |
| 390 Weight = Desc[I].Weight; | |
| 391 if (Weight > BestWeight) { | |
| 392 BestIndex = I; | |
| 393 BestWeight = Weight; | |
| 394 } | |
| 395 } | |
| 396 assert(BestWeight >= 0); | |
| 397 assert(Desc[BestIndex].NumPred <= 1); | |
| 398 Variable *Dest = Desc[BestIndex].Dest; | |
| 399 Operand *Src = Desc[BestIndex].Src; | |
| 400 // Break a cycle by introducing a temporary. Except don't | |
| 401 // bother for a self-assignment, which looks like a self-cycle | |
| 402 // with NumPred=1, since it can be left alone and will | |
| 403 // eventually be removed as a redundant assignment. | |
| 404 bool IsSelfAssignment = sameVarOrReg(Dest, Src); | |
| 405 if (!IsSelfAssignment && Desc[BestIndex].NumPred) { | |
| 406 bool Found = false; | |
| 407 // If the target instruction "A=B" is part of a cycle, find | |
| 408 // the "X=A" assignment in the cycle because it will have to | |
| 409 // be rewritten as "X=tmp". | |
| 410 for (size_t J = 0; !Found && J < NumPhis; ++J) { | |
| 411 if (Desc[J].Processed) | |
| 412 continue; | |
| 413 Operand *OtherSrc = Desc[J].Src; | |
| 414 if (Desc[J].NumPred && sameVarOrReg(Dest, OtherSrc)) { | |
| 415 SizeT VarNum = Func->getNumVariables(); | |
| 416 Variable *Tmp = Func->makeVariable( | |
| 417 OtherSrc->getType(), "__split_" + std::to_string(VarNum)); | |
| 418 Tmp->setNeedsStackSlot(); | |
| 419 Assignments.push_back(InstAssign::create(Func, Tmp, OtherSrc)); | |
| 420 Desc[J].Src = Tmp; | |
| 421 Found = true; | |
| 422 } | |
| 423 } | |
| 424 assert(Found); | |
| 425 } | |
| 426 // Now that a cycle (if any) has been broken, create the actual | |
| 427 // assignment. Except if Dest and Src are syntactically the | |
| 428 // same, don't bother adding the assignment, because in all | |
| 429 // respects it would be redundant, and if Dest/Src are on the | |
| 430 // stack, the target lowering may naively decide to lower it | |
| 431 // using a temporary register. | |
| 432 if (Dest != Src) | |
|
jvoung (off chromium)
2014/10/29 13:46:50
Could it be broadened beyond syntactically the sam
Jim Stichnoth
2014/10/30 00:25:25
This was actually deliberate because if the test i
| |
| 433 Assignments.push_back(InstAssign::create(Func, Dest, Src)); | |
| 434 // Update NumPred for all Phi assignments using this Phi's Src | |
| 435 // as their Dest variable. Also update Weight if NumPred | |
| 436 // dropped from 1 to 0. | |
| 437 if (auto Var = llvm::dyn_cast<Variable>(Src)) { | |
| 438 for (size_t I = 0; I < NumPhis; ++I) { | |
| 439 if (Desc[I].Processed) | |
| 440 continue; | |
| 441 if (sameVarOrReg(Var, Desc[I].Dest)) { | |
| 442 if (--Desc[I].NumPred == 0) | |
| 443 Desc[I].Weight += WeightNoPreds; | |
| 444 } | |
| 445 } | |
| 446 } | |
| 447 Desc[BestIndex].Processed = true; | |
| 448 } | |
| 449 | |
| 450 Func->getTarget()->lowerPhiAssignments(Split, Assignments); | |
| 451 | |
| 452 // Renumber the instructions to be monotonically increasing so | |
| 453 // that addNode() doesn't assert when multi-definitions are added | |
| 454 // out of order. | |
| 455 Split->renumberInstructions(); | |
| 456 Func->getVMetadata()->addNode(Split); | |
| 457 } | |
| 458 | |
| 459 for (InstPhi *Inst : getPhis()) | |
| 460 Inst->setDeleted(); | |
| 461 } | |
| 462 | |
| 207 // Does address mode optimization. Pass each instruction to the | 463 // Does address mode optimization. Pass each instruction to the |
| 208 // TargetLowering object. If it returns a new instruction | 464 // TargetLowering object. If it returns a new instruction |
| 209 // (representing the optimized address mode), then insert the new | 465 // (representing the optimized address mode), then insert the new |
| 210 // instruction and delete the old. | 466 // instruction and delete the old. |
| 211 void CfgNode::doAddressOpt() { | 467 void CfgNode::doAddressOpt() { |
| 212 TargetLowering *Target = Func->getTarget(); | 468 TargetLowering *Target = Func->getTarget(); |
| 213 LoweringContext &Context = Target->getContext(); | 469 LoweringContext &Context = Target->getContext(); |
| 214 Context.init(this); | 470 Context.init(this); |
| 215 while (!Context.atEnd()) { | 471 while (!Context.atEnd()) { |
| 216 Target->doAddressOpt(); | 472 Target->doAddressOpt(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 233 Context.advanceNext(); | 489 Context.advanceNext(); |
| 234 Context.advanceCur(); | 490 Context.advanceCur(); |
| 235 Target->doNopInsertion(); | 491 Target->doNopInsertion(); |
| 236 } | 492 } |
| 237 | 493 |
| 238 // Drives the target lowering. Passes the current instruction and the | 494 // Drives the target lowering. Passes the current instruction and the |
| 239 // next non-deleted instruction for target lowering. | 495 // next non-deleted instruction for target lowering. |
| 240 void CfgNode::genCode() { | 496 void CfgNode::genCode() { |
| 241 TargetLowering *Target = Func->getTarget(); | 497 TargetLowering *Target = Func->getTarget(); |
| 242 LoweringContext &Context = Target->getContext(); | 498 LoweringContext &Context = Target->getContext(); |
| 243 // Lower only the regular instructions. Defer the Phi instructions. | 499 // Lower the regular instructions. |
| 244 Context.init(this); | 500 Context.init(this); |
| 245 while (!Context.atEnd()) { | 501 while (!Context.atEnd()) { |
| 246 InstList::iterator Orig = Context.getCur(); | 502 InstList::iterator Orig = Context.getCur(); |
| 247 if (llvm::isa<InstRet>(*Orig)) | 503 if (llvm::isa<InstRet>(*Orig)) |
| 248 setHasReturn(); | 504 setHasReturn(); |
| 249 Target->lower(); | 505 Target->lower(); |
| 250 // Ensure target lowering actually moved the cursor. | 506 // Ensure target lowering actually moved the cursor. |
| 251 assert(Context.getCur() != Orig); | 507 assert(Context.getCur() != Orig); |
| 252 } | 508 } |
| 509 // Do preliminary lowering of the Phi instructions. | |
| 510 Target->prelowerPhis(); | |
| 253 } | 511 } |
| 254 | 512 |
| 255 void CfgNode::livenessLightweight() { | 513 void CfgNode::livenessLightweight() { |
| 256 SizeT NumVars = Func->getNumVariables(); | 514 SizeT NumVars = Func->getNumVariables(); |
| 257 LivenessBV Live(NumVars); | 515 LivenessBV Live(NumVars); |
| 258 // Process regular instructions in reverse order. | 516 // Process regular instructions in reverse order. |
| 259 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. | 517 // TODO(stichnot): Use llvm::make_range with LLVM 3.5. |
| 260 for (auto I = Insts.rbegin(), E = Insts.rend(); I != E; ++I) { | 518 for (auto I = Insts.rbegin(), E = Insts.rend(); I != E; ++I) { |
| 261 if ((*I)->isDeleted()) | 519 if ((*I)->isDeleted()) |
| 262 continue; | 520 continue; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 293 // Initialize Live to be the union of all successors' LiveIn. | 551 // Initialize Live to be the union of all successors' LiveIn. |
| 294 for (CfgNode *Succ : OutEdges) { | 552 for (CfgNode *Succ : OutEdges) { |
| 295 Live |= Liveness->getLiveIn(Succ); | 553 Live |= Liveness->getLiveIn(Succ); |
| 296 // Mark corresponding argument of phis in successor as live. | 554 // Mark corresponding argument of phis in successor as live. |
| 297 for (InstPhi *I : Succ->Phis) | 555 for (InstPhi *I : Succ->Phis) |
| 298 I->livenessPhiOperand(Live, this, Liveness); | 556 I->livenessPhiOperand(Live, this, Liveness); |
| 299 } | 557 } |
| 300 Liveness->getLiveOut(this) = Live; | 558 Liveness->getLiveOut(this) = Live; |
| 301 | 559 |
| 302 // Process regular instructions in reverse order. | 560 // 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) { | 561 for (auto I = Insts.rbegin(), E = Insts.rend(); I != E; ++I) { |
| 305 if ((*I)->isDeleted()) | 562 if ((*I)->isDeleted()) |
| 306 continue; | 563 continue; |
| 307 (*I)->liveness((*I)->getNumber(), Live, Liveness, LiveBegin, LiveEnd); | 564 (*I)->liveness((*I)->getNumber(), Live, Liveness, LiveBegin, LiveEnd); |
| 308 } | 565 } |
| 309 // Process phis in forward order so that we can override the | 566 // Process phis in forward order so that we can override the |
| 310 // instruction number to be that of the earliest phi instruction in | 567 // instruction number to be that of the earliest phi instruction in |
| 311 // the block. | 568 // the block. |
| 569 SizeT NumNonDeadPhis = 0; | |
| 312 InstNumberT FirstPhiNumber = Inst::NumberSentinel; | 570 InstNumberT FirstPhiNumber = Inst::NumberSentinel; |
| 313 for (InstPhi *I : Phis) { | 571 for (InstPhi *I : Phis) { |
| 314 if (I->isDeleted()) | 572 if (I->isDeleted()) |
| 315 continue; | 573 continue; |
| 316 if (FirstPhiNumber == Inst::NumberSentinel) | 574 if (FirstPhiNumber == Inst::NumberSentinel) |
| 317 FirstPhiNumber = I->getNumber(); | 575 FirstPhiNumber = I->getNumber(); |
| 318 I->liveness(FirstPhiNumber, Live, Liveness, LiveBegin, LiveEnd); | 576 if (I->liveness(FirstPhiNumber, Live, Liveness, LiveBegin, LiveEnd)) |
| 577 ++NumNonDeadPhis; | |
| 319 } | 578 } |
| 320 | 579 |
| 321 // When using the sparse representation, after traversing the | 580 // When using the sparse representation, after traversing the |
| 322 // instructions in the block, the Live bitvector should only contain | 581 // instructions in the block, the Live bitvector should only contain |
| 323 // set bits for global variables upon block entry. We validate this | 582 // set bits for global variables upon block entry. We validate this |
| 324 // by shrinking the Live vector and then testing it against the | 583 // by shrinking the Live vector and then testing it against the |
| 325 // pre-shrunk version. (The shrinking is required, but the | 584 // pre-shrunk version. (The shrinking is required, but the |
| 326 // validation is not.) | 585 // validation is not.) |
| 327 LivenessBV LiveOrig = Live; | 586 LivenessBV LiveOrig = Live; |
| 328 Live.resize(Liveness->getNumGlobalVars()); | 587 Live.resize(Liveness->getNumGlobalVars()); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 343 } | 602 } |
| 344 Str << "\n"; | 603 Str << "\n"; |
| 345 llvm_unreachable("Fatal inconsistency in liveness analysis"); | 604 llvm_unreachable("Fatal inconsistency in liveness analysis"); |
| 346 } | 605 } |
| 347 | 606 |
| 348 bool Changed = false; | 607 bool Changed = false; |
| 349 LivenessBV &LiveIn = Liveness->getLiveIn(this); | 608 LivenessBV &LiveIn = Liveness->getLiveIn(this); |
| 350 // Add in current LiveIn | 609 // Add in current LiveIn |
| 351 Live |= LiveIn; | 610 Live |= LiveIn; |
| 352 // Check result, set LiveIn=Live | 611 // Check result, set LiveIn=Live |
| 353 Changed = (Live != LiveIn); | 612 SizeT &PrevNumNonDeadPhis = Liveness->getNumNonDeadPhis(this); |
| 354 if (Changed) | 613 bool LiveInChanged = (Live != LiveIn); |
| 614 Changed = (NumNonDeadPhis != PrevNumNonDeadPhis || LiveInChanged); | |
| 615 if (LiveInChanged) | |
| 355 LiveIn = Live; | 616 LiveIn = Live; |
| 617 PrevNumNonDeadPhis = NumNonDeadPhis; | |
| 356 return Changed; | 618 return Changed; |
| 357 } | 619 } |
| 358 | 620 |
| 359 // Now that basic liveness is complete, remove dead instructions that | 621 // Now that basic liveness is complete, remove dead instructions that |
| 360 // were tentatively marked as dead, and compute actual live ranges. | 622 // were tentatively marked as dead, and compute actual live ranges. |
| 361 // It is assumed that within a single basic block, a live range begins | 623 // 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 | 624 // 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 | 625 // 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 | 626 // 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 | 627 // 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; | 727 ++IEB; |
| 466 } | 728 } |
| 467 // Process the variables that are live across the entire block. | 729 // Process the variables that are live across the entire block. |
| 468 for (int i = LiveInAndOut.find_first(); i != -1; | 730 for (int i = LiveInAndOut.find_first(); i != -1; |
| 469 i = LiveInAndOut.find_next(i)) { | 731 i = LiveInAndOut.find_next(i)) { |
| 470 Variable *Var = Liveness->getVariable(i, this); | 732 Variable *Var = Liveness->getVariable(i, this); |
| 471 Var->addLiveRange(FirstInstNum, LastInstNum + 1, 1); | 733 Var->addLiveRange(FirstInstNum, LastInstNum + 1, 1); |
| 472 } | 734 } |
| 473 } | 735 } |
| 474 | 736 |
| 737 // If this node contains only deleted instructions, and ends in an | |
| 738 // unconditional branch, contract the node by repointing all its | |
| 739 // in-edges to its successor. | |
| 740 void CfgNode::contractIfEmpty() { | |
| 741 if (InEdges.size() == 0) | |
| 742 return; | |
| 743 Inst *Branch = NULL; | |
| 744 for (Inst *I : Insts) { | |
| 745 if (!I->isDeleted() && !I->isUnconditionalBranch()) | |
| 746 return; | |
| 747 Branch = I; | |
| 748 } | |
| 749 Branch->setDeleted(); | |
| 750 assert(OutEdges.size() == 1); | |
| 751 // Repoint all this node's in-edges to this node's successor. | |
| 752 for (CfgNode *Pred : InEdges) { | |
| 753 for (auto I = Pred->OutEdges.begin(), E = Pred->OutEdges.end(); I != E; | |
| 754 ++I) { | |
| 755 if (*I == this) { | |
| 756 *I = OutEdges[0]; | |
| 757 OutEdges[0]->InEdges.push_back(Pred); | |
| 758 } | |
| 759 } | |
| 760 for (Inst *I : Pred->getInsts()) { | |
| 761 if (!I->isDeleted()) | |
| 762 I->repointEdge(this, OutEdges[0]); | |
| 763 } | |
| 764 } | |
| 765 InEdges.clear(); | |
| 766 // Don't bother removing the single out-edge, which would also | |
| 767 // require finding the corresponding in-edge in the successor and | |
| 768 // removing it. | |
| 769 } | |
| 770 | |
| 475 void CfgNode::doBranchOpt(const CfgNode *NextNode) { | 771 void CfgNode::doBranchOpt(const CfgNode *NextNode) { |
| 476 TargetLowering *Target = Func->getTarget(); | 772 TargetLowering *Target = Func->getTarget(); |
| 477 // Check every instruction for a branch optimization opportunity. | 773 // Check every instruction for a branch optimization opportunity. |
| 478 // It may be more efficient to iterate in reverse and stop after the | 774 // It may be more efficient to iterate in reverse and stop after the |
| 479 // first opportunity, unless there is some target lowering where we | 775 // first opportunity, unless there is some target lowering where we |
| 480 // have the possibility of multiple such optimizations per block | 776 // have the possibility of multiple such optimizations per block |
| 481 // (currently not the case for x86 lowering). | 777 // (currently not the case for x86 lowering). |
| 482 for (Inst *I : Insts) | 778 for (Inst *I : Insts) { |
| 483 Target->doBranchOpt(I, NextNode); | 779 if (!I->isDeleted()) { |
| 780 Target->doBranchOpt(I, NextNode); | |
| 781 } | |
| 782 } | |
| 484 } | 783 } |
| 485 | 784 |
| 486 // ======================== Dump routines ======================== // | 785 // ======================== Dump routines ======================== // |
| 487 | 786 |
| 488 void CfgNode::emit(Cfg *Func) const { | 787 void CfgNode::emit(Cfg *Func) const { |
| 489 Func->setCurrentNode(this); | 788 Func->setCurrentNode(this); |
| 490 Ostream &Str = Func->getContext()->getStrEmit(); | 789 Ostream &Str = Func->getContext()->getStrEmit(); |
| 491 if (Func->getEntryNode() == this) { | 790 if (Func->getEntryNode() == this) { |
| 492 Str << Func->getContext()->mangleName(Func->getFunctionName()) << ":\n"; | 791 Str << Func->getContext()->mangleName(Func->getFunctionName()) << ":\n"; |
| 493 } | 792 } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 600 if (!First) | 899 if (!First) |
| 601 Str << ", "; | 900 Str << ", "; |
| 602 First = false; | 901 First = false; |
| 603 Str << "%" << I->getName(); | 902 Str << "%" << I->getName(); |
| 604 } | 903 } |
| 605 Str << "\n"; | 904 Str << "\n"; |
| 606 } | 905 } |
| 607 } | 906 } |
| 608 | 907 |
| 609 } // end of namespace Ice | 908 } // end of namespace Ice |
| OLD | NEW |