1
# Copyright (C) 2011 Apple Inc. All rights reserved.
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
6
# 1. Redistributions of source code must retain the above copyright
7
# notice, this list of conditions and the following disclaimer.
8
# 2. Redistributions in binary form must reproduce the above copyright
9
# notice, this list of conditions and the following disclaimer in the
10
# documentation and/or other materials provided with the distribution.
12
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
13
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
14
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
15
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
16
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
22
# THE POSSIBILITY OF SUCH DAMAGE.
28
# "Optimization" passes. These are used to lower the representation for
29
# backends that cannot handle some of our higher-level instructions.
33
# A temporary - a variable that will be allocated to a register after we're
38
def replaceTemporariesWithRegisters(kind)
41
node.replaceTemporariesWithRegisters(kind)
46
class Tmp < NoChildren
47
attr_reader :firstMention, :lastMention
49
attr_accessor :register
51
def initialize(codeOrigin, kind)
60
def mention!(position)
61
if not @firstMention or position < @firstMention
62
@firstMention = position
64
if not @lastMention or position > @lastMention
65
@lastMention = position
69
def replaceTemporariesWithRegisters(kind)
71
raise "Did not allocate register to temporary at #{codeOriginString}" unless @register
95
# Assign registers to temporaries, by finding which temporaries interfere
96
# with each other. Note that this relies on temporary live ranges not crossing
97
# basic block boundaries.
99
def assignRegistersToTemporaries(list, kind, registers)
100
list.each_with_index {
102
node.filter(Tmp).uniq.each {
110
freeRegisters = registers.dup
111
list.each_with_index {
113
tmpList = node.filter(Tmp).uniq
116
if tmp.kind == kind and tmp.firstMention == index
117
raise "Could not allocate register to temporary at #{node.codeOriginString}" if freeRegisters.empty?
118
tmp.register = freeRegisters.pop
123
if tmp.kind == kind and tmp.lastMention == index
124
freeRegisters.push tmp.register
125
raise "Register allocation inconsistency at #{node.codeOriginString}" if freeRegisters.size > registers.size
132
node.replaceTemporariesWithRegisters(kind)