blob: 2f532d88381656b68b2bc6f7254d2ec4a0c3c23f [file] [log] [blame]
Tanya Lattnerd14b8372004-03-01 02:50:01 +00001//===-- ModuloScheduling.cpp - ModuloScheduling ----------------*- C++ -*-===//
2//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Tanya Lattnerd14b8372004-03-01 02:50:01 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Tanya Lattnerd14b8372004-03-01 02:50:01 +00009//
Tanya Lattner0a88d2d2004-07-30 23:36:10 +000010// This ModuloScheduling pass is based on the Swing Modulo Scheduling
11// algorithm.
Misha Brukman82fd8d82004-08-02 13:59:10 +000012//
Guochun Shif1c154f2003-03-27 17:57:44 +000013//===----------------------------------------------------------------------===//
14
Tanya Lattnerd14b8372004-03-01 02:50:01 +000015#define DEBUG_TYPE "ModuloSched"
16
17#include "ModuloScheduling.h"
Tanya Lattner0a88d2d2004-07-30 23:36:10 +000018#include "llvm/Instructions.h"
19#include "llvm/Function.h"
Tanya Lattnerd14b8372004-03-01 02:50:01 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/Support/CFG.h"
23#include "llvm/Target/TargetSchedInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/GraphWriter.h"
26#include "llvm/ADT/StringExtras.h"
Misha Brukman82fd8d82004-08-02 13:59:10 +000027#include <cmath>
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +000028#include <algorithm>
Tanya Lattnerd14b8372004-03-01 02:50:01 +000029#include <fstream>
30#include <sstream>
Misha Brukman82fd8d82004-08-02 13:59:10 +000031#include <utility>
32#include <vector>
Misha Brukman7da1e6e2004-10-10 23:34:50 +000033#include "../MachineCodeForInstruction.h"
34#include "../SparcV9TmpInstr.h"
35#include "../SparcV9Internals.h"
36#include "../SparcV9RegisterInfo.h"
Tanya Lattnerd14b8372004-03-01 02:50:01 +000037using namespace llvm;
38
39/// Create ModuloSchedulingPass
40///
41FunctionPass *llvm::createModuloSchedulingPass(TargetMachine & targ) {
42 DEBUG(std::cerr << "Created ModuloSchedulingPass\n");
43 return new ModuloSchedulingPass(targ);
44}
45
Tanya Lattner0a88d2d2004-07-30 23:36:10 +000046
47//Graph Traits for printing out the dependence graph
Tanya Lattnerd14b8372004-03-01 02:50:01 +000048template<typename GraphType>
49static void WriteGraphToFile(std::ostream &O, const std::string &GraphName,
50 const GraphType &GT) {
51 std::string Filename = GraphName + ".dot";
52 O << "Writing '" << Filename << "'...";
53 std::ofstream F(Filename.c_str());
54
55 if (F.good())
56 WriteGraph(F, GT);
57 else
58 O << " error opening file for writing!";
59 O << "\n";
60};
Guochun Shif1c154f2003-03-27 17:57:44 +000061
Tanya Lattner0a88d2d2004-07-30 23:36:10 +000062//Graph Traits for printing out the dependence graph
Brian Gaeked0fde302003-11-11 22:41:34 +000063namespace llvm {
64
Tanya Lattnerd14b8372004-03-01 02:50:01 +000065 template<>
66 struct DOTGraphTraits<MSchedGraph*> : public DefaultDOTGraphTraits {
67 static std::string getGraphName(MSchedGraph *F) {
68 return "Dependence Graph";
69 }
Guochun Shi8f1d4ab2003-06-08 23:16:07 +000070
Tanya Lattnerd14b8372004-03-01 02:50:01 +000071 static std::string getNodeLabel(MSchedGraphNode *Node, MSchedGraph *Graph) {
72 if (Node->getInst()) {
73 std::stringstream ss;
74 ss << *(Node->getInst());
75 return ss.str(); //((MachineInstr*)Node->getInst());
76 }
77 else
78 return "No Inst";
79 }
80 static std::string getEdgeSourceLabel(MSchedGraphNode *Node,
81 MSchedGraphNode::succ_iterator I) {
82 //Label each edge with the type of dependence
83 std::string edgelabel = "";
84 switch (I.getEdge().getDepOrderType()) {
85
86 case MSchedGraphEdge::TrueDep:
87 edgelabel = "True";
88 break;
89
90 case MSchedGraphEdge::AntiDep:
91 edgelabel = "Anti";
92 break;
93
94 case MSchedGraphEdge::OutputDep:
95 edgelabel = "Output";
96 break;
97
98 default:
99 edgelabel = "Unknown";
100 break;
101 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000102
103 //FIXME
104 int iteDiff = I.getEdge().getIteDiff();
105 std::string intStr = "(IteDiff: ";
106 intStr += itostr(iteDiff);
107
108 intStr += ")";
109 edgelabel += intStr;
110
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000111 return edgelabel;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000112 }
Guochun Shif1c154f2003-03-27 17:57:44 +0000113 };
Guochun Shif1c154f2003-03-27 17:57:44 +0000114}
Tanya Lattner4f839cc2003-08-28 17:12:14 +0000115
Misha Brukmanaa41c3c2003-10-10 17:41:32 +0000116/// ModuloScheduling::runOnFunction - main transformation entry point
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000117/// The Swing Modulo Schedule algorithm has three basic steps:
118/// 1) Computation and Analysis of the dependence graph
119/// 2) Ordering of the nodes
120/// 3) Scheduling
121///
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000122bool ModuloSchedulingPass::runOnFunction(Function &F) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000123
Tanya Lattner4f839cc2003-08-28 17:12:14 +0000124 bool Changed = false;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000125
Tanya Lattner420025b2004-10-10 22:44:35 +0000126 DEBUG(std::cerr << "Creating ModuloSchedGraph for each valid BasicBlock in " + F.getName() + "\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000127
128 //Get MachineFunction
129 MachineFunction &MF = MachineFunction::get(&F);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000130
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000131 //Worklist
132 std::vector<MachineBasicBlock*> Worklist;
133
134 //Iterate over BasicBlocks and put them into our worklist if they are valid
135 for (MachineFunction::iterator BI = MF.begin(); BI != MF.end(); ++BI)
136 if(MachineBBisValid(BI))
137 Worklist.push_back(&*BI);
138
Tanya Lattner420025b2004-10-10 22:44:35 +0000139 DEBUG(if(Worklist.size() == 0) std::cerr << "No single basic block loops in function to ModuloSchedule\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000140
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000141 //Iterate over the worklist and perform scheduling
142 for(std::vector<MachineBasicBlock*>::iterator BI = Worklist.begin(),
143 BE = Worklist.end(); BI != BE; ++BI) {
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000144
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000145 MSchedGraph *MSG = new MSchedGraph(*BI, target);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000146
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000147 //Write Graph out to file
148 DEBUG(WriteGraphToFile(std::cerr, F.getName(), MSG));
149
150 //Print out BB for debugging
Tanya Lattner420025b2004-10-10 22:44:35 +0000151 DEBUG(std::cerr << "ModuloScheduling BB: \n"; (*BI)->print(std::cerr));
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000152
153 //Calculate Resource II
154 int ResMII = calculateResMII(*BI);
155
156 //Calculate Recurrence II
157 int RecMII = calculateRecMII(MSG, ResMII);
158
159 //Our starting initiation interval is the maximum of RecMII and ResMII
160 II = std::max(RecMII, ResMII);
161
162 //Print out II, RecMII, and ResMII
163 DEBUG(std::cerr << "II starts out as " << II << " ( RecMII=" << RecMII << "and ResMII=" << ResMII << "\n");
164
165 //Calculate Node Properties
166 calculateNodeAttributes(MSG, ResMII);
167
168 //Dump node properties if in debug mode
169 DEBUG(for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(),
170 E = nodeToAttributesMap.end(); I !=E; ++I) {
171 std::cerr << "Node: " << *(I->first) << " ASAP: " << I->second.ASAP << " ALAP: "
172 << I->second.ALAP << " MOB: " << I->second.MOB << " Depth: " << I->second.depth
173 << " Height: " << I->second.height << "\n";
174 });
175
176 //Put nodes in order to schedule them
177 computePartialOrder();
178
179 //Dump out partial order
180 DEBUG(for(std::vector<std::vector<MSchedGraphNode*> >::iterator I = partialOrder.begin(),
181 E = partialOrder.end(); I !=E; ++I) {
182 std::cerr << "Start set in PO\n";
183 for(std::vector<MSchedGraphNode*>::iterator J = I->begin(), JE = I->end(); J != JE; ++J)
184 std::cerr << "PO:" << **J << "\n";
185 });
186
187 //Place nodes in final order
188 orderNodes();
189
190 //Dump out order of nodes
191 DEBUG(for(std::vector<MSchedGraphNode*>::iterator I = FinalNodeOrder.begin(), E = FinalNodeOrder.end(); I != E; ++I) {
192 std::cerr << "FO:" << **I << "\n";
193 });
194
195 //Finally schedule nodes
196 computeSchedule();
197
198 //Print out final schedule
199 DEBUG(schedule.print(std::cerr));
200
201
202 //Final scheduling step is to reconstruct the loop
203 reconstructLoop(*BI);
204
205 //Print out new loop
206
207
208 //Clear out our maps for the next basic block that is processed
209 nodeToAttributesMap.clear();
210 partialOrder.clear();
211 recurrenceList.clear();
212 FinalNodeOrder.clear();
213 schedule.clear();
Tanya Lattner420025b2004-10-10 22:44:35 +0000214
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000215 //Clean up. Nuke old MachineBB and llvmBB
216 //BasicBlock *llvmBB = (BasicBlock*) (*BI)->getBasicBlock();
217 //Function *parent = (Function*) llvmBB->getParent();
218 //Should't std::find work??
219 //parent->getBasicBlockList().erase(std::find(parent->getBasicBlockList().begin(), parent->getBasicBlockList().end(), *llvmBB));
220 //parent->getBasicBlockList().erase(llvmBB);
221
222 //delete(llvmBB);
223 //delete(*BI);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000224 }
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000225
226
Tanya Lattner4f839cc2003-08-28 17:12:14 +0000227 return Changed;
228}
Brian Gaeked0fde302003-11-11 22:41:34 +0000229
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000230
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000231/// This function checks if a Machine Basic Block is valid for modulo
232/// scheduling. This means that it has no control flow (if/else or
233/// calls) in the block. Currently ModuloScheduling only works on
234/// single basic block loops.
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000235bool ModuloSchedulingPass::MachineBBisValid(const MachineBasicBlock *BI) {
236
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000237 bool isLoop = false;
238
239 //Check first if its a valid loop
240 for(succ_const_iterator I = succ_begin(BI->getBasicBlock()),
241 E = succ_end(BI->getBasicBlock()); I != E; ++I) {
242 if (*I == BI->getBasicBlock()) // has single block loop
243 isLoop = true;
244 }
245
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000246 if(!isLoop)
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000247 return false;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000248
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000249 //Get Target machine instruction info
250 const TargetInstrInfo *TMI = target.getInstrInfo();
251
252 //Check each instruction and look for calls
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000253 for(MachineBasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000254 //Get opcode to check instruction type
255 MachineOpCode OC = I->getOpcode();
256 if(TMI->isCall(OC))
257 return false;
258
259 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000260 return true;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000261}
262
263//ResMII is calculated by determining the usage count for each resource
264//and using the maximum.
265//FIXME: In future there should be a way to get alternative resources
266//for each instruction
267int ModuloSchedulingPass::calculateResMII(const MachineBasicBlock *BI) {
268
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000269 const TargetInstrInfo *mii = target.getInstrInfo();
270 const TargetSchedInfo *msi = target.getSchedInfo();
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000271
272 int ResMII = 0;
273
274 //Map to keep track of usage count of each resource
275 std::map<unsigned, unsigned> resourceUsageCount;
276
277 for(MachineBasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) {
278
279 //Get resource usage for this instruction
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000280 InstrRUsage rUsage = msi->getInstrRUsage(I->getOpcode());
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000281 std::vector<std::vector<resourceId_t> > resources = rUsage.resourcesByCycle;
282
283 //Loop over resources in each cycle and increments their usage count
284 for(unsigned i=0; i < resources.size(); ++i)
285 for(unsigned j=0; j < resources[i].size(); ++j) {
286 if( resourceUsageCount.find(resources[i][j]) == resourceUsageCount.end()) {
287 resourceUsageCount[resources[i][j]] = 1;
288 }
289 else {
290 resourceUsageCount[resources[i][j]] = resourceUsageCount[resources[i][j]] + 1;
291 }
292 }
293 }
294
295 //Find maximum usage count
296
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000297 //Get max number of instructions that can be issued at once. (FIXME)
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000298 int issueSlots = msi->maxNumIssueTotal;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000299
300 for(std::map<unsigned,unsigned>::iterator RB = resourceUsageCount.begin(), RE = resourceUsageCount.end(); RB != RE; ++RB) {
Tanya Lattner4cffb582004-05-26 06:27:18 +0000301
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000302 //Get the total number of the resources in our cpu
Tanya Lattner4cffb582004-05-26 06:27:18 +0000303 int resourceNum = CPUResource::getCPUResource(RB->first)->maxNumUsers;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000304
305 //Get total usage count for this resources
306 unsigned usageCount = RB->second;
307
308 //Divide the usage count by either the max number we can issue or the number of
309 //resources (whichever is its upper bound)
310 double finalUsageCount;
Tanya Lattner4cffb582004-05-26 06:27:18 +0000311 if( resourceNum <= issueSlots)
312 finalUsageCount = ceil(1.0 * usageCount / resourceNum);
313 else
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000314 finalUsageCount = ceil(1.0 * usageCount / issueSlots);
315
316
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000317 //Only keep track of the max
318 ResMII = std::max( (int) finalUsageCount, ResMII);
319
320 }
321
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000322 return ResMII;
323
324}
325
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000326/// calculateRecMII - Calculates the value of the highest recurrence
327/// By value we mean the total latency
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000328int ModuloSchedulingPass::calculateRecMII(MSchedGraph *graph, int MII) {
329 std::vector<MSchedGraphNode*> vNodes;
330 //Loop over all nodes in the graph
331 for(MSchedGraph::iterator I = graph->begin(), E = graph->end(); I != E; ++I) {
332 findAllReccurrences(I->second, vNodes, MII);
333 vNodes.clear();
334 }
335
336 int RecMII = 0;
337
338 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::iterator I = recurrenceList.begin(), E=recurrenceList.end(); I !=E; ++I) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000339 DEBUG(for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000340 std::cerr << **N << "\n";
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000341 });
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000342 RecMII = std::max(RecMII, I->first);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000343 }
344
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000345 return MII;
346}
347
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000348/// calculateNodeAttributes - The following properties are calculated for
349/// each node in the dependence graph: ASAP, ALAP, Depth, Height, and
350/// MOB.
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000351void ModuloSchedulingPass::calculateNodeAttributes(MSchedGraph *graph, int MII) {
352
353 //Loop over the nodes and add them to the map
354 for(MSchedGraph::iterator I = graph->begin(), E = graph->end(); I != E; ++I) {
355 //Assert if its already in the map
356 assert(nodeToAttributesMap.find(I->second) == nodeToAttributesMap.end() && "Node attributes are already in the map");
357
358 //Put into the map with default attribute values
359 nodeToAttributesMap[I->second] = MSNodeAttributes();
360 }
361
362 //Create set to deal with reccurrences
363 std::set<MSchedGraphNode*> visitedNodes;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000364
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000365 //Now Loop over map and calculate the node attributes
366 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000367 calculateASAP(I->first, MII, (MSchedGraphNode*) 0);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000368 visitedNodes.clear();
369 }
370
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000371 int maxASAP = findMaxASAP();
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000372 //Calculate ALAP which depends on ASAP being totally calculated
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000373 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
374 calculateALAP(I->first, MII, maxASAP, (MSchedGraphNode*) 0);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000375 visitedNodes.clear();
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000376 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000377
378 //Calculate MOB which depends on ASAP being totally calculated, also do depth and height
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000379 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
380 (I->second).MOB = std::max(0,(I->second).ALAP - (I->second).ASAP);
381
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000382 DEBUG(std::cerr << "MOB: " << (I->second).MOB << " (" << *(I->first) << ")\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000383 calculateDepth(I->first, (MSchedGraphNode*) 0);
384 calculateHeight(I->first, (MSchedGraphNode*) 0);
385 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000386
387
388}
389
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000390/// ignoreEdge - Checks to see if this edge of a recurrence should be ignored or not
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000391bool ModuloSchedulingPass::ignoreEdge(MSchedGraphNode *srcNode, MSchedGraphNode *destNode) {
392 if(destNode == 0 || srcNode ==0)
393 return false;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000394
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000395 bool findEdge = edgesToIgnore.count(std::make_pair(srcNode, destNode->getInEdgeNum(srcNode)));
Tanya Lattner4cffb582004-05-26 06:27:18 +0000396
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000397 return findEdge;
398}
399
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000400
401/// calculateASAP - Calculates the
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000402int ModuloSchedulingPass::calculateASAP(MSchedGraphNode *node, int MII, MSchedGraphNode *destNode) {
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000403
404 DEBUG(std::cerr << "Calculating ASAP for " << *node << "\n");
405
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000406 //Get current node attributes
407 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
408
409 if(attributes.ASAP != -1)
410 return attributes.ASAP;
411
412 int maxPredValue = 0;
413
414 //Iterate over all of the predecessors and find max
415 for(MSchedGraphNode::pred_iterator P = node->pred_begin(), E = node->pred_end(); P != E; ++P) {
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000416
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000417 //Only process if we are not ignoring the edge
418 if(!ignoreEdge(*P, node)) {
419 int predASAP = -1;
420 predASAP = calculateASAP(*P, MII, node);
421
422 assert(predASAP != -1 && "ASAP has not been calculated");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000423 int iteDiff = node->getInEdge(*P).getIteDiff();
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000424
425 int currentPredValue = predASAP + (*P)->getLatency() - (iteDiff * MII);
426 DEBUG(std::cerr << "pred ASAP: " << predASAP << ", iteDiff: " << iteDiff << ", PredLatency: " << (*P)->getLatency() << ", Current ASAP pred: " << currentPredValue << "\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000427 maxPredValue = std::max(maxPredValue, currentPredValue);
428 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000429 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000430
431 attributes.ASAP = maxPredValue;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000432
433 DEBUG(std::cerr << "ASAP: " << attributes.ASAP << " (" << *node << ")\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000434
435 return maxPredValue;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000436}
437
438
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000439int ModuloSchedulingPass::calculateALAP(MSchedGraphNode *node, int MII,
440 int maxASAP, MSchedGraphNode *srcNode) {
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000441
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000442 DEBUG(std::cerr << "Calculating ALAP for " << *node << "\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000443
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000444 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
445
446 if(attributes.ALAP != -1)
447 return attributes.ALAP;
448
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000449 if(node->hasSuccessors()) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000450
451 //Trying to deal with the issue where the node has successors, but
452 //we are ignoring all of the edges to them. So this is my hack for
453 //now.. there is probably a more elegant way of doing this (FIXME)
454 bool processedOneEdge = false;
455
456 //FIXME, set to something high to start
457 int minSuccValue = 9999999;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000458
459 //Iterate over all of the predecessors and fine max
460 for(MSchedGraphNode::succ_iterator P = node->succ_begin(),
461 E = node->succ_end(); P != E; ++P) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000462
463 //Only process if we are not ignoring the edge
464 if(!ignoreEdge(node, *P)) {
465 processedOneEdge = true;
466 int succALAP = -1;
467 succALAP = calculateALAP(*P, MII, maxASAP, node);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000468
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000469 assert(succALAP != -1 && "Successors ALAP should have been caclulated");
470
471 int iteDiff = P.getEdge().getIteDiff();
472
473 int currentSuccValue = succALAP - node->getLatency() + iteDiff * MII;
474
475 DEBUG(std::cerr << "succ ALAP: " << succALAP << ", iteDiff: " << iteDiff << ", SuccLatency: " << (*P)->getLatency() << ", Current ALAP succ: " << currentSuccValue << "\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000476
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000477 minSuccValue = std::min(minSuccValue, currentSuccValue);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000478 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000479 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000480
481 if(processedOneEdge)
482 attributes.ALAP = minSuccValue;
483
484 else
485 attributes.ALAP = maxASAP;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000486 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000487 else
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000488 attributes.ALAP = maxASAP;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000489
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000490 DEBUG(std::cerr << "ALAP: " << attributes.ALAP << " (" << *node << ")\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000491
492 if(attributes.ALAP < 0)
493 attributes.ALAP = 0;
494
495 return attributes.ALAP;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000496}
497
498int ModuloSchedulingPass::findMaxASAP() {
499 int maxASAP = 0;
500
501 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(),
502 E = nodeToAttributesMap.end(); I != E; ++I)
503 maxASAP = std::max(maxASAP, I->second.ASAP);
504 return maxASAP;
505}
506
507
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000508int ModuloSchedulingPass::calculateHeight(MSchedGraphNode *node,MSchedGraphNode *srcNode) {
509
510 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000511
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000512 if(attributes.height != -1)
513 return attributes.height;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000514
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000515 int maxHeight = 0;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000516
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000517 //Iterate over all of the predecessors and find max
518 for(MSchedGraphNode::succ_iterator P = node->succ_begin(),
519 E = node->succ_end(); P != E; ++P) {
520
521
522 if(!ignoreEdge(node, *P)) {
523 int succHeight = calculateHeight(*P, node);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000524
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000525 assert(succHeight != -1 && "Successors Height should have been caclulated");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000526
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000527 int currentHeight = succHeight + node->getLatency();
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000528 maxHeight = std::max(maxHeight, currentHeight);
529 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000530 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000531 attributes.height = maxHeight;
532 DEBUG(std::cerr << "Height: " << attributes.height << " (" << *node << ")\n");
533 return maxHeight;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000534}
535
536
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000537int ModuloSchedulingPass::calculateDepth(MSchedGraphNode *node,
538 MSchedGraphNode *destNode) {
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000539
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000540 MSNodeAttributes &attributes = nodeToAttributesMap.find(node)->second;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000541
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000542 if(attributes.depth != -1)
543 return attributes.depth;
544
545 int maxDepth = 0;
546
547 //Iterate over all of the predecessors and fine max
548 for(MSchedGraphNode::pred_iterator P = node->pred_begin(), E = node->pred_end(); P != E; ++P) {
549
550 if(!ignoreEdge(*P, node)) {
551 int predDepth = -1;
552 predDepth = calculateDepth(*P, node);
553
554 assert(predDepth != -1 && "Predecessors ASAP should have been caclulated");
555
556 int currentDepth = predDepth + (*P)->getLatency();
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000557 maxDepth = std::max(maxDepth, currentDepth);
558 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000559 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000560 attributes.depth = maxDepth;
561
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000562 DEBUG(std::cerr << "Depth: " << attributes.depth << " (" << *node << "*)\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000563 return maxDepth;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000564}
565
566
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000567
568void ModuloSchedulingPass::addReccurrence(std::vector<MSchedGraphNode*> &recurrence, int II, MSchedGraphNode *srcBENode, MSchedGraphNode *destBENode) {
569 //Check to make sure that this recurrence is unique
570 bool same = false;
571
572
573 //Loop over all recurrences already in our list
574 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::iterator R = recurrenceList.begin(), RE = recurrenceList.end(); R != RE; ++R) {
575
576 bool all_same = true;
577 //First compare size
578 if(R->second.size() == recurrence.size()) {
579
580 for(std::vector<MSchedGraphNode*>::const_iterator node = R->second.begin(), end = R->second.end(); node != end; ++node) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000581 if(std::find(recurrence.begin(), recurrence.end(), *node) == recurrence.end()) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000582 all_same = all_same && false;
583 break;
584 }
585 else
586 all_same = all_same && true;
587 }
588 if(all_same) {
589 same = true;
590 break;
591 }
592 }
593 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000594
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000595 if(!same) {
Tanya Lattner4cffb582004-05-26 06:27:18 +0000596 srcBENode = recurrence.back();
597 destBENode = recurrence.front();
598
599 //FIXME
600 if(destBENode->getInEdge(srcBENode).getIteDiff() == 0) {
601 //DEBUG(std::cerr << "NOT A BACKEDGE\n");
602 //find actual backedge HACK HACK
603 for(unsigned i=0; i< recurrence.size()-1; ++i) {
604 if(recurrence[i+1]->getInEdge(recurrence[i]).getIteDiff() == 1) {
605 srcBENode = recurrence[i];
606 destBENode = recurrence[i+1];
607 break;
608 }
609
610 }
611
612 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000613 DEBUG(std::cerr << "Back Edge to Remove: " << *srcBENode << " to " << *destBENode << "\n");
614 edgesToIgnore.insert(std::make_pair(srcBENode, destBENode->getInEdgeNum(srcBENode)));
615 recurrenceList.insert(std::make_pair(II, recurrence));
616 }
617
618}
619
620void ModuloSchedulingPass::findAllReccurrences(MSchedGraphNode *node,
621 std::vector<MSchedGraphNode*> &visitedNodes,
622 int II) {
623
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000624 if(std::find(visitedNodes.begin(), visitedNodes.end(), node) != visitedNodes.end()) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000625 std::vector<MSchedGraphNode*> recurrence;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000626 bool first = true;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000627 int delay = 0;
628 int distance = 0;
629 int RecMII = II; //Starting value
630 MSchedGraphNode *last = node;
Chris Lattner46c2b3a2004-08-04 03:51:55 +0000631 MSchedGraphNode *srcBackEdge = 0;
632 MSchedGraphNode *destBackEdge = 0;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000633
634
635
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000636 for(std::vector<MSchedGraphNode*>::iterator I = visitedNodes.begin(), E = visitedNodes.end();
637 I !=E; ++I) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000638
639 if(*I == node)
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000640 first = false;
641 if(first)
642 continue;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000643
644 delay = delay + (*I)->getLatency();
645
646 if(*I != node) {
647 int diff = (*I)->getInEdge(last).getIteDiff();
648 distance += diff;
649 if(diff > 0) {
650 srcBackEdge = last;
651 destBackEdge = *I;
652 }
653 }
654
655 recurrence.push_back(*I);
656 last = *I;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000657 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000658
659
660
661 //Get final distance calc
662 distance += node->getInEdge(last).getIteDiff();
663
664
665 //Adjust II until we get close to the inequality delay - II*distance <= 0
666
667 int value = delay-(RecMII * distance);
668 int lastII = II;
669 while(value <= 0) {
670
671 lastII = RecMII;
672 RecMII--;
673 value = delay-(RecMII * distance);
674 }
675
676
677 DEBUG(std::cerr << "Final II for this recurrence: " << lastII << "\n");
678 addReccurrence(recurrence, lastII, srcBackEdge, destBackEdge);
679 assert(distance != 0 && "Recurrence distance should not be zero");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000680 return;
681 }
682
683 for(MSchedGraphNode::succ_iterator I = node->succ_begin(), E = node->succ_end(); I != E; ++I) {
684 visitedNodes.push_back(node);
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000685 findAllReccurrences(*I, visitedNodes, II);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000686 visitedNodes.pop_back();
687 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000688}
689
690
691
692
693
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000694void ModuloSchedulingPass::computePartialOrder() {
695
696
697 //Loop over all recurrences and add to our partial order
698 //be sure to remove nodes that are already in the partial order in
699 //a different recurrence and don't add empty recurrences.
700 for(std::set<std::pair<int, std::vector<MSchedGraphNode*> > >::reverse_iterator I = recurrenceList.rbegin(), E=recurrenceList.rend(); I !=E; ++I) {
701
702 //Add nodes that connect this recurrence to the previous recurrence
703
704 //If this is the first recurrence in the partial order, add all predecessors
705 for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000706
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000707 }
708
709
710 std::vector<MSchedGraphNode*> new_recurrence;
711 //Loop through recurrence and remove any nodes already in the partial order
712 for(std::vector<MSchedGraphNode*>::const_iterator N = I->second.begin(), NE = I->second.end(); N != NE; ++N) {
713 bool found = false;
714 for(std::vector<std::vector<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PE = partialOrder.end(); PO != PE; ++PO) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000715 if(std::find(PO->begin(), PO->end(), *N) != PO->end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000716 found = true;
717 }
718 if(!found) {
719 new_recurrence.push_back(*N);
720
721 if(partialOrder.size() == 0)
722 //For each predecessors, add it to this recurrence ONLY if it is not already in it
723 for(MSchedGraphNode::pred_iterator P = (*N)->pred_begin(),
724 PE = (*N)->pred_end(); P != PE; ++P) {
725
726 //Check if we are supposed to ignore this edge or not
727 if(!ignoreEdge(*P, *N))
728 //Check if already in this recurrence
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000729 if(std::find(I->second.begin(), I->second.end(), *P) == I->second.end()) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000730 //Also need to check if in partial order
731 bool predFound = false;
732 for(std::vector<std::vector<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PEND = partialOrder.end(); PO != PEND; ++PO) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000733 if(std::find(PO->begin(), PO->end(), *P) != PO->end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000734 predFound = true;
735 }
736
737 if(!predFound)
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000738 if(std::find(new_recurrence.begin(), new_recurrence.end(), *P) == new_recurrence.end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000739 new_recurrence.push_back(*P);
740
741 }
742 }
743 }
744 }
745
746
747 if(new_recurrence.size() > 0)
748 partialOrder.push_back(new_recurrence);
749 }
750
751 //Add any nodes that are not already in the partial order
752 std::vector<MSchedGraphNode*> lastNodes;
753 for(std::map<MSchedGraphNode*, MSNodeAttributes>::iterator I = nodeToAttributesMap.begin(), E = nodeToAttributesMap.end(); I != E; ++I) {
754 bool found = false;
755 //Check if its already in our partial order, if not add it to the final vector
756 for(std::vector<std::vector<MSchedGraphNode*> >::iterator PO = partialOrder.begin(), PE = partialOrder.end(); PO != PE; ++PO) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000757 if(std::find(PO->begin(), PO->end(), I->first) != PO->end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000758 found = true;
759 }
760 if(!found)
761 lastNodes.push_back(I->first);
762 }
763
764 if(lastNodes.size() > 0)
765 partialOrder.push_back(lastNodes);
766
767}
768
769
770void ModuloSchedulingPass::predIntersect(std::vector<MSchedGraphNode*> &CurrentSet, std::vector<MSchedGraphNode*> &IntersectResult) {
771
772 //Sort CurrentSet so we can use lowerbound
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000773 std::sort(CurrentSet.begin(), CurrentSet.end());
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000774
775 for(unsigned j=0; j < FinalNodeOrder.size(); ++j) {
776 for(MSchedGraphNode::pred_iterator P = FinalNodeOrder[j]->pred_begin(),
777 E = FinalNodeOrder[j]->pred_end(); P != E; ++P) {
778
779 //Check if we are supposed to ignore this edge or not
780 if(ignoreEdge(*P,FinalNodeOrder[j]))
781 continue;
782
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000783 if(std::find(CurrentSet.begin(),
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000784 CurrentSet.end(), *P) != CurrentSet.end())
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000785 if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), *P) == FinalNodeOrder.end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000786 IntersectResult.push_back(*P);
787 }
788 }
789}
790
791void ModuloSchedulingPass::succIntersect(std::vector<MSchedGraphNode*> &CurrentSet, std::vector<MSchedGraphNode*> &IntersectResult) {
792
793 //Sort CurrentSet so we can use lowerbound
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000794 std::sort(CurrentSet.begin(), CurrentSet.end());
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000795
796 for(unsigned j=0; j < FinalNodeOrder.size(); ++j) {
797 for(MSchedGraphNode::succ_iterator P = FinalNodeOrder[j]->succ_begin(),
798 E = FinalNodeOrder[j]->succ_end(); P != E; ++P) {
799
800 //Check if we are supposed to ignore this edge or not
801 if(ignoreEdge(FinalNodeOrder[j],*P))
802 continue;
803
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000804 if(std::find(CurrentSet.begin(),
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000805 CurrentSet.end(), *P) != CurrentSet.end())
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000806 if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), *P) == FinalNodeOrder.end())
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000807 IntersectResult.push_back(*P);
808 }
809 }
810}
811
812void dumpIntersection(std::vector<MSchedGraphNode*> &IntersectCurrent) {
813 std::cerr << "Intersection (";
814 for(std::vector<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(), E = IntersectCurrent.end(); I != E; ++I)
815 std::cerr << **I << ", ";
816 std::cerr << ")\n";
817}
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000818
819
820
821void ModuloSchedulingPass::orderNodes() {
822
823 int BOTTOM_UP = 0;
824 int TOP_DOWN = 1;
825
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000826 //Set default order
827 int order = BOTTOM_UP;
828
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000829
830 //Loop over all the sets and place them in the final node order
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000831 for(std::vector<std::vector<MSchedGraphNode*> >::iterator CurrentSet = partialOrder.begin(), E= partialOrder.end(); CurrentSet != E; ++CurrentSet) {
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000832
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000833 DEBUG(std::cerr << "Processing set in S\n");
Tanya Lattner0a88d2d2004-07-30 23:36:10 +0000834 DEBUG(dumpIntersection(*CurrentSet));
835
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000836 //Result of intersection
837 std::vector<MSchedGraphNode*> IntersectCurrent;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000838
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000839 predIntersect(*CurrentSet, IntersectCurrent);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000840
841 //If the intersection of predecessor and current set is not empty
842 //sort nodes bottom up
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000843 if(IntersectCurrent.size() != 0) {
844 DEBUG(std::cerr << "Final Node Order Predecessors and Current Set interesection is NOT empty\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000845 order = BOTTOM_UP;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000846 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000847 //If empty, use successors
848 else {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000849 DEBUG(std::cerr << "Final Node Order Predecessors and Current Set interesection is empty\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000850
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000851 succIntersect(*CurrentSet, IntersectCurrent);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000852
853 //sort top-down
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000854 if(IntersectCurrent.size() != 0) {
855 DEBUG(std::cerr << "Final Node Order Successors and Current Set interesection is NOT empty\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000856 order = TOP_DOWN;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000857 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000858 else {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000859 DEBUG(std::cerr << "Final Node Order Successors and Current Set interesection is empty\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000860 //Find node with max ASAP in current Set
861 MSchedGraphNode *node;
862 int maxASAP = 0;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000863 DEBUG(std::cerr << "Using current set of size " << CurrentSet->size() << "to find max ASAP\n");
864 for(unsigned j=0; j < CurrentSet->size(); ++j) {
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000865 //Get node attributes
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000866 MSNodeAttributes nodeAttr= nodeToAttributesMap.find((*CurrentSet)[j])->second;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000867 //assert(nodeAttr != nodeToAttributesMap.end() && "Node not in attributes map!");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000868 DEBUG(std::cerr << "CurrentSet index " << j << "has ASAP: " << nodeAttr.ASAP << "\n");
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000869 if(maxASAP < nodeAttr.ASAP) {
870 maxASAP = nodeAttr.ASAP;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000871 node = (*CurrentSet)[j];
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000872 }
873 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000874 assert(node != 0 && "In node ordering node should not be null");
875 IntersectCurrent.push_back(node);
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000876 order = BOTTOM_UP;
877 }
878 }
879
880 //Repeat until all nodes are put into the final order from current set
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000881 while(IntersectCurrent.size() > 0) {
882
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000883 if(order == TOP_DOWN) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000884 DEBUG(std::cerr << "Order is TOP DOWN\n");
885
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000886 while(IntersectCurrent.size() > 0) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000887 DEBUG(std::cerr << "Intersection is not empty, so find heighest height\n");
888
889 int MOB = 0;
890 int height = 0;
891 MSchedGraphNode *highestHeightNode = IntersectCurrent[0];
892
893 //Find node in intersection with highest heigh and lowest MOB
894 for(std::vector<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(),
895 E = IntersectCurrent.end(); I != E; ++I) {
896
897 //Get current nodes properties
898 MSNodeAttributes nodeAttr= nodeToAttributesMap.find(*I)->second;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000899
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000900 if(height < nodeAttr.height) {
901 highestHeightNode = *I;
902 height = nodeAttr.height;
903 MOB = nodeAttr.MOB;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000904 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000905 else if(height == nodeAttr.height) {
906 if(MOB > nodeAttr.height) {
907 highestHeightNode = *I;
908 height = nodeAttr.height;
909 MOB = nodeAttr.MOB;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000910 }
911 }
912 }
913
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000914 //Append our node with greatest height to the NodeOrder
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000915 if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), highestHeightNode) == FinalNodeOrder.end()) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000916 DEBUG(std::cerr << "Adding node to Final Order: " << *highestHeightNode << "\n");
917 FinalNodeOrder.push_back(highestHeightNode);
918 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000919
920 //Remove V from IntersectOrder
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000921 IntersectCurrent.erase(std::find(IntersectCurrent.begin(),
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000922 IntersectCurrent.end(), highestHeightNode));
923
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000924
925 //Intersect V's successors with CurrentSet
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000926 for(MSchedGraphNode::succ_iterator P = highestHeightNode->succ_begin(),
927 E = highestHeightNode->succ_end(); P != E; ++P) {
928 //if(lower_bound(CurrentSet->begin(),
929 // CurrentSet->end(), *P) != CurrentSet->end()) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000930 if(std::find(CurrentSet->begin(), CurrentSet->end(), *P) != CurrentSet->end()) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000931 if(ignoreEdge(highestHeightNode, *P))
932 continue;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000933 //If not already in Intersect, add
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000934 if(std::find(IntersectCurrent.begin(), IntersectCurrent.end(), *P) == IntersectCurrent.end())
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000935 IntersectCurrent.push_back(*P);
936 }
937 }
938 } //End while loop over Intersect Size
939
940 //Change direction
941 order = BOTTOM_UP;
942
943 //Reset Intersect to reflect changes in OrderNodes
944 IntersectCurrent.clear();
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000945 predIntersect(*CurrentSet, IntersectCurrent);
946
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000947 } //End If TOP_DOWN
948
949 //Begin if BOTTOM_UP
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000950 else {
951 DEBUG(std::cerr << "Order is BOTTOM UP\n");
952 while(IntersectCurrent.size() > 0) {
953 DEBUG(std::cerr << "Intersection of size " << IntersectCurrent.size() << ", finding highest depth\n");
954
955 //dump intersection
956 DEBUG(dumpIntersection(IntersectCurrent));
957 //Get node with highest depth, if a tie, use one with lowest
958 //MOB
959 int MOB = 0;
960 int depth = 0;
961 MSchedGraphNode *highestDepthNode = IntersectCurrent[0];
962
963 for(std::vector<MSchedGraphNode*>::iterator I = IntersectCurrent.begin(),
964 E = IntersectCurrent.end(); I != E; ++I) {
965 //Find node attribute in graph
966 MSNodeAttributes nodeAttr= nodeToAttributesMap.find(*I)->second;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000967
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000968 if(depth < nodeAttr.depth) {
969 highestDepthNode = *I;
970 depth = nodeAttr.depth;
971 MOB = nodeAttr.MOB;
972 }
973 else if(depth == nodeAttr.depth) {
974 if(MOB > nodeAttr.MOB) {
975 highestDepthNode = *I;
976 depth = nodeAttr.depth;
977 MOB = nodeAttr.MOB;
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000978 }
979 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000980 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000981
Tanya Lattnerd14b8372004-03-01 02:50:01 +0000982
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000983
984 //Append highest depth node to the NodeOrder
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000985 if(std::find(FinalNodeOrder.begin(), FinalNodeOrder.end(), highestDepthNode) == FinalNodeOrder.end()) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000986 DEBUG(std::cerr << "Adding node to Final Order: " << *highestDepthNode << "\n");
987 FinalNodeOrder.push_back(highestDepthNode);
988 }
989 //Remove heightestDepthNode from IntersectOrder
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000990 IntersectCurrent.erase(std::find(IntersectCurrent.begin(),
Tanya Lattner73e3e2e2004-05-08 16:12:10 +0000991 IntersectCurrent.end(),highestDepthNode));
992
993
994 //Intersect heightDepthNode's pred with CurrentSet
995 for(MSchedGraphNode::pred_iterator P = highestDepthNode->pred_begin(),
996 E = highestDepthNode->pred_end(); P != E; ++P) {
997 //if(lower_bound(CurrentSet->begin(),
998 // CurrentSet->end(), *P) != CurrentSet->end()) {
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +0000999 if(std::find(CurrentSet->begin(), CurrentSet->end(), *P) != CurrentSet->end()) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001000
1001 if(ignoreEdge(*P, highestDepthNode))
1002 continue;
1003
1004 //If not already in Intersect, add
Alkis Evlogimenosc72c6172004-09-28 14:42:44 +00001005 if(std::find(IntersectCurrent.begin(),
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001006 IntersectCurrent.end(), *P) == IntersectCurrent.end())
Tanya Lattnerd14b8372004-03-01 02:50:01 +00001007 IntersectCurrent.push_back(*P);
1008 }
Tanya Lattnerd14b8372004-03-01 02:50:01 +00001009 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001010
1011 } //End while loop over Intersect Size
1012
1013 //Change order
1014 order = TOP_DOWN;
1015
1016 //Reset IntersectCurrent to reflect changes in OrderNodes
1017 IntersectCurrent.clear();
1018 succIntersect(*CurrentSet, IntersectCurrent);
Tanya Lattnerd14b8372004-03-01 02:50:01 +00001019 } //End if BOTTOM_DOWN
1020
Tanya Lattner420025b2004-10-10 22:44:35 +00001021 DEBUG(std::cerr << "Current Intersection Size: " << IntersectCurrent.size() << "\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001022 }
1023 //End Wrapping while loop
Tanya Lattner420025b2004-10-10 22:44:35 +00001024 DEBUG(std::cerr << "Ending Size of Current Set: " << CurrentSet->size() << "\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001025 }//End for over all sets of nodes
Tanya Lattner420025b2004-10-10 22:44:35 +00001026
1027 //FIXME: As the algorithm stands it will NEVER add an instruction such as ba (with no
1028 //data dependencies) to the final order. We add this manually. It will always be
1029 //in the last set of S since its not part of a recurrence
1030 //Loop over all the sets and place them in the final node order
1031 std::vector<std::vector<MSchedGraphNode*> > ::reverse_iterator LastSet = partialOrder.rbegin();
1032 for(std::vector<MSchedGraphNode*>::iterator CurrentNode = LastSet->begin(), LastNode = LastSet->end();
1033 CurrentNode != LastNode; ++CurrentNode) {
1034 if((*CurrentNode)->getInst()->getOpcode() == V9::BA)
1035 FinalNodeOrder.push_back(*CurrentNode);
1036 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001037 //Return final Order
1038 //return FinalNodeOrder;
1039}
1040
1041void ModuloSchedulingPass::computeSchedule() {
1042
1043 bool success = false;
1044
1045 while(!success) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001046
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001047 //Loop over the final node order and process each node
1048 for(std::vector<MSchedGraphNode*>::iterator I = FinalNodeOrder.begin(),
1049 E = FinalNodeOrder.end(); I != E; ++I) {
1050
1051 //CalculateEarly and Late start
1052 int EarlyStart = -1;
1053 int LateStart = 99999; //Set to something higher then we would ever expect (FIXME)
1054 bool hasSucc = false;
1055 bool hasPred = false;
Tanya Lattner4cffb582004-05-26 06:27:18 +00001056
1057 if(!(*I)->isBranch()) {
1058 //Loop over nodes in the schedule and determine if they are predecessors
1059 //or successors of the node we are trying to schedule
1060 for(MSSchedule::schedule_iterator nodesByCycle = schedule.begin(), nodesByCycleEnd = schedule.end();
1061 nodesByCycle != nodesByCycleEnd; ++nodesByCycle) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001062
Tanya Lattner4cffb582004-05-26 06:27:18 +00001063 //For this cycle, get the vector of nodes schedule and loop over it
1064 for(std::vector<MSchedGraphNode*>::iterator schedNode = nodesByCycle->second.begin(), SNE = nodesByCycle->second.end(); schedNode != SNE; ++schedNode) {
1065
1066 if((*I)->isPredecessor(*schedNode)) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001067 if(!ignoreEdge(*schedNode, *I)) {
1068 int diff = (*I)->getInEdge(*schedNode).getIteDiff();
Tanya Lattner4cffb582004-05-26 06:27:18 +00001069 int ES_Temp = nodesByCycle->first + (*schedNode)->getLatency() - diff * II;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001070 DEBUG(std::cerr << "Diff: " << diff << " Cycle: " << nodesByCycle->first << "\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001071 DEBUG(std::cerr << "Temp EarlyStart: " << ES_Temp << " Prev EarlyStart: " << EarlyStart << "\n");
1072 EarlyStart = std::max(EarlyStart, ES_Temp);
1073 hasPred = true;
1074 }
1075 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001076 if((*I)->isSuccessor(*schedNode)) {
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001077 if(!ignoreEdge(*I,*schedNode)) {
1078 int diff = (*schedNode)->getInEdge(*I).getIteDiff();
Tanya Lattner4cffb582004-05-26 06:27:18 +00001079 int LS_Temp = nodesByCycle->first - (*I)->getLatency() + diff * II;
1080 DEBUG(std::cerr << "Diff: " << diff << " Cycle: " << nodesByCycle->first << "\n");
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001081 DEBUG(std::cerr << "Temp LateStart: " << LS_Temp << " Prev LateStart: " << LateStart << "\n");
1082 LateStart = std::min(LateStart, LS_Temp);
1083 hasSucc = true;
1084 }
1085 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001086 }
1087 }
1088 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001089 else {
1090 //WARNING: HACK! FIXME!!!!
Tanya Lattner420025b2004-10-10 22:44:35 +00001091 if((*I)->getInst()->getOpcode() == V9::BA) {
1092 EarlyStart = II-1;
1093 LateStart = II-1;
1094 }
1095 else {
1096 EarlyStart = II-1;
1097 LateStart = II-1;
1098 assert( (EarlyStart >= 0) && (LateStart >=0) && "EarlyStart and LateStart must be greater then 0");
1099 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001100 hasPred = 1;
1101 hasSucc = 1;
1102 }
1103
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001104
1105 DEBUG(std::cerr << "Has Successors: " << hasSucc << ", Has Pred: " << hasPred << "\n");
1106 DEBUG(std::cerr << "EarlyStart: " << EarlyStart << ", LateStart: " << LateStart << "\n");
1107
1108 //Check if the node has no pred or successors and set Early Start to its ASAP
1109 if(!hasSucc && !hasPred)
1110 EarlyStart = nodeToAttributesMap.find(*I)->second.ASAP;
1111
1112 //Now, try to schedule this node depending upon its pred and successor in the schedule
1113 //already
1114 if(!hasSucc && hasPred)
1115 success = scheduleNode(*I, EarlyStart, (EarlyStart + II -1));
1116 else if(!hasPred && hasSucc)
1117 success = scheduleNode(*I, LateStart, (LateStart - II +1));
1118 else if(hasPred && hasSucc)
1119 success = scheduleNode(*I, EarlyStart, std::min(LateStart, (EarlyStart + II -1)));
1120 else
1121 success = scheduleNode(*I, EarlyStart, EarlyStart + II - 1);
1122
1123 if(!success) {
1124 ++II;
1125 schedule.clear();
1126 break;
1127 }
1128
1129 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001130
1131 DEBUG(std::cerr << "Constructing Kernel\n");
1132 success = schedule.constructKernel(II);
1133 if(!success) {
1134 ++II;
1135 schedule.clear();
1136 }
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001137 }
1138}
1139
1140
1141bool ModuloSchedulingPass::scheduleNode(MSchedGraphNode *node,
1142 int start, int end) {
1143 bool success = false;
1144
1145 DEBUG(std::cerr << *node << " (Start Cycle: " << start << ", End Cycle: " << end << ")\n");
1146
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001147 //Make sure start and end are not negative
1148 if(start < 0)
1149 start = 0;
1150 if(end < 0)
1151 end = 0;
1152
1153 bool forward = true;
1154 if(start > end)
1155 forward = false;
1156
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001157 bool increaseSC = true;
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001158 int cycle = start ;
1159
1160
1161 while(increaseSC) {
1162
1163 increaseSC = false;
1164
Tanya Lattner4cffb582004-05-26 06:27:18 +00001165 increaseSC = schedule.insert(node, cycle);
1166
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001167 if(!increaseSC)
1168 return true;
1169
1170 //Increment cycle to try again
1171 if(forward) {
1172 ++cycle;
1173 DEBUG(std::cerr << "Increase cycle: " << cycle << "\n");
1174 if(cycle > end)
1175 return false;
1176 }
1177 else {
1178 --cycle;
1179 DEBUG(std::cerr << "Decrease cycle: " << cycle << "\n");
1180 if(cycle < end)
1181 return false;
1182 }
1183 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001184
Tanya Lattner73e3e2e2004-05-08 16:12:10 +00001185 return success;
Tanya Lattnerd14b8372004-03-01 02:50:01 +00001186}
Tanya Lattner4cffb582004-05-26 06:27:18 +00001187
Tanya Lattner420025b2004-10-10 22:44:35 +00001188void ModuloSchedulingPass::writePrologues(std::vector<MachineBasicBlock *> &prologues, MachineBasicBlock *origBB, std::vector<BasicBlock*> &llvm_prologues, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues, std::map<Value*, MachineBasicBlock*> &newValLocation) {
Tanya Lattner4cffb582004-05-26 06:27:18 +00001189
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001190 //Keep a map to easily know whats in the kernel
Tanya Lattner4cffb582004-05-26 06:27:18 +00001191 std::map<int, std::set<const MachineInstr*> > inKernel;
1192 int maxStageCount = 0;
1193
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001194 MSchedGraphNode *branch = 0;
Tanya Lattner420025b2004-10-10 22:44:35 +00001195 MSchedGraphNode *BAbranch = 0;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001196
Tanya Lattner4cffb582004-05-26 06:27:18 +00001197 for(MSSchedule::kernel_iterator I = schedule.kernel_begin(), E = schedule.kernel_end(); I != E; ++I) {
1198 maxStageCount = std::max(maxStageCount, I->second);
1199
1200 //Ignore the branch, we will handle this separately
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001201 if(I->first->isBranch()) {
Tanya Lattner420025b2004-10-10 22:44:35 +00001202 if (I->first->getInst()->getOpcode() == V9::BA)
1203 BAbranch = I->first;
1204 else
1205 branch = I->first;
Tanya Lattner4cffb582004-05-26 06:27:18 +00001206 continue;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001207 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001208
1209 //Put int the map so we know what instructions in each stage are in the kernel
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001210 DEBUG(std::cerr << "Inserting instruction " << *(I->first->getInst()) << " into map at stage " << I->second << "\n");
1211 inKernel[I->second].insert(I->first->getInst());
Tanya Lattner4cffb582004-05-26 06:27:18 +00001212 }
1213
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001214 //Get target information to look at machine operands
1215 const TargetInstrInfo *mii = target.getInstrInfo();
1216
1217 //Now write the prologues
1218 for(int i = 0; i < maxStageCount; ++i) {
1219 BasicBlock *llvmBB = new BasicBlock("PROLOGUE", (Function*) (origBB->getBasicBlock()->getParent()));
Tanya Lattner4cffb582004-05-26 06:27:18 +00001220 MachineBasicBlock *machineBB = new MachineBasicBlock(llvmBB);
1221
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001222 DEBUG(std::cerr << "i=" << i << "\n");
1223 for(int j = 0; j <= i; ++j) {
1224 for(MachineBasicBlock::const_iterator MI = origBB->begin(), ME = origBB->end(); ME != MI; ++MI) {
1225 if(inKernel[j].count(&*MI)) {
Tanya Lattner420025b2004-10-10 22:44:35 +00001226 MachineInstr *instClone = MI->clone();
1227 machineBB->push_back(instClone);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001228
Tanya Lattner420025b2004-10-10 22:44:35 +00001229 DEBUG(std::cerr << "Cloning: " << *MI << "\n");
1230
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001231 Instruction *tmp;
1232
1233 //After cloning, we may need to save the value that this instruction defines
1234 for(unsigned opNum=0; opNum < MI->getNumOperands(); ++opNum) {
1235 //get machine operand
Tanya Lattner420025b2004-10-10 22:44:35 +00001236 const MachineOperand &mOp = instClone->getOperand(opNum);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001237 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isDef()) {
1238
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001239 //Check if this is a value we should save
1240 if(valuesToSave.count(mOp.getVRegValue())) {
1241 //Save copy in tmpInstruction
1242 tmp = new TmpInstruction(mOp.getVRegValue());
1243
Tanya Lattner420025b2004-10-10 22:44:35 +00001244 DEBUG(std::cerr << "Value: " << *(mOp.getVRegValue()) << " New Value: " << *tmp << " Stage: " << i << "\n");
1245
1246 newValues[mOp.getVRegValue()][i]= tmp;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001247 newValLocation[tmp] = machineBB;
1248
Tanya Lattner420025b2004-10-10 22:44:35 +00001249 DEBUG(std::cerr << "Machine Instr Operands: " << *(mOp.getVRegValue()) << ", 0, " << *tmp << "\n");
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001250
1251 //Create machine instruction and put int machineBB
1252 MachineInstr *saveValue = BuildMI(machineBB, V9::ORr, 3).addReg(mOp.getVRegValue()).addImm(0).addRegDef(tmp);
1253
1254 DEBUG(std::cerr << "Created new machine instr: " << *saveValue << "\n");
1255 }
1256 }
Tanya Lattner420025b2004-10-10 22:44:35 +00001257
1258 //We may also need to update the value that we use if its from an earlier prologue
1259 if(j != 0) {
1260 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isUse()) {
1261 if(newValues.count(mOp.getVRegValue()))
1262 if(newValues[mOp.getVRegValue()].count(j-1)) {
1263 DEBUG(std::cerr << "Replaced this value: " << mOp.getVRegValue() << " With:" << (newValues[mOp.getVRegValue()][i-1]) << "\n");
1264 //Update the operand with the right value
1265 instClone->getOperand(opNum).setValueReg(newValues[mOp.getVRegValue()][i-1]);
1266 }
1267 }
1268 }
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001269 }
1270 }
Tanya Lattner20890832004-05-28 20:14:12 +00001271 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001272 }
1273
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001274
1275 //Stick in branch at the end
1276 machineBB->push_back(branch->getInst()->clone());
Tanya Lattner420025b2004-10-10 22:44:35 +00001277
1278 //Stick in BA branch at the end
1279 machineBB->push_back(BAbranch->getInst()->clone());
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001280
1281 (((MachineBasicBlock*)origBB)->getParent())->getBasicBlockList().push_back(machineBB);
Tanya Lattner4cffb582004-05-26 06:27:18 +00001282 prologues.push_back(machineBB);
1283 llvm_prologues.push_back(llvmBB);
1284 }
1285}
1286
Tanya Lattner420025b2004-10-10 22:44:35 +00001287void ModuloSchedulingPass::writeEpilogues(std::vector<MachineBasicBlock *> &epilogues, const MachineBasicBlock *origBB, std::vector<BasicBlock*> &llvm_epilogues, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues,std::map<Value*, MachineBasicBlock*> &newValLocation, std::map<Value*, std::map<int, Value*> > &kernelPHIs ) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001288
Tanya Lattner20890832004-05-28 20:14:12 +00001289 std::map<int, std::set<const MachineInstr*> > inKernel;
Tanya Lattner420025b2004-10-10 22:44:35 +00001290
Tanya Lattner20890832004-05-28 20:14:12 +00001291 for(MSSchedule::kernel_iterator I = schedule.kernel_begin(), E = schedule.kernel_end(); I != E; ++I) {
Tanya Lattner20890832004-05-28 20:14:12 +00001292
1293 //Ignore the branch, we will handle this separately
1294 if(I->first->isBranch())
1295 continue;
1296
1297 //Put int the map so we know what instructions in each stage are in the kernel
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001298 inKernel[I->second].insert(I->first->getInst());
Tanya Lattner20890832004-05-28 20:14:12 +00001299 }
1300
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001301 std::map<Value*, Value*> valPHIs;
1302
Tanya Lattner420025b2004-10-10 22:44:35 +00001303 //some debug stuff, will remove later
1304 DEBUG(for(std::map<Value*, std::map<int, Value*> >::iterator V = newValues.begin(), E = newValues.end(); V !=E; ++V) {
1305 std::cerr << "Old Value: " << *(V->first) << "\n";
1306 for(std::map<int, Value*>::iterator I = V->second.begin(), IE = V->second.end(); I != IE; ++I)
1307 std::cerr << "Stage: " << I->first << " Value: " << *(I->second) << "\n";
1308 });
1309
1310 //some debug stuff, will remove later
1311 DEBUG(for(std::map<Value*, std::map<int, Value*> >::iterator V = kernelPHIs.begin(), E = kernelPHIs.end(); V !=E; ++V) {
1312 std::cerr << "Old Value: " << *(V->first) << "\n";
1313 for(std::map<int, Value*>::iterator I = V->second.begin(), IE = V->second.end(); I != IE; ++I)
1314 std::cerr << "Stage: " << I->first << " Value: " << *(I->second) << "\n";
1315 });
1316
Tanya Lattner20890832004-05-28 20:14:12 +00001317 //Now write the epilogues
Tanya Lattner420025b2004-10-10 22:44:35 +00001318 for(int i = schedule.getMaxStage()-1; i >= 0; --i) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001319 BasicBlock *llvmBB = new BasicBlock("EPILOGUE", (Function*) (origBB->getBasicBlock()->getParent()));
Tanya Lattner20890832004-05-28 20:14:12 +00001320 MachineBasicBlock *machineBB = new MachineBasicBlock(llvmBB);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001321
Tanya Lattner420025b2004-10-10 22:44:35 +00001322 DEBUG(std::cerr << " Epilogue #: " << i << "\n");
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001323
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001324
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001325
Tanya Lattner420025b2004-10-10 22:44:35 +00001326
1327 for(MachineBasicBlock::const_iterator MI = origBB->begin(), ME = origBB->end(); ME != MI; ++MI) {
1328 for(int j=schedule.getMaxStage(); j > i; --j) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001329 if(inKernel[j].count(&*MI)) {
1330 DEBUG(std::cerr << "Cloning instruction " << *MI << "\n");
1331 MachineInstr *clone = MI->clone();
1332
1333 //Update operands that need to use the result from the phi
Tanya Lattner420025b2004-10-10 22:44:35 +00001334 for(unsigned opNum=0; opNum < clone->getNumOperands(); ++opNum) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001335 //get machine operand
Tanya Lattner420025b2004-10-10 22:44:35 +00001336 const MachineOperand &mOp = clone->getOperand(opNum);
1337
1338 //If this is the last instructions for the max iterations ago, don't update operands
1339 if(j == schedule.getMaxStage() && (i == 0))
1340 continue;
1341
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001342 if((mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isUse())) {
Tanya Lattner420025b2004-10-10 22:44:35 +00001343
1344 DEBUG(std::cerr << "Writing PHI for " << *(mOp.getVRegValue()) << "\n");
1345
1346 //Quickly write appropriate phis for this operand
1347 if(newValues.count(mOp.getVRegValue())) {
1348 if(newValues[mOp.getVRegValue()].count(i)) {
1349 Instruction *tmp = new TmpInstruction(newValues[mOp.getVRegValue()][i]);
1350 MachineInstr *saveValue = BuildMI(machineBB, V9::PHI, 3).addReg(newValues[mOp.getVRegValue()][i]).addReg(kernelPHIs[mOp.getVRegValue()][i]).addRegDef(tmp);
1351 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1352 valPHIs[mOp.getVRegValue()] = tmp;
1353 }
1354 }
1355
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001356 if(valPHIs.count(mOp.getVRegValue())) {
1357 //Update the operand in the cloned instruction
Tanya Lattner420025b2004-10-10 22:44:35 +00001358 clone->getOperand(opNum).setValueReg(valPHIs[mOp.getVRegValue()]);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001359 }
1360 }
1361 }
1362 machineBB->push_back(clone);
1363 }
1364 }
Tanya Lattner420025b2004-10-10 22:44:35 +00001365 }
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001366
Tanya Lattner20890832004-05-28 20:14:12 +00001367 (((MachineBasicBlock*)origBB)->getParent())->getBasicBlockList().push_back(machineBB);
1368 epilogues.push_back(machineBB);
1369 llvm_epilogues.push_back(llvmBB);
Tanya Lattner420025b2004-10-10 22:44:35 +00001370
1371 DEBUG(std::cerr << "EPILOGUE #" << i << "\n");
1372 DEBUG(machineBB->print(std::cerr));
Tanya Lattner20890832004-05-28 20:14:12 +00001373 }
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001374}
1375
Tanya Lattner420025b2004-10-10 22:44:35 +00001376void ModuloSchedulingPass::writeKernel(BasicBlock *llvmBB, MachineBasicBlock *machineBB, std::map<const Value*, std::pair<const MSchedGraphNode*, int> > &valuesToSave, std::map<Value*, std::map<int, Value*> > &newValues, std::map<Value*, MachineBasicBlock*> &newValLocation, std::map<Value*, std::map<int, Value*> > &kernelPHIs) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001377
1378 //Keep track of operands that are read and saved from a previous iteration. The new clone
1379 //instruction will use the result of the phi instead.
1380 std::map<Value*, Value*> finalPHIValue;
1381 std::map<Value*, Value*> kernelValue;
1382
1383 //Create TmpInstructions for the final phis
1384 for(MSSchedule::kernel_iterator I = schedule.kernel_begin(), E = schedule.kernel_end(); I != E; ++I) {
1385
Tanya Lattner420025b2004-10-10 22:44:35 +00001386 DEBUG(std::cerr << "Stage: " << I->second << " Inst: " << *(I->first->getInst()) << "\n";);
1387
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001388 //Clone instruction
1389 const MachineInstr *inst = I->first->getInst();
1390 MachineInstr *instClone = inst->clone();
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001391
Tanya Lattner420025b2004-10-10 22:44:35 +00001392 //Insert into machine basic block
1393 machineBB->push_back(instClone);
1394
1395
1396 //Loop over Machine Operands
1397 for(unsigned i=0; i < inst->getNumOperands(); ++i) {
1398 //get machine operand
1399 const MachineOperand &mOp = inst->getOperand(i);
1400
1401 if(I->second != 0) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001402 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isUse()) {
Tanya Lattner420025b2004-10-10 22:44:35 +00001403
1404 //Check to see where this operand is defined if this instruction is from max stage
1405 if(I->second == schedule.getMaxStage()) {
1406 DEBUG(std::cerr << "VREG: " << *(mOp.getVRegValue()) << "\n");
1407 }
1408
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001409 //If its in the value saved, we need to create a temp instruction and use that instead
1410 if(valuesToSave.count(mOp.getVRegValue())) {
1411 TmpInstruction *tmp = new TmpInstruction(mOp.getVRegValue());
1412
1413 //Update the operand in the cloned instruction
1414 instClone->getOperand(i).setValueReg(tmp);
1415
1416 //save this as our final phi
1417 finalPHIValue[mOp.getVRegValue()] = tmp;
1418 newValLocation[tmp] = machineBB;
1419 }
1420 }
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001421 }
Tanya Lattner420025b2004-10-10 22:44:35 +00001422 if(I->second != schedule.getMaxStage()) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001423 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isDef()) {
1424 if(valuesToSave.count(mOp.getVRegValue())) {
1425
1426 TmpInstruction *tmp = new TmpInstruction(mOp.getVRegValue());
1427
1428 //Create new machine instr and put in MBB
1429 MachineInstr *saveValue = BuildMI(machineBB, V9::ORr, 3).addReg(mOp.getVRegValue()).addImm(0).addRegDef(tmp);
1430
1431 //Save for future cleanup
1432 kernelValue[mOp.getVRegValue()] = tmp;
1433 newValLocation[tmp] = machineBB;
Tanya Lattner420025b2004-10-10 22:44:35 +00001434 kernelPHIs[mOp.getVRegValue()][schedule.getMaxStage()-1] = tmp;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001435 }
1436 }
1437 }
1438 }
Tanya Lattner420025b2004-10-10 22:44:35 +00001439
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001440 }
1441
Tanya Lattner420025b2004-10-10 22:44:35 +00001442 DEBUG(std::cerr << "KERNEL before PHIs\n");
1443 DEBUG(machineBB->print(std::cerr));
1444
1445
1446 //Loop over each value we need to generate phis for
1447 for(std::map<Value*, std::map<int, Value*> >::iterator V = newValues.begin(),
1448 E = newValues.end(); V != E; ++V) {
1449
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001450
1451 DEBUG(std::cerr << "Writing phi for" << *(V->first));
Tanya Lattner420025b2004-10-10 22:44:35 +00001452 DEBUG(std::cerr << "\nMap of Value* for this phi\n");
1453 DEBUG(for(std::map<int, Value*>::iterator I = V->second.begin(),
1454 IE = V->second.end(); I != IE; ++I) {
1455 std::cerr << "Stage: " << I->first;
1456 std::cerr << " Value: " << *(I->second) << "\n";
1457 });
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001458
Tanya Lattner420025b2004-10-10 22:44:35 +00001459 //If we only have one current iteration live, its safe to set lastPhi = to kernel value
1460 if(V->second.size() == 1) {
1461 assert(kernelValue[V->first] != 0 && "Kernel value* must exist to create phi");
1462 MachineInstr *saveValue = BuildMI(*machineBB, machineBB->begin(),V9::PHI, 3).addReg(V->second.begin()->second).addReg(kernelValue[V->first]).addRegDef(finalPHIValue[V->first]);
1463 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1464 kernelPHIs[V->first][schedule.getMaxStage()-1] = kernelValue[V->first];
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001465 }
Tanya Lattner420025b2004-10-10 22:44:35 +00001466 else {
1467
1468 //Keep track of last phi created.
1469 Instruction *lastPhi = 0;
1470
1471 unsigned count = 1;
1472 //Loop over the the map backwards to generate phis
1473 for(std::map<int, Value*>::reverse_iterator I = V->second.rbegin(), IE = V->second.rend();
1474 I != IE; ++I) {
1475
1476 if(count < (V->second).size()) {
1477 if(lastPhi == 0) {
1478 lastPhi = new TmpInstruction(I->second);
1479 MachineInstr *saveValue = BuildMI(*machineBB, machineBB->begin(), V9::PHI, 3).addReg(kernelValue[V->first]).addReg(I->second).addRegDef(lastPhi);
1480 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1481 newValLocation[lastPhi] = machineBB;
1482 }
1483 else {
1484 Instruction *tmp = new TmpInstruction(I->second);
1485 MachineInstr *saveValue = BuildMI(*machineBB, machineBB->begin(), V9::PHI, 3).addReg(lastPhi).addReg(I->second).addRegDef(tmp);
1486 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1487 lastPhi = tmp;
1488 kernelPHIs[V->first][I->first] = lastPhi;
1489 newValLocation[lastPhi] = machineBB;
1490 }
1491 }
1492 //Final phi value
1493 else {
1494 //The resulting value must be the Value* we created earlier
1495 assert(lastPhi != 0 && "Last phi is NULL!\n");
1496 MachineInstr *saveValue = BuildMI(*machineBB, machineBB->begin(), V9::PHI, 3).addReg(lastPhi).addReg(I->second).addRegDef(finalPHIValue[V->first]);
1497 DEBUG(std::cerr << "Resulting PHI: " << *saveValue << "\n");
1498 kernelPHIs[V->first][I->first] = finalPHIValue[V->first];
1499 }
1500
1501 ++count;
1502 }
1503
1504 }
1505 }
1506
1507 DEBUG(std::cerr << "KERNEL after PHIs\n");
1508 DEBUG(machineBB->print(std::cerr));
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001509}
1510
Tanya Lattner420025b2004-10-10 22:44:35 +00001511
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001512void ModuloSchedulingPass::removePHIs(const MachineBasicBlock *origBB, std::vector<MachineBasicBlock *> &prologues, std::vector<MachineBasicBlock *> &epilogues, MachineBasicBlock *kernelBB, std::map<Value*, MachineBasicBlock*> &newValLocation) {
1513
1514 //Worklist to delete things
1515 std::vector<std::pair<MachineBasicBlock*, MachineBasicBlock::iterator> > worklist;
1516
1517 const TargetInstrInfo *TMI = target.getInstrInfo();
1518
1519 //Start with the kernel and for each phi insert a copy for the phi def and for each arg
1520 for(MachineBasicBlock::iterator I = kernelBB->begin(), E = kernelBB->end(); I != E; ++I) {
1521 //Get op code and check if its a phi
Brian Gaeke418379e2004-08-18 20:04:24 +00001522 if(I->getOpcode() == V9::PHI) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001523 Instruction *tmp = 0;
1524 for(unsigned i = 0; i < I->getNumOperands(); ++i) {
1525 //Get Operand
1526 const MachineOperand &mOp = I->getOperand(i);
1527 assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Should be a Value*\n");
1528
1529 if(!tmp) {
1530 tmp = new TmpInstruction(mOp.getVRegValue());
1531 }
1532
1533 //Now for all our arguments we read, OR to the new TmpInstruction that we created
1534 if(mOp.isUse()) {
1535 DEBUG(std::cerr << "Use: " << mOp << "\n");
1536 //Place a copy at the end of its BB but before the branches
1537 assert(newValLocation.count(mOp.getVRegValue()) && "We must know where this value is located\n");
1538 //Reverse iterate to find the branches, we can safely assume no instructions have been
1539 //put in the nop positions
1540 for(MachineBasicBlock::iterator inst = --(newValLocation[mOp.getVRegValue()])->end(), endBB = (newValLocation[mOp.getVRegValue()])->begin(); inst != endBB; --inst) {
1541 MachineOpCode opc = inst->getOpcode();
1542 if(TMI->isBranch(opc) || TMI->isNop(opc))
1543 continue;
1544 else {
1545 BuildMI(*(newValLocation[mOp.getVRegValue()]), ++inst, V9::ORr, 3).addReg(mOp.getVRegValue()).addImm(0).addRegDef(tmp);
1546 break;
1547 }
1548
1549 }
1550
1551 }
1552 else {
1553 //Remove the phi and replace it with an OR
1554 DEBUG(std::cerr << "Def: " << mOp << "\n");
1555 BuildMI(*kernelBB, I, V9::ORr, 3).addReg(tmp).addImm(0).addRegDef(mOp.getVRegValue());
1556 worklist.push_back(std::make_pair(kernelBB, I));
1557 }
1558
1559 }
1560 }
1561
1562 }
1563
1564 //Remove phis from epilogue
1565 for(std::vector<MachineBasicBlock*>::iterator MB = epilogues.begin(), ME = epilogues.end(); MB != ME; ++MB) {
1566 for(MachineBasicBlock::iterator I = (*MB)->begin(), E = (*MB)->end(); I != E; ++I) {
1567 //Get op code and check if its a phi
Brian Gaeke418379e2004-08-18 20:04:24 +00001568 if(I->getOpcode() == V9::PHI) {
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001569 Instruction *tmp = 0;
1570 for(unsigned i = 0; i < I->getNumOperands(); ++i) {
1571 //Get Operand
1572 const MachineOperand &mOp = I->getOperand(i);
1573 assert(mOp.getType() == MachineOperand::MO_VirtualRegister && "Should be a Value*\n");
1574
1575 if(!tmp) {
1576 tmp = new TmpInstruction(mOp.getVRegValue());
1577 }
1578
1579 //Now for all our arguments we read, OR to the new TmpInstruction that we created
1580 if(mOp.isUse()) {
1581 DEBUG(std::cerr << "Use: " << mOp << "\n");
1582 //Place a copy at the end of its BB but before the branches
1583 assert(newValLocation.count(mOp.getVRegValue()) && "We must know where this value is located\n");
1584 //Reverse iterate to find the branches, we can safely assume no instructions have been
1585 //put in the nop positions
1586 for(MachineBasicBlock::iterator inst = --(newValLocation[mOp.getVRegValue()])->end(), endBB = (newValLocation[mOp.getVRegValue()])->begin(); inst != endBB; --inst) {
1587 MachineOpCode opc = inst->getOpcode();
1588 if(TMI->isBranch(opc) || TMI->isNop(opc))
1589 continue;
1590 else {
1591 BuildMI(*(newValLocation[mOp.getVRegValue()]), ++inst, V9::ORr, 3).addReg(mOp.getVRegValue()).addImm(0).addRegDef(tmp);
1592 break;
1593 }
1594
1595 }
1596
1597 }
1598 else {
1599 //Remove the phi and replace it with an OR
1600 DEBUG(std::cerr << "Def: " << mOp << "\n");
1601 BuildMI(**MB, I, V9::ORr, 3).addReg(tmp).addImm(0).addRegDef(mOp.getVRegValue());
1602 worklist.push_back(std::make_pair(*MB,I));
1603 }
1604
1605 }
1606 }
1607 }
1608 }
1609
1610 //Delete the phis
1611 for(std::vector<std::pair<MachineBasicBlock*, MachineBasicBlock::iterator> >::iterator I = worklist.begin(), E = worklist.end(); I != E; ++I) {
Tanya Lattner420025b2004-10-10 22:44:35 +00001612 DEBUG(std::cerr << "Deleting PHI " << I->second << "\n");
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001613 I->first->erase(I->second);
1614
1615 }
1616
Tanya Lattner20890832004-05-28 20:14:12 +00001617}
1618
1619
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001620void ModuloSchedulingPass::reconstructLoop(MachineBasicBlock *BB) {
Tanya Lattner4cffb582004-05-26 06:27:18 +00001621
Tanya Lattner420025b2004-10-10 22:44:35 +00001622 DEBUG(std::cerr << "Reconstructing Loop\n");
1623
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001624 //First find the value *'s that we need to "save"
1625 std::map<const Value*, std::pair<const MSchedGraphNode*, int> > valuesToSave;
Tanya Lattner4cffb582004-05-26 06:27:18 +00001626
Tanya Lattner420025b2004-10-10 22:44:35 +00001627 //Keep track of instructions we have already seen and their stage because
1628 //we don't want to "save" values if they are used in the kernel immediately
1629 std::map<const MachineInstr*, int> lastInstrs;
1630
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001631 //Loop over kernel and only look at instructions from a stage > 0
1632 //Look at its operands and save values *'s that are read
Tanya Lattner4cffb582004-05-26 06:27:18 +00001633 for(MSSchedule::kernel_iterator I = schedule.kernel_begin(), E = schedule.kernel_end(); I != E; ++I) {
Tanya Lattner4cffb582004-05-26 06:27:18 +00001634
Tanya Lattner420025b2004-10-10 22:44:35 +00001635 if(I->second !=0) {
Tanya Lattner4cffb582004-05-26 06:27:18 +00001636 //For this instruction, get the Value*'s that it reads and put them into the set.
1637 //Assert if there is an operand of another type that we need to save
1638 const MachineInstr *inst = I->first->getInst();
Tanya Lattner420025b2004-10-10 22:44:35 +00001639 lastInstrs[inst] = I->second;
1640
Tanya Lattner4cffb582004-05-26 06:27:18 +00001641 for(unsigned i=0; i < inst->getNumOperands(); ++i) {
1642 //get machine operand
1643 const MachineOperand &mOp = inst->getOperand(i);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001644
Tanya Lattner4cffb582004-05-26 06:27:18 +00001645 if(mOp.getType() == MachineOperand::MO_VirtualRegister && mOp.isUse()) {
1646 //find the value in the map
Tanya Lattner420025b2004-10-10 22:44:35 +00001647 if (const Value* srcI = mOp.getVRegValue()) {
1648
1649 //Before we declare this Value* one that we should save
1650 //make sure its def is not of the same stage as this instruction
1651 //because it will be consumed before its used
1652 Instruction *defInst = (Instruction*) srcI;
1653
1654 //Should we save this value?
1655 bool save = true;
1656
1657 //Get Machine code for this instruction, and loop backwards over the array
1658 //to find the def
1659 MachineCodeForInstruction & tempMvec = MachineCodeForInstruction::get(defInst);
1660 for (int j = tempMvec.size()-1; j >= 0; j--) {
1661 MachineInstr *temp = tempMvec[j];
1662
1663 //Loop over instructions
1664 for(unsigned opNum = 0; opNum < temp->getNumOperands(); ++opNum) {
1665 MachineOperand &mDefOp = temp->getOperand(opNum);
1666
1667 if (mDefOp.getType() == MachineOperand::MO_VirtualRegister && mDefOp.isDef()) {
1668 const Value* defVReg = mDefOp.getVRegValue();
1669 if(defVReg == srcI) {
1670 //Check if instruction has been seen already and is of same stage
1671 if(lastInstrs.count(temp)) {
1672 if(lastInstrs[temp] == I->second)
1673 save = false;
1674 }
1675 }
1676 }
1677 }
1678 }
1679 if(save)
1680 valuesToSave[srcI] = std::make_pair(I->first, i);
1681 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001682 }
1683
1684 if(mOp.getType() != MachineOperand::MO_VirtualRegister && mOp.isUse()) {
1685 assert("Our assumption is wrong. We have another type of register that needs to be saved\n");
1686 }
1687 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001688 }
1689 }
1690
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001691 //The new loop will consist of one or more prologues, the kernel, and one or more epilogues.
1692
1693 //Map to keep track of old to new values
Tanya Lattner420025b2004-10-10 22:44:35 +00001694 std::map<Value*, std::map<int, Value*> > newValues;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001695
Tanya Lattner420025b2004-10-10 22:44:35 +00001696 //Map to keep track of old to new values in kernel
1697 std::map<Value*, std::map<int, Value*> > kernelPHIs;
1698
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001699 //Another map to keep track of what machine basic blocks these new value*s are in since
1700 //they have no llvm instruction equivalent
1701 std::map<Value*, MachineBasicBlock*> newValLocation;
1702
1703 std::vector<MachineBasicBlock*> prologues;
1704 std::vector<BasicBlock*> llvm_prologues;
1705
1706
1707 //Write prologue
1708 writePrologues(prologues, BB, llvm_prologues, valuesToSave, newValues, newValLocation);
Tanya Lattner420025b2004-10-10 22:44:35 +00001709
1710 //Print out epilogues and prologue
1711 DEBUG(for(std::vector<MachineBasicBlock*>::iterator I = prologues.begin(), E = prologues.end();
1712 I != E; ++I) {
1713 std::cerr << "PROLOGUE\n";
1714 (*I)->print(std::cerr);
1715 });
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001716
1717 BasicBlock *llvmKernelBB = new BasicBlock("Kernel", (Function*) (BB->getBasicBlock()->getParent()));
1718 MachineBasicBlock *machineKernelBB = new MachineBasicBlock(llvmKernelBB);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001719 (((MachineBasicBlock*)BB)->getParent())->getBasicBlockList().push_back(machineKernelBB);
Tanya Lattner420025b2004-10-10 22:44:35 +00001720 writeKernel(llvmKernelBB, machineKernelBB, valuesToSave, newValues, newValLocation, kernelPHIs);
1721
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001722
1723 std::vector<MachineBasicBlock*> epilogues;
1724 std::vector<BasicBlock*> llvm_epilogues;
1725
1726 //Write epilogues
Tanya Lattner420025b2004-10-10 22:44:35 +00001727 writeEpilogues(epilogues, BB, llvm_epilogues, valuesToSave, newValues, newValLocation, kernelPHIs);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001728
1729
1730 const TargetInstrInfo *TMI = target.getInstrInfo();
1731
1732 //Fix up machineBB and llvmBB branches
1733 for(unsigned I = 0; I < prologues.size(); ++I) {
1734
1735 MachineInstr *branch = 0;
1736
1737 //Find terminator since getFirstTerminator does not work!
1738 for(MachineBasicBlock::reverse_iterator mInst = prologues[I]->rbegin(), mInstEnd = prologues[I]->rend(); mInst != mInstEnd; ++mInst) {
1739 MachineOpCode OC = mInst->getOpcode();
1740 if(TMI->isBranch(OC)) {
1741 branch = &*mInst;
1742 DEBUG(std::cerr << *mInst << "\n");
1743 break;
1744 }
1745 }
1746
1747
1748
1749 //Update branch
1750 for(unsigned opNum = 0; opNum < branch->getNumOperands(); ++opNum) {
1751 MachineOperand &mOp = branch->getOperand(opNum);
1752 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
1753 mOp.setValueReg(llvm_epilogues[(llvm_epilogues.size()-1-I)]);
1754 }
1755 }
1756
1757 //Update llvm basic block with our new branch instr
1758 DEBUG(std::cerr << BB->getBasicBlock()->getTerminator() << "\n");
1759 const BranchInst *branchVal = dyn_cast<BranchInst>(BB->getBasicBlock()->getTerminator());
1760 TmpInstruction *tmp = new TmpInstruction(branchVal->getCondition());
1761 if(I == prologues.size()-1) {
1762 TerminatorInst *newBranch = new BranchInst(llvmKernelBB,
1763 llvm_epilogues[(llvm_epilogues.size()-1-I)],
1764 tmp,
1765 llvm_prologues[I]);
1766 }
1767 else
1768 TerminatorInst *newBranch = new BranchInst(llvm_prologues[I+1],
1769 llvm_epilogues[(llvm_epilogues.size()-1-I)],
1770 tmp,
1771 llvm_prologues[I]);
1772
1773 assert(branch != 0 && "There must be a terminator for this machine basic block!\n");
1774
1775 //Push nop onto end of machine basic block
1776 BuildMI(prologues[I], V9::NOP, 0);
1777
Tanya Lattner420025b2004-10-10 22:44:35 +00001778 //Add a unconditional branch to the next prologue
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001779 if(I != prologues.size()-1)
Tanya Lattner420025b2004-10-10 22:44:35 +00001780 BuildMI(prologues[I], V9::BA, 1).addPCDisp(llvm_prologues[I+1]);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001781 else
Tanya Lattner420025b2004-10-10 22:44:35 +00001782 BuildMI(prologues[I], V9::BA, 1).addPCDisp(llvmKernelBB);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001783
1784 //Add one more nop!
1785 BuildMI(prologues[I], V9::NOP, 0);
1786 }
1787
1788 //Fix up kernel machine branches
1789 MachineInstr *branch = 0;
1790 for(MachineBasicBlock::reverse_iterator mInst = machineKernelBB->rbegin(), mInstEnd = machineKernelBB->rend(); mInst != mInstEnd; ++mInst) {
1791 MachineOpCode OC = mInst->getOpcode();
1792 if(TMI->isBranch(OC)) {
1793 branch = &*mInst;
1794 DEBUG(std::cerr << *mInst << "\n");
1795 break;
1796 }
1797 }
1798
1799 assert(branch != 0 && "There must be a terminator for the kernel machine basic block!\n");
1800
1801 //Update kernel self loop branch
1802 for(unsigned opNum = 0; opNum < branch->getNumOperands(); ++opNum) {
1803 MachineOperand &mOp = branch->getOperand(opNum);
1804
1805 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
1806 mOp.setValueReg(llvmKernelBB);
1807 }
1808 }
1809
1810 //Update kernelLLVM branches
1811 const BranchInst *branchVal = dyn_cast<BranchInst>(BB->getBasicBlock()->getTerminator());
1812 TerminatorInst *newBranch = new BranchInst(llvmKernelBB,
1813 llvm_epilogues[0],
1814 new TmpInstruction(branchVal->getCondition()),
1815 llvmKernelBB);
1816
1817 //Add kernel noop
1818 BuildMI(machineKernelBB, V9::NOP, 0);
1819
1820 //Add unconditional branch to first epilogue
Tanya Lattner420025b2004-10-10 22:44:35 +00001821 BuildMI(machineKernelBB, V9::BA, 1).addPCDisp(llvm_epilogues[0]);
1822
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001823
1824 //Add kernel noop
1825 BuildMI(machineKernelBB, V9::NOP, 0);
1826
1827 //Lastly add unconditional branches for the epilogues
1828 for(unsigned I = 0; I < epilogues.size(); ++I) {
Tanya Lattner4cffb582004-05-26 06:27:18 +00001829
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001830 //Now since I don't trust fall throughs, add a unconditional branch to the next prologue
1831 if(I != epilogues.size()-1) {
Tanya Lattner420025b2004-10-10 22:44:35 +00001832 BuildMI(epilogues[I], V9::BA, 1).addPCDisp(llvm_epilogues[I+1]);
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001833 //Add unconditional branch to end of epilogue
1834 TerminatorInst *newBranch = new BranchInst(llvm_epilogues[I+1],
1835 llvm_epilogues[I]);
1836
Tanya Lattner4cffb582004-05-26 06:27:18 +00001837 }
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001838 else {
1839 MachineBasicBlock *origBlock = (MachineBasicBlock*) BB;
1840 for(MachineBasicBlock::reverse_iterator inst = origBlock->rbegin(), instEnd = origBlock->rend(); inst != instEnd; ++inst) {
1841 MachineOpCode OC = inst->getOpcode();
1842 if(TMI->isBranch(OC)) {
1843 branch = &*inst;
Tanya Lattner420025b2004-10-10 22:44:35 +00001844 DEBUG(std::cerr << "Exit branch from loop" << *inst << "\n");
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001845 break;
1846
1847 }
1848
1849 for(unsigned opNum = 0; opNum < branch->getNumOperands(); ++opNum) {
1850 MachineOperand &mOp = branch->getOperand(opNum);
1851
1852 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
Tanya Lattner420025b2004-10-10 22:44:35 +00001853 BuildMI(epilogues[I], V9::BA, 1).addPCDisp(mOp.getVRegValue());
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001854 break;
1855 }
1856 }
1857
1858 }
1859
1860 //Update last epilogue exit branch
1861 BranchInst *branchVal = (BranchInst*) dyn_cast<BranchInst>(BB->getBasicBlock()->getTerminator());
1862 //Find where we are supposed to branch to
Chris Lattner46c2b3a2004-08-04 03:51:55 +00001863 BasicBlock *nextBlock = 0;
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001864 for(unsigned j=0; j <branchVal->getNumSuccessors(); ++j) {
1865 if(branchVal->getSuccessor(j) != BB->getBasicBlock())
1866 nextBlock = branchVal->getSuccessor(j);
1867 }
1868 TerminatorInst *newBranch = new BranchInst(nextBlock, llvm_epilogues[I]);
1869 }
1870 //Add one more nop!
1871 BuildMI(epilogues[I], V9::NOP, 0);
Tanya Lattner4cffb582004-05-26 06:27:18 +00001872
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001873 }
Tanya Lattner4cffb582004-05-26 06:27:18 +00001874
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001875 //FIX UP Machine BB entry!!
1876 //We are looking at the predecesor of our loop basic block and we want to change its ba instruction
1877
Tanya Lattner4cffb582004-05-26 06:27:18 +00001878
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001879 //Find all llvm basic blocks that branch to the loop entry and change to our first prologue.
1880 const BasicBlock *llvmBB = BB->getBasicBlock();
1881
1882 for(pred_const_iterator P = pred_begin(llvmBB), PE = pred_end(llvmBB); P != PE; ++PE) {
1883 if(*P == llvmBB)
1884 continue;
1885 else {
1886 DEBUG(std::cerr << "Found our entry BB\n");
1887 //Get the Terminator instruction for this basic block and print it out
1888 DEBUG(std::cerr << *((*P)->getTerminator()) << "\n");
1889 //Update the terminator
1890 TerminatorInst *term = ((BasicBlock*)*P)->getTerminator();
1891 for(unsigned i=0; i < term->getNumSuccessors(); ++i) {
1892 if(term->getSuccessor(i) == llvmBB) {
1893 DEBUG(std::cerr << "Replacing successor bb\n");
1894 if(llvm_prologues.size() > 0) {
1895 term->setSuccessor(i, llvm_prologues[0]);
1896 //Also update its corresponding machine instruction
1897 MachineCodeForInstruction & tempMvec =
1898 MachineCodeForInstruction::get(term);
1899 for (unsigned j = 0; j < tempMvec.size(); j++) {
1900 MachineInstr *temp = tempMvec[j];
1901 MachineOpCode opc = temp->getOpcode();
1902 if(TMI->isBranch(opc)) {
1903 DEBUG(std::cerr << *temp << "\n");
1904 //Update branch
1905 for(unsigned opNum = 0; opNum < temp->getNumOperands(); ++opNum) {
1906 MachineOperand &mOp = temp->getOperand(opNum);
1907 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
1908 mOp.setValueReg(llvm_prologues[0]);
1909 }
1910 }
1911 }
1912 }
1913 }
1914 else {
1915 term->setSuccessor(i, llvmKernelBB);
1916 //Also update its corresponding machine instruction
1917 MachineCodeForInstruction & tempMvec =
1918 MachineCodeForInstruction::get(term);
1919 for (unsigned j = 0; j < tempMvec.size(); j++) {
1920 MachineInstr *temp = tempMvec[j];
1921 MachineOpCode opc = temp->getOpcode();
1922 if(TMI->isBranch(opc)) {
1923 DEBUG(std::cerr << *temp << "\n");
1924 //Update branch
1925 for(unsigned opNum = 0; opNum < temp->getNumOperands(); ++opNum) {
1926 MachineOperand &mOp = temp->getOperand(opNum);
1927 if (mOp.getType() == MachineOperand::MO_PCRelativeDisp) {
1928 mOp.setValueReg(llvmKernelBB);
1929 }
1930 }
1931 }
1932 }
1933 }
1934 }
1935 }
1936 break;
1937 }
1938 }
1939
1940 removePHIs(BB, prologues, epilogues, machineKernelBB, newValLocation);
Tanya Lattner4cffb582004-05-26 06:27:18 +00001941
1942
Tanya Lattner0a88d2d2004-07-30 23:36:10 +00001943
1944 //Print out epilogues and prologue
1945 DEBUG(for(std::vector<MachineBasicBlock*>::iterator I = prologues.begin(), E = prologues.end();
1946 I != E; ++I) {
1947 std::cerr << "PROLOGUE\n";
1948 (*I)->print(std::cerr);
1949 });
1950
1951 DEBUG(std::cerr << "KERNEL\n");
1952 DEBUG(machineKernelBB->print(std::cerr));
1953
1954 DEBUG(for(std::vector<MachineBasicBlock*>::iterator I = epilogues.begin(), E = epilogues.end();
1955 I != E; ++I) {
1956 std::cerr << "EPILOGUE\n";
1957 (*I)->print(std::cerr);
1958 });
1959
1960
1961 DEBUG(std::cerr << "New Machine Function" << "\n");
1962 DEBUG(std::cerr << BB->getParent() << "\n");
1963
Tanya Lattner420025b2004-10-10 22:44:35 +00001964 //BB->getParent()->getBasicBlockList().erase(BB);
Tanya Lattner4cffb582004-05-26 06:27:18 +00001965
1966}
1967