+
+ blocksChecked
+ = map checkBlockEnd
+ $ BasicBlock id top : other_blocks
+
+ return (blocksChecked, statics)
+
+
+-- | Enforce the invariant that all basic blocks must end with a jump.
+-- For SPARC this is a jump, then a nop for the branch delay slot.
+--
+-- If the branch isn't there then the register liveness determinator
+-- will get the liveness information wrong. This will cause a bad
+-- allocation, which is seriously difficult to debug.
+--
+-- If there is an instr in the branch delay slot, then the allocator
+-- will also get confused and give a bad allocation.
+--
+checkBlockEnd
+ :: NatBasicBlock Instr -> NatBasicBlock Instr
+
+checkBlockEnd block@(BasicBlock _ instrs)
+ | Just (i1, i2) <- takeLast2 instrs
+ , isJumpishInstr i1
+ , NOP <- i2
+ = block
+
+ | otherwise
+ = pprPanic
+ ("SPARC.CodeGen: bad instrs at end of block\n")
+ (text "block:\n" <> ppr block)
+
+takeLast2 :: [a] -> Maybe (a, a)
+takeLast2 xx
+ = case xx of
+ [] -> Nothing
+ _:[] -> Nothing
+ x1:x2:[] -> Just (x1, x2)
+ _:xs -> takeLast2 xs