# Buggy code
def remove_dead_parfor(parfor, lives, args):
    # remove dead get/sets in last block
    # FIXME: I think that "in the last block" is not sufficient in general.  We might need to
    # remove from any block.
    last_label = max(parfor.loop_body.keys())
    last_block = parfor.loop_body[last_label]

    # save array values set to replace getitems
    saved_values = {}
    new_body = []
    for stmt in last_block.body:
        if (
            isinstance(stmt, ir.SetItem)
            and stmt.index.name == parfor.index_var.name
            and stmt.target.name not in lives
        ):
            saved_values[stmt.target.name] = stmt.value
        if isinstance(stmt, ir.Assign) and isinstance(stmt.value, ir.Expr):
            rhs = stmt.value
            if rhs.op == "getitem" and rhs.index.name == parfor.index_var.name:
                # replace getitem if value saved
                stmt.value = saved_values.get(rhs.value.name, rhs)
        new_body.append(stmt)
    last_block.body = new_body
    # process parfor body recursively
    remove_dead_parfor_recursive(parfor, lives, args)
    return


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-3b23f7331ceb> in <module>()
----> 1 @jit(arg_types=[numba.double], ret_type=numba.double)
2 def is_REALLY_five(some_value):
3     for i in range(5):
4         if some_value == 5.0:
5             return 1.0

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/decorators.pyc in _jit(func)
77                   "garbage collected!" % (func,))
78         t = Translate(func, *args, **kws)
---> 79         t.translate()
80         __tr_map__[func] = t
81         return t.get_ctypes_func(llvm)

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/translate.pyc in translate(self)
926                     # Ensure we are playing with locals that might
927                     # actually precede the next block.
--> 928                     self.check_locals(i)
929
930                 self.crnt_block = i

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/numba/translate.pyc in check_locals(self, i)
1135                 else:
1136                     assert next_locals is not None, "Internal compiler error!"
-> 1137             self._locals = next_locals[:]
1138
1139     def get_ctypes_func(self, llvm=True):

TypeError: 'NoneType' object has no attribute '__getitem__'

# Fixed code
def remove_dead_parfor(parfor, lives, arg_aliases, alias_map, typemap):
    # remove dead get/sets in last block
    # FIXME: I think that "in the last block" is not sufficient in general.  We might need to
    # remove from any block.
    last_label = max(parfor.loop_body.keys())
    last_block = parfor.loop_body[last_label]

    # save array values set to replace getitems
    saved_values = {}
    new_body = []
    for stmt in last_block.body:
        if (
            isinstance(stmt, ir.SetItem)
            and stmt.index.name == parfor.index_var.name
            and stmt.target.name not in lives
        ):
            saved_values[stmt.target.name] = stmt.value
        if isinstance(stmt, ir.Assign) and isinstance(stmt.value, ir.Expr):
            rhs = stmt.value
            if rhs.op == "getitem" and rhs.index.name == parfor.index_var.name:
                # replace getitem if value saved
                stmt.value = saved_values.get(rhs.value.name, rhs)
        new_body.append(stmt)
    last_block.body = new_body

    alias_set = set(alias_map.keys())
    # after getitem replacement, remove extra setitems
    new_body = []
    in_lives = copy.copy(lives)
    for stmt in reversed(last_block.body):
        # aliases of lives are also live for setitems
        alias_lives = in_lives & alias_set
        for v in alias_lives:
            in_lives |= alias_map[v]
        if (
            isinstance(stmt, ir.SetItem)
            and stmt.index.name == parfor.index_var.name
            and stmt.target.name not in in_lives
        ):
            continue
        in_lives |= {v.name for v in stmt.list_vars()}
        new_body.append(stmt)
    new_body.reverse()
    last_block.body = new_body

    # process parfor body recursively
    remove_dead_parfor_recursive(parfor, lives, arg_aliases, alias_map, typemap)
    return