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 -------------------------------------------------------------------------
65 -- Random small functions
67 -------------------------------------------------------------------------
69 addIdReps :: [Id] -> [(CgRep, Id)]
70 addIdReps ids = [(idCgRep id, id) | id <- ids]
72 -------------------------------------------------------------------------
76 -------------------------------------------------------------------------
78 cgLit :: Literal -> FCode CmmLit
79 cgLit (MachStr s) = mkStringCLit (unpackFS s)
80 cgLit other_lit = return (mkSimpleLit other_lit)
82 mkSimpleLit :: Literal -> CmmLit
83 mkSimpleLit (MachChar c) = CmmInt (fromIntegral (ord c)) wordRep
84 mkSimpleLit MachNullAddr = zeroCLit
85 mkSimpleLit (MachInt i) = CmmInt i wordRep
86 mkSimpleLit (MachInt64 i) = CmmInt i I64
87 mkSimpleLit (MachWord i) = CmmInt i wordRep
88 mkSimpleLit (MachWord64 i) = CmmInt i I64
89 mkSimpleLit (MachFloat r) = CmmFloat r F32
90 mkSimpleLit (MachDouble r) = CmmFloat r F64
91 mkSimpleLit (MachLabel fs ms) = CmmLabel (mkForeignLabel fs ms is_dyn)
93 is_dyn = False -- ToDo: fix me
95 mkLtOp :: Literal -> MachOp
96 -- On signed literals we must do a signed comparison
97 mkLtOp (MachInt _) = MO_S_Lt wordRep
98 mkLtOp (MachFloat _) = MO_S_Lt F32
99 mkLtOp (MachDouble _) = MO_S_Lt F64
100 mkLtOp lit = MO_U_Lt (cmmLitRep (mkSimpleLit lit))
103 ---------------------------------------------------
105 -- Cmm data type functions
107 ---------------------------------------------------
109 -----------------------
110 -- The "B" variants take byte offsets
111 cmmRegOffB :: CmmReg -> ByteOff -> CmmExpr
112 cmmRegOffB = cmmRegOff
114 cmmOffsetB :: CmmExpr -> ByteOff -> CmmExpr
115 cmmOffsetB = cmmOffset
117 cmmOffsetExprB :: CmmExpr -> CmmExpr -> CmmExpr
118 cmmOffsetExprB = cmmOffsetExpr
120 cmmLabelOffB :: CLabel -> ByteOff -> CmmLit
121 cmmLabelOffB = cmmLabelOff
123 cmmOffsetLitB :: CmmLit -> ByteOff -> CmmLit
124 cmmOffsetLitB = cmmOffsetLit
126 -----------------------
127 -- The "W" variants take word offsets
128 cmmOffsetExprW :: CmmExpr -> CmmExpr -> CmmExpr
129 -- The second arg is a *word* offset; need to change it to bytes
130 cmmOffsetExprW e (CmmLit (CmmInt n _)) = cmmOffsetW e (fromInteger n)
131 cmmOffsetExprW e wd_off = cmmIndexExpr wordRep e wd_off
133 cmmOffsetW :: CmmExpr -> WordOff -> CmmExpr
134 cmmOffsetW e n = cmmOffsetB e (wORD_SIZE * n)
136 cmmRegOffW :: CmmReg -> WordOff -> CmmExpr
137 cmmRegOffW reg wd_off = cmmRegOffB reg (wd_off * wORD_SIZE)
139 cmmOffsetLitW :: CmmLit -> WordOff -> CmmLit
140 cmmOffsetLitW lit wd_off = cmmOffsetLitB lit (wORD_SIZE * wd_off)
142 cmmLabelOffW :: CLabel -> WordOff -> CmmLit
143 cmmLabelOffW lbl wd_off = cmmLabelOffB lbl (wORD_SIZE * wd_off)
145 cmmLoadIndexW :: CmmExpr -> Int -> CmmExpr
146 cmmLoadIndexW base off
147 = CmmLoad (cmmOffsetW base off) wordRep
149 -----------------------
150 cmmNeWord, cmmEqWord, cmmOrWord, cmmAndWord :: CmmExpr -> CmmExpr -> CmmExpr
151 cmmOrWord e1 e2 = CmmMachOp mo_wordOr [e1, e2]
152 cmmAndWord e1 e2 = CmmMachOp mo_wordAnd [e1, e2]
153 cmmNeWord e1 e2 = CmmMachOp mo_wordNe [e1, e2]
154 cmmEqWord e1 e2 = CmmMachOp mo_wordEq [e1, e2]
155 cmmULtWord e1 e2 = CmmMachOp mo_wordULt [e1, e2]
156 cmmUGtWord e1 e2 = CmmMachOp mo_wordUGt [e1, e2]
158 cmmNegate :: CmmExpr -> CmmExpr
159 cmmNegate (CmmLit (CmmInt n rep)) = CmmLit (CmmInt (-n) rep)
160 cmmNegate e = CmmMachOp (MO_S_Neg (cmmExprRep e)) [e]
162 blankWord :: CmmStatic
163 blankWord = CmmUninitialised wORD_SIZE
165 -----------------------
168 mkWordCLit :: StgWord -> CmmLit
169 mkWordCLit wd = CmmInt (fromIntegral wd) wordRep
171 packHalfWordsCLit :: (Integral a, Integral b) => a -> b -> CmmLit
172 -- Make a single word literal in which the lower_half_word is
173 -- at the lower address, and the upper_half_word is at the
175 -- ToDo: consider using half-word lits instead
176 -- but be careful: that's vulnerable when reversed
177 packHalfWordsCLit lower_half_word upper_half_word
178 #ifdef WORDS_BIGENDIAN
179 = mkWordCLit ((fromIntegral lower_half_word `shiftL` hALF_WORD_SIZE_IN_BITS)
180 .|. fromIntegral upper_half_word)
182 = mkWordCLit ((fromIntegral lower_half_word)
183 .|. (fromIntegral upper_half_word `shiftL` hALF_WORD_SIZE_IN_BITS))
186 --------------------------------------------------------------------------
188 -- Incrementing a memory location
190 --------------------------------------------------------------------------
192 addToMem :: MachRep -- rep of the counter
193 -> CmmExpr -- Address
194 -> Int -- What to add (a word)
196 addToMem rep ptr n = addToMemE rep ptr (CmmLit (CmmInt (toInteger n) rep))
198 addToMemE :: MachRep -- rep of the counter
199 -> CmmExpr -- Address
200 -> CmmExpr -- What to add (a word-typed expression)
203 = CmmStore ptr (CmmMachOp (MO_Add rep) [CmmLoad ptr rep, n])
205 -------------------------------------------------------------------------
207 -- Converting a closure tag to a closure for enumeration types
208 -- (this is the implementation of tagToEnum#).
210 -------------------------------------------------------------------------
212 tagToClosure :: DynFlags -> TyCon -> CmmExpr -> CmmExpr
213 tagToClosure dflags tycon tag
214 = CmmLoad (cmmOffsetExprW closure_tbl tag) wordRep
215 where closure_tbl = CmmLit (CmmLabel lbl)
216 lbl = mkClosureTableLabel dflags (tyConName tycon)
218 -------------------------------------------------------------------------
220 -- Conditionals and rts calls
222 -------------------------------------------------------------------------
224 emitIf :: CmmExpr -- Boolean
227 -- Emit (if e then x)
228 -- ToDo: reverse the condition to avoid the extra branch instruction if possible
229 -- (some conditionals aren't reversible. eg. floating point comparisons cannot
230 -- be inverted because there exist some values for which both comparisons
231 -- return False, such as NaN.)
232 emitIf cond then_part
233 = do { then_id <- newLabelC
234 ; join_id <- newLabelC
235 ; stmtC (CmmCondBranch cond then_id)
236 ; stmtC (CmmBranch join_id)
242 emitIfThenElse :: CmmExpr -- Boolean
246 -- Emit (if e then x else y)
247 emitIfThenElse cond then_part else_part
248 = do { then_id <- newLabelC
249 ; else_id <- newLabelC
250 ; join_id <- newLabelC
251 ; stmtC (CmmCondBranch cond then_id)
253 ; stmtC (CmmBranch join_id)
259 emitRtsCall :: LitString -> [(CmmExpr,MachHint)] -> Code
260 emitRtsCall fun args = emitRtsCall' [] fun args Nothing
261 -- The 'Nothing' says "save all global registers"
263 emitRtsCallWithVols :: LitString -> [(CmmExpr,MachHint)] -> [GlobalReg] -> Code
264 emitRtsCallWithVols fun args vols
265 = emitRtsCall' [] fun args (Just vols)
267 emitRtsCallWithResult :: CmmReg -> MachHint -> LitString
268 -> [(CmmExpr,MachHint)] -> Code
269 emitRtsCallWithResult res hint fun args
270 = emitRtsCall' [(res,hint)] fun args Nothing
272 -- Make a call to an RTS C procedure
274 :: [(CmmReg,MachHint)]
276 -> [(CmmExpr,MachHint)]
279 emitRtsCall' res fun args vols = stmtC (CmmCall target res args vols)
281 target = CmmForeignCall fun_expr CCallConv
282 fun_expr = mkLblExpr (mkRtsCodeLabel fun)
285 -------------------------------------------------------------------------
287 -- Strings gnerate a top-level data block
289 -------------------------------------------------------------------------
291 emitDataLits :: CLabel -> [CmmLit] -> Code
292 -- Emit a data-segment data block
293 emitDataLits lbl lits
294 = emitData Data (CmmDataLabel lbl : map CmmStaticLit lits)
296 emitRODataLits :: CLabel -> [CmmLit] -> Code
297 -- Emit a read-only data block
298 emitRODataLits lbl lits
299 = emitData section (CmmDataLabel lbl : map CmmStaticLit lits)
300 where section | any needsRelocation lits = RelocatableReadOnlyData
301 | otherwise = ReadOnlyData
302 needsRelocation (CmmLabel _) = True
303 needsRelocation (CmmLabelOff _ _) = True
304 needsRelocation _ = False
306 mkStringCLit :: String -> FCode CmmLit
307 -- Make a global definition for the string,
308 -- and return its label
310 = do { uniq <- newUnique
311 ; let lbl = mkStringLitLabel uniq
312 ; emitData ReadOnlyData [CmmDataLabel lbl, CmmString str]
313 ; return (CmmLabel lbl) }
315 -------------------------------------------------------------------------
317 -- Assigning expressions to temporaries
319 -------------------------------------------------------------------------
321 assignTemp :: CmmExpr -> FCode CmmExpr
322 -- For a non-trivial expression, e, create a local
323 -- variable and assign the expression to it
325 | isTrivialCmmExpr e = return e
326 | otherwise = do { reg <- newTemp (cmmExprRep e)
327 ; stmtC (CmmAssign reg e)
328 ; return (CmmReg reg) }
331 newTemp :: MachRep -> FCode CmmReg
332 newTemp rep = do { uniq <- newUnique; return (CmmLocal (LocalReg uniq rep)) }
335 -------------------------------------------------------------------------
337 -- Building case analysis
339 -------------------------------------------------------------------------
342 :: CmmExpr -- Tag to switch on
343 -> [(ConTagZ, CgStmts)] -- Tagged branches
344 -> Maybe CgStmts -- Default branch (if any)
345 -> ConTagZ -> ConTagZ -- Min and Max possible values; behaviour
346 -- outside this range is undefined
349 -- ONLY A DEFAULT BRANCH: no case analysis to do
350 emitSwitch tag_expr [] (Just stmts) _ _
354 emitSwitch tag_expr branches mb_deflt lo_tag hi_tag
355 = -- Just sort the branches before calling mk_sritch
358 Nothing -> return Nothing
359 Just stmts -> do id <- forkCgStmts stmts; return (Just id)
361 ; stmts <- mk_switch tag_expr (sortLe le branches)
362 mb_deflt_id lo_tag hi_tag
366 (t1,_) `le` (t2,_) = t1 <= t2
369 mk_switch :: CmmExpr -> [(ConTagZ, CgStmts)]
370 -> Maybe BlockId -> ConTagZ -> ConTagZ
373 -- SINGLETON TAG RANGE: no case analysis to do
374 mk_switch tag_expr [(tag,stmts)] _ lo_tag hi_tag
376 = ASSERT( tag == lo_tag )
379 -- SINGLETON BRANCH, NO DEFUALT: no case analysis to do
380 mk_switch tag_expr [(tag,stmts)] Nothing lo_tag hi_tag
382 -- The simplifier might have eliminated a case
383 -- so we may have e.g. case xs of
385 -- In that situation we can be sure the (:) case
386 -- can't happen, so no need to test
388 -- SINGLETON BRANCH: one equality check to do
389 mk_switch tag_expr [(tag,stmts)] (Just deflt) lo_tag hi_tag
390 = return (CmmCondBranch cond deflt `consCgStmt` stmts)
392 cond = cmmNeWord tag_expr (CmmLit (mkIntCLit tag))
393 -- We have lo_tag < hi_tag, but there's only one branch,
394 -- so there must be a default
396 -- ToDo: we might want to check for the two branch case, where one of
397 -- the branches is the tag 0, because comparing '== 0' is likely to be
398 -- more efficient than other kinds of comparison.
400 -- DENSE TAG RANGE: use a switch statment
401 mk_switch tag_expr branches mb_deflt lo_tag hi_tag
402 | use_switch -- Use a switch
403 = do { branch_ids <- mapM forkCgStmts (map snd branches)
405 tagged_blk_ids = zip (map fst branches) (map Just branch_ids)
407 find_branch :: ConTagZ -> Maybe BlockId
408 find_branch i = assocDefault mb_deflt tagged_blk_ids i
410 -- NB. we have eliminated impossible branches at
411 -- either end of the range (see below), so the first
412 -- tag of a real branch is real_lo_tag (not lo_tag).
413 arms = [ find_branch i | i <- [real_lo_tag..real_hi_tag]]
415 switch_stmt = CmmSwitch (cmmOffset tag_expr (- real_lo_tag)) arms
417 ; ASSERT(not (all isNothing arms))
418 return (oneCgStmt switch_stmt)
421 -- if we can knock off a bunch of default cases with one if, then do so
422 | Just deflt <- mb_deflt, (lowest_branch - lo_tag) >= n_branches
423 = do { (assign_tag, tag_expr') <- assignTemp' tag_expr
424 ; let cond = cmmULtWord tag_expr' (CmmLit (mkIntCLit lowest_branch))
425 branch = CmmCondBranch cond deflt
426 ; stmts <- mk_switch tag_expr' branches mb_deflt lowest_branch hi_tag
427 ; return (assign_tag `consCgStmt` (branch `consCgStmt` stmts))
430 | Just deflt <- mb_deflt, (hi_tag - highest_branch) >= n_branches
431 = do { (assign_tag, tag_expr') <- assignTemp' tag_expr
432 ; let cond = cmmUGtWord tag_expr' (CmmLit (mkIntCLit highest_branch))
433 branch = CmmCondBranch cond deflt
434 ; stmts <- mk_switch tag_expr' branches mb_deflt lo_tag highest_branch
435 ; return (assign_tag `consCgStmt` (branch `consCgStmt` stmts))
438 | otherwise -- Use an if-tree
439 = do { (assign_tag, tag_expr') <- assignTemp' tag_expr
440 -- To avoid duplication
441 ; lo_stmts <- mk_switch tag_expr' lo_branches mb_deflt lo_tag (mid_tag-1)
442 ; hi_stmts <- mk_switch tag_expr' hi_branches mb_deflt mid_tag hi_tag
443 ; lo_id <- forkCgStmts lo_stmts
444 ; let cond = cmmULtWord tag_expr' (CmmLit (mkIntCLit mid_tag))
445 branch_stmt = CmmCondBranch cond lo_id
446 ; return (assign_tag `consCgStmt` (branch_stmt `consCgStmt` hi_stmts))
449 use_switch = ASSERT( n_branches > 1 && n_tags > 1 )
450 {- pprTrace "mk_switch" (ppr tag_expr <+> text "n_tags: "
451 <+> int n_tags <+> text "dense: "
452 <+> int n_branches) $ -}
453 n_tags > 2 && (small || dense)
454 -- a 2-branch switch always turns into an if.
456 dense = n_branches > (n_tags `div` 2)
457 exhaustive = n_tags == n_branches
458 n_branches = length branches
460 -- ignore default slots at each end of the range if there's
461 -- no default branch defined.
462 lowest_branch = fst (head branches)
463 highest_branch = fst (last branches)
466 | isNothing mb_deflt = lowest_branch
470 | isNothing mb_deflt = highest_branch
473 n_tags = real_hi_tag - real_lo_tag + 1
475 -- INVARIANT: Provided hi_tag > lo_tag (which is true)
476 -- lo_tag <= mid_tag < hi_tag
477 -- lo_branches have tags < mid_tag
478 -- hi_branches have tags >= mid_tag
480 (mid_tag,_) = branches !! (n_branches `div` 2)
481 -- 2 branches => n_branches `div` 2 = 1
482 -- => branches !! 1 give the *second* tag
483 -- There are always at least 2 branches here
485 (lo_branches, hi_branches) = span is_lo branches
486 is_lo (t,_) = t < mid_tag
490 | isTrivialCmmExpr e = return (CmmNop, e)
491 | otherwise = do { reg <- newTemp (cmmExprRep e)
492 ; return (CmmAssign reg e, CmmReg reg) }
495 emitLitSwitch :: CmmExpr -- Tag to switch on
496 -> [(Literal, CgStmts)] -- Tagged branches
497 -> CgStmts -- Default branch (always)
498 -> Code -- Emit the code
499 -- Used for general literals, whose size might not be a word,
500 -- where there is always a default case, and where we don't know
501 -- the range of values for certain. For simplicity we always generate a tree.
503 -- ToDo: for integers we could do better here, perhaps by generalising
504 -- mk_switch and using that. --SDM 15/09/2004
505 emitLitSwitch scrut [] deflt
507 emitLitSwitch scrut branches deflt_blk
508 = do { scrut' <- assignTemp scrut
509 ; deflt_blk_id <- forkCgStmts deflt_blk
510 ; blk <- mk_lit_switch scrut' deflt_blk_id (sortLe le branches)
513 le (t1,_) (t2,_) = t1 <= t2
515 mk_lit_switch :: CmmExpr -> BlockId
516 -> [(Literal,CgStmts)]
518 mk_lit_switch scrut deflt_blk_id [(lit,blk)]
519 = return (consCgStmt if_stmt blk)
521 cmm_lit = mkSimpleLit lit
522 rep = cmmLitRep cmm_lit
523 cond = CmmMachOp (MO_Ne rep) [scrut, CmmLit cmm_lit]
524 if_stmt = CmmCondBranch cond deflt_blk_id
526 mk_lit_switch scrut deflt_blk_id branches
527 = do { hi_blk <- mk_lit_switch scrut deflt_blk_id hi_branches
528 ; lo_blk <- mk_lit_switch scrut deflt_blk_id lo_branches
529 ; lo_blk_id <- forkCgStmts lo_blk
530 ; let if_stmt = CmmCondBranch cond lo_blk_id
531 ; return (if_stmt `consCgStmt` hi_blk) }
533 n_branches = length branches
534 (mid_lit,_) = branches !! (n_branches `div` 2)
535 -- See notes above re mid_tag
537 (lo_branches, hi_branches) = span is_lo branches
538 is_lo (t,_) = t < mid_lit
540 cond = CmmMachOp (mkLtOp mid_lit)
541 [scrut, CmmLit (mkSimpleLit mid_lit)]
543 -------------------------------------------------------------------------
545 -- Simultaneous assignment
547 -------------------------------------------------------------------------
550 emitSimultaneously :: CmmStmts -> Code
551 -- Emit code to perform the assignments in the
552 -- input simultaneously, using temporary variables when necessary.
554 -- The Stmts must be:
555 -- CmmNop, CmmComment, CmmAssign, CmmStore
559 -- We use the strongly-connected component algorithm, in which
560 -- * the vertices are the statements
561 -- * an edge goes from s1 to s2 iff
562 -- s1 assigns to something s2 uses
563 -- that is, if s1 should *follow* s2 in the final order
565 type CVertex = (Int, CmmStmt) -- Give each vertex a unique number,
566 -- for fast comparison
568 emitSimultaneously stmts
570 case filterOut isNopStmt (stmtList stmts) of
573 [stmt] -> stmtC stmt -- It's often just one stmt
574 stmt_list -> doSimultaneously1 (zip [(1::Int)..] stmt_list)
576 doSimultaneously1 :: [CVertex] -> Code
577 doSimultaneously1 vertices
579 edges = [ (vertex, key1, edges_from stmt1)
580 | vertex@(key1, stmt1) <- vertices
582 edges_from stmt1 = [ key2 | (key2, stmt2) <- vertices,
583 stmt1 `mustFollow` stmt2
585 components = stronglyConnComp edges
587 -- do_components deal with one strongly-connected component
588 -- Not cyclic, or singleton? Just do it
589 do_component (AcyclicSCC (n,stmt)) = stmtC stmt
590 do_component (CyclicSCC [(n,stmt)]) = stmtC stmt
592 -- Cyclic? Then go via temporaries. Pick one to
593 -- break the loop and try again with the rest.
594 do_component (CyclicSCC ((n,first_stmt) : rest))
595 = do { from_temp <- go_via_temp first_stmt
596 ; doSimultaneously1 rest
599 go_via_temp (CmmAssign dest src)
600 = do { tmp <- newTemp (cmmRegRep dest)
601 ; stmtC (CmmAssign tmp src)
602 ; return (CmmAssign dest (CmmReg tmp)) }
603 go_via_temp (CmmStore dest src)
604 = do { tmp <- newTemp (cmmExprRep src)
605 ; stmtC (CmmAssign tmp src)
606 ; return (CmmStore dest (CmmReg tmp)) }
608 mapCs do_component components
610 mustFollow :: CmmStmt -> CmmStmt -> Bool
611 CmmAssign reg _ `mustFollow` stmt = anySrc (reg `regUsedIn`) stmt
612 CmmStore loc e `mustFollow` stmt = anySrc (locUsedIn loc (cmmExprRep e)) stmt
613 CmmNop `mustFollow` stmt = False
614 CmmComment _ `mustFollow` stmt = False
617 anySrc :: (CmmExpr -> Bool) -> CmmStmt -> Bool
618 -- True if the fn is true of any input of the stmt
619 anySrc p (CmmAssign _ e) = p e
620 anySrc p (CmmStore e1 e2) = p e1 || p e2 -- Might be used in either side
621 anySrc p (CmmComment _) = False
622 anySrc p CmmNop = False
623 anySrc p other = True -- Conservative
625 regUsedIn :: CmmReg -> CmmExpr -> Bool
626 reg `regUsedIn` CmmLit _ = False
627 reg `regUsedIn` CmmLoad e _ = reg `regUsedIn` e
628 reg `regUsedIn` CmmReg reg' = reg == reg'
629 reg `regUsedIn` CmmRegOff reg' _ = reg == reg'
630 reg `regUsedIn` CmmMachOp _ es = any (reg `regUsedIn`) es
632 locUsedIn :: CmmExpr -> MachRep -> CmmExpr -> Bool
633 -- (locUsedIn a r e) checks whether writing to r[a] could affect the value of
634 -- 'e'. Returns True if it's not sure.
635 locUsedIn loc rep (CmmLit _) = False
636 locUsedIn loc rep (CmmLoad e ld_rep) = possiblySameLoc loc rep e ld_rep
637 locUsedIn loc rep (CmmReg reg') = False
638 locUsedIn loc rep (CmmRegOff reg' _) = False
639 locUsedIn loc rep (CmmMachOp _ es) = any (locUsedIn loc rep) es
641 possiblySameLoc :: CmmExpr -> MachRep -> CmmExpr -> MachRep -> Bool
642 -- Assumes that distinct registers (eg Hp, Sp) do not
643 -- point to the same location, nor any offset thereof.
644 possiblySameLoc (CmmReg r1) rep1 (CmmReg r2) rep2 = r1==r2
645 possiblySameLoc (CmmReg r1) rep1 (CmmRegOff r2 0) rep2 = r1==r2
646 possiblySameLoc (CmmRegOff r1 0) rep1 (CmmReg r2) rep2 = r1==r2
647 possiblySameLoc (CmmRegOff r1 start1) rep1 (CmmRegOff r2 start2) rep2
648 = r1==r2 && end1 > start2 && end2 > start1
650 end1 = start1 + machRepByteWidth rep1
651 end2 = start2 + machRepByteWidth rep2
653 possiblySameLoc l1 rep1 (CmmLit _) rep2 = False
654 possiblySameLoc l1 rep1 l2 rep2 = True -- Conservative