Changes between Version 8 and Version 9 of Commentary/Libraries/Integer


Ignore:
Timestamp:
Jul 13, 2012 9:28:42 PM (22 months ago)
Author:
simonpj
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Commentary/Libraries/Integer

    v8 v9  
    2929 * '''Constant folding'''.  There are many constant-folding optimisations for `Integer` expressed as built-in rules in [[GhcFile(compiler/prelude/PrelRules.lhs)]]; look at `builtinIntegerRules`.  All of the types and functions in the `Integer` interface have built-in names, e.g. `plusIntegerName`, defined in [[GhcFile(compiler/prelude/PrelNames.lhs)]] and included in `basicKnownKeyNames`. This allows us to match on all of the functions in `builtinIntegerRules` in [[GhcFile(compiler/prelude/PrelRules.lhs)]], so we can constant-fold Integer expressions. 
    3030 
    31  * '''Representing integers'''.  We stick to the `LitInteger` representation (which hides the concrete representation) as late as possible in the compiler.   In particular, it's important that this representation is used in unfoldings in interface files, so that constant folding can happen on expressions that get inlined.  We finally convert `LitInteger` to a proper core representation of Integer in [[GhcFile(compiler/coreSyn/CorePrep.lhs)]], which looks up the Id for `mkInteger` and uses it to build an expression like `mkInteger True [123, 456]` (where the `Bool` represents the sign, and the list of `Int`s are 31 bit chunks of the absolute value from lowest to highest). 
     31 * '''Converting between Int and Integer'''.  It's quite commonly the case that, after some inlining, we get something like `integerToInt (intToInteger i)`, which converts an `Int` to an `Integer` and back.  This ''must'' optimise away.  We do this by requiring that the `integer` package exposes 
     32{{{ 
     33  smallInteger :: Int# -> Int 
     34}}} 
     35 Now we can define `intToInteger` (or, more precisely, the `toInteger` method of the `Integral Int` instance in `GHC.Real` ) thus 
     36{{{ 
     37  toInteger (I# i) = smallInteger i 
     38}}} 
     39 And we have a RULE for `integerToInt (smallInteger i)`. 
     40 
     41 * '''Representing integers'''.  We stick to the `LitInteger` representation (which hides the concrete representation) as late as possible in the compiler.   In particular, it's important that the `LitInteger` representation is used in unfoldings in interface files, so that constant folding can happen on expressions that get inlined.   
     42 
     43 We finally convert `LitInteger` to a proper core representation of Integer in [[GhcFile(compiler/coreSyn/CorePrep.lhs)]], which looks up the Id for `mkInteger` and uses it to build an expression like `mkInteger True [123, 456]` (where the `Bool` represents the sign, and the list of `Int`s are 31 bit chunks of the absolute value from lowest to highest). 
    3244 
    3345 However, there is a special case for `Integer`s that are within the range of `Int` when the `integer-gmp` implementation is being used; in that case, we use the `S#` constructor (via `integerGmpSDataCon` in [[GhcFile(compiler/prelude/TysWiredIn.lhs)]]) to break the abstraction and directly create the datastructure.