annotate python/ppci/mem2reg.py @ 365:98ff43cfdd36

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