annotate python/mem2reg.py @ 253:74c6a20302d5

Added better logging
author Windel Bouwman
date Wed, 31 Jul 2013 17:57:03 +0200
parents
children 444b9df2ed99
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
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
8 return all(type(use) in [Load,Store] for use in allocinst.value.used_by)
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
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
27 for use_ins in load.value.used_by:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
28 use_ins.replaceValue(load.value, store.value)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
29 assert not load.value.Used
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
30 logging.debug('replaced {} with {}'.format(load, store.value))
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
31 bb.removeInstruction(load)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
32
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
33 # Remove store instructions:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
34 for store in stores:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
35 sv = store.value
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
36 logging.debug('removing {}'.format(store))
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
37 bb.removeInstruction(store)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
38 #assert sv.Used
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
39
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
40 # Remove alloca instruction:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
41 assert not ai.value.Used, ai.value.used_by
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
42 bb.removeInstruction(ai)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
43
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
44 def promote(self, ai):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
45 # Find load operations and replace them with assignments
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
46 v = ai.value
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
47 if len(ai.value.UsedInBlocks) == 1:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
48 self.promoteSingleBlock(ai)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
49 return
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
50
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
51 loads = [i for i in v.used_by if isinstance(i, Load)]
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
52 stores = [i for i in v.used_by if isinstance(i, Store)]
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
53
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
54 # Each store instruction can be removed (later).
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
55 # Instead of storing the value, we use it
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
56 # where the load would have been!
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
57 replMap = {}
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
58 for store in stores:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
59 replMap[store] = store.value
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
60
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
61 # for each load, track back what the defining store
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
62 # was.
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
63 for load in loads:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
64 pass
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
65
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
66 def onFunction(self, f):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
67 for bb in f.BasicBlocks:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
68 allocs = [i for i in bb.Instructions if isinstance(i, Alloc)]
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
69 for i in allocs:
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
70 if isAllocPromotable(i):
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
71 self.promote(i)
74c6a20302d5 Added better logging
Windel Bouwman
parents:
diff changeset
72