1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
10 #include "schedulebuilder.h"
11 #include "mcexecution.h"
12 #include "mcschedule.h"
14 #include "branchrecord.h"
15 #include "storeloadset.h"
17 ScheduleBuilder::ScheduleBuilder(MCExecution *_execution, ConstGen *cgen) :
19 execution(_execution),
20 scheduler(execution->get_scheduler())
24 ScheduleBuilder::~ScheduleBuilder() {
28 void neatPrint(EPRecord *r, ConstGen *cgen, bool *satsolution) {
30 switch(r->getType()) {
32 StoreLoadSet * sls=cgen->getStoreLoadSet(r);
33 model_print("address=%p ", sls->getAddressEncoding(cgen, r, satsolution));
34 model_print("rd=%lu ", sls->getValueEncoding(cgen, r, satsolution));
38 StoreLoadSet * sls=cgen->getStoreLoadSet(r);
39 model_print("address=%p ", sls->getAddressEncoding(cgen, r, satsolution));
40 model_print("wr=%lu ", sls->getValueEncoding(cgen, r, satsolution));
44 StoreLoadSet * sls=cgen->getStoreLoadSet(r);
45 model_print("address=%p ", sls->getAddressEncoding(cgen, r, satsolution));
46 model_print("rd=%lu ", sls->getRMWRValueEncoding(cgen, r, satsolution));
47 model_print("wr=%lu ", sls->getValueEncoding(cgen, r, satsolution));
56 void ScheduleBuilder::buildSchedule(bool * satsolution) {
57 threads.push_back(execution->getEPRecord(MCID_FIRST));
58 lastoperation.push_back(NULL);
60 stores.push_back(new SnapList<EPRecord *>());
61 storelastoperation.push_back(NULL);
64 //Loop over threads...getting rid of non-load/store actions
65 for(uint index=0;index<threads.size();index++) {
66 EPRecord *record=threads[index];
69 EPRecord *next=processRecord(record, satsolution);
73 if (next->getType()==STORE) {
74 stores[index]->push_back(next);
75 next=getNextRecord(next);
76 } else if (next->getType()==FENCE) {
77 if (!stores[index]->empty())
78 scheduler->addWaitPair(next, stores[index]->back());
79 next=getNextRecord(next);
80 } else if (next->getType()==RMW) {
81 if (!stores[index]->empty())
82 scheduler->addWaitPair(next, stores[index]->back());
91 //Now we have just memory operations, find the first one...make it go first
92 EPRecord *earliest=NULL;
93 for(uint index=0;index<threads.size();index++) {
94 EPRecord *record=threads[index];
96 if (record!=NULL && (earliest==NULL ||
97 cg->getOrder(record, earliest, satsolution))) {
101 if (!stores[index]->empty()) {
102 record=stores[index]->front();
103 if (record!=NULL && (earliest==NULL ||
104 cg->getOrder(record, earliest, satsolution))) {
111 if (earliest == NULL)
114 for(uint index=0;index<threads.size();index++) {
115 EPRecord *record=threads[index];
116 if (record==earliest) {
117 //Update index in vector for current thread
118 EPRecord *next=getNextRecord(record);
120 lastoperation[index]=record;
122 EPRecord *last=lastoperation[index];
124 scheduler->addWaitPair(earliest, last);
129 if (!stores[index]->empty() && (recstore=stores[index]->front())==earliest) {
130 //Update index in vector for current thread
131 stores[index]->pop_front();
132 storelastoperation[index]=recstore;
134 EPRecord *last=storelastoperation[index];
136 scheduler->addWaitPair(earliest, last);
144 EPRecord * ScheduleBuilder::getNextRecord(EPRecord *record) {
145 EPRecord *next=record->getNextRecord();
147 //If this branch exit isn't in the current model, we should not go
149 if (record->getType()==BRANCHDIR && next!=NULL) {
150 BranchRecord *br=cg->getBranchRecord(record);
151 if (!br->hasNextRecord())
155 if (next==NULL && record->getBranch()!=NULL) {
156 EPValue * epbr=record->getBranch();
157 EPRecord *branch=epbr->getRecord();
159 return getNextRecord(branch);
165 EPRecord * ScheduleBuilder::processRecord(EPRecord *record, bool *satsolution) {
166 switch(record->getType()) {
175 return record->getChildRecord();
179 BranchRecord *br=cg->getBranchRecord(record);
180 int index=br->getBranchValue(satsolution);
182 if (index>=0 && index < br->numDirections()) {
183 EPValue *val=record->getValue(index);
184 EPRecord *branchrecord=val->getFirstRecord();
185 if (branchrecord!=NULL)
188 //otherwise just go past the branch...we don't know what this code is going to do
196 /* Continue executing */
199 /* Start next thread */
200 threads.push_back(record->getChildRecord());
201 lastoperation.push_back(NULL);
203 stores.push_back(new SnapList<EPRecord *>());
204 storelastoperation.push_back(NULL);
222 return getNextRecord(record);