blob: f32165eda1bb407bcda2e5e53ed70a31d77a58bb [file] [log] [blame]
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00001// $Id$ -*-c++-*-
2//***************************************************************************
3// File:
Vikram S. Adve89df1ae2001-08-28 23:04:38 +00004// InstrSelection.cpp
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00005//
6// Purpose:
Vikram S. Adve6e447182001-09-18 12:56:28 +00007// Machine-independent driver file for instruction selection.
8// This file constructs a forest of BURG instruction trees and then
Vikram S. Adve9aba1d32001-10-10 20:49:07 +00009// uses the BURG-generated tree grammar (BURM) to find the optimal
Vikram S. Adve6e447182001-09-18 12:56:28 +000010// instruction sequences for a given machine.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000011//
12// History:
13// 7/02/01 - Vikram Adve - Created
Vikram S. Adve960066a2001-07-31 21:53:25 +000014//**************************************************************************/
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000015
16
Chris Lattnerfeb60592001-09-07 17:15:18 +000017#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve6d353262001-10-17 23:57:50 +000018#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnerd268ad62001-09-11 23:52:11 +000019#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000020#include "llvm/Support/CommandLine.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000021#include "llvm/Instruction.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000022#include "llvm/BasicBlock.h"
23#include "llvm/Method.h"
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000024#include "llvm/iOther.h"
25#include "llvm/Target/MachineRegInfo.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000026
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000027
Vikram S. Adve7ad10462001-10-22 13:51:09 +000028//******************** Internal Data Declarations ************************/
29
30// Use a static vector to avoid allocating a new one per VM instruction
31static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
32
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000033
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000034enum SelectDebugLevel_t {
35 Select_NoDebugInfo,
36 Select_PrintMachineCode,
37 Select_DebugInstTrees,
38 Select_DebugBurgTrees,
39};
40
41// Enable Debug Options to be specified on the command line
Chris Lattner5f6baf72001-09-12 16:34:03 +000042cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags,
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000043 "enable instruction selection debugging information",
44 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
45 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
Vikram S. Adve6e447182001-09-18 12:56:28 +000046 clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "),
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000047 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
48
49
Vikram S. Adve7ad10462001-10-22 13:51:09 +000050//******************** Forward Function Declarations ***********************/
51
52
53static bool SelectInstructionsForTree (InstrTreeNode* treeRoot,
54 int goalnt,
55 TargetMachine &target);
56
57static void PostprocessMachineCodeForTree(InstructionNode* instrNode,
58 int ruleForNode,
59 short* nts,
60 TargetMachine &target);
61
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000062static void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target);
63
64
Vikram S. Adve7ad10462001-10-22 13:51:09 +000065
66//******************* Externally Visible Functions *************************/
67
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000068
69//---------------------------------------------------------------------------
70// Entry point for instruction selection using BURG.
71// Returns true if instruction selection failed, false otherwise.
72//---------------------------------------------------------------------------
73
Vikram S. Adve6e447182001-09-18 12:56:28 +000074bool
Vikram S. Adve6d353262001-10-17 23:57:50 +000075SelectInstructionsForMethod(Method* method, TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +000076{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000077 bool failed = false;
78
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000079 //
80 // Build the instruction trees to be given as inputs to BURG.
81 //
Chris Lattner5f6baf72001-09-12 16:34:03 +000082 InstrForest instrForest(method);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000083
84 if (SelectDebugLevel >= Select_DebugInstTrees)
85 {
86 cout << "\n\n*** Instruction trees for method "
87 << (method->hasName()? method->getName() : "")
88 << endl << endl;
89 instrForest.dump();
90 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000091
92 //
93 // Invoke BURG instruction selection for each tree
94 //
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000095 const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet();
Chris Lattner75279cc2001-07-23 03:50:57 +000096 for (hash_set<InstructionNode*>::const_iterator
Chris Lattner0e6530e2001-09-14 03:37:52 +000097 treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end();
Vikram S. Adve6e447182001-09-18 12:56:28 +000098 ++treeRootIter)
99 {
100 InstrTreeNode* basicNode = *treeRootIter;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000101
Vikram S. Adve6e447182001-09-18 12:56:28 +0000102 // Invoke BURM to label each tree node with a state
103 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000104
Vikram S. Adve6e447182001-09-18 12:56:28 +0000105 if (SelectDebugLevel >= Select_DebugBurgTrees)
106 {
107 printcover(basicNode, 1, 0);
108 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
109 printMatches(basicNode);
110 }
111
112 // Then recursively walk the tree to select instructions
Vikram S. Adve6d353262001-10-17 23:57:50 +0000113 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000114 {
115 failed = true;
116 break;
117 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000118 }
119
Vikram S. Adve76d35202001-07-30 18:48:43 +0000120 //
121 // Record instructions in the vector for each basic block
122 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000123 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI)
124 {
125 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
126 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
127 {
128 MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
129 for (unsigned i=0; i < mvec.size(); i++)
130 bbMvec.push_back(mvec[i]);
131 }
Vikram S. Adve76d35202001-07-30 18:48:43 +0000132 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000133
134 // Insert phi elimination code -- added by Ruchira
135 InsertCode4AllPhisInMeth(method, target);
136
Vikram S. Adve76d35202001-07-30 18:48:43 +0000137
Vikram S. Adve6e447182001-09-18 12:56:28 +0000138 if (SelectDebugLevel >= Select_PrintMachineCode)
139 {
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000140 cout << endl
141 << "*** Machine instructions after INSTRUCTION SELECTION" << endl;
Vikram S. Advebe495262001-11-08 04:47:06 +0000142 MachineCodeForMethod::get(method).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000143 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000144
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000145 return false;
146}
147
148
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000149//*********************** Private Functions *****************************/
150
151
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000152//-------------------------------------------------------------------------
153// Thid method inserts a copy instruction to a predecessor BB as a result
154// of phi elimination.
155//-------------------------------------------------------------------------
156
Ruchira Sasanka71309382001-11-12 19:42:27 +0000157void InsertPhiElimInst(BasicBlock *BB, MachineInstr *CpMI) {
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000158
159 TerminatorInst *TermInst = BB->getTerminator();
160 MachineCodeForVMInstr &MC4Term = TermInst->getMachineInstrVec();
161 MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
162
163 assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
164
165 // get an iterator to machine instructions in the BB
166 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
167 MachineCodeForBasicBlock::iterator MCIt = bbMvec.begin();
168
169 // find the position of first machine instruction generated by the
170 // terminator of this BB
171 for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt ) ;
172
173 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000174
175 // insert the copy instruction just before the first machine instruction
176 // generated for the terminator
Ruchira Sasanka71309382001-11-12 19:42:27 +0000177 bbMvec.insert( MCIt , CpMI );
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000178
Ruchira Sasanka71309382001-11-12 19:42:27 +0000179 //cerr << "\nPhiElimination copy inst: " << *CopyInstVec[0];
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000180
181}
182
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000183#if 0
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000184//-------------------------------------------------------------------------
185// This method inserts phi elimination code for all BBs in a method
186//-------------------------------------------------------------------------
187void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target) {
188
189
190 // for all basic blocks in method
191 //
192 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) {
193
194 BasicBlock *BB = *BI;
195 const BasicBlock::InstListType &InstList = BB->getInstList();
196 BasicBlock::InstListType::const_iterator IIt = InstList.begin();
197
198 // for all instructions in the basic block
199 //
200 for( ; IIt != InstList.end(); ++IIt ) {
201
202 if( (*IIt)->getOpcode() == Instruction::PHINode ) {
203
204 PHINode *PN = (PHINode *) (*IIt);
205
206 // for each incoming value of the phi, insert phi elimination
207 //
208 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
209
210 // insert the copy instruction to the predecessor BB
211
212 vector<MachineInstr*> CopyInstVec;
213
Ruchira Sasanka71309382001-11-12 19:42:27 +0000214 MachineInstr *CpMI =
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000215 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PN);
216
Ruchira Sasanka71309382001-11-12 19:42:27 +0000217 InsertPhiElimInst( PN->getIncomingBlock(i), CpMI);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000218 }
219 }
220 else break; // since PHI nodes can only be at the top
221
222 } // for each Phi Instr in BB
223
224 } // for all BBs in method
225
226}
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000227#endif
228
229
230//-------------------------------------------------------------------------
231// This method inserts phi elimination code for all BBs in a method
232//-------------------------------------------------------------------------
233void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target) {
234
235
236 // for all basic blocks in method
237 //
238 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) {
239
240 BasicBlock *BB = *BI;
241 const BasicBlock::InstListType &InstList = BB->getInstList();
242 BasicBlock::InstListType::const_iterator IIt = InstList.begin();
243
244 // for all instructions in the basic block
245 //
246 for( ; IIt != InstList.end(); ++IIt ) {
247
248 if( (*IIt)->getOpcode() == Instruction::PHINode ) {
249
250 PHINode *PN = (PHINode *) (*IIt);
251
252 Value *PhiCpRes =
253 new Value(PN->getType(), PN->getValueType() );
254
255 // for each incoming value of the phi, insert phi elimination
256 //
257 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
258
259 // insert the copy instruction to the predecessor BB
260
261 MachineInstr *CpMI =
262 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i),
263 PhiCpRes);
264
265 InsertPhiElimInst(PN->getIncomingBlock(i), CpMI);
266
267 }
268
269
270 MachineInstr *CpMI2 =
271 target.getRegInfo().cpValue2Value(PhiCpRes, PN);
272
273 // get an iterator to machine instructions in the BB
274 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
275
276 bbMvec.insert( bbMvec.begin(), CpMI2);
277
278
279 }
280 else break; // since PHI nodes can only be at the top
281
282 } // for each Phi Instr in BB
283
284 } // for all BBs in method
285
286}
287
288
289
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000290
291
292
293
294
295
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000296//---------------------------------------------------------------------------
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000297// Function AppendMachineCodeForVMInstr
298//
299// Append machine instr sequence to the machine code vec for a VM instr
300//---------------------------------------------------------------------------
301
302inline void
303AppendMachineCodeForVMInstr(MachineInstr** minstrVec,
304 unsigned int N,
305 Instruction* vmInstr)
306{
307 if (N == 0)
308 return;
309 MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec();
310 mvec.insert(mvec.end(), minstrVec, minstrVec+N);
311}
312
313
314
315//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000316// Function PostprocessMachineCodeForTree
317//
318// Apply any final cleanups to machine code for the root of a subtree
319// after selection for all its children has been completed.
320//---------------------------------------------------------------------------
321
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000322static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000323PostprocessMachineCodeForTree(InstructionNode* instrNode,
324 int ruleForNode,
325 short* nts,
326 TargetMachine &target)
327{
328 // Fix up any constant operands in the machine instructions to either
329 // use an immediate field or to load the constant into a register
330 // Walk backwards and use direct indexes to allow insertion before current
331 //
332 Instruction* vmInstr = instrNode->getInstruction();
333 MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec();
334 for (int i = (int) mvec.size()-1; i >= 0; i--)
335 {
336 vector<MachineInstr*> loadConstVec =
337 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
338
339 if (loadConstVec.size() > 0)
340 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
341 }
342}
343
344//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000345// Function SelectInstructionsForTree
346//
347// Recursively walk the tree to select instructions.
348// Do this top-down so that child instructions can exploit decisions
349// made at the child instructions.
350//
351// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
352// a branch-on-integer-register instruction, then the setle node
353// can use that information to avoid generating the SUBcc instruction.
354//
355// Note that this cannot be done bottom-up because setle must do this
356// only if it is a child of the branch (otherwise, the result of setle
357// may be used by multiple instructions).
358//---------------------------------------------------------------------------
359
Vikram S. Adve6e447182001-09-18 12:56:28 +0000360bool
361SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000362 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000363{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000364 // Get the rule that matches this node.
365 //
366 int ruleForNode = burm_rule(treeRoot->state, goalnt);
367
Vikram S. Adve6e447182001-09-18 12:56:28 +0000368 if (ruleForNode == 0)
369 {
370 cerr << "Could not match instruction tree for instr selection" << endl;
371 assert(0);
372 return true;
373 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000374
375 // Get this rule's non-terminals and the corresponding child nodes (if any)
376 //
377 short *nts = burm_nts[ruleForNode];
378
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000379 // First, select instructions for the current node and rule.
380 // (If this is a list node, not an instruction, then skip this step).
381 // This function is specific to the target architecture.
382 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000383 if (treeRoot->opLabel != VRegListOp)
384 {
385 InstructionNode* instrNode = (InstructionNode*)treeRoot;
386 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000387
Vikram S. Adve6d353262001-10-17 23:57:50 +0000388 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, target,
Vikram S. Adve6e447182001-09-18 12:56:28 +0000389 minstrVec);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000390 if (N > 0)
391 {
392 assert(N <= MAX_INSTR_PER_VMINSTR);
393 AppendMachineCodeForVMInstr(minstrVec,N,instrNode->getInstruction());
394 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000395 }
396
397 // Then, recursively compile the child nodes, if any.
398 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000399 if (nts[0])
400 { // i.e., there is at least one kid
401 InstrTreeNode* kids[2];
402 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000403 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000404
405 // First skip over any chain rules so that we don't visit
406 // the current node again.
407 //
408 while (ThisIsAChainRule(currentRule))
409 {
410 currentRule = burm_rule(treeRoot->state, nts[0]);
411 nts = burm_nts[currentRule];
412 burm_kids(treeRoot, currentRule, kids);
413 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000414
Vikram S. Adve6e447182001-09-18 12:56:28 +0000415 // Now we have the first non-chain rule so we have found
416 // the actual child nodes. Recursively compile them.
417 //
418 for (int i = 0; nts[i]; i++)
419 {
420 assert(i < 2);
421 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
422 if (nodeType == InstrTreeNode::NTVRegListNode ||
423 nodeType == InstrTreeNode::NTInstructionNode)
424 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000425 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000426 return true; // failure
427 }
428 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000429 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000430
Vikram S. Adve6d353262001-10-17 23:57:50 +0000431 // Finally, do any postprocessing on this node after its children
432 // have been translated
433 //
434 if (treeRoot->opLabel != VRegListOp)
435 {
436 InstructionNode* instrNode = (InstructionNode*)treeRoot;
437 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
438 }
439
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000440 return false; // success
441}
442