[project @ 2005-04-12 09:17:47 by simonmar]
[ghc-hetmet.git] / ghc / compiler / nativeGen / AsmCodeGen.lhs
index 95a5b6c..cb2d4f7 100644 (file)
@@ -33,13 +33,11 @@ import UniqFM
 import Unique          ( Unique, getUnique )
 import UniqSupply
 import FastTypes
-#if darwin_TARGET_OS || (powerpc_TARGET_ARCH && linux_TARGET_OS)
 import List            ( groupBy, sortBy )
 import CLabel           ( pprCLabel )
-#endif
 import ErrUtils                ( dumpIfSet_dyn )
-import CmdLineOpts     ( DynFlags, DynFlag(..), dopt, opt_Static,
-                         opt_EnsureSplittableC, opt_PIC )
+import DynFlags                ( DynFlags, DynFlag(..), dopt )
+import StaticFlags     ( opt_Static, opt_PIC )
 
 import Digraph
 import qualified Pretty
@@ -123,17 +121,23 @@ nativeCodeGen dflags cmms us
           returnUs (Cmm cmms, my_vcat docs, concat imps)
     in do
     dumpIfSet_dyn dflags Opt_D_dump_opt_cmm "Optimised Cmm" (pprCmms [ppr_cmms])
-    return (insn_sdoc Pretty.$$ dyld_stubs imports)
+    return (insn_sdoc Pretty.$$ dyld_stubs imports
+#if HAVE_SUBSECTIONS_VIA_SYMBOLS
+                -- On recent versions of Darwin, the linker supports
+                -- dead-stripping of code and data on a per-symbol basis.
+                -- There's a hack to make this work in PprMach.pprNatCmmTop.
+            Pretty.$$ Pretty.text ".subsections_via_symbols"
+#endif
+            )
 
   where
 
     add_split (Cmm tops)
-       | opt_EnsureSplittableC = split_marker : tops
-       | otherwise             = tops
+       | dopt Opt_SplitObjs dflags = split_marker : tops
+       | otherwise                 = tops
 
     split_marker = CmmProc [] mkSplitMarkerLabel [] []
 
-#if darwin_TARGET_OS || (powerpc_TARGET_ARCH && linux_TARGET_OS)
         -- Generate "symbol stubs" for all external symbols that might
         -- come from a dynamic library.
 {-    dyld_stubs imps = Pretty.vcat $ map pprDyldSymbolStub $
@@ -155,9 +159,6 @@ nativeCodeGen dflags cmms us
         
         where doPpr lbl = (lbl, Pretty.render $ pprCLabel lbl astyle)
               astyle = mkCodeStyle AsmStyle
-#else
-    dyld_stubs imps = Pretty.empty
-#endif
 
 #ifndef NCG_DEBUG
     my_vcat sds = Pretty.vcat sds
@@ -378,6 +379,9 @@ Ideas for other things we could do (ToDo):
 
   - shortcut jumps-to-jumps
   - eliminate dead code blocks
+  - simple CSE: if an expr is assigned to a temp, then replace later occs of
+    that expr with the temp, until the expr is no longer valid (can push through
+    temp assignments, and certain assigns to mem...)
 -}
 
 cmmToCmm :: CmmTop -> (CmmTop, [CLabel])
@@ -551,12 +555,37 @@ cmmMachOpFold op arg@[CmmLit (CmmInt x rep)]
 cmmMachOpFold (MO_S_Conv rep1 rep2) [x] | rep1 == rep2 = x
 cmmMachOpFold (MO_U_Conv rep1 rep2) [x] | rep1 == rep2 = x
 
--- ToDo: eliminate multiple conversions.  Be careful though: can't remove
--- a narrowing, and can't remove conversions to/from floating point types.
-
--- ToDo: eliminate nested comparisons:
---    CmmMachOp MO_Lt [CmmMachOp MO_Eq [x,y], CmmLit (CmmInt 0 _)]
--- turns into a simple equality test.
+-- Eliminate nested conversions where possible
+cmmMachOpFold conv_outer args@[CmmMachOp conv_inner [x]]
+  | Just (rep1,rep2,signed1) <- isIntConversion conv_inner,
+    Just (_,   rep3,signed2) <- isIntConversion conv_outer
+  = case () of
+       -- widen then narrow to the same size is a nop
+      _ | rep1 < rep2 && rep1 == rep3 -> x
+       -- Widen then narrow to different size: collapse to single conversion
+       -- but remember to use the signedness from the widening, just in case
+       -- the final conversion is a widen.
+       | rep1 < rep2 && rep2 > rep3 ->
+           cmmMachOpFold (intconv signed1 rep1 rep3) [x]
+       -- Nested widenings: collapse if the signedness is the same
+       | rep1 < rep2 && rep2 < rep3 && signed1 == signed2 ->
+           cmmMachOpFold (intconv signed1 rep1 rep3) [x]
+       -- Nested narrowings: collapse
+       | rep1 > rep2 && rep2 > rep3 ->
+           cmmMachOpFold (MO_U_Conv rep1 rep3) [x]
+       | otherwise ->
+           CmmMachOp conv_outer args
+  where
+       isIntConversion (MO_U_Conv rep1 rep2) = Just (rep1,rep2,False)
+       isIntConversion (MO_S_Conv rep1 rep2) = Just (rep1,rep2,True)
+       isIntConversion _ = Nothing
+       intconv True  = MO_S_Conv
+       intconv False = MO_U_Conv
+
+-- ToDo: a narrow of a load can be collapsed into a narrow load, right?
+-- but what if the architecture only supports word-sized loads, should
+-- we do the transformation anyway?
 
 cmmMachOpFold mop args@[CmmLit (CmmInt x xrep), CmmLit (CmmInt y _)]
   = case mop of