30
30
#include " bolt/Core/BinaryLoop.h"
31
31
#include " bolt/Core/BinarySection.h"
32
32
#include " bolt/Core/DebugData.h"
33
+ #include " bolt/Core/FunctionLayout.h"
33
34
#include " bolt/Core/JumpTable.h"
34
35
#include " bolt/Core/MCPlus.h"
35
36
#include " bolt/Utils/NameResolver.h"
@@ -552,10 +553,8 @@ class BinaryFunction {
552
553
using BasicBlockListType = SmallVector<BinaryBasicBlock *, 0 >;
553
554
BasicBlockListType BasicBlocks;
554
555
BasicBlockListType DeletedBasicBlocks;
555
- BasicBlockOrderType BasicBlocksLayout;
556
- // / Previous layout replaced by modifyLayout
557
- BasicBlockOrderType BasicBlocksPreviousLayout;
558
- bool ModifiedLayout{false };
556
+
557
+ FunctionLayout Layout;
559
558
560
559
// / BasicBlockOffsets are used during CFG construction to map from code
561
560
// / offsets to BinaryBasicBlocks. Any modifications made to the CFG
@@ -754,12 +753,6 @@ class BinaryFunction {
754
753
using const_reverse_iterator =
755
754
pointee_iterator<BasicBlockListType::const_reverse_iterator>;
756
755
757
- typedef BasicBlockOrderType::iterator order_iterator;
758
- typedef BasicBlockOrderType::const_iterator const_order_iterator;
759
- typedef BasicBlockOrderType::reverse_iterator reverse_order_iterator;
760
- typedef BasicBlockOrderType::const_reverse_iterator
761
- const_reverse_order_iterator;
762
-
763
756
// CFG iterators.
764
757
iterator begin () { return BasicBlocks.begin (); }
765
758
const_iterator begin () const { return BasicBlocks.begin (); }
@@ -788,49 +781,6 @@ class BinaryFunction {
788
781
BasicBlockListType::iterator pbegin () { return BasicBlocks.begin (); }
789
782
BasicBlockListType::iterator pend () { return BasicBlocks.end (); }
790
783
791
- order_iterator layout_begin () { return BasicBlocksLayout.begin (); }
792
- const_order_iterator layout_begin () const
793
- { return BasicBlocksLayout.begin (); }
794
- order_iterator layout_end () { return BasicBlocksLayout.end (); }
795
- const_order_iterator layout_end () const
796
- { return BasicBlocksLayout.end (); }
797
- reverse_order_iterator layout_rbegin ()
798
- { return BasicBlocksLayout.rbegin (); }
799
- const_reverse_order_iterator layout_rbegin () const
800
- { return BasicBlocksLayout.rbegin (); }
801
- reverse_order_iterator layout_rend ()
802
- { return BasicBlocksLayout.rend (); }
803
- const_reverse_order_iterator layout_rend () const
804
- { return BasicBlocksLayout.rend (); }
805
- size_t layout_size () const { return BasicBlocksLayout.size (); }
806
- bool layout_empty () const { return BasicBlocksLayout.empty (); }
807
- const BinaryBasicBlock *layout_front () const
808
- { return BasicBlocksLayout.front (); }
809
- BinaryBasicBlock *layout_front () { return BasicBlocksLayout.front (); }
810
- const BinaryBasicBlock *layout_back () const
811
- { return BasicBlocksLayout.back (); }
812
- BinaryBasicBlock *layout_back () { return BasicBlocksLayout.back (); }
813
-
814
- inline iterator_range<order_iterator> layout () {
815
- return iterator_range<order_iterator>(BasicBlocksLayout.begin (),
816
- BasicBlocksLayout.end ());
817
- }
818
-
819
- inline iterator_range<const_order_iterator> layout () const {
820
- return iterator_range<const_order_iterator>(BasicBlocksLayout.begin (),
821
- BasicBlocksLayout.end ());
822
- }
823
-
824
- inline iterator_range<reverse_order_iterator> rlayout () {
825
- return iterator_range<reverse_order_iterator>(BasicBlocksLayout.rbegin (),
826
- BasicBlocksLayout.rend ());
827
- }
828
-
829
- inline iterator_range<const_reverse_order_iterator> rlayout () const {
830
- return iterator_range<const_reverse_order_iterator>(
831
- BasicBlocksLayout.rbegin (), BasicBlocksLayout.rend ());
832
- }
833
-
834
784
cfi_iterator cie_begin () { return CIEFrameInstructions.begin (); }
835
785
const_cfi_iterator cie_begin () const { return CIEFrameInstructions.begin (); }
836
786
cfi_iterator cie_end () { return CIEFrameInstructions.end (); }
@@ -881,27 +831,16 @@ class BinaryFunction {
881
831
return *this ;
882
832
}
883
833
884
- // / Update layout of basic blocks used for output.
885
- void updateBasicBlockLayout (BasicBlockOrderType &NewLayout) {
886
- assert (NewLayout.size () == BasicBlocks.size () && " Layout size mismatch." );
834
+ FunctionLayout &getLayout () { return Layout; }
887
835
888
- BasicBlocksPreviousLayout = BasicBlocksLayout;
889
- if (NewLayout != BasicBlocksLayout) {
890
- ModifiedLayout = true ;
891
- BasicBlocksLayout.clear ();
892
- BasicBlocksLayout.swap (NewLayout);
893
- }
894
- }
836
+ const FunctionLayout &getLayout () const { return Layout; }
895
837
896
838
// / Recompute landing pad information for the function and all its blocks.
897
839
void recomputeLandingPads ();
898
840
899
- // / Return current basic block layout.
900
- const BasicBlockOrderType &getLayout () const { return BasicBlocksLayout; }
901
-
902
841
// / Return a list of basic blocks sorted using DFS and update layout indices
903
842
// / using the same order. Does not modify the current layout.
904
- BasicBlockOrderType dfs () const ;
843
+ BasicBlockListType dfs () const ;
905
844
906
845
// / Find the loops in the CFG of the function and store information about
907
846
// / them.
@@ -958,26 +897,6 @@ class BinaryFunction {
958
897
return I == LabelToBB.end () ? nullptr : I->second ;
959
898
}
960
899
961
- // / Returns the basic block after the given basic block in the layout or
962
- // / nullptr the last basic block is given.
963
- const BinaryBasicBlock *getBasicBlockAfter (const BinaryBasicBlock *BB,
964
- bool IgnoreSplits = true ) const {
965
- return const_cast <BinaryFunction *>(this )->getBasicBlockAfter (BB,
966
- IgnoreSplits);
967
- }
968
-
969
- BinaryBasicBlock *getBasicBlockAfter (const BinaryBasicBlock *BB,
970
- bool IgnoreSplits = true ) {
971
- for (auto I = layout_begin (), E = layout_end (); I != E; ++I) {
972
- auto Next = std::next (I);
973
- if (*I == BB && Next != E) {
974
- return (IgnoreSplits || (*I)->isCold () == (*Next)->isCold ()) ? *Next
975
- : nullptr ;
976
- }
977
- }
978
- return nullptr ;
979
- }
980
-
981
900
// / Retrieve the landing pad BB associated with invoke instruction \p Invoke
982
901
// / that is in \p BB. Return nullptr if none exists
983
902
BinaryBasicBlock *getLandingPadBBFor (const BinaryBasicBlock &BB,
@@ -1455,10 +1374,7 @@ class BinaryFunction {
1455
1374
bool hasUnknownControlFlow () const { return HasUnknownControlFlow; }
1456
1375
1457
1376
// / Return true if the function body is non-contiguous.
1458
- bool isSplit () const {
1459
- return isSimple () && layout_size () &&
1460
- layout_front ()->isCold () != layout_back ()->isCold ();
1461
- }
1377
+ bool isSplit () const { return isSimple () && getLayout ().isSplit (); }
1462
1378
1463
1379
bool shouldPreserveNops () const { return PreserveNops; }
1464
1380
@@ -1578,8 +1494,7 @@ class BinaryFunction {
1578
1494
BinaryBasicBlock *BB = BasicBlocks.back ();
1579
1495
1580
1496
BB->setIndex (BasicBlocks.size () - 1 );
1581
- BB->setLayoutIndex (layout_size ());
1582
- BasicBlocksLayout.emplace_back (BB);
1497
+ Layout.addBasicBlock (BB);
1583
1498
1584
1499
return BB;
1585
1500
}
@@ -1626,13 +1541,6 @@ class BinaryFunction {
1626
1541
// / layout after the BB indicated by Start.
1627
1542
void updateLayout (BinaryBasicBlock *Start, const unsigned NumNewBlocks);
1628
1543
1629
- // / Make sure basic blocks' indices match the current layout.
1630
- void updateLayoutIndices () const {
1631
- unsigned Index = 0 ;
1632
- for (BinaryBasicBlock *BB : layout ())
1633
- BB->setLayoutIndex (Index++);
1634
- }
1635
-
1636
1544
// / Recompute the CFI state for NumNewBlocks following Start after inserting
1637
1545
// / new blocks into the CFG. This must be called after updateLayout.
1638
1546
void updateCFIState (BinaryBasicBlock *Start, const unsigned NumNewBlocks);
@@ -2213,14 +2121,6 @@ class BinaryFunction {
2213
2121
// / and size.
2214
2122
uint64_t getFunctionScore () const ;
2215
2123
2216
- // / Return true if the layout has been changed by basic block reordering,
2217
- // / false otherwise.
2218
- bool hasLayoutChanged () const ;
2219
-
2220
- // / Get the edit distance of the new layout with respect to the previous
2221
- // / layout after basic block reordering.
2222
- uint64_t getEditDistance () const ;
2223
-
2224
2124
// / Get the number of instructions within this function.
2225
2125
uint64_t getInstructionCount () const ;
2226
2126
@@ -2429,7 +2329,7 @@ template <>
2429
2329
struct GraphTraits <bolt::BinaryFunction *>
2430
2330
: public GraphTraits<bolt::BinaryBasicBlock *> {
2431
2331
static NodeRef getEntryNode (bolt::BinaryFunction *F) {
2432
- return * F->layout_begin ();
2332
+ return F->getLayout (). block_front ();
2433
2333
}
2434
2334
2435
2335
using nodes_iterator = pointer_iterator<bolt::BinaryFunction::iterator>;
@@ -2449,7 +2349,7 @@ template <>
2449
2349
struct GraphTraits <const bolt::BinaryFunction *>
2450
2350
: public GraphTraits<const bolt::BinaryBasicBlock *> {
2451
2351
static NodeRef getEntryNode (const bolt::BinaryFunction *F) {
2452
- return * F->layout_begin ();
2352
+ return F->getLayout (). block_front ();
2453
2353
}
2454
2354
2455
2355
using nodes_iterator = pointer_iterator<bolt::BinaryFunction::const_iterator>;
@@ -2469,15 +2369,15 @@ template <>
2469
2369
struct GraphTraits <Inverse<bolt::BinaryFunction *>>
2470
2370
: public GraphTraits<Inverse<bolt::BinaryBasicBlock *>> {
2471
2371
static NodeRef getEntryNode (Inverse<bolt::BinaryFunction *> G) {
2472
- return * G.Graph ->layout_begin ();
2372
+ return G.Graph ->getLayout (). block_front ();
2473
2373
}
2474
2374
};
2475
2375
2476
2376
template <>
2477
2377
struct GraphTraits <Inverse<const bolt::BinaryFunction *>>
2478
2378
: public GraphTraits<Inverse<const bolt::BinaryBasicBlock *>> {
2479
2379
static NodeRef getEntryNode (Inverse<const bolt::BinaryFunction *> G) {
2480
- return * G.Graph ->layout_begin ();
2380
+ return G.Graph ->getLayout (). block_front ();
2481
2381
}
2482
2382
};
2483
2383
0 commit comments