OLD | NEW |
---|---|
1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// | 1 //===- subzero/src/IceCfg.cpp - Control flow graph 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 /// \file | 10 /// \file |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
507 for (CfgNode *Node : reverse_range(ReversedReachable)) | 507 for (CfgNode *Node : reverse_range(ReversedReachable)) |
508 Shuffled.push_back(Node); | 508 Shuffled.push_back(Node); |
509 for (CfgNode *Node : Unreachable) | 509 for (CfgNode *Node : Unreachable) |
510 Shuffled.push_back(Node); | 510 Shuffled.push_back(Node); |
511 assert(Nodes.size() == Shuffled.size()); | 511 assert(Nodes.size() == Shuffled.size()); |
512 swapNodes(Shuffled); | 512 swapNodes(Shuffled); |
513 | 513 |
514 dump("After basic block shuffling"); | 514 dump("After basic block shuffling"); |
515 } | 515 } |
516 | 516 |
517 void Cfg::localCSE() { | 517 void Cfg::localCSE(const bool NoSSA) { |
518 // Performs basic-block local common-subexpression elimination | 518 // Performs basic-block local common-subexpression elimination |
519 // If we have | 519 // If we have |
520 // t1 = op b c | 520 // t1 = op b c |
521 // t2 = op b c | 521 // t2 = op b c |
522 // This pass will replace future references to t2 in a basic block by t1 | 522 // This pass will replace future references to t2 in a basic block by t1 |
523 // Points to note: | 523 // Points to note: |
524 // 1. Does not assume SSA, but not tested on non-SSA input yet as it is run | 524 // 1. Assumes SSA by default. To change this, use -lcse=no-ssa |
525 // at the beginning. | 525 // This is needed if this pass is moved to a point later in the pipeline. |
526 // If variables have a single definition (in the node), CSE can work just | |
527 // on the basis of an equality compare on instructions (sans Dest). When | |
528 // variables can be updated (hence, non-SSA) the result of a previous | |
529 // instruction which used that variable as an operand can not be reused. | |
526 // 2. Leaves removal of instructions to DCE. | 530 // 2. Leaves removal of instructions to DCE. |
527 // 3. Only enabled on arithmetic instructions. pnacl-clang (-O2) is expected | 531 // 3. Only enabled on arithmetic instructions. pnacl-clang (-O2) is expected |
528 // to take care of cases not arising from GEP simplification. | 532 // to take care of cases not arising from GEP simplification. |
529 // 4. By default, two passes are made over each basic block. Control this | 533 // 4. By default, a single pass is made over each basic block. Control this |
530 // with -lcse-max-iters=N | 534 // with -lcse-max-iters=N |
531 | 535 |
532 TimerMarker T(TimerStack::TT_localCse, this); | 536 TimerMarker T(TimerStack::TT_localCse, this); |
533 struct VariableHash { | 537 struct VariableHash { |
534 size_t operator()(const Variable *Var) const { return Var->hashValue(); } | 538 size_t operator()(const Variable *Var) const { return Var->hashValue(); } |
535 }; | 539 }; |
536 | 540 |
537 struct InstHash { | 541 struct InstHash { |
538 size_t operator()(const Inst *Instr) const { | 542 size_t operator()(const Inst *Instr) const { |
539 auto Kind = Instr->getKind(); | 543 auto Kind = Instr->getKind(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
574 }; | 578 }; |
575 | 579 |
576 for (CfgNode *Node : getNodes()) { | 580 for (CfgNode *Node : getNodes()) { |
577 CfgUnorderedSet<Inst *, InstHash, InstEq> Seen; | 581 CfgUnorderedSet<Inst *, InstHash, InstEq> Seen; |
578 | 582 |
579 CfgUnorderedMap<Variable *, Variable *, VariableHash> Replacements; | 583 CfgUnorderedMap<Variable *, Variable *, VariableHash> Replacements; |
580 // Combining the above two into a single data structure might consume less | 584 // Combining the above two into a single data structure might consume less |
581 // memory but will be slower i.e map of Instruction -> Set of Variables | 585 // memory but will be slower i.e map of Instruction -> Set of Variables |
582 | 586 |
583 CfgUnorderedMap<Variable *, std::vector<Inst *>, VariableHash> Dependency; | 587 CfgUnorderedMap<Variable *, std::vector<Inst *>, VariableHash> Dependency; |
584 // Not necessary for SSA, still keeping it in case this pass is not run at | 588 // Not necessary for SSA |
Jim Stichnoth
2016/07/29 14:49:02
Can you describe this with respect to the arg name
manasijm
2016/08/01 17:39:23
Done.
| |
585 // the beginning. Remove to improve performace. | |
586 | 589 |
587 int IterCount = getFlags().getLocalCseMaxIterations(); | 590 int IterCount = getFlags().getLocalCseMaxIterations(); |
588 | 591 |
589 while (IterCount--) { | 592 while (IterCount--) { |
Jim Stichnoth
2016/07/29 14:49:02
When I look at this loop, I get nervous about off-
manasijm
2016/08/01 17:39:23
Done.
| |
590 // TODO : Stats on IterCount -> performance | 593 // TODO(manasijm): Stats on IterCount -> performance |
591 for (Inst &Instr : Node->getInsts()) { | 594 for (Inst &Instr : Node->getInsts()) { |
592 if (Instr.isDeleted() || !llvm::isa<InstArithmetic>(&Instr)) | 595 if (Instr.isDeleted() || !llvm::isa<InstArithmetic>(&Instr)) |
593 continue; | 596 continue; |
597 if (NoSSA) { | |
598 // Invalidate replacements | |
599 auto Iter = Replacements.find(Instr.getDest()); | |
600 if (Iter != Replacements.end()) { | |
601 Replacements.erase(Iter); | |
602 } | |
594 | 603 |
595 // Invalidate replacements | 604 // Invalidate 'seen' instructions whose operands were just updated. |
596 auto Iter = Replacements.find(Instr.getDest()); | 605 auto DepIter = Dependency.find(Instr.getDest()); |
597 if (Iter != Replacements.end()) { | 606 if (DepIter != Dependency.end()) { |
598 Replacements.erase(Iter); | 607 for (auto *DepInst : DepIter->second) { |
608 Seen.erase(DepInst); | |
609 } | |
610 } | |
599 } | 611 } |
600 | 612 |
601 // Invalidate 'seen' instructions whose operands were just updated. | |
602 auto DepIter = Dependency.find(Instr.getDest()); | |
603 if (DepIter != Dependency.end()) { | |
604 for (auto DepInst : DepIter->second) { | |
605 Seen.erase(DepInst); | |
606 } | |
607 } | |
608 // The above two can be removed if SSA is assumed. | |
609 | |
610 // Replace - doing this before checking for repetitions might enable | 613 // Replace - doing this before checking for repetitions might enable |
611 // more | 614 // more optimizations |
612 // optimizations | |
613 for (SizeT i = 0; i < Instr.getSrcSize(); ++i) { | 615 for (SizeT i = 0; i < Instr.getSrcSize(); ++i) { |
614 auto *Opnd = Instr.getSrc(i); | 616 auto *Opnd = Instr.getSrc(i); |
615 if (auto *Var = llvm::dyn_cast<Variable>(Opnd)) { | 617 if (auto *Var = llvm::dyn_cast<Variable>(Opnd)) { |
616 if (Replacements.find(Var) != Replacements.end()) { | 618 if (Replacements.find(Var) != Replacements.end()) { |
617 Instr.replaceSource(i, Replacements[Var]); | 619 Instr.replaceSource(i, Replacements[Var]); |
618 } | 620 } |
619 } | 621 } |
620 } | 622 } |
621 | 623 |
622 // Check for repetitions | 624 // Check for repetitions |
623 auto SeenIter = Seen.find(&Instr); | 625 auto SeenIter = Seen.find(&Instr); |
624 if (SeenIter != Seen.end()) { // seen before | 626 if (SeenIter != Seen.end()) { // seen before |
625 const Inst *Found = *SeenIter; | 627 const Inst *Found = *SeenIter; |
626 Replacements[Instr.getDest()] = Found->getDest(); | 628 Replacements[Instr.getDest()] = Found->getDest(); |
627 } else { // new | 629 } else { // new |
628 Seen.insert(&Instr); | 630 Seen.insert(&Instr); |
629 | 631 |
630 // Update dependencies | 632 if (NoSSA) { |
631 for (SizeT i = 0; i < Instr.getSrcSize(); ++i) { | 633 // Update dependencies |
632 auto *Opnd = Instr.getSrc(i); | 634 for (SizeT i = 0; i < Instr.getSrcSize(); ++i) { |
633 if (auto *Var = llvm::dyn_cast<Variable>(Opnd)) { | 635 auto *Opnd = Instr.getSrc(i); |
634 Dependency[Var].push_back(&Instr); | 636 if (auto *Var = llvm::dyn_cast<Variable>(Opnd)) { |
637 Dependency[Var].push_back(&Instr); | |
638 } | |
635 } | 639 } |
636 } | 640 } |
637 } | 641 } |
638 } | 642 } |
639 } | 643 } |
640 } | 644 } |
641 } | 645 } |
642 | 646 |
643 void Cfg::loopInvariantCodeMotion() { | 647 void Cfg::loopInvariantCodeMotion() { |
644 TimerMarker T(TimerStack::TT_loopInvariantCodeMotion, this); | 648 TimerMarker T(TimerStack::TT_loopInvariantCodeMotion, this); |
(...skipping 1122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1767 } | 1771 } |
1768 } | 1772 } |
1769 // Print each basic block | 1773 // Print each basic block |
1770 for (CfgNode *Node : Nodes) | 1774 for (CfgNode *Node : Nodes) |
1771 Node->dump(this); | 1775 Node->dump(this); |
1772 if (isVerbose(IceV_Instructions)) | 1776 if (isVerbose(IceV_Instructions)) |
1773 Str << "}\n"; | 1777 Str << "}\n"; |
1774 } | 1778 } |
1775 | 1779 |
1776 } // end of namespace Ice | 1780 } // end of namespace Ice |
OLD | NEW |