blob: ab489c507e81f9c2d83211c07f99aa6e72c9f1b6 [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. Adve70bc4b52001-07-21 12:41:50 +000020#include "llvm/Instruction.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000021#include "llvm/BasicBlock.h"
22#include "llvm/Method.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000023#include "llvm/iPHINode.h"
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000024#include "llvm/Target/MachineRegInfo.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000026#include <iostream>
27using std::cerr;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000028
Vikram S. Adve7ad10462001-10-22 13:51:09 +000029//******************** Internal Data Declarations ************************/
30
31// Use a static vector to avoid allocating a new one per VM instruction
32static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
33
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000034
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000035enum SelectDebugLevel_t {
36 Select_NoDebugInfo,
37 Select_PrintMachineCode,
38 Select_DebugInstTrees,
39 Select_DebugBurgTrees,
40};
41
42// Enable Debug Options to be specified on the command line
Chris Lattner5f6baf72001-09-12 16:34:03 +000043cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags,
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000044 "enable instruction selection debugging information",
45 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
46 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
Vikram S. Adve6e447182001-09-18 12:56:28 +000047 clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "),
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000048 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
49
50
Vikram S. Adve7ad10462001-10-22 13:51:09 +000051//******************** Forward Function Declarations ***********************/
52
53
54static bool SelectInstructionsForTree (InstrTreeNode* treeRoot,
55 int goalnt,
56 TargetMachine &target);
57
58static void PostprocessMachineCodeForTree(InstructionNode* instrNode,
59 int ruleForNode,
60 short* nts,
61 TargetMachine &target);
62
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000063static void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target);
64
65
Vikram S. Adve7ad10462001-10-22 13:51:09 +000066
67//******************* Externally Visible Functions *************************/
68
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000069
70//---------------------------------------------------------------------------
71// Entry point for instruction selection using BURG.
72// Returns true if instruction selection failed, false otherwise.
73//---------------------------------------------------------------------------
74
Vikram S. Adve6e447182001-09-18 12:56:28 +000075bool
Vikram S. Adve6d353262001-10-17 23:57:50 +000076SelectInstructionsForMethod(Method* method, TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +000077{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000078 bool failed = false;
79
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000080 //
81 // Build the instruction trees to be given as inputs to BURG.
82 //
Chris Lattner5f6baf72001-09-12 16:34:03 +000083 InstrForest instrForest(method);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000084
85 if (SelectDebugLevel >= Select_DebugInstTrees)
86 {
Chris Lattner697954c2002-01-20 22:54:45 +000087 cerr << "\n\n*** Instruction trees for method "
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000088 << (method->hasName()? method->getName() : "")
Chris Lattner697954c2002-01-20 22:54:45 +000089 << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000090 instrForest.dump();
91 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000092
93 //
94 // Invoke BURG instruction selection for each tree
95 //
Chris Lattner697954c2002-01-20 22:54:45 +000096 const std::hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet();
97 for (std::hash_set<InstructionNode*>::const_iterator
Chris Lattner0e6530e2001-09-14 03:37:52 +000098 treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end();
Vikram S. Adve6e447182001-09-18 12:56:28 +000099 ++treeRootIter)
100 {
101 InstrTreeNode* basicNode = *treeRootIter;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000102
Vikram S. Adve6e447182001-09-18 12:56:28 +0000103 // Invoke BURM to label each tree node with a state
104 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000105
Vikram S. Adve6e447182001-09-18 12:56:28 +0000106 if (SelectDebugLevel >= Select_DebugBurgTrees)
107 {
108 printcover(basicNode, 1, 0);
109 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
110 printMatches(basicNode);
111 }
112
113 // Then recursively walk the tree to select instructions
Vikram S. Adve6d353262001-10-17 23:57:50 +0000114 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000115 {
116 failed = true;
117 break;
118 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000119 }
120
Vikram S. Adve76d35202001-07-30 18:48:43 +0000121 //
122 // Record instructions in the vector for each basic block
123 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000124 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI)
125 {
126 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
127 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
128 {
129 MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
130 for (unsigned i=0; i < mvec.size(); i++)
131 bbMvec.push_back(mvec[i]);
132 }
Vikram S. Adve76d35202001-07-30 18:48:43 +0000133 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000134
135 // Insert phi elimination code -- added by Ruchira
136 InsertCode4AllPhisInMeth(method, target);
137
Vikram S. Adve76d35202001-07-30 18:48:43 +0000138
Vikram S. Adve6e447182001-09-18 12:56:28 +0000139 if (SelectDebugLevel >= Select_PrintMachineCode)
140 {
Chris Lattner697954c2002-01-20 22:54:45 +0000141 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
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
Chris Lattner697954c2002-01-20 22:54:45 +0000212 std::vector<MachineInstr*> CopyInstVec;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000213
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
Chris Lattner697954c2002-01-20 22:54:45 +0000252 Value *PhiCpRes = new Value(PN->getType(), PN->getValueType(),"PhiCp:");
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000253
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000254 // for each incoming value of the phi, insert phi elimination
255 //
256 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
257
258 // insert the copy instruction to the predecessor BB
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000259 MachineInstr *CpMI =
260 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i),
261 PhiCpRes);
262
263 InsertPhiElimInst(PN->getIncomingBlock(i), CpMI);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000264 }
265
266
267 MachineInstr *CpMI2 =
268 target.getRegInfo().cpValue2Value(PhiCpRes, PN);
269
270 // get an iterator to machine instructions in the BB
271 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
272
273 bbMvec.insert( bbMvec.begin(), CpMI2);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000274 }
275 else break; // since PHI nodes can only be at the top
276
277 } // for each Phi Instr in BB
278
279 } // for all BBs in method
280
281}
282
283
284
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000285
286
287
288
289
290
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000291//---------------------------------------------------------------------------
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000292// Function AppendMachineCodeForVMInstr
293//
294// Append machine instr sequence to the machine code vec for a VM instr
295//---------------------------------------------------------------------------
296
297inline void
298AppendMachineCodeForVMInstr(MachineInstr** minstrVec,
299 unsigned int N,
300 Instruction* vmInstr)
301{
302 if (N == 0)
303 return;
304 MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec();
305 mvec.insert(mvec.end(), minstrVec, minstrVec+N);
306}
307
308
309
310//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000311// Function PostprocessMachineCodeForTree
312//
313// Apply any final cleanups to machine code for the root of a subtree
314// after selection for all its children has been completed.
315//---------------------------------------------------------------------------
316
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000317static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000318PostprocessMachineCodeForTree(InstructionNode* instrNode,
319 int ruleForNode,
320 short* nts,
321 TargetMachine &target)
322{
323 // Fix up any constant operands in the machine instructions to either
324 // use an immediate field or to load the constant into a register
325 // Walk backwards and use direct indexes to allow insertion before current
326 //
327 Instruction* vmInstr = instrNode->getInstruction();
328 MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec();
329 for (int i = (int) mvec.size()-1; i >= 0; i--)
330 {
Chris Lattner697954c2002-01-20 22:54:45 +0000331 std::vector<MachineInstr*> loadConstVec =
Vikram S. Adve6d353262001-10-17 23:57:50 +0000332 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
333
334 if (loadConstVec.size() > 0)
335 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
336 }
337}
338
339//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000340// Function SelectInstructionsForTree
341//
342// Recursively walk the tree to select instructions.
343// Do this top-down so that child instructions can exploit decisions
344// made at the child instructions.
345//
346// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
347// a branch-on-integer-register instruction, then the setle node
348// can use that information to avoid generating the SUBcc instruction.
349//
350// Note that this cannot be done bottom-up because setle must do this
351// only if it is a child of the branch (otherwise, the result of setle
352// may be used by multiple instructions).
353//---------------------------------------------------------------------------
354
Vikram S. Adve6e447182001-09-18 12:56:28 +0000355bool
356SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000357 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000358{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000359 // Get the rule that matches this node.
360 //
361 int ruleForNode = burm_rule(treeRoot->state, goalnt);
362
Vikram S. Adve6e447182001-09-18 12:56:28 +0000363 if (ruleForNode == 0)
364 {
Chris Lattner697954c2002-01-20 22:54:45 +0000365 cerr << "Could not match instruction tree for instr selection\n";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000366 assert(0);
367 return true;
368 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000369
370 // Get this rule's non-terminals and the corresponding child nodes (if any)
371 //
372 short *nts = burm_nts[ruleForNode];
373
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000374 // First, select instructions for the current node and rule.
375 // (If this is a list node, not an instruction, then skip this step).
376 // This function is specific to the target architecture.
377 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000378 if (treeRoot->opLabel != VRegListOp)
379 {
380 InstructionNode* instrNode = (InstructionNode*)treeRoot;
381 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000382
Vikram S. Adve6d353262001-10-17 23:57:50 +0000383 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, target,
Vikram S. Adve6e447182001-09-18 12:56:28 +0000384 minstrVec);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000385 if (N > 0)
386 {
387 assert(N <= MAX_INSTR_PER_VMINSTR);
388 AppendMachineCodeForVMInstr(minstrVec,N,instrNode->getInstruction());
389 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000390 }
391
392 // Then, recursively compile the child nodes, if any.
393 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000394 if (nts[0])
395 { // i.e., there is at least one kid
396 InstrTreeNode* kids[2];
397 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000398 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000399
400 // First skip over any chain rules so that we don't visit
401 // the current node again.
402 //
403 while (ThisIsAChainRule(currentRule))
404 {
405 currentRule = burm_rule(treeRoot->state, nts[0]);
406 nts = burm_nts[currentRule];
407 burm_kids(treeRoot, currentRule, kids);
408 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000409
Vikram S. Adve6e447182001-09-18 12:56:28 +0000410 // Now we have the first non-chain rule so we have found
411 // the actual child nodes. Recursively compile them.
412 //
413 for (int i = 0; nts[i]; i++)
414 {
415 assert(i < 2);
416 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
417 if (nodeType == InstrTreeNode::NTVRegListNode ||
418 nodeType == InstrTreeNode::NTInstructionNode)
419 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000420 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000421 return true; // failure
422 }
423 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000424 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000425
Vikram S. Adve6d353262001-10-17 23:57:50 +0000426 // Finally, do any postprocessing on this node after its children
427 // have been translated
428 //
429 if (treeRoot->opLabel != VRegListOp)
430 {
431 InstructionNode* instrNode = (InstructionNode*)treeRoot;
432 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
433 }
434
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000435 return false; // success
436}
437