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) {
29 StoreLoadSet * sls=cgen->getStoreLoadSet(r);
31 model_print("address=%p ", sls->getAddressEncoding(cgen, r, satsolution));
32 switch(r->getType()) {
34 model_print("rd=%lu ", sls->getValueEncoding(cgen, r, satsolution));
38 model_print("wr=%lu ", sls->getValueEncoding(cgen, r, satsolution));
43 model_print("rd=%lu ", sls->getRMWRValueEncoding(cgen, r, satsolution));
44 model_print("wr=%lu ", sls->getValueEncoding(cgen, r, satsolution));
52 void ScheduleBuilder::buildSchedule(bool * satsolution) {
53 threads.push_back(execution->getEPRecord(MCID_FIRST));
54 lastoperation.push_back(NULL);
56 stores.push_back(new SnapList<EPRecord *>());
57 storelastoperation.push_back(NULL);
60 //Loop over threads...getting rid of non-load/store actions
61 for(uint index=0;index<threads.size();index++) {
62 EPRecord *record=threads[index];
65 EPRecord *next=processRecord(record, satsolution);
69 if (next->getType()==STORE) {
70 stores[index]->push_back(next);
71 next=getNextRecord(next);
72 } else if (next->getType()==FENCE) {
73 if (!stores[index]->empty())
74 scheduler->addWaitPair(next, stores[index]->back());
75 next=getNextRecord(next);
76 } else if (next->getType()==RMW) {
77 if (!stores[index]->empty())
78 scheduler->addWaitPair(next, stores[index]->back());
87 //Now we have just memory operations, find the first one...make it go first
88 EPRecord *earliest=NULL;
90 for(uint index=0;index<threads.size();index++) {
91 EPRecord *record=threads[index];
93 if (record!=NULL && (earliest==NULL ||
94 cg->getOrder(record, earliest, satsolution))) {
98 if (!stores[index]->empty()) {
99 record=stores[index]->front();
101 if (record!=NULL && (earliest==NULL ||
102 cg->getOrder(record, earliest, satsolution))) {
109 if (earliest == NULL)
112 for(uint index=0;index<threads.size();index++) {
113 EPRecord *record=threads[index];
114 if (record==earliest) {
115 //Update index in vector for current thread
116 EPRecord *next=getNextRecord(record);
118 lastoperation[index]=record;
120 EPRecord *last=lastoperation[index];
122 scheduler->addWaitPair(earliest, last);
127 if (!stores[index]->empty() && (recstore=stores[index]->front())==earliest) {
128 //Update index in vector for current thread
129 stores[index]->pop_front();
130 storelastoperation[index]=recstore;
132 EPRecord *last=storelastoperation[index];
134 scheduler->addWaitPair(earliest, last);
142 EPRecord * ScheduleBuilder::getNextRecord(EPRecord *record) {
143 EPRecord *next=record->getNextRecord();
145 //If this branch exit isn't in the current model, we should not go
147 if (record->getType()==BRANCHDIR && next!=NULL) {
148 BranchRecord *br=cg->getBranchRecord(record);
149 if (!br->hasNextRecord())
153 if (next==NULL && record->getBranch()!=NULL) {
154 EPValue * epbr=record->getBranch();
155 EPRecord *branch=epbr->getRecord();
157 return getNextRecord(branch);
163 EPRecord * ScheduleBuilder::processRecord(EPRecord *record, bool *satsolution) {
164 switch(record->getType()) {
173 return record->getChildRecord();
177 BranchRecord *br=cg->getBranchRecord(record);
178 int index=br->getBranchValue(satsolution);
180 if (index>=0 && index < br->numDirections()) {
181 EPValue *val=record->getValue(index);
182 EPRecord *branchrecord=val->getFirstRecord();
183 if (branchrecord!=NULL)
186 //otherwise just go past the branch...we don't know what this code is going to do
194 /* Continue executing */
197 /* Start next thread */
198 threads.push_back(record->getChildRecord());
199 lastoperation.push_back(NULL);
201 stores.push_back(new SnapList<EPRecord *>());
202 storelastoperation.push_back(NULL);
218 return getNextRecord(record);