[105ed6]: microbe / optimizer.cpp Maximize Restore History

Download this file

optimizer.cpp    513 lines (403 with data), 17.2 kB

  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
/***************************************************************************
* Copyright (C) 2005 by David Saxton *
* david@bluehaze.org *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "instruction.h"
#include "optimizer.h"
#include <kdebug.h>
#include <cassert>
#include <iostream>
using namespace std;
QString binary( uchar val )
{
QString bin = QString::number( val, 2 );
QString pad;
pad.fill( '0', 8-bin.length() );
return pad + bin;
}
Optimizer::Optimizer()
{
m_pCode = 0l;
}
Optimizer::~Optimizer()
{
}
void Optimizer::optimize( Code * code )
{
// return;
m_pCode = code;
bool changed;
do
{
changed = false;
// Repeatedly generate links and states until
// we know as much as possible about the system.
propagateLinksAndStates();
// Remove instructions without input links
changed |= pruneInstructions();
// Perform optimizations based on processor states
changed |= optimizeInstructions();
}
while ( changed );
}
void Optimizer::propagateLinksAndStates()
{
int count = 0;
do
{
count++;
m_pCode->generateLinksAndStates();
}
while ( giveInputStates() );
// cout << "count="<<count<<endl;
}
bool Optimizer::giveInputStates()
{
bool changed = false;
Code::iterator end = m_pCode->end();
for ( Code::iterator it = m_pCode->begin(); it != end; ++it )
{
// Now, build up the most specific known processor state from the instructins
// that could be executed immediately before this instruction.
// This is done by taking the output state of the first input link, and
// then reducing it to the greatest common denominator of all the input states.
const InstructionList list = (*it)->inputLinks();
if ( list.isEmpty() )
continue;
InstructionList::const_iterator inputIt = list.begin();
InstructionList::const_iterator inputsEnd = list.end();
ProcessorState input = (*(inputIt++))->outputState();
while ( inputIt != inputsEnd )
input.merge( (*inputIt++)->outputState() );
if ( !changed )
{
ProcessorState before = (*it)->inputState();
bool stateChanged = ( before != input );
changed |= stateChanged;
}
(*it)->setInputState( input );
}
return changed;
}
bool Optimizer::pruneInstructions()
{
bool removed = false;
//BEGIN remove instructions without any input links
Code::iterator it = m_pCode->begin();
Code::iterator end = m_pCode->end();
// Jump past the first instruction, as nothing (necessarily) points to that
if ( it != end )
++it;
while ( it != end )
{
if ( (*it)->inputLinks().isEmpty() )
{
// cout << "Removing: " << (*it)->code() << endl;
it.removeAndIncrement();
removed = true;
}
else
++it;
}
end = m_pCode->end(); // Reset end as instructions may have been removed
//END remove instructions without any input links
//BEGIN remove labels without any reference to them
// First: build up a list of labels which are referenced
QStringList referencedLabels;
for ( it = m_pCode->begin(); it != end; ++it )
{
if ( Instr_goto * ins = dynamic_cast<Instr_goto*>(*it) )
referencedLabels << ins->label();
else if ( Instr_call * ins = dynamic_cast<Instr_call*>(*it) )
referencedLabels << ins->label();
}
// Now remove labels from instructions that aren't in the referencedLabels list
for ( it = m_pCode->begin(); it != end; ++it )
{
QStringList labels = (*it)->labels();
QStringList::iterator labelsEnd = labels.end();
for ( QStringList::iterator labelsIt = labels.begin(); labelsIt != labelsEnd; )
{
if ( !referencedLabels.contains( *labelsIt ) )
{
labelsIt = labels.erase( labelsIt );
removed = true;
}
else
++labelsIt;
}
(*it)->setLabels( labels);
}
//END remove labels without any reference to them
return removed;
}
bool Optimizer::optimizeInstructions()
{
//BEGIN Optimization 1: Concatenate chained GOTOs
// We go through the instructions looking for GOTO statements. If we find any, then
// we trace back through their input links to any other GOTO statements - any that
// are found are then redirected to point to the label that the original GOTO statement
// was pointing at.
Code::iterator end = m_pCode->end();
for ( Code::iterator it = m_pCode->begin(); it != end; ++it )
{
Instr_goto * gotoIns = dynamic_cast<Instr_goto*>(*it);
if ( !gotoIns )
continue;
if ( redirectGotos( gotoIns, gotoIns->label() ) )
return true;
m_pCode->setAllUnused();
}
//END Optimization 1: Concatenate chained GOTOs
//BEGIN Optimization 2: Remove GOTOs when jumping to the subsequent instruction
// Any GOTO instructions that just jump to the next instruction can be removed.
for ( Code::iterator it = m_pCode->begin(); it != end; ++it )
{
Instruction * next = *(++Code::iterator(it));
Instruction * gotoIns = dynamic_cast<Instr_goto*>(*it);
if ( !gotoIns || !next || (gotoIns->outputLinks().first() != next) )
continue;
// cout << "Removing: " << gotoIns->code() << endl;
it.removeAndIncrement();
return true;
}
end = m_pCode->end();
//END Optimization 2: Remove GOTOs when jumping to the subsequent instruction
//BEGIN Optimization 3: Replace MOVWF with CLRF with W is 0
// We look for MOVWF instructions where the working register holds zero.
// We then replace the MOVWf instruction with a CLRF instruction.
for ( Code::iterator it = m_pCode->begin(); it != end; ++it )
{
Instr_movwf * ins = dynamic_cast<Instr_movwf*>(*it);
if ( !ins )
continue;
ProcessorState inputState = ins->inputState();
RegisterState working = inputState.working;
if ( (working.value != 0x0) || (working.known != 0xff) )
continue;
// CLRF sets the Z flag of STATUS to 1, but MOVWF does not set any flags.
// So we need to check for dependence of the Z flag if we are possibly
// changing the flag by replacing the instruction.
if ( !(inputState.status.definiteOnes() & (1 << RegisterBit::Z)) )
{
// Input state of Z flag is either unknown or low.
uchar depends = generateRegisterDepends( *it, Register::STATUS );
if ( depends & (1 << RegisterBit::Z) )
{
// Looks like there's some instruction that depends on the zero bit,
// and we about potentially about to change it.
continue;
}
}
Instr_clrf * instr_clrf = new Instr_clrf( ins->file() );
// cout << "Replacing \""<<(*it)->code()<<"\" with \""<<instr_clrf->code()<<"\"\n";
it.insertBefore( instr_clrf );
it.removeAndIncrement();
return true;
}
//END Optimization 3: Replace MOVWF with CLRF with W is 0
//BEGIN Optimization 4: Replace writes to W with MOVLW when value is known
// We look for instructions with AssemblyType either WorkingOriented, or FileOriented
// and writing to W. Then, if the value is known and there are no instructions that
// depend on the STATUS bits set by the instruction, then we replace it with a MOVLW
for ( Code::iterator it = m_pCode->begin(); it != end; ++it )
{
if ( dynamic_cast<Instr_movlw*>(*it) )
{
// If we don't catch this condition, we'll end up in an infinite loop,
// repeatedly replacing the first MOVLW that we come across.
continue;
}
bool workingOriented = (*it)->assemblyType() == Instruction::WorkingOriented;
bool fileOriented = (*it)->assemblyType() == Instruction::FileOriented;
if ( !workingOriented && (!fileOriented || ((*it)->dest() != 0)) )
continue;
// So can now assume that workingOriented and fileOriented are logical opposites
RegisterState outputState = (*it)->outputState().working;
if ( outputState.known != 0xff )
continue;
ProcessorBehaviour behaviour = (*it)->behaviour();
// MOVLW does not set any STATUS flags, but the instruction that we are replacing
// might. So we must check if any of these STATUS flags are depended upon, and if so
// only allow replacement if the STATUS flags are not being changed.
if ( !canRemove( *it, Register::STATUS, behaviour.reg( Register::STATUS ).indep ) )
continue;
Instr_movlw * movlw = new Instr_movlw( outputState.value );
// cout << "Replacing \""<<(*it)->code()<<"\" with \""<<movlw->code()<<"\"\n";
it.insertBefore( movlw );
it.removeAndIncrement();
return true;
}
//END Optimization 4: Replace writes to W with MOVLW when value is known
//BEGIN Optimization 5: Remove writes to a bit when the value is ignored and overwritten again
// We go through the instructions looking for statements that write to a bit (bcf, bsf).
// If we find any, then we trace through their output links to see if their value is
// overwritten before it is used - and if so, the instruction can be removed.
for ( Code::iterator it = m_pCode->begin(); it != end; ++it )
{
if ( (*it)->assemblyType() != Instruction::BitOriented )
continue;
const Register regSet = (*it)->file();
if ( regSet.affectsExternal() )
continue;
uchar bitPos = (*it)->bit().bitPos();
ProcessorState inputState = (*it)->inputState();
ProcessorState outputState = (*it)->outputState();
ProcessorBehaviour behaviour = (*it)->behaviour();
// Are we rewriting over a bit that already has the same value?
// (Note this check is just for the bit changing instructions, as there is a similar
// check for register changing actions later on when we know which bits care about
// being overwritten).
if ( inputState.reg( regSet ).known & (1 << bitPos) )
{
bool beforeVal = (inputState.reg( regSet ).value & (1 << bitPos));
bool afterVal = (outputState.reg( regSet ).value & (1 << bitPos));
if ( beforeVal == afterVal )
{
// cout << "Removing: " << (*it)->code() << endl;
it.removeAndIncrement();
return true;
}
}
uchar depends = generateRegisterDepends( *it, regSet );
if ( !(depends & (1 << bitPos)) )
{
// Bit is overwritten before being used - so lets remove this instruction :)
// cout << "Removing: " << (*it)->code() << endl;
it.removeAndIncrement();
return true;
}
}
m_pCode->setAllUnused();
//END Optimization 5: Remove writes to a bit when the value is ignored and overwritten again
//BEGIN Optimization 6: Remove writes to a register when the value is ignored and overwritten again
// We go through the instructions looking for statements that write to a register (such as MOVLW).
// If we find any, then we trace through their output links to see if their value is
// overwritten before it is used - and if so, the instruction can be removed.
for ( Code::iterator it = m_pCode->begin(); it != end; ++it )
{
bool noFile = false;
switch ( (*it)->assemblyType() )
{
case Instruction::WorkingOriented:
noFile = true;
// (no break)
case Instruction::FileOriented:
break;
case Instruction::BitOriented:
case Instruction::Other:
case Instruction::None:
continue;
}
const Register regSet = noFile ? Register( Register::WORKING ) : (*it)->outputReg();
if ( regSet.affectsExternal() )
continue;
ProcessorState inputState = (*it)->inputState();
ProcessorState outputState = (*it)->outputState();
ProcessorBehaviour behaviour = (*it)->behaviour();
// All ins_file instructions will affect at most two registers; the
// register it is writing to (regSet) and the status register.
// In i==0, test regSet
// In i==1, test STATUS
bool ok = true;
for ( unsigned i = 0; i < 2; ++ i)
{
// If we are testing STATUS, then we assume that the bits changed
// are only those that are marked as independent.
uchar bitmask = ( i == 1 ) ? behaviour.reg( Register::STATUS ).indep : 0xff;
if ( !canRemove( *it, (i == 0) ? regSet : Register::STATUS, bitmask ) )
{
ok = false;
break;
}
}
if ( !ok )
continue;
// Looks like we're free to remove the instruction :);
// cout << "Removing: " << (*it)->code() << endl;
it.removeAndIncrement();
return true;
}
m_pCode->setAllUnused();
//END Optimization 6: Remove writes to a register when the value is ignored and overwritten again
return false;
}
bool Optimizer::redirectGotos( Instruction * current, const QString & label )
{
if ( current->isUsed() )
return false;
current->setUsed( true );
bool changed = false;
const InstructionList list = current->inputLinks();
InstructionList::const_iterator end = list.end();
for ( InstructionList::const_iterator it = list.begin(); it != end; ++it )
{
Instr_goto * gotoIns = dynamic_cast<Instr_goto*>(*it);
if ( !gotoIns || (gotoIns->label() == label) )
continue;
// cout << "Redirecting goto to label \"" << label << "\" : " << gotoIns->code() << endl;
gotoIns->setLabel( label );
changed = true;
}
return changed;
}
uchar Optimizer::generateRegisterDepends( Instruction * current, const Register & reg )
{
m_pCode->setAllUnused();
const InstructionList list = current->outputLinks();
InstructionList::const_iterator listEnd = list.end();
uchar depends = 0x0;
for ( InstructionList::const_iterator listIt = list.begin(); listIt != listEnd; ++listIt )
depends |= registerDepends( *listIt, reg );
return depends;
}
uchar Optimizer::registerDepends( Instruction * current, const Register & reg )
{
if ( current->isUsed() )
return current->registerDepends( reg );
current->setUsed( true );
uchar depends = 0x0;
const InstructionList list = current->outputLinks();
InstructionList::const_iterator end = list.end();
for ( InstructionList::const_iterator it = list.begin(); it != end; ++it )
depends |= registerDepends( *it, reg );
RegisterBehaviour behaviour = current->behaviour().reg( reg );
depends &= ~(behaviour.indep); // Get rid of depend bits that are set in this instruction
depends |= behaviour.depends; // And add the ones that are dependent in this instruction
current->setRegisterDepends( depends, reg );
return depends;
}
bool Optimizer::canRemove( Instruction * ins, const Register & reg, uchar bitMask )
{
// The bits that are depended upon in the future for this register
uchar depends = generateRegisterDepends( ins, reg );
// Only interested in those bits allowed by the bit mask
depends &= bitMask;
RegisterState inputState = ins->inputState().reg( reg );
RegisterState outputState = ins->outputState().reg( reg );
if ( inputState.unknown() & depends )
{
// There's at least one bit whose value is depended on, but is not known before this
// instruction is executed. Therefore, it is not safe to remove this instruction.
return false;
}
if ( outputState.unknown() & depends )
{
// There's at least one bit whose value is depended on, but is not known after this
// instruction is executed. Therefore, it is not safe to remove this instruction.
return false;
}
uchar dependsInput = inputState.value & depends;
uchar dependsOutput = outputState.value & depends;
if ( dependsInput != dependsOutput )
{
// At least one bit whose value is depended upon was changed.
return false;
}
return true;
}