1 -----------------------------------------------------------------------------
3 -- Code generator utilities; mostly monadic
5 -- (c) The University of Glasgow 2004
7 -----------------------------------------------------------------------------
12 emitDataLits, emitRODataLits, emitIf, emitIfThenElse,
13 emitRtsCall, emitRtsCallWithVols, emitRtsCallWithResult,
16 emitSwitch, emitLitSwitch,
19 cmmAndWord, cmmOrWord, cmmNegate, cmmEqWord, cmmNeWord,
20 cmmOffsetExprW, cmmOffsetExprB,
21 cmmRegOffW, cmmRegOffB,
22 cmmLabelOffW, cmmLabelOffB,
23 cmmOffsetW, cmmOffsetB,
24 cmmOffsetLitW, cmmOffsetLitB,
34 #include "HsVersions.h"
37 import TyCon ( TyCon, tyConName )
39 import Constants ( wORD_SIZE )
40 import SMRep ( CgRep, StgWord, hALF_WORD_SIZE_IN_BITS, ByteOff,
42 import PprCmm ( {- instances -} )
46 import MachOp ( MachRep(..), wordRep, MachOp(..), MachHint(..),
47 mo_wordOr, mo_wordAnd, mo_wordNe, mo_wordEq,
48 mo_wordULt, mo_wordUGt, machRepByteWidth )
49 import ForeignCall ( CCallConv(..) )
50 import Literal ( Literal(..) )
51 import CLabel ( CLabel, mkStringLitLabel )
52 import Digraph ( SCC(..), stronglyConnComp )
53 import ListSetOps ( assocDefault )
54 import Util ( filterOut, sortLe )
55 import CmdLineOpts ( DynFlags )
56 import FastString ( LitString, FastString, unpackFS )
61 import Maybe ( isNothing )
63 #include "../includes/ghcconfig.h"
64 -- For WORDS_BIGENDIAN
66 -------------------------------------------------------------------------
68 -- Random small functions
70 -------------------------------------------------------------------------
72 addIdReps :: [Id] -> [(CgRep, Id)]
73 addIdReps ids = [(idCgRep id, id) | id <- ids]
75 -------------------------------------------------------------------------
79 -------------------------------------------------------------------------
81 cgLit :: Literal -> FCode CmmLit
82 cgLit (MachStr s) = mkStringCLit (unpackFS s)
83 cgLit other_lit = return (mkSimpleLit other_lit)
85 mkSimpleLit :: Literal -> CmmLit
86 mkSimpleLit (MachChar c) = CmmInt (fromIntegral (ord c)) wordRep
87 mkSimpleLit MachNullAddr = zeroCLit
88 mkSimpleLit (MachInt i) = CmmInt i wordRep
89 mkSimpleLit (MachInt64 i) = CmmInt i I64
90 mkSimpleLit (MachWord i) = CmmInt i wordRep
91 mkSimpleLit (MachWord64 i) = CmmInt i I64
92 mkSimpleLit (MachFloat r) = CmmFloat r F32
93 mkSimpleLit (MachDouble r) = CmmFloat r F64
94 mkSimpleLit (MachLabel fs ms) = CmmLabel (mkForeignLabel fs ms is_dyn)
96 is_dyn = False -- ToDo: fix me
98 mkLtOp :: Literal -> MachOp
99 -- On signed literals we must do a signed comparison
100 mkLtOp (MachInt _) = MO_S_Lt wordRep
101 mkLtOp (MachFloat _) = MO_S_Lt F32
102 mkLtOp (MachDouble _) = MO_S_Lt F64
103 mkLtOp lit = MO_U_Lt (cmmLitRep (mkSimpleLit lit))
106 ---------------------------------------------------
108 -- Cmm data type functions
110 ---------------------------------------------------
112 -----------------------
113 -- The "B" variants take byte offsets
114 cmmRegOffB :: CmmReg -> ByteOff -> CmmExpr
115 cmmRegOffB = cmmRegOff
117 cmmOffsetB :: CmmExpr -> ByteOff -> CmmExpr
118 cmmOffsetB = cmmOffset
120 cmmOffsetExprB :: CmmExpr -> CmmExpr -> CmmExpr
121 cmmOffsetExprB = cmmOffsetExpr
123 cmmLabelOffB :: CLabel -> ByteOff -> CmmLit
124 cmmLabelOffB = cmmLabelOff
126 cmmOffsetLitB :: CmmLit -> ByteOff -> CmmLit
127 cmmOffsetLitB = cmmOffsetLit
129 -----------------------
130 -- The "W" variants take word offsets
131 cmmOffsetExprW :: CmmExpr -> CmmExpr -> CmmExpr
132 -- The second arg is a *word* offset; need to change it to bytes
133 cmmOffsetExprW e (CmmLit (CmmInt n _)) = cmmOffsetW e (fromInteger n)
134 cmmOffsetExprW e wd_off = cmmIndexExpr wordRep e wd_off
136 cmmOffsetW :: CmmExpr -> WordOff -> CmmExpr
137 cmmOffsetW e n = cmmOffsetB e (wORD_SIZE * n)
139 cmmRegOffW :: CmmReg -> WordOff -> CmmExpr
140 cmmRegOffW reg wd_off = cmmRegOffB reg (wd_off * wORD_SIZE)
142 cmmOffsetLitW :: CmmLit -> WordOff -> CmmLit
143 cmmOffsetLitW lit wd_off = cmmOffsetLitB lit (wORD_SIZE * wd_off)
145 cmmLabelOffW :: CLabel -> WordOff -> CmmLit
146 cmmLabelOffW lbl wd_off = cmmLabelOffB lbl (wORD_SIZE * wd_off)
148 cmmLoadIndexW :: CmmExpr -> Int -> CmmExpr
149 cmmLoadIndexW base off
150 = CmmLoad (cmmOffsetW base off) wordRep
152 -----------------------
153 cmmNeWord, cmmEqWord, cmmOrWord, cmmAndWord :: CmmExpr -> CmmExpr -> CmmExpr
154 cmmOrWord e1 e2 = CmmMachOp mo_wordOr [e1, e2]
155 cmmAndWord e1 e2 = CmmMachOp mo_wordAnd [e1, e2]
156 cmmNeWord e1 e2 = CmmMachOp mo_wordNe [e1, e2]
157 cmmEqWord e1 e2 = CmmMachOp mo_wordEq [e1, e2]
158 cmmULtWord e1 e2 = CmmMachOp mo_wordULt [e1, e2]
159 cmmUGtWord e1 e2 = CmmMachOp mo_wordUGt [e1, e2]
161 cmmNegate :: CmmExpr -> CmmExpr
162 cmmNegate (CmmLit (CmmInt n rep)) = CmmLit (CmmInt (-n) rep)
163 cmmNegate e = CmmMachOp (MO_S_Neg (cmmExprRep e)) [e]
165 blankWord :: CmmStatic
166 blankWord = CmmUninitialised wORD_SIZE
168 -----------------------
171 mkWordCLit :: StgWord -> CmmLit
172 mkWordCLit wd = CmmInt (fromIntegral wd) wordRep
174 packHalfWordsCLit :: (Integral a, Integral b) => a -> b -> CmmLit
175 -- Make a single word literal in which the lower_half_word is
176 -- at the lower address, and the upper_half_word is at the
178 -- ToDo: consider using half-word lits instead
179 -- but be careful: that's vulnerable when reversed
180 packHalfWordsCLit lower_half_word upper_half_word
181 #ifdef WORDS_BIGENDIAN
182 = mkWordCLit ((fromIntegral lower_half_word `shiftL` hALF_WORD_SIZE_IN_BITS)
183 .|. fromIntegral upper_half_word)
185 = mkWordCLit ((fromIntegral lower_half_word)
186 .|. (fromIntegral upper_half_word `shiftL` hALF_WORD_SIZE_IN_BITS))
189 --------------------------------------------------------------------------
191 -- Incrementing a memory location
193 --------------------------------------------------------------------------
195 addToMem :: MachRep -- rep of the counter
196 -> CmmExpr -- Address
197 -> Int -- What to add (a word)
199 addToMem rep ptr n = addToMemE rep ptr (CmmLit (CmmInt (toInteger n) rep))
201 addToMemE :: MachRep -- rep of the counter
202 -> CmmExpr -- Address
203 -> CmmExpr -- What to add (a word-typed expression)
206 = CmmStore ptr (CmmMachOp (MO_Add rep) [CmmLoad ptr rep, n])
208 -------------------------------------------------------------------------
210 -- Converting a closure tag to a closure for enumeration types
211 -- (this is the implementation of tagToEnum#).
213 -------------------------------------------------------------------------
215 tagToClosure :: DynFlags -> TyCon -> CmmExpr -> CmmExpr
216 tagToClosure dflags tycon tag
217 = CmmLoad (cmmOffsetExprW closure_tbl tag) wordRep
218 where closure_tbl = CmmLit (CmmLabel lbl)
219 lbl = mkClosureTableLabel dflags (tyConName tycon)
221 -------------------------------------------------------------------------
223 -- Conditionals and rts calls
225 -------------------------------------------------------------------------
227 emitIf :: CmmExpr -- Boolean
230 -- Emit (if e then x)
231 -- ToDo: reverse the condition to avoid the extra branch instruction if possible
232 -- (some conditionals aren't reversible. eg. floating point comparisons cannot
233 -- be inverted because there exist some values for which both comparisons
234 -- return False, such as NaN.)
235 emitIf cond then_part
236 = do { then_id <- newLabelC
237 ; join_id <- newLabelC
238 ; stmtC (CmmCondBranch cond then_id)
239 ; stmtC (CmmBranch join_id)
245 emitIfThenElse :: CmmExpr -- Boolean
249 -- Emit (if e then x else y)
250 emitIfThenElse cond then_part else_part
251 = do { then_id <- newLabelC
252 ; else_id <- newLabelC
253 ; join_id <- newLabelC
254 ; stmtC (CmmCondBranch cond then_id)
256 ; stmtC (CmmBranch join_id)
262 emitRtsCall :: LitString -> [(CmmExpr,MachHint)] -> Code
263 emitRtsCall fun args = emitRtsCall' [] fun args Nothing
264 -- The 'Nothing' says "save all global registers"
266 emitRtsCallWithVols :: LitString -> [(CmmExpr,MachHint)] -> [GlobalReg] -> Code
267 emitRtsCallWithVols fun args vols
268 = emitRtsCall' [] fun args (Just vols)
270 emitRtsCallWithResult :: CmmReg -> MachHint -> LitString
271 -> [(CmmExpr,MachHint)] -> Code
272 emitRtsCallWithResult res hint fun args
273 = emitRtsCall' [(res,hint)] fun args Nothing
275 -- Make a call to an RTS C procedure
277 :: [(CmmReg,MachHint)]
279 -> [(CmmExpr,MachHint)]
282 emitRtsCall' res fun args vols = stmtC (CmmCall target res args vols)
284 target = CmmForeignCall fun_expr CCallConv
285 fun_expr = mkLblExpr (mkRtsCodeLabel fun)
288 -------------------------------------------------------------------------
290 -- Strings gnerate a top-level data block
292 -------------------------------------------------------------------------
294 emitDataLits :: CLabel -> [CmmLit] -> Code
295 -- Emit a data-segment data block
296 emitDataLits lbl lits
297 = emitData Data (CmmDataLabel lbl : map CmmStaticLit lits)
299 emitRODataLits :: CLabel -> [CmmLit] -> Code
300 -- Emit a read-only data block
301 emitRODataLits lbl lits
302 = emitData section (CmmDataLabel lbl : map CmmStaticLit lits)
303 where section | any needsRelocation lits = RelocatableReadOnlyData
304 | otherwise = ReadOnlyData
305 needsRelocation (CmmLabel _) = True
306 needsRelocation (CmmLabelOff _ _) = True
307 needsRelocation _ = False
309 mkStringCLit :: String -> FCode CmmLit
310 -- Make a global definition for the string,
311 -- and return its label
313 = do { uniq <- newUnique
314 ; let lbl = mkStringLitLabel uniq
315 ; emitData ReadOnlyData [CmmDataLabel lbl, CmmString str]
316 ; return (CmmLabel lbl) }
318 -------------------------------------------------------------------------
320 -- Assigning expressions to temporaries
322 -------------------------------------------------------------------------
324 assignTemp :: CmmExpr -> FCode CmmExpr
325 -- For a non-trivial expression, e, create a local
326 -- variable and assign the expression to it
328 | isTrivialCmmExpr e = return e
329 | otherwise = do { reg <- newTemp (cmmExprRep e)
330 ; stmtC (CmmAssign reg e)
331 ; return (CmmReg reg) }
334 newTemp :: MachRep -> FCode CmmReg
335 newTemp rep = do { uniq <- newUnique; return (CmmLocal (LocalReg uniq rep)) }
338 -------------------------------------------------------------------------
340 -- Building case analysis
342 -------------------------------------------------------------------------
345 :: CmmExpr -- Tag to switch on
346 -> [(ConTagZ, CgStmts)] -- Tagged branches
347 -> Maybe CgStmts -- Default branch (if any)
348 -> ConTagZ -> ConTagZ -- Min and Max possible values; behaviour
349 -- outside this range is undefined
352 -- ONLY A DEFAULT BRANCH: no case analysis to do
353 emitSwitch tag_expr [] (Just stmts) _ _
357 emitSwitch tag_expr branches mb_deflt lo_tag hi_tag
358 = -- Just sort the branches before calling mk_sritch
361 Nothing -> return Nothing
362 Just stmts -> do id <- forkCgStmts stmts; return (Just id)
364 ; stmts <- mk_switch tag_expr (sortLe le branches)
365 mb_deflt_id lo_tag hi_tag
369 (t1,_) `le` (t2,_) = t1 <= t2
372 mk_switch :: CmmExpr -> [(ConTagZ, CgStmts)]
373 -> Maybe BlockId -> ConTagZ -> ConTagZ
376 -- SINGLETON TAG RANGE: no case analysis to do
377 mk_switch tag_expr [(tag,stmts)] _ lo_tag hi_tag
379 = ASSERT( tag == lo_tag )
382 -- SINGLETON BRANCH, NO DEFUALT: no case analysis to do
383 mk_switch tag_expr [(tag,stmts)] Nothing lo_tag hi_tag
385 -- The simplifier might have eliminated a case
386 -- so we may have e.g. case xs of
388 -- In that situation we can be sure the (:) case
389 -- can't happen, so no need to test
391 -- SINGLETON BRANCH: one equality check to do
392 mk_switch tag_expr [(tag,stmts)] (Just deflt) lo_tag hi_tag
393 = return (CmmCondBranch cond deflt `consCgStmt` stmts)
395 cond = cmmNeWord tag_expr (CmmLit (mkIntCLit tag))
396 -- We have lo_tag < hi_tag, but there's only one branch,
397 -- so there must be a default
399 -- ToDo: we might want to check for the two branch case, where one of
400 -- the branches is the tag 0, because comparing '== 0' is likely to be
401 -- more efficient than other kinds of comparison.
403 -- DENSE TAG RANGE: use a switch statment
404 mk_switch tag_expr branches mb_deflt lo_tag hi_tag
405 | use_switch -- Use a switch
406 = do { branch_ids <- mapM forkCgStmts (map snd branches)
408 tagged_blk_ids = zip (map fst branches) (map Just branch_ids)
410 find_branch :: ConTagZ -> Maybe BlockId
411 find_branch i = assocDefault mb_deflt tagged_blk_ids i
413 -- NB. we have eliminated impossible branches at
414 -- either end of the range (see below), so the first
415 -- tag of a real branch is real_lo_tag (not lo_tag).
416 arms = [ find_branch i | i <- [real_lo_tag..real_hi_tag]]
418 switch_stmt = CmmSwitch (cmmOffset tag_expr (- real_lo_tag)) arms
420 ; ASSERT(not (all isNothing arms))
421 return (oneCgStmt switch_stmt)
424 -- if we can knock off a bunch of default cases with one if, then do so
425 | Just deflt <- mb_deflt, (lowest_branch - lo_tag) >= n_branches
426 = do { (assign_tag, tag_expr') <- assignTemp' tag_expr
427 ; let cond = cmmULtWord tag_expr' (CmmLit (mkIntCLit lowest_branch))
428 branch = CmmCondBranch cond deflt
429 ; stmts <- mk_switch tag_expr' branches mb_deflt lowest_branch hi_tag
430 ; return (assign_tag `consCgStmt` (branch `consCgStmt` stmts))
433 | Just deflt <- mb_deflt, (hi_tag - highest_branch) >= n_branches
434 = do { (assign_tag, tag_expr') <- assignTemp' tag_expr
435 ; let cond = cmmUGtWord tag_expr' (CmmLit (mkIntCLit highest_branch))
436 branch = CmmCondBranch cond deflt
437 ; stmts <- mk_switch tag_expr' branches mb_deflt lo_tag highest_branch
438 ; return (assign_tag `consCgStmt` (branch `consCgStmt` stmts))
441 | otherwise -- Use an if-tree
442 = do { (assign_tag, tag_expr') <- assignTemp' tag_expr
443 -- To avoid duplication
444 ; lo_stmts <- mk_switch tag_expr' lo_branches mb_deflt lo_tag (mid_tag-1)
445 ; hi_stmts <- mk_switch tag_expr' hi_branches mb_deflt mid_tag hi_tag
446 ; lo_id <- forkCgStmts lo_stmts
447 ; let cond = cmmULtWord tag_expr' (CmmLit (mkIntCLit mid_tag))
448 branch_stmt = CmmCondBranch cond lo_id
449 ; return (assign_tag `consCgStmt` (branch_stmt `consCgStmt` hi_stmts))
452 use_switch = ASSERT( n_branches > 1 && n_tags > 1 )
453 {- pprTrace "mk_switch" (ppr tag_expr <+> text "n_tags: "
454 <+> int n_tags <+> text "dense: "
455 <+> int n_branches) $ -}
456 n_tags > 2 && (small || dense)
457 -- a 2-branch switch always turns into an if.
459 dense = n_branches > (n_tags `div` 2)
460 exhaustive = n_tags == n_branches
461 n_branches = length branches
463 -- ignore default slots at each end of the range if there's
464 -- no default branch defined.
465 lowest_branch = fst (head branches)
466 highest_branch = fst (last branches)
469 | isNothing mb_deflt = lowest_branch
473 | isNothing mb_deflt = highest_branch
476 n_tags = real_hi_tag - real_lo_tag + 1
478 -- INVARIANT: Provided hi_tag > lo_tag (which is true)
479 -- lo_tag <= mid_tag < hi_tag
480 -- lo_branches have tags < mid_tag
481 -- hi_branches have tags >= mid_tag
483 (mid_tag,_) = branches !! (n_branches `div` 2)
484 -- 2 branches => n_branches `div` 2 = 1
485 -- => branches !! 1 give the *second* tag
486 -- There are always at least 2 branches here
488 (lo_branches, hi_branches) = span is_lo branches
489 is_lo (t,_) = t < mid_tag
493 | isTrivialCmmExpr e = return (CmmNop, e)
494 | otherwise = do { reg <- newTemp (cmmExprRep e)
495 ; return (CmmAssign reg e, CmmReg reg) }
498 emitLitSwitch :: CmmExpr -- Tag to switch on
499 -> [(Literal, CgStmts)] -- Tagged branches
500 -> CgStmts -- Default branch (always)
501 -> Code -- Emit the code
502 -- Used for general literals, whose size might not be a word,
503 -- where there is always a default case, and where we don't know
504 -- the range of values for certain. For simplicity we always generate a tree.
506 -- ToDo: for integers we could do better here, perhaps by generalising
507 -- mk_switch and using that. --SDM 15/09/2004
508 emitLitSwitch scrut [] deflt
510 emitLitSwitch scrut branches deflt_blk
511 = do { scrut' <- assignTemp scrut
512 ; deflt_blk_id <- forkCgStmts deflt_blk
513 ; blk <- mk_lit_switch scrut' deflt_blk_id (sortLe le branches)
516 le (t1,_) (t2,_) = t1 <= t2
518 mk_lit_switch :: CmmExpr -> BlockId
519 -> [(Literal,CgStmts)]
521 mk_lit_switch scrut deflt_blk_id [(lit,blk)]
522 = return (consCgStmt if_stmt blk)
524 cmm_lit = mkSimpleLit lit
525 rep = cmmLitRep cmm_lit
526 cond = CmmMachOp (MO_Ne rep) [scrut, CmmLit cmm_lit]
527 if_stmt = CmmCondBranch cond deflt_blk_id
529 mk_lit_switch scrut deflt_blk_id branches
530 = do { hi_blk <- mk_lit_switch scrut deflt_blk_id hi_branches
531 ; lo_blk <- mk_lit_switch scrut deflt_blk_id lo_branches
532 ; lo_blk_id <- forkCgStmts lo_blk
533 ; let if_stmt = CmmCondBranch cond lo_blk_id
534 ; return (if_stmt `consCgStmt` hi_blk) }
536 n_branches = length branches
537 (mid_lit,_) = branches !! (n_branches `div` 2)
538 -- See notes above re mid_tag
540 (lo_branches, hi_branches) = span is_lo branches
541 is_lo (t,_) = t < mid_lit
543 cond = CmmMachOp (mkLtOp mid_lit)
544 [scrut, CmmLit (mkSimpleLit mid_lit)]
546 -------------------------------------------------------------------------
548 -- Simultaneous assignment
550 -------------------------------------------------------------------------
553 emitSimultaneously :: CmmStmts -> Code
554 -- Emit code to perform the assignments in the
555 -- input simultaneously, using temporary variables when necessary.
557 -- The Stmts must be:
558 -- CmmNop, CmmComment, CmmAssign, CmmStore
562 -- We use the strongly-connected component algorithm, in which
563 -- * the vertices are the statements
564 -- * an edge goes from s1 to s2 iff
565 -- s1 assigns to something s2 uses
566 -- that is, if s1 should *follow* s2 in the final order
568 type CVertex = (Int, CmmStmt) -- Give each vertex a unique number,
569 -- for fast comparison
571 emitSimultaneously stmts
573 case filterOut isNopStmt (stmtList stmts) of
576 [stmt] -> stmtC stmt -- It's often just one stmt
577 stmt_list -> doSimultaneously1 (zip [(1::Int)..] stmt_list)
579 doSimultaneously1 :: [CVertex] -> Code
580 doSimultaneously1 vertices
582 edges = [ (vertex, key1, edges_from stmt1)
583 | vertex@(key1, stmt1) <- vertices
585 edges_from stmt1 = [ key2 | (key2, stmt2) <- vertices,
586 stmt1 `mustFollow` stmt2
588 components = stronglyConnComp edges
590 -- do_components deal with one strongly-connected component
591 -- Not cyclic, or singleton? Just do it
592 do_component (AcyclicSCC (n,stmt)) = stmtC stmt
593 do_component (CyclicSCC [(n,stmt)]) = stmtC stmt
595 -- Cyclic? Then go via temporaries. Pick one to
596 -- break the loop and try again with the rest.
597 do_component (CyclicSCC ((n,first_stmt) : rest))
598 = do { from_temp <- go_via_temp first_stmt
599 ; doSimultaneously1 rest
602 go_via_temp (CmmAssign dest src)
603 = do { tmp <- newTemp (cmmRegRep dest)
604 ; stmtC (CmmAssign tmp src)
605 ; return (CmmAssign dest (CmmReg tmp)) }
606 go_via_temp (CmmStore dest src)
607 = do { tmp <- newTemp (cmmExprRep src)
608 ; stmtC (CmmAssign tmp src)
609 ; return (CmmStore dest (CmmReg tmp)) }
611 mapCs do_component components
613 mustFollow :: CmmStmt -> CmmStmt -> Bool
614 CmmAssign reg _ `mustFollow` stmt = anySrc (reg `regUsedIn`) stmt
615 CmmStore loc e `mustFollow` stmt = anySrc (locUsedIn loc (cmmExprRep e)) stmt
616 CmmNop `mustFollow` stmt = False
617 CmmComment _ `mustFollow` stmt = False
620 anySrc :: (CmmExpr -> Bool) -> CmmStmt -> Bool
621 -- True if the fn is true of any input of the stmt
622 anySrc p (CmmAssign _ e) = p e
623 anySrc p (CmmStore e1 e2) = p e1 || p e2 -- Might be used in either side
624 anySrc p (CmmComment _) = False
625 anySrc p CmmNop = False
626 anySrc p other = True -- Conservative
628 regUsedIn :: CmmReg -> CmmExpr -> Bool
629 reg `regUsedIn` CmmLit _ = False
630 reg `regUsedIn` CmmLoad e _ = reg `regUsedIn` e
631 reg `regUsedIn` CmmReg reg' = reg == reg'
632 reg `regUsedIn` CmmRegOff reg' _ = reg == reg'
633 reg `regUsedIn` CmmMachOp _ es = any (reg `regUsedIn`) es
635 locUsedIn :: CmmExpr -> MachRep -> CmmExpr -> Bool
636 -- (locUsedIn a r e) checks whether writing to r[a] could affect the value of
637 -- 'e'. Returns True if it's not sure.
638 locUsedIn loc rep (CmmLit _) = False
639 locUsedIn loc rep (CmmLoad e ld_rep) = possiblySameLoc loc rep e ld_rep
640 locUsedIn loc rep (CmmReg reg') = False
641 locUsedIn loc rep (CmmRegOff reg' _) = False
642 locUsedIn loc rep (CmmMachOp _ es) = any (locUsedIn loc rep) es
644 possiblySameLoc :: CmmExpr -> MachRep -> CmmExpr -> MachRep -> Bool
645 -- Assumes that distinct registers (eg Hp, Sp) do not
646 -- point to the same location, nor any offset thereof.
647 possiblySameLoc (CmmReg r1) rep1 (CmmReg r2) rep2 = r1==r2
648 possiblySameLoc (CmmReg r1) rep1 (CmmRegOff r2 0) rep2 = r1==r2
649 possiblySameLoc (CmmRegOff r1 0) rep1 (CmmReg r2) rep2 = r1==r2
650 possiblySameLoc (CmmRegOff r1 start1) rep1 (CmmRegOff r2 start2) rep2
651 = r1==r2 && end1 > start2 && end2 > start1
653 end1 = start1 + machRepByteWidth rep1
654 end2 = start2 + machRepByteWidth rep2
656 possiblySameLoc l1 rep1 (CmmLit _) rep2 = False
657 possiblySameLoc l1 rep1 l2 rep2 = True -- Conservative