From f4a0c81371553d45b3536d29d73a1aee8216db66 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Fri, 17 Dec 2010 18:13:52 +0000 Subject: [PATCH] Add MachineLoopRange comparators for sorting loop lists by number and by area. llvm-svn: 122073 --- include/llvm/CodeGen/MachineLoopRanges.h | 19 +++++++++++++ lib/CodeGen/MachineLoopRanges.cpp | 35 ++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/include/llvm/CodeGen/MachineLoopRanges.h b/include/llvm/CodeGen/MachineLoopRanges.h index 730b729dba7..3f2f24194a4 100644 --- a/include/llvm/CodeGen/MachineLoopRanges.h +++ b/include/llvm/CodeGen/MachineLoopRanges.h @@ -42,6 +42,9 @@ private: /// Bit 0 = inside loop block. Map Intervals; + /// Loop area as measured by SlotIndex::distance. + unsigned Area; + /// Create a MachineLoopRange, only accessible to MachineLoopRanges. MachineLoopRange(const MachineLoop*, Allocator&, SlotIndexes&); @@ -50,11 +53,27 @@ public: /// inteructions. bool overlaps(SlotIndex Start, SlotIndex Stop); + /// getNumber - Return the loop number. This is the same as the number of the + /// header block. + unsigned getNumber() const; + + /// getArea - Return the loop area. This number is approximately proportional + /// to the number of instructions in the loop. + unsigned getArea() const { return Area; } + /// getMap - Allow public read-only access for IntervalMapOverlaps. const Map &getMap() { return Intervals; } /// print - Print loop ranges on OS. void print(raw_ostream&) const; + + /// byNumber - Comparator for array_pod_sort that sorts a list of + /// MachineLoopRange pointers by number. + static int byNumber(const void*, const void*); + + /// byAreaDesc - Comparator for array_pod_sort that sorts a list of + /// MachineLoopRange pointers by descending area, then by number. + static int byAreaDesc(const void*, const void*); }; raw_ostream &operator<<(raw_ostream&, const MachineLoopRange&); diff --git a/lib/CodeGen/MachineLoopRanges.cpp b/lib/CodeGen/MachineLoopRanges.cpp index 9ee6c5bd125..17fe67f6504 100644 --- a/lib/CodeGen/MachineLoopRanges.cpp +++ b/lib/CodeGen/MachineLoopRanges.cpp @@ -57,12 +57,13 @@ MachineLoopRange *MachineLoopRanges::getLoopRange(const MachineLoop *Loop) { MachineLoopRange::MachineLoopRange(const MachineLoop *loop, MachineLoopRange::Allocator &alloc, SlotIndexes &Indexes) - : Loop(loop), Intervals(alloc) { + : Loop(loop), Intervals(alloc), Area(0) { // Compute loop coverage. for (MachineLoop::block_iterator I = Loop->block_begin(), E = Loop->block_end(); I != E; ++I) { const std::pair &Range = Indexes.getMBBRange(*I); Intervals.insert(Range.first, Range.second, 1u); + Area += Range.first.distance(Range.second); } } @@ -73,8 +74,38 @@ bool MachineLoopRange::overlaps(SlotIndex Start, SlotIndex Stop) { return I.valid() && Stop > I.start(); } +unsigned MachineLoopRange::getNumber() const { + return Loop->getHeader()->getNumber(); +} + +/// byNumber - Comparator for array_pod_sort that sorts a list of +/// MachineLoopRange pointers by number. +int MachineLoopRange::byNumber(const void *pa, const void *pb) { + const MachineLoopRange *a = *static_cast(pa); + const MachineLoopRange *b = *static_cast(pb); + unsigned na = a->getNumber(); + unsigned nb = b->getNumber(); + if (na < nb) + return -1; + if (na > nb) + return 1; + return 0; +} + +/// byAreaDesc - Comparator for array_pod_sort that sorts a list of +/// MachineLoopRange pointers by: +/// 1. Descending area. +/// 2. Ascending number. +int MachineLoopRange::byAreaDesc(const void *pa, const void *pb) { + const MachineLoopRange *a = *static_cast(pa); + const MachineLoopRange *b = *static_cast(pb); + if (a->getArea() != b->getArea()) + return a->getArea() > b->getArea() ? -1 : 1; + return byNumber(pa, pb); +} + void MachineLoopRange::print(raw_ostream &OS) const { - OS << "Loop#" << Loop->getHeader()->getNumber() << " ="; + OS << "Loop#" << getNumber() << " ="; for (Map::const_iterator I = Intervals.begin(); I.valid(); ++I) OS << " [" << I.start() << ';' << I.stop() << ')'; }