Add {-# OPTIONS_GHC -w #-} and some blurb to all compiler modules
[ghc-hetmet.git] / compiler / cmm / CmmOpt.hs
index f279b84..8d8119e 100644 (file)
@@ -6,6 +6,13 @@
 --
 -----------------------------------------------------------------------------
 
+{-# OPTIONS_GHC -w #-}
+-- The above warning supression flag is a temporary kludge.
+-- While working on this module you are encouraged to remove it and fix
+-- any warnings in the module. See
+--     http://hackage.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
+-- for details
+
 module CmmOpt (
        cmmMiniInline,
        cmmMachOpFold,
@@ -18,7 +25,7 @@ import Cmm
 import CmmUtils
 import CLabel
 import MachOp
-import SMRep
+import StaticFlags
 
 import UniqFM
 import Unique
@@ -92,7 +99,7 @@ cmmMiniInline blocks = map do_inline blocks
 
 cmmMiniInlineStmts :: UniqFM Int -> [CmmStmt] -> [CmmStmt]
 cmmMiniInlineStmts uses [] = []
-cmmMiniInlineStmts uses (stmt@(CmmAssign (CmmLocal (LocalReg u _)) expr) : stmts)
+cmmMiniInlineStmts uses (stmt@(CmmAssign (CmmLocal (LocalReg u _ _)) expr) : stmts)
   | Just 1 <- lookupUFM uses u,
     Just stmts' <- lookForInline u expr stmts
   = 
@@ -108,7 +115,7 @@ cmmMiniInlineStmts uses (stmt:stmts)
 -- Try to inline a temporary assignment.  We can skip over assignments to
 -- other tempoararies, because we know that expressions aren't side-effecting
 -- and temporaries are single-assignment.
-lookForInline u expr (stmt@(CmmAssign (CmmLocal (LocalReg u' _)) rhs) : rest)
+lookForInline u expr (stmt@(CmmAssign (CmmLocal (LocalReg u' _ _)) rhs) : rest)
   | u /= u' 
   = case lookupUFM (getExprUses rhs) u of
        Just 1 -> Just (inlineStmt u expr stmt : rest)
@@ -139,9 +146,9 @@ lookForInline u expr (stmt:stmts)
 getStmtUses :: CmmStmt -> UniqFM Int
 getStmtUses (CmmAssign _ e) = getExprUses e
 getStmtUses (CmmStore e1 e2) = plusUFM_C (+) (getExprUses e1) (getExprUses e2)
-getStmtUses (CmmCall target _ es _)
+getStmtUses (CmmCall target _ es _ _)
    = plusUFM_C (+) (uses target) (getExprsUses (map fst es))
-   where uses (CmmForeignCall e _) = getExprUses e
+   where uses (CmmCallee e _) = getExprUses e
         uses _ = emptyUFM
 getStmtUses (CmmCondBranch e _) = getExprUses e
 getStmtUses (CmmSwitch e _) = getExprUses e
@@ -149,8 +156,8 @@ getStmtUses (CmmJump e _) = getExprUses e
 getStmtUses _ = emptyUFM
 
 getExprUses :: CmmExpr -> UniqFM Int
-getExprUses (CmmReg (CmmLocal (LocalReg u _))) = unitUFM u 1
-getExprUses (CmmRegOff (CmmLocal (LocalReg u _)) _) = unitUFM u 1
+getExprUses (CmmReg (CmmLocal (LocalReg u _ _))) = unitUFM u 1
+getExprUses (CmmRegOff (CmmLocal (LocalReg u _ _)) _) = unitUFM u 1
 getExprUses (CmmLoad e _) = getExprUses e
 getExprUses (CmmMachOp _ es) = getExprsUses es
 getExprUses _other = emptyUFM
@@ -160,9 +167,9 @@ getExprsUses es = foldr (plusUFM_C (+)) emptyUFM (map getExprUses es)
 inlineStmt :: Unique -> CmmExpr -> CmmStmt -> CmmStmt
 inlineStmt u a (CmmAssign r e) = CmmAssign r (inlineExpr u a e)
 inlineStmt u a (CmmStore e1 e2) = CmmStore (inlineExpr u a e1) (inlineExpr u a e2)
-inlineStmt u a (CmmCall target regs es vols)
-   = CmmCall (infn target) regs es' vols
-   where infn (CmmForeignCall fn cconv) = CmmForeignCall fn cconv
+inlineStmt u a (CmmCall target regs es srt ret)
+   = CmmCall (infn target) regs es' srt ret
+   where infn (CmmCallee fn cconv) = CmmCallee fn cconv
         infn (CmmPrim p) = CmmPrim p
         es' = [ (inlineExpr u a e, hint) | (e,hint) <- es ]
 inlineStmt u a (CmmCondBranch e d) = CmmCondBranch (inlineExpr u a e) d
@@ -171,10 +178,10 @@ inlineStmt u a (CmmJump e d) = CmmJump (inlineExpr u a e) d
 inlineStmt u a other_stmt = other_stmt
 
 inlineExpr :: Unique -> CmmExpr -> CmmExpr -> CmmExpr
-inlineExpr u a e@(CmmReg (CmmLocal (LocalReg u' _)))
+inlineExpr u a e@(CmmReg (CmmLocal (LocalReg u' _ _)))
   | u == u' = a
   | otherwise = e
-inlineExpr u a e@(CmmRegOff (CmmLocal (LocalReg u' rep)) off)
+inlineExpr u a e@(CmmRegOff (CmmLocal (LocalReg u' rep _)) off)
   | u == u' = CmmMachOp (MO_Add rep) [a, CmmLit (CmmInt (fromIntegral off) rep)]
   | otherwise = e
 inlineExpr u a (CmmLoad e rep) = CmmLoad (inlineExpr u a e) rep
@@ -311,8 +318,12 @@ cmmMachOpFold op [x@(CmmLit _), y]
 -- put arg1 on the left of the rearranged expression, we'll get into a
 -- loop:  (x1+x2)+x3 => x1+(x2+x3)  => (x2+x3)+x1 => x2+(x3+x1) ...
 --
+-- Also don't do it if arg1 is PicBaseReg, so that we don't separate the
+-- PicBaseReg from the corresponding label (or label difference).
+--
 cmmMachOpFold mop1 [CmmMachOp mop2 [arg1,arg2], arg3]
-   | mop1 == mop2 && isAssociativeMachOp mop1 && not (isLit arg1)
+   | mop1 == mop2 && isAssociativeMachOp mop1
+     && not (isLit arg1) && not (isPicReg arg1)
    = cmmMachOpFold mop1 [arg1, cmmMachOpFold mop2 [arg2,arg3]]
 
 -- Make a RegOff if we can
@@ -334,6 +345,38 @@ cmmMachOpFold (MO_Add _) [CmmLit (CmmInt i rep), CmmLit (CmmLabel lbl)]
 cmmMachOpFold (MO_Sub _) [CmmLit (CmmLabel lbl), CmmLit (CmmInt i rep)]
   = CmmLit (CmmLabelOff lbl (fromIntegral (negate (narrowU rep i))))
 
+
+-- Comparison of literal with narrowed/widened operand: perform
+-- the comparison at a different width, as long as the literal is
+-- within range.
+
+#if i386_TARGET_ARCH || x86_64_TARGET_ARCH
+-- powerPC NCG has a TODO for I8/I16 comparisons, so don't try
+
+cmmMachOpFold cmp [CmmMachOp conv [x], CmmLit (CmmInt i _)]
+  | Just (rep, narrow) <- maybe_conversion conv,
+    Just narrow_cmp <- maybe_comparison cmp rep,
+    let narrow_i = narrow rep i,
+    narrow_i == i
+  = cmmMachOpFold narrow_cmp [x, CmmLit (CmmInt narrow_i rep)]
+ where
+    maybe_conversion (MO_U_Conv from _) = Just (from, narrowU)
+    maybe_conversion (MO_S_Conv from _) = Just (from, narrowS)
+    maybe_conversion _ = Nothing
+    
+    maybe_comparison (MO_U_Gt _) rep = Just (MO_U_Gt rep)
+    maybe_comparison (MO_U_Ge _) rep = Just (MO_U_Ge rep)
+    maybe_comparison (MO_U_Lt _) rep = Just (MO_U_Lt rep)
+    maybe_comparison (MO_U_Le _) rep = Just (MO_U_Le rep)
+    maybe_comparison (MO_S_Gt _) rep = Just (MO_S_Gt rep)
+    maybe_comparison (MO_S_Ge _) rep = Just (MO_S_Ge rep)
+    maybe_comparison (MO_S_Lt _) rep = Just (MO_S_Lt rep)
+    maybe_comparison (MO_S_Le _) rep = Just (MO_S_Le rep)
+    maybe_comparison (MO_Eq   _) rep = Just (MO_Eq   rep)
+    maybe_comparison _ _ = Nothing
+
+#endif
+
 -- We can often do something with constants of 0 and 1 ...
 
 cmmMachOpFold mop args@[x, y@(CmmLit (CmmInt 0 _))]
@@ -494,7 +537,7 @@ narrowS _ _ = panic "narrowTo"
   except factorial, but what the hell.
 -}
 
-cmmLoopifyForC :: CmmTop -> CmmTop
+cmmLoopifyForC :: RawCmmTop -> RawCmmTop
 cmmLoopifyForC p@(CmmProc info entry_lbl [] blocks@(BasicBlock top_id _ : _))
   | null info = p  -- only if there's an info table, ignore case alts
   | otherwise =  
@@ -526,3 +569,6 @@ maybeInvertConditionalExpr :: CmmExpr -> Maybe CmmExpr
 maybeInvertConditionalExpr (CmmMachOp op args) 
   | Just op' <- maybeInvertComparison op = Just (CmmMachOp op' args)
 maybeInvertConditionalExpr _ = Nothing
+
+isPicReg (CmmReg (CmmGlobal PicBaseReg)) = True
+isPicReg _ = False