Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: src/IceCfg.cpp

Issue 2138443002: Subzero: Loop Invariant Code Motion (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Put invariant detection logic into separate function Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 622 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 if (auto *Var = llvm::dyn_cast<Variable>(Opnd)) { 633 if (auto *Var = llvm::dyn_cast<Variable>(Opnd)) {
634 Dependency[Var].push_back(&Instr); 634 Dependency[Var].push_back(&Instr);
635 } 635 }
636 } 636 }
637 } 637 }
638 } 638 }
639 } 639 }
640 } 640 }
641 } 641 }
642 642
643 void Cfg::loopInvariantCodeMotion() {
644 TimerMarker T(TimerStack::TT_loopInvariantCodeMotion, this);
645 // Does not introduce new nodes as of now.
646 for (auto &Pair : LoopInfo) {
647 CfgNode *Header = Nodes[Pair.first];
648 assert(Header);
649 if (Header->getLoopNestDepth() < 1)
650 continue;
651 CfgNode *PreHeader = nullptr;
652 for (auto *Pred : Header->getInEdges()) {
653 if (Pred->getLoopNestDepth() == Header->getLoopNestDepth() - 1) {
654 if (PreHeader == nullptr) {
655 PreHeader = Pred;
656 } else {
657 PreHeader = nullptr;
658 break;
659 // Do not consider cases with two incoming edges.
660 // Will require insertion of nodes.
661 }
662 }
663 }
664 if (PreHeader == nullptr || PreHeader->getInsts().size() == 0) {
665 continue; // to next loop
666 }
667
668 auto &Insts = PreHeader->getInsts();
669 auto &LastInst = Insts.back();
670 Insts.pop_back();
671
672 for (auto *Inst : findLoopInvariantInstructions(Pair.first)) {
673 PreHeader->appendInst(Inst);
674 }
675 PreHeader->appendInst(&LastInst);
676 }
677 }
678 Ice::CfgVector<Inst *>
Jim Stichnoth 2016/07/12 10:51:51 add extra blank line
679 Cfg::findLoopInvariantInstructions(Ice::SizeT LoopHeaderIndex) {
680 CfgUnorderedSet<Inst *> InvariantInsts;
681 CfgUnorderedSet<Variable *> InvariantVars;
682 for (auto *Var : getArgs()) {
683 InvariantVars.insert(Var);
684 }
685 bool Changed = false;
686 do {
687 Changed = false;
688 for (auto NodeIndex : LoopInfo[LoopHeaderIndex]) {
689 auto *Node = Nodes[NodeIndex];
690 CfgVector<std::reference_wrapper<Inst>> Insts(Node->getInsts().begin(),
691 Node->getInsts().end());
692
693 for (auto &InstRef : Insts) {
694 auto &Inst = InstRef.get();
695 if (Inst.isDeleted() ||
696 InvariantInsts.find(&Inst) != InvariantInsts.end())
697 continue;
698 switch (Inst.getKind()) {
699 case Inst::InstKind::Alloca:
700 case Inst::InstKind::Br:
701 case Inst::InstKind::Ret:
702 case Inst::InstKind::Phi:
703 case Inst::InstKind::Call:
704 case Inst::InstKind::IntrinsicCall:
705 case Inst::InstKind::Load:
706 case Inst::InstKind::Store:
707 case Inst::InstKind::Switch:
708 continue;
709 default:
710 break;
711 }
712
713 bool IsInvariant = true;
714 for (SizeT i = 0; i < Inst.getSrcSize(); ++i) {
715 if (auto *Var = llvm::dyn_cast<Variable>(Inst.getSrc(i))) {
716 if (InvariantVars.find(Var) == InvariantVars.end()) {
717 IsInvariant = false;
718 }
719 }
720 }
721 if (IsInvariant) {
722 Changed = true;
723 InvariantInsts.insert(&Inst);
724 Node->getInsts().remove(Inst);
725 if (Inst.getDest() != nullptr) {
726 InvariantVars.insert(Inst.getDest());
727 }
728 }
729 }
730 }
731 } while (Changed);
732
733 CfgVector<Inst *> InstVector(InvariantInsts.begin(), InvariantInsts.end());
734 std::sort(InstVector.begin(), InstVector.end(),
735 [](Inst *A, Inst *B) { return A->getNumber() < B->getNumber(); });
736 return InstVector;
737 }
738
643 void Cfg::shortCircuitJumps() { 739 void Cfg::shortCircuitJumps() {
644 // Split Nodes whenever an early jump is possible. 740 // Split Nodes whenever an early jump is possible.
645 // __N : 741 // __N :
646 // a = <something> 742 // a = <something>
647 // Instruction 1 without side effect 743 // Instruction 1 without side effect
648 // ... b = <something> ... 744 // ... b = <something> ...
649 // Instruction N without side effect 745 // Instruction N without side effect
650 // t1 = or a b 746 // t1 = or a b
651 // br t1 __X __Y 747 // br t1 __X __Y
652 // 748 //
(...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after
1331 1427
1332 // Compute the stack frame layout. 1428 // Compute the stack frame layout.
1333 void Cfg::genFrame() { 1429 void Cfg::genFrame() {
1334 TimerMarker T(TimerStack::TT_genFrame, this); 1430 TimerMarker T(TimerStack::TT_genFrame, this);
1335 getTarget()->addProlog(Entry); 1431 getTarget()->addProlog(Entry);
1336 for (CfgNode *Node : Nodes) 1432 for (CfgNode *Node : Nodes)
1337 if (Node->getHasReturn()) 1433 if (Node->getHasReturn())
1338 getTarget()->addEpilog(Node); 1434 getTarget()->addEpilog(Node);
1339 } 1435 }
1340 1436
1341 void Cfg::computeLoopNestDepth() { 1437 void Cfg::generateLoopInfo() {
1342 TimerMarker T(TimerStack::TT_computeLoopNestDepth, this); 1438 TimerMarker T(TimerStack::TT_computeLoopNestDepth, this);
1343 LoopAnalyzer LA(this); 1439 LoopInfo = LoopAnalyzer(this).getLoopInfo();
1344 LA.computeLoopNestDepth();
1345 } 1440 }
1346 1441
1347 // This is a lightweight version of live-range-end calculation. Marks the last 1442 // This is a lightweight version of live-range-end calculation. Marks the last
1348 // use of only those variables whose definition and uses are completely with a 1443 // use of only those variables whose definition and uses are completely with a
1349 // single block. It is a quick single pass and doesn't need to iterate until 1444 // single block. It is a quick single pass and doesn't need to iterate until
1350 // convergence. 1445 // convergence.
1351 void Cfg::livenessLightweight() { 1446 void Cfg::livenessLightweight() {
1352 TimerMarker T(TimerStack::TT_livenessLightweight, this); 1447 TimerMarker T(TimerStack::TT_livenessLightweight, this);
1353 getVMetadata()->init(VMK_Uses); 1448 getVMetadata()->init(VMK_Uses);
1354 for (CfgNode *Node : Nodes) 1449 for (CfgNode *Node : Nodes)
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1683 } 1778 }
1684 } 1779 }
1685 // Print each basic block 1780 // Print each basic block
1686 for (CfgNode *Node : Nodes) 1781 for (CfgNode *Node : Nodes)
1687 Node->dump(this); 1782 Node->dump(this);
1688 if (isVerbose(IceV_Instructions)) 1783 if (isVerbose(IceV_Instructions))
1689 Str << "}\n"; 1784 Str << "}\n";
1690 } 1785 }
1691 1786
1692 } // end of namespace Ice 1787 } // end of namespace Ice
OLDNEW
« no previous file with comments | « src/IceCfg.h ('k') | src/IceClFlags.def » ('j') | src/IceLoopAnalyzer.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698