source: src/ResolvExpr/Cost.h @ d97c3a4

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since d97c3a4 was d97c3a4, checked in by Aaron Moss <a3moss@…>, 5 years ago

Fix new cost model by boosting precedence of safe costs

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[a32b204]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[b46e3bd]7// Cost.h --
[a32b204]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 09:39:50 2015
[1dd1bd2]11// Last Modified By : Aaron B. Moss
[6d6e829]12// Last Modified On : Fri Oct 05 14:32:00 2018
13// Update Count     : 7
[a32b204]14//
15
[6b0b624]16#pragma once
[51b7345]17
18#include <iostream>
19
20namespace ResolvExpr {
[a32b204]21        class Cost {
[89be1c68]22          private:
[d97c3a4]23                Cost( int unsafeCost, int polyCost, int safeCost, int varCost, int specCost,
24                        int referenceCost );
[b46e3bd]25
[89be1c68]26          public:
27                Cost & incUnsafe( int inc = 1 );
28                Cost & incPoly( int inc = 1 );
[d97c3a4]29                Cost & incSafe( int inc = 1 );
[1dd1bd2]30                Cost & incVar( int inc = 1 );
31                Cost & decSpec( int inc = 1 );
[7ebaa56]32                Cost & incReference( int inc = 1 );
[b46e3bd]33
[ddf8a29]34                int get_unsafeCost() const { return unsafeCost; }
35                int get_polyCost() const { return polyCost; }
[d97c3a4]36                int get_safeCost() const { return safeCost; }
[1dd1bd2]37                int get_varCost() const { return varCost; }
38                int get_specCost() const { return specCost; }
[ddf8a29]39                int get_referenceCost() const { return referenceCost; }
40
[a32b204]41                Cost operator+( const Cost &other ) const;
42                Cost operator-( const Cost &other ) const;
43                Cost &operator+=( const Cost &other );
44                bool operator<( const Cost &other ) const;
45                bool operator==( const Cost &other ) const;
46                bool operator!=( const Cost &other ) const;
47                friend std::ostream &operator<<( std::ostream &os, const Cost &cost );
[6d6e829]48                // returns negative for *this < other, 0 for *this == other, positive for *this > other
49                int compare( const Cost &other ) const;
[b46e3bd]50
[a32b204]51                static const Cost zero;
52                static const Cost infinity;
[7ebaa56]53
[b46e3bd]54                static const Cost unsafe;
55                static const Cost poly;
[d97c3a4]56                static const Cost safe;
[1dd1bd2]57                static const Cost var;
58                static const Cost spec;
[7ebaa56]59                static const Cost reference;
[a32b204]60
[6d6e829]61          private:
[1dd1bd2]62                int unsafeCost;     ///< Unsafe (narrowing) conversions
63                int polyCost;       ///< Count of parameters and return values bound to some poly type
[d97c3a4]64                int safeCost;       ///< Safe (widening) conversions
[1dd1bd2]65                int varCost;        ///< Count of polymorphic type variables
66                int specCost;       ///< Polymorphic type specializations (type assertions), negative cost
67                int referenceCost;  ///< reference conversions
[a32b204]68        };
69
[d97c3a4]70        inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int varCost, int specCost, 
[1dd1bd2]71                        int referenceCost ) 
[d97c3a4]72                : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), varCost( varCost ), 
73                  specCost( specCost ), referenceCost( referenceCost ) {}
[a32b204]74
[89be1c68]75        inline Cost & Cost::incUnsafe( int inc ) {
[17f22e78]76                if ( *this == infinity ) return *this;
[b46e3bd]77                unsafeCost += inc;
[89be1c68]78                return *this;
[a32b204]79        }
80
[89be1c68]81        inline Cost & Cost::incPoly( int inc ) {
[17f22e78]82                if ( *this == infinity ) return *this;
[b46e3bd]83                polyCost += inc;
[89be1c68]84                return *this;
[a32b204]85        }
86
[d97c3a4]87        inline Cost & Cost::incSafe( int inc ) {
[1dd1bd2]88                if ( *this == infinity ) return *this;
[d97c3a4]89                safeCost += inc;
[1dd1bd2]90                return *this;
91        }
92
[d97c3a4]93        inline Cost & Cost::incVar( int inc ) {
[1dd1bd2]94                if ( *this == infinity ) return *this;
[d97c3a4]95                varCost += inc;
[1dd1bd2]96                return *this;
97        }
98
[d97c3a4]99        inline Cost& Cost::decSpec( int dec ) {
[17f22e78]100                if ( *this == infinity ) return *this;
[d97c3a4]101                specCost -= dec;
[89be1c68]102                return *this;
[a32b204]103        }
104
[7ebaa56]105        inline Cost & Cost::incReference( int inc ) {
[17f22e78]106                if ( *this == infinity ) return *this;
[7ebaa56]107                referenceCost += inc;
108                return *this;
109        }
110
[a32b204]111        inline Cost Cost::operator+( const Cost &other ) const {
[17f22e78]112                if ( *this == infinity || other == infinity ) return infinity;
[1dd1bd2]113                return Cost{ 
[d97c3a4]114                        unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, 
[1dd1bd2]115                        varCost + other.varCost, specCost + other.specCost, 
[d97c3a4]116                        referenceCost + other.referenceCost };
[a32b204]117        }
118
119        inline Cost Cost::operator-( const Cost &other ) const {
[17f22e78]120                if ( *this == infinity || other == infinity ) return infinity;
[1dd1bd2]121                return Cost{ 
[d97c3a4]122                        unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost, 
[1dd1bd2]123                        varCost - other.varCost, specCost - other.specCost, 
[d97c3a4]124                        referenceCost - other.referenceCost };
[a32b204]125        }
126
127        inline Cost &Cost::operator+=( const Cost &other ) {
[17f22e78]128                if ( *this == infinity ) return *this;
129                if ( other == infinity ) {
130                        *this = infinity;
131                        return *this;
132                }
[b46e3bd]133                unsafeCost += other.unsafeCost;
134                polyCost += other.polyCost;
[d97c3a4]135                safeCost += other.safeCost;
[1dd1bd2]136                varCost += other.varCost;
137                specCost += other.specCost;
[7ebaa56]138                referenceCost += other.referenceCost;
[a32b204]139                return *this;
140        }
141
142        inline bool Cost::operator<( const Cost &other ) const {
[b46e3bd]143                if ( *this == infinity ) return false;
144                if ( other == infinity ) return true;
145
146                if ( unsafeCost > other.unsafeCost ) {
[a32b204]147                        return false;
[b46e3bd]148                } else if ( unsafeCost < other.unsafeCost ) {
[a32b204]149                        return true;
[b46e3bd]150                } else if ( polyCost > other.polyCost ) {
[a32b204]151                        return false;
[b46e3bd]152                } else if ( polyCost < other.polyCost ) {
[a32b204]153                        return true;
[d97c3a4]154                } else if ( safeCost > other.safeCost ) {
155                        return false;
156                } else if ( safeCost < other.safeCost ) {
157                        return true;
[1dd1bd2]158                } else if ( varCost > other.varCost ) {
159                        return false;
160                } else if ( varCost < other.varCost ) {
161                        return true;
162                } else if ( specCost > other.specCost ) {
163                        return false;
164                } else if ( specCost > other.specCost ) {
165                        return true;
[7ebaa56]166                } else if ( referenceCost > other.referenceCost ) {
167                        return false;
168                } else if ( referenceCost < other.referenceCost ) {
169                        return true;
[b46e3bd]170                } else {
[a32b204]171                        return false;
[b46e3bd]172                } // if
[d9a0e76]173        }
174
[6d6e829]175        inline int Cost::compare( const Cost &other ) const {
176                if ( *this == infinity ) return +1;
177                if ( other == infinity ) return -1;
178
179                int c = unsafeCost - other.unsafeCost; if ( c ) return c;
180                c = polyCost - other.polyCost; if ( c ) return c;
[d97c3a4]181                c = safeCost - other.safeCost; if ( c ) return c;
[6d6e829]182                c = varCost - other.varCost; if ( c ) return c;
183                c = specCost - other.specCost; if ( c ) return c;
184                return referenceCost - other.referenceCost;
185        }
186
[a32b204]187        inline bool Cost::operator==( const Cost &other ) const {
[b46e3bd]188                return unsafeCost == other.unsafeCost
189                        && polyCost == other.polyCost
[d97c3a4]190                        && safeCost == other.safeCost
[1dd1bd2]191                        && varCost == other.varCost
192                        && specCost == other.specCost
[7ebaa56]193                        && referenceCost == other.referenceCost;
[a32b204]194        }
[d9a0e76]195
[a32b204]196        inline bool Cost::operator!=( const Cost &other ) const {
197                return !( *this == other );
198        }
[d9a0e76]199
[a32b204]200        inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
[1dd1bd2]201                return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " 
[d97c3a4]202                          << cost.safeCost << ", " << cost.varCost << ", " << cost.specCost << ", "
203                          << cost.referenceCost << " )";
[a32b204]204        }
[51b7345]205} // namespace ResolvExpr
206
[a32b204]207// Local Variables: //
208// tab-width: 4 //
209// mode: c++ //
210// compile-command: "make install" //
211// End: //
Note: See TracBrowser for help on using the repository browser.