-
Notifications
You must be signed in to change notification settings - Fork 1
/
regalloc.py
1662 lines (1353 loc) · 63.3 KB
/
regalloc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2012 Madhusudan C.S.
#
# This file regalloc.py is part of PL241-MCS compiler.
#
# PL241-MCS compiler is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PL241-MCS compiler is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PL241-MCS compiler. If not, see <http://www.gnu.org/licenses/>.
"""Algorithms to allocate registers for the program.
This code is architecture independent. The register assignments are mapped to
real machine registers just before binaries generation. So this file can still
be used for any architecture. The number of registers that must be used for
coloring is a constructor parameter to the RegisterAllocator class.
"""
import collections
import logging
import subprocess
import sys
from argparse import ArgumentParser
from datastructures import InterferenceGraph
from datastructures import InterferenceNode
from datastructures import PriorityQueue
from datastructures import Stack
from ir import is_variable
from ir import is_variable_or_label
from ir import Immediate
from ir import Instruction
from ir import IntermediateRepresentation
from ir import Memory
from optimizations import Optimize
from parser import LanguageSyntaxError
from parser import Parser
from ssa import SSA
# Module level logger object
LOGGER = logging.getLogger(__name__)
# Whenever we are within the loop the execution frequency is these many times
# more than for the same instruction outside the loop.
LOOP_EXECUTION_FREQUENCY_MULTIPLIER = 10
class SSABrokenException(Exception):
"""Represents the exception when SSA structure is broken.
"""
def __init__(self, instruction, *args, **kwargs):
"""Constructs the exception with the message.
Args:
instruction: The Instruction object at which the SSA structure is broken.
"""
super(SSABrokenException, self).__init__(*args, **kwargs)
self.msg = 'SSA structure broken variable redefined at instruction\n%s.' %(
instruction)
def __str__(self):
return '%s: %s' % (self.__class__.__name__, self._msg)
class RegisterAllocationFailedException(Exception):
"""Represents the exception when register allocation cannot complete.
"""
def __init__(self, msg, *args, **kwargs):
"""Constructs the exception with the message.
Args:
msg: The exception message (this is optional).
"""
super(RegisterAllocationFailedException, self).__init__(*args, **kwargs)
self.msg = msg if msg else ''
def __str__(self):
return '%s: %s' % (self.__class__.__name__, self._msg)
class Register(object):
"""Represents a register.
"""
name_counter = 0
@classmethod
def reset_name_counter(cls):
"""Resets the name counter for the new register allocation.
"""
cls.name_counter = 0
def __init__(self, name=None, new_allocation=False):
"""Constructs a register required for the program.
Args:
name: The name of the register this represents. This is an integer
although it is called name for easier counting.
new_allocation: If the current name run counter should be reset.
"""
if new_allocation:
self.__class__.reset_name_counter()
if name:
self.name = name
else:
self.name = self.__class__.name_counter
self.__class__.name_counter += 1
# The memory object this register points to.
self.memory = None
# The instruction object where the register is defined, part of the
# def-use chain for the register.
self.def_instruction = None
# The list of instructions where the register is defined, part of the
# def-use chain for the register.
self.use_instructions = []
# Flag to indicate if use_instructions was sorted.
self.sorted = False
# Flag to indicate if the register is spilled to memory or not.
self.spilled = False
# Color assignment after solving the K-coloring problem using SAT solver.
# Note even though they are called colors they are just integral values.
self.color = None
# The cost of spilling the register computed usin Chaitin's allocator type
# cost function using execution frequencies. When a register is created it
# must have a cost of 1, since a new register is created only when its
# first occurence is found.
self.cost = None
def set_def(self, instruction):
"""Sets the instruction where this register is defined.
Essentially sets up a def-use chain for the register.
IMPORTANT: Since the program is in SSA form, if we already have a def for
this register we should raise an exception.
Args:
instruction: The Instruction object where the variable is defined.
"""
if self.def_instruction:
raise SSABrokenException(instruction)
self.def_instruction = instruction
def definition(self):
"""Returns the instruction object where this register was defined.
Returns None if the register is not defined yet.
"""
return self.def_instruction
def set_use(self, *instructions):
"""Sets the instruction where this register is used.
Essentially sets up a def-use chain for the register. A register can be
used multiple times, so it is a set object.
Args:
instruction: The Instruction object where the variable is defined.
"""
self.sorted = False
self.use_instructions.extend(instructions)
def uses(self):
"""Returns the set of instruction objects where this register is used.
Returns an empty set if it is not used anywhere.
"""
if not self.sorted:
self.use_instructions.sort(key=lambda i: i.label)
return self.use_instructions
def assignment(self):
"""Returns the register if the register is not spilled or the memory object.
"""
return self.memory if self.spilled else self
def def_use_chains(self):
"""Returns the def-use chain pair for the register.
"""
for use_instruction in self.use_instructions:
yield self.def_instruction, use_instruction
def assignments_equal(self, register):
"""Checks if the two registers have the same color assignment.
"""
return True if (isinstance(register, self.__class__) and
self.color == register.color) else False
def __eq__(self, register):
"""Checks if the two registers are same.
"""
# A bit too pessimistic at times, just making sure to check if the
# def instructions match. It should not be needed most of the times.
# But once we get everything up and running we can get rid of that
# additional check of def_instruction.
# IMPORTANT: Don't check if they are the same objects using "is", because
# later some one may decide to simulate the register but with a new object
# is created. The given two checks should be sufficient.
return True if (isinstance(register, self.__class__) and
self.name == register.name and
self.def_instruction == register.def_instruction) else False
def __str__(self):
"""Returns the string representation for this register (preceded by r).
"""
return 'r%d' % self.name
class RegisterAllocator(object):
"""Allocates the registers for the given SSA form of the source code.
"""
def __init__(self, ssa, num_registers=8):
"""Initializes the allocator with SSA.
Args:
ssa: the SSA object for the program IR in SSA form.
num_registers: Number of Machine registers available.
"""
self.ssa = ssa
self.num_registers = num_registers
# Reset the register counter for this register allocator.
Register.reset_name_counter()
# Dictionary whose keys are operands (or variables) and the values are
# the registers assigned in the virtual registers space.
self.variable_register_map = {}
# Dictionary whose keys are registers and the values are the corresponding
# nodes in the interference graph.
self.register_nodes = {}
# Priority Queue that stores the registers in the order of the least
# spill costs to highest spill costs.
self.spill_costs = None
# List of function parameters. This list is used for pre-coloring
self.function_parameters = []
# Dictionary containing the live ranges for each register
self.live_ranges = {}
# Interference graph for this allocator.
self.interference_graph = None
# Dictionary of color assigned SSA deconstructed instructions.
# Key is the original label of the instruction and value is the list of
# instructions including the spill/reload in the order.
self.ssa_deconstructed_instructions = collections.defaultdict(list)
def assign_memory(self, operand, register):
"""Assign the memory object to the register based on the symbol table.
Args:
operand: The operand that this register belongs to.
register: The register which should have a corresponding memory location.
"""
# We will not allocate memory for non-variable registers, i.e. the result
# of the instructions right now. This is because most of them may not
# need a memory. We will allocate memory to them as they are needed, in
# case of spills.
if is_variable(operand):
variable, ssanumber = operand.rsplit('_', 1)
symtab_entry = self.ssa.ir.local_symbol_table[variable]
if 'memory' in symtab_entry:
register.memory = symtab_entry['memory']
return symtab_entry['memory']
else:
memory = Memory(name=variable, scope=self.ssa.ir.function_name)
register.memory = memory
symtab_entry['memory'] = memory
return memory
def register_for_operand(self, operand, execution_frequency=None):
"""Finds an already existing register for the operand or creates a new one.
Args:
operand: The operand for which the register must be found.
"""
if operand in self.variable_register_map:
register = self.variable_register_map[operand]
register.cost += execution_frequency
return register
else:
register = Register()
self.variable_register_map[operand] = register
self.assign_memory(operand, register)
register.cost = execution_frequency
return register
def virtual_reg_basic_block(self, start_node, node,
parent_node_execution_frequency=None):
"""Assign virtual registers to all the statements in a basic block.
Args:
start_node: The start node of the CFG
node: The current node that is to be processed for assigning virtual
registers.
parent_node_execution_frequency: The execution frequency of the parent
node.
"""
if node.loop_header:
# Determine which of the loop header's out_edges node are not part of
# the loop body
# To determine this we will start with the loop footer and traverse
# upwards through each node's in_edges. When we reach the loop header
# node the node other than the node we can from is outside the body
# of the loop.
up_node = node.loop_header
while node not in set(up_node.in_edges):
up_node = up_node.in_edges[0]
# Set the execution frequency of the non-loop node to the execution
# frequency of the loop header's parent since this node will be executed
# as many times as the parent of the loop body.
non_loop_node = (set(node.out_edges) - set([up_node])).pop()
non_loop_node.execution_frequency = parent_node_execution_frequency
self.footer_stack.push(non_loop_node)
node.execution_frequency = (
parent_node_execution_frequency * LOOP_EXECUTION_FREQUENCY_MULTIPLIER)
elif node.execution_frequency is None:
# Update the current node's execution frequency since we will want it later
# when we have to calculate the execution frequencies for phi operands whose
# virtual register assignment is done in the end.
node.execution_frequency = parent_node_execution_frequency
if node.phi_functions:
start_node.phi_nodes.append(node)
for phi_function in node.phi_functions.values():
# The LHS of the phi function is actually the result the function
# RHS are the operands.
operand = phi_function['LHS']
new_register = self.register_for_operand(operand, 0)
new_register.set_def(self.ssa.ir.ir[node.value[0]])
phi_function['LHS'] = new_register
for instruction in self.ssa.optimized(node.value[0], node.value[1] + 1):
if instruction.instruction == '.end_':
continue
elif instruction.instruction == '.begin_':
new_operands = []
for operand in instruction.operands:
if instruction.is_variable_or_label(operand):
# We need not calculate for the execution frequency within the
# function prologue since they are anyway in the registers that
# we are not going to allocate to them for function parameters.
new_register = self.register_for_operand(
operand, node.execution_frequency)
new_register.set_def(instruction)
new_operands.append(new_register)
instruction.operands = new_operands
self.function_parameters = instruction.operands
else:
# The first operand of the branch instruction should still be a label.
# We directly assign that off as the assigned operand since no register
# or memory allocation is required for it.
if instruction.instruction == 'bra':
instruction.assigned_operand1 = instruction.operand1
continue
# The second operand of the branch instruction should still be a label.
# We directly assign that off as the assigned operand since no register
# or memory allocation is required for it.
if instruction.instruction in ['beq', 'bne', 'blt',
'ble', 'bgt', 'bge']:
if self.is_register(cmp_instruction.result):
if instruction.is_variable_or_label(instruction.operand1):
instruction.operand1 = self.register_for_operand(
instruction.operand1, node.execution_frequency)
instruction.operand1.set_use(instruction)
instruction.assigned_operand2 = instruction.operand2
continue
if instruction.is_variable_or_label(instruction.operand1):
instruction.operand1 = self.register_for_operand(
instruction.operand1, node.execution_frequency)
instruction.operand1.set_use(instruction)
if instruction.is_variable_or_label(instruction.operand2):
instruction.operand2 = self.register_for_operand(
instruction.operand2, node.execution_frequency)
instruction.operand2.set_use(instruction)
new_operands = []
for operand in instruction.operands:
if instruction.is_variable_or_label(operand):
new_register = self.register_for_operand(
operand, node.execution_frequency)
new_register.set_use(instruction)
new_operands.append(new_register)
else:
new_operands.append(operand)
instruction.operands = new_operands
# After Copy propagation the only move instructions that remain
# are for the function eplilogue, prologue and at the callee site
# we need not allocate a register for results of these instructions.
# We also need not allocate registers for the result of the return
# instruction
if instruction.instruction in ['move', 'store', 'ret']:
continue
# FIXME: This is architecture specific
# No need to assign a register for the compare instruction for x86_64
# since the architecture uses special flag register for this which
# is not the General Purpose Register.
# However if one of the operands is not a register, will want a
# register to hold
if instruction.instruction == 'cmp':
cmp_instruction = instruction
if not (
self.is_memory(instruction.operand1) and
self.is_memory(instruction.operand2)):
continue
# Assign a register for the result of the instruction
register = Register()
register.set_def(instruction)
self.variable_register_map[instruction.label] = register
instruction.result = register
# The result of the load instruction has infinite cost of spilling
# since that memory location can as well be reloaded
if instruction.instruction == 'load':
register.cost = float('inf')
else:
register.cost = node.execution_frequency
# Reverse order because of the representation we have chosen. When a loop
# header has a loop body node and a node which is outside the loop body,
# it puts the node outside the loop body at the front of the out_edges
# list so we reverse this order to ensure that non-loop node is processed
# in the end.
for child in node.out_edges:
if self.visited.get(child, False):
continue
self.visited[child] = True
self.virtual_reg_basic_block(start_node, child, node.execution_frequency)
def allocate_virtual_registers(self, start_node):
"""Allocate registers in virtual infinite space of registers.
This is the most important phase because, we actually explicitly allocate
some register for the result of the instructions in this phase.
"""
self.visited = {}
self.footer_stack = Stack()
self.virtual_reg_basic_block(start_node, start_node, 1)
# Process all the phi-functions in the end of the function because
# there are phi-functions especially in case of blocks whose operands
# are defined after this block. And also it may so happen that, that
# definition is removed because of copy propagation and only some
# other instructions label remaining. This cannot be determined or
# fixed before the result of the instruction whose result is the
# operand for the phi function is computed
for phi_node in start_node.phi_nodes:
for phi_function in phi_node.phi_functions.values():
lhs_exec_freq = 0
for i, operand in enumerate(phi_function['RHS']):
if is_variable_or_label(operand):
# The execution frequency of a particular phi-operand is the
# execution frequency of the node it comes from. To arrive at
# this, we need to look at how SSA deconstruction happens. When
# SSA is deconstructed, phi-operands are moved to the phi-result
# at the end of the basic block from where they come. So it all
# makes sense to use that node's execution frequency.
exec_freq = phi_node.in_edges[i].execution_frequency
lhs_exec_freq += exec_freq
new_register = self.register_for_operand(
operand, exec_freq)
use = self.ssa.ir.ir[phi_node.in_edges[i].value[1]]
new_register.set_use(use)
new_register.use_instructions.sort(
key=lambda i: i.label)
phi_function['RHS'][i] = new_register
phi_function['LHS'].cost += lhs_exec_freq
def analyze_basic_block_liveness(self, start_node, node):
"""Analyzes the liveness of the variables in the given basic block
Performs a post-order traversal of the control flow graph for processing
the liveness, so the traversal is on the dominator tree.
This code is almost the translation of the pseudo-code from the paper
titled, "Linear Scan Register Allocation on SSA Form" by Christian Wimmer
and Michael Franz available at:
http://www.christianwimmer.at/Publications/Wimmer10a/Wimmer10a.pdf
Args:
start: The starting node of the control flow subgraph that stores the
complete subgraph specific information like liveness of each
variable etc.
node: The node of the control flow subgraph on which post-order
traversal should be performed.
"""
for child in node.out_edges:
if self.visited.get(child, False):
continue
self.visited[child] = True
self.analyze_basic_block_liveness(start_node, child)
# The live variables set in the block where each key is the variable and
# the value is a two elements first representing the start of the range
# and second representing the end of the range.
live = {}
intervals = {}
phi_operands = {}
# Dictionary containing the in_edge and the operands that
# should be included only for those predecessors
include = collections.defaultdict(list)
for successor in node.out_edges:
exclude = []
for successor_in in successor.live_include:
if successor_in != node:
exclude.extend(successor.live_include[successor_in])
successor_live_in = set(successor.live_in.keys()) - set(exclude)
for variable in successor_live_in:
# None for first element of the list means it starts from the
# beginning of the block and None for the second element means
# it runs until the end of the block. See datastructures.CFGNode
# __init__() method for matching how things work with live
# dictionaries.
if self.is_register(variable):
live[variable] = True
intervals[variable] = [node.value[0], node.value[1] + 1]
for phi_function in successor.phi_functions.values():
# Get the in-edges of the successor to determine which entry in the
# phi-functions input corresponds to a given node, since the order
# in the phi-function's input is same as the order of the in-edges.
# Look at ssa.SSA.search() method to see why this ordering is
# preserved.
# This is adding one more degree to the O(n) polynomial since the
# index implementation on the Python lists is O(n) and this is within
# a nested loop. Think of something efficient?
input_position = successor.in_edges.index(node)
operand = phi_function['RHS'][input_position]
if self.is_register(operand):
live[operand] = True
intervals[operand] = [node.value[0], node.value[1] + 1]
# start and end labels of the basic blocks in the SSA CFG which is the
# other universe of regular IR's CFG.
start, end = node.value
# Walk the instructions in the reverse order, note -1 for stepping.
for instruction in self.ssa.optimized(end, start - 1, reversed=True):
# Keep track of the end of the function and append the last register
# name value to the start node
# FIXME: Get rid of this when functions can be compiled independently.
if instruction.instruction.startswith('.end_'):
continue
if instruction.instruction == '.begin_':
for operand in instruction.operands:
if self.is_register(operand):
if operand in live:
intervals[operand][0] = node.value[0]
# Else, since we are traversing in the reverse order, begin
# instruction of the function is the last function we encounter,
# so, if this register is not live anywhere, it is Dead-on-Arrival
# so don't bother about doing anything for it, just pass
continue
if self.is_register(instruction.result):
if instruction.result not in live:
# Dead-on-Arrival. I love Dead-on-Arrival stuff, more
# optimizations! :-P
# NOTE: pop is not added because it doesn't even exist
# So don't bother about doing anything.
if instruction.instruction == 'call':
# For call instruction process its other operands.
pass
else:
self.ssa.optimized_removal.add(instruction.label)
continue
else:
intervals[instruction.result][0] = instruction.label
live.pop(instruction.result)
# We need to process the input operands if and only if they don't
# really exist, if they already exist, it means that they are used
# else where after this basic block and hence should stay alive.
# Only the last use (or the first sighting since we are coming from
# reverse now should get an lifetime end set.)
operand1 = instruction.operand1
if self.is_register(operand1) and operand1 not in live:
live[instruction.operand1] = True
intervals[instruction.operand1] = [node.value[0], instruction.label]
operand2 = instruction.operand2
if self.is_register(operand2) and operand2 not in live:
live[instruction.operand2] = True
intervals[instruction.operand2] = [node.value[0], instruction.label]
for operand in instruction.operands:
if self.is_register(operand) and operand not in live:
intervals[operand] = [node.value[0], instruction.label]
live[operand] = True
phi_function_keys_for_removal = []
for variable, phi_function in node.phi_functions.iteritems():
if phi_function['LHS'] in intervals:
intervals[phi_function['LHS']][0] = node.value[0]
if intervals[phi_function['LHS']][0] == intervals[phi_function['LHS']][1]:
# This patching is required because if the last instruction where the
# operand is used is an instruction just after the phi functions, the
# live range is empty since it is already dead at that instruction. So
# we record the live range as empty as we see it as dead-on-arrival
# operand. But unfortunately is not the case, it should live at least
# until all the phi-move operands are inserted, so we don't end up
# assigning the same color to another phi function result.
intervals[phi_function['LHS']][1] += 1
live.pop(phi_function['LHS'])
phi_operands[phi_function['LHS']] = True
else:
# If the result of the phi function is dead on arrival, completely get
# rid of the phi function.
phi_function_keys_for_removal.append(variable)
for variable in phi_function_keys_for_removal:
node.phi_functions.pop(variable)
for phi_function in node.phi_functions.values():
for i, operand in enumerate(phi_function['RHS']):
include[node.in_edges[i]].append(operand)
phi_operands[operand] = True
if operand in intervals:
intervals[operand][1] = node.value[0]
# Don't do else part, that over writes registers that get defined
# inside the loop
# Every operand that is live at the loop header and is not a phi-operand
# or a phi-result should live from the beginning of the block to the end.
# This is a guard to not spill these registers before other registers
# whose next use is farther outside the loop.
if node.loop_header:
for operand in live:
if operand not in phi_operands:
# node's loop_header property points to loop footer.
loop_footer = node.loop_header
# Lives till the end of the loop footer block. Just make it to live
# out of the block too by adding 1
intervals[operand][1] = loop_footer.value[1]
node.live_in = live
node.live_include = include
for operand in intervals:
range_start = intervals[operand][0] if intervals[operand][0] is not None \
else node.value[0]
range_end = intervals[operand][1] if intervals[operand][0] is not None \
else node.value[1] + 1
if operand in start_node.live_intervals:
# Merge the intervals
start_node.live_intervals[operand].update(range(range_start, range_end))
else:
# Add a new interval
start_node.live_intervals[operand] = set(range(range_start, range_end))
def liveness(self, start):
"""Computes the liveness range for each variable.
Args:
start: the starting node of the subgraph on which liveness must be
computed.
"""
# FIXME: Liveness checking for global variables is completely different
# since they are live until the last call to the function that uses
# global variables return. So may be it is easier to keep the live
# ranges alive during the entire range of the program? Too much
# spilling? Actually Prof. Franz seeded this idea about which I had
# not thought about till now. "Compile each function independently."
# A temporary dictionary containing the nodes visited as the keys and
# dummy True as the value. This dictionary must be reset for every
# traversal.
self.visited = {}
self.analyze_basic_block_liveness(start, start)
self.live_intervals = start.live_intervals
for phi_node in start.phi_nodes:
for phi_function in phi_node.phi_functions.values():
for i, operand in enumerate(phi_function['RHS']):
if self.is_register(operand):
if (operand in self.live_intervals) and operand.definition():
if operand.definition().label > operand.uses()[-1].label:
self.live_intervals[operand].add(operand.definition().label)
else:
phi_function['RHS'][i] = None
self.live_intervals.pop(operand, None)
def populate_collisions(self):
"""Populate collisions based on the live intervals dictionary.
To populate collisions, we first build an inverted map of the live
intervals dictionary and then from that we create the collision nodes.
"""
instructions_registers_map = collections.defaultdict(set)
for register in self.live_intervals:
# Create a new interference node for the current register.
self.register_nodes[register] = InterferenceNode(
register, self.live_intervals[register])
for instruction_label in self.live_intervals[register]:
instructions_registers_map[instruction_label].add(
self.register_nodes[register])
# Now process each instruction label and then add all the register nodes
# per instruction as the instructions that collide with each other.
# Actual populating of the collisions for each register node happens in
# in this loop.
for label, nodes in instructions_registers_map.iteritems():
for node in nodes:
node.append_edges(*nodes)
# As a post processing step remove node itself as an interfering node
# with self.
# IMPORTANT: This post processing step is required because, checking
# and removing the current node among the set of nodes or equivalently
# set difference is an O(n) operation. If we do this inside the nested
# loops in the actual processing step, we will have to do this O(n)
# operation for each register that live at each label. But removing that
# node as a post processing step brings it out by one loop and we have
# to do it only once per register.
for node in self.register_nodes.values():
node.edges.discard(node)
def build_interference_graph(self):
"""Builds the interference graph for the given control flow subgraph.
"""
# We should reset this dictionary every time we build the interference
# graph.
self.register_nodes = {}
self.populate_collisions()
nodes = self.register_nodes.values()
interference_graph = InterferenceGraph(nodes)
self.interference_graph = interference_graph
return interference_graph
def compute_spill_costs(self):
"""Computes the spill costs for the registers.
The spill costs are computed using the Chaitin's Allocator style spill cost
function, i.e.:
spill_cost(reg) = def-use-cost(reg) / degree of the node in the
interference graph
"""
self.spill_costs = PriorityQueue(
self.live_intervals.keys(),
key=lambda r: r.cost / len(self.register_nodes[r].edges) if \
self.register_nodes[r].edges else float('inf'))
def spill(self):
"""Spills one variable whose spill cost is the least.
"""
self.compute_spill_costs()
register = self.spill_costs.pop()
register.spilled = True
if not register.memory:
register.memory = Memory(scope=self.ssa.ir.function_name)
self.live_intervals.pop(register)
def get_cnf_var(self, register, bit_position):
"""Returns the CNF variable for a register if it exists or creates one.
Args:
register: The register for which the CNF variable should be obtained.
bit_position: The bit position for which the CNF variable should be
obtained.
"""
register_var = (register, bit_position)
if register_var in self.register_cnf_map:
cnf_var = self.register_cnf_map.get(register_var)
else:
self.cnf_var_count += 1
self.register_cnf_map[register_var] = self.cnf_var_count
self.cnf_register_map[self.cnf_var_count] = register_var
cnf_var = self.register_cnf_map[register_var]
return cnf_var
def generate_node_bit_template(self):
"""Generates a template containing patterns for each node's clauses.
This generates a template of position numbers whose literals should be
negated. For example if we have 11 as the number of registers then
the max bit value is 10 which in binary is 1010. We generate the template
of the following format:
[[0, 1],
[0, 2, 3]
]
"""
self.node_bit_template = []
max_reg_binary = bin(self.num_registers - 1)[2:]
# IMPORTANT: The bit_position is 0 for the Most Significant Bit (MSB)
# to k - 1 for the Least Significant Bit (LSB) where k is the number
# number of bits in the largest register value.
ones_positions = []
for bit_position, bit in enumerate(max_reg_binary):
if bit == '1':
ones_positions.append(bit_position)
elif bit == '0':
self.node_bit_template.append(ones_positions + [bit_position])
def generate_edge_bit_template(self):
"""Generates a template containing patterns fo each edge's clauses.
This template represents a variable or the negation of a variable. However
because of the graph coloring problem, each of this variable is a
combination of the corresponding literals in the binary representation
of the color for two nodes joined by an edge. Example: if two nodes are
r1 and r2 and there are 8 colors this can be represented as r10, r11, r12
and r20, r21 and r22. Now if the bit template has has an entry "101",
this can be encoded as (r12 | r22 | ~r11 | ~r21 | r10 | r20). This method
only return the bit patterns, not the actual literals.
"""
last_pattern = bin(self.num_registers - 1)[2:]
# Number of literals per register.
num_literals_per_reg = len(last_pattern)
self.edge_bit_template = [last_pattern]
for i in range(self.num_registers - 2, -1, -1):
pattern = ('%s' % bin(i)[2:]).zfill(num_literals_per_reg)
self.edge_bit_template.append(pattern)
return num_literals_per_reg
def generate_node_clauses(self, register):
"""Generates the clauses for the given node.
Generate the clauses to exclude the numbers higher than the maximum
number of registers - 1. For example, if we have 10 registers, then
the maximum register number is 9 (0 to 9 registers), but since we
use the 4 bit representation for this case, we need to ensure that
the registers cannot take values from 10 to 15. So exlusion clauses
are added to exclude these numbers for each register/node.
Args:
register: The node for which the exclusion clauses should be added.
"""
clauses = []
for template in self.node_bit_template:
clause = ''
for bit_position in template:
cnf_var = self.get_cnf_var(register, bit_position)
clause += '-%s' % (cnf_var)
clauses.append(clause)
return clauses
def generate_edge_clauses(self, register1, register2):
"""Generates all the clauses for an edge.
Args:
register1: One of the two ends of the edge for which the clauses
should be generated.
register2: One of the two ends of the edge for which the clauses
should be generated. This can be None if a register has absolutely
no collisions.
"""
clauses = []
# Handle the case when the register has absolutely no conflicts.
# We have to just generate clauses containing one CNF variable for each
# bit position.
if not register2:
# Since this template is expected to have the same bit-width for all
# the patterns just pick the first one for the number of bits count.
num_bits = len(self.edge_bit_template[0])
for bit_position in range(num_bits):
cnf1_var = self.get_cnf_var(register1, bit_position)
clauses.append('%s ' % (cnf1_var))
return clauses
for template in self.edge_bit_template:
clause = ''
# IMPORTANT: The bit_position is 0 for the Most Significant Bit (MSB)
# to k - 1 for the Least Significant Bit (LSB) where k is the number
# number of bits in the largest register value.
for bit_position, bit in enumerate(template):
cnf1_var = self.get_cnf_var(register1, bit_position)
cnf2_var = self.get_cnf_var(register2, bit_position)
if bit == '0':
clause += '-%s -%s ' % (cnf1_var, cnf2_var)
else:
clause += '%s %s ' % (cnf1_var, cnf2_var)
clauses.append(clause)
return clauses
def precolor(self):
"""Generate clauses for precolored registers like function parameters.
As per Linux AMD64 ABI function calling convention, function arguments
must be passed in %rdi, %rsi, %rdx, %rcx, %r8 and %r9 in that order and
the remaining arguments on the stack.So by forcing the function parameters
to be in those registers by precoloring, we reduce the number of moves
required.
NOTE: This can still be architecture independent because to ensure that
we use the architecture specific calling convention, we just need to
name the registers in such a way that function argument registers have
the same color as AMD64 function arguments registers.
"""
# Imported here because of cyclic import issues.
from x86_64 import FUNCTION_ARGUMENTS_COLORS
max_reg_binary_len = len(bin(self.num_registers - 1)[2:])
clauses = []
for register, color in zip(
self.function_parameters, FUNCTION_ARGUMENTS_COLORS):
colorbits = bin(color)[2:]
num_leading_zeros = 0
while num_leading_zeros < (max_reg_binary_len - len(colorbits)):
cnf_var = self.get_cnf_var(register, num_leading_zeros)
clauses.append('-%s ' % (cnf_var))
num_leading_zeros += 1
for bit_position, bit in enumerate(colorbits):
cnf_var = self.get_cnf_var(register, bit_position + num_leading_zeros)
if bit == '0':
clauses.append('-%s ' % (cnf_var))
else:
clauses.append('%s ' % (cnf_var))
return clauses
def reduce_to_sat(self, interference_graph):
"""Reduces the graph coloring problem to the boolean satisfiability.
* We use circuit encoding technique for reducing graph coloring to SAT.
* The SAT represented is in Conjunctive Normal Form (CNF).