26
26
#include " llvm/Support/MemoryBuffer.h"
27
27
#include " llvm/Support/YAMLParser.h"
28
28
#include " llvm/Support/YAMLTraits.h"
29
+ #include < iomanip>
29
30
30
31
using namespace swift ;
31
32
using namespace modulesummary ;
@@ -35,6 +36,7 @@ enum class ActionType : unsigned {
35
36
BinaryToYAML,
36
37
YAMLToBinary,
37
38
BinaryToDominators,
39
+ BinaryToDominatorTree,
38
40
BinaryToDot,
39
41
};
40
42
@@ -62,8 +64,11 @@ static llvm::cl::opt<ActionType> Action(
62
64
" Convert new binary .swiftmodule.summary format to YAML" ),
63
65
clEnumValN(ActionType::YAMLToBinary, " from-yaml" ,
64
66
" Convert YAML to new binary .swiftmodule.summary format" ),
67
+ clEnumValN(ActionType::BinaryToDominators, " binary-to-dom" ,
68
+ " Convert new binary .swiftmodule.summary format to "
69
+ " dominators list" ),
65
70
clEnumValN(
66
- ActionType::BinaryToDominators , " binary-to-dom" ,
71
+ ActionType::BinaryToDominatorTree , " binary-to-dom-tree " ,
67
72
" Convert new binary .swiftmodule.summary format to dominator tree" ),
68
73
clEnumValN(ActionType::BinaryToDot, " binary-to-dot" ,
69
74
" Convert new binary .swiftmodule.summary format to Dot" )));
@@ -418,14 +423,14 @@ class DomCallTree : public DomCallTreeBase {
418
423
class RetainedSizeCalculator {
419
424
struct Entry {
420
425
DomCallInfoNode *DomNode;
421
- size_t InstSize;
426
+ uint32_t InstSize;
422
427
};
423
- std::map<DomCallInfoNode *, size_t > SizeCache;
428
+ std::map<DomCallInfoNode *, uint32_t > SizeCache;
424
429
std::vector<Entry> Entries;
425
430
426
- size_t getInstSize (DomCallInfoNode *domNode) {
431
+ uint32_t getInstSize (DomCallInfoNode *domNode) {
427
432
auto FS = domNode->getBlock ()->getFS ();
428
- size_t size = FS->getInstSize ();
433
+ uint32_t size = FS->getInstSize ();
429
434
for (auto I = domNode->begin (), E = domNode->end (); I != E; ++I) {
430
435
auto Child = *I;
431
436
size += getInstSize (Child);
@@ -457,10 +462,46 @@ class RetainedSizeCalculator {
457
462
O << entry.InstSize << " \t | " << FS->getName () << " \n " ;
458
463
}
459
464
}
465
+
466
+ void displayTreeRec (llvm::raw_ostream &O, uint32_t TotalSize,
467
+ DomCallInfoNode *Root, unsigned Lev) {
468
+ auto Size = getInstSize (Root);
469
+ auto Percent = (double (Size) / TotalSize * 100 );
470
+ auto FS = Root->getBlock ()->getFS ();
471
+ O << Size << " \t | " ;
472
+ llvm::format_provider<double >::format (Percent, O, " f2" );
473
+ O << " \t | " ;
474
+ O.indent (2 * Lev) << FS->getName () << " \n " ;
475
+
476
+ auto Children = Root->getChildren ();
477
+ std::sort (Children.begin (), Children.end (),
478
+ [&](DomCallInfoNode *lhs, DomCallInfoNode *rhs) {
479
+ return getInstSize (lhs) > getInstSize (rhs);
480
+ });
481
+ for (auto Child : Children) {
482
+ displayTreeRec (O, TotalSize, Child, Lev + 1 );
483
+ }
484
+ }
485
+ void displayTree (llvm::raw_ostream &O, DomCallInfoNode *Root) {
486
+ O << " size\t | %\t | symbol\n " ;
487
+ uint32_t TotalSize = 0 ;
488
+ for (auto Child : Root->getChildren ()) {
489
+ TotalSize += getInstSize (Child);
490
+ }
491
+ auto Children = Root->getChildren ();
492
+ std::sort (Children.begin (), Children.end (),
493
+ [&](DomCallInfoNode *lhs, DomCallInfoNode *rhs) {
494
+ return getInstSize (lhs) > getInstSize (rhs);
495
+ });
496
+ for (auto Child : Children) {
497
+ displayTreeRec (O, TotalSize, Child, 0 );
498
+ }
499
+ }
460
500
};
461
501
462
502
int dominance_analysis (CallGraph &CG) {
463
503
DomCallTree DT (CG);
504
+ DT.print (llvm::dbgs ());
464
505
DomCallInfoNode *Root = DT.getRootNode ();
465
506
RetainedSizeCalculator calculator;
466
507
calculator.calculate (Root);
@@ -534,7 +575,8 @@ int main(int argc, char *argv[]) {
534
575
});
535
576
break ;
536
577
}
537
- case ActionType::BinaryToDominators: {
578
+ case ActionType::BinaryToDominators:
579
+ case ActionType::BinaryToDominatorTree: {
538
580
modulesummary::ModuleSummaryIndex summary;
539
581
modulesummary::loadModuleSummaryIndex (fileBufOrErr.get ()->getMemBufferRef (),
540
582
summary);
@@ -544,7 +586,11 @@ int main(int argc, char *argv[]) {
544
586
RetainedSizeCalculator calculator;
545
587
calculator.calculate (Root);
546
588
withOutputFile (diags, options::OutputFilename, [&](raw_ostream &out) {
547
- calculator.display (out);
589
+ if (options::Action == ActionType::BinaryToDominators) {
590
+ calculator.display (out);
591
+ } else {
592
+ calculator.displayTree (out, Root);
593
+ }
548
594
return false ;
549
595
});
550
596
break ;
0 commit comments