annotate python/mem2reg.py @ 271:cf7d5fb7d9c8

Reorganization
author Windel Bouwman
date Tue, 20 Aug 2013 18:56:02 +0200
parents 444b9df2ed99
children 534b94b40aa8
rev   line source
253
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
1 import logging
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
2 from transform import FunctionPass
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
3 from ir import *
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
4
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
5 def isAllocPromotable(allocinst):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
6 # Check if alloc value is only used by load and store operations.
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
7 assert type(allocinst) is Alloc
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 253
diff changeset
8 return all(type(use) in [Load, Store] for use in allocinst.value.used_by)
253
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
9
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
10 class Mem2RegPromotor(FunctionPass):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
11 def promoteSingleBlock(self, ai):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
12 v = ai.value
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
13 bb = ai.Block
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
14
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
15 # Replace all loads with the value:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
16 loads = [i for i in v.used_by if isinstance(i, Load)]
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
17 stores = [i for i in v.used_by if isinstance(i, Store)]
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
18 stores.sort(key=lambda s: s.Position)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
19 stores.reverse()
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
20
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
21 for load in loads:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
22 idx = load.Position
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
23 # Search upwards:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
24 for store in stores:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
25 if store.Position < load.Position:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
26 break
261
444b9df2ed99 try to split up code generation
Windel Bouwman
parents: 253
diff changeset
27 load.value.replaceby(store.value)
253
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
28 logging.debug('replaced {} with {}'.format(load, store.value))
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
29 bb.removeInstruction(load)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
30
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
31 # Remove store instructions:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
32 for store in stores:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
33 sv = store.value
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
34 logging.debug('removing {}'.format(store))
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
35 bb.removeInstruction(store)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
36 #assert sv.Used
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
37
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
38 # Remove alloca instruction:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
39 assert not ai.value.Used, ai.value.used_by
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
40 bb.removeInstruction(ai)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
41
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
42 def promote(self, ai):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
43 # Find load operations and replace them with assignments
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
44 v = ai.value
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
45 if len(ai.value.UsedInBlocks) == 1:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
46 self.promoteSingleBlock(ai)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
47 return
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
48
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
49 loads = [i for i in v.used_by if isinstance(i, Load)]
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
50 stores = [i for i in v.used_by if isinstance(i, Store)]
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
51
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
52 # Each store instruction can be removed (later).
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
53 # Instead of storing the value, we use it
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
54 # where the load would have been!
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
55 replMap = {}
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
56 for store in stores:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
57 replMap[store] = store.value
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
58
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
59 # for each load, track back what the defining store
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
60 # was.
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
61 for load in loads:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
62 pass
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
63
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
64 def onFunction(self, f):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
65 for bb in f.BasicBlocks:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
66 allocs = [i for i in bb.Instructions if isinstance(i, Alloc)]
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
67 for i in allocs:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
68 if isAllocPromotable(i):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
69 self.promote(i)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
70