blob: af02e57616d4a7565d578073d184b3d0f58a0270 [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"
Chris Lattner06cb1b72002-02-03 07:33:46 +000020#include "llvm/CodeGen/InstrForest.h"
21#include "llvm/CodeGen/MachineCodeForInstruction.h"
22#include "llvm/CodeGen/MachineCodeForMethod.h"
23#include "llvm/Target/MachineRegInfo.h"
24#include "llvm/Target/TargetMachine.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000025#include "llvm/BasicBlock.h"
26#include "llvm/Method.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000027#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000028#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000029#include <iostream>
30using std::cerr;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000031
Vikram S. Adve7ad10462001-10-22 13:51:09 +000032//******************** Internal Data Declarations ************************/
33
34// Use a static vector to avoid allocating a new one per VM instruction
35static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
36
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000037
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000038enum SelectDebugLevel_t {
39 Select_NoDebugInfo,
40 Select_PrintMachineCode,
41 Select_DebugInstTrees,
42 Select_DebugBurgTrees,
43};
44
45// Enable Debug Options to be specified on the command line
Chris Lattner5f6baf72001-09-12 16:34:03 +000046cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags,
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000047 "enable instruction selection debugging information",
48 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
49 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
Vikram S. Adve6e447182001-09-18 12:56:28 +000050 clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "),
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000051 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
52
53
Vikram S. Adve7ad10462001-10-22 13:51:09 +000054//******************** Forward Function Declarations ***********************/
55
56
57static bool SelectInstructionsForTree (InstrTreeNode* treeRoot,
58 int goalnt,
59 TargetMachine &target);
60
61static void PostprocessMachineCodeForTree(InstructionNode* instrNode,
62 int ruleForNode,
63 short* nts,
64 TargetMachine &target);
65
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000066static void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target);
67
68
Vikram S. Adve7ad10462001-10-22 13:51:09 +000069
70//******************* Externally Visible Functions *************************/
71
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000072
73//---------------------------------------------------------------------------
74// Entry point for instruction selection using BURG.
75// Returns true if instruction selection failed, false otherwise.
76//---------------------------------------------------------------------------
77
Vikram S. Adve6e447182001-09-18 12:56:28 +000078bool
Vikram S. Adve6d353262001-10-17 23:57:50 +000079SelectInstructionsForMethod(Method* method, TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +000080{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000081 bool failed = false;
82
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000083 //
84 // Build the instruction trees to be given as inputs to BURG.
85 //
Chris Lattner5f6baf72001-09-12 16:34:03 +000086 InstrForest instrForest(method);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000087
88 if (SelectDebugLevel >= Select_DebugInstTrees)
89 {
Chris Lattner697954c2002-01-20 22:54:45 +000090 cerr << "\n\n*** Instruction trees for method "
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000091 << (method->hasName()? method->getName() : "")
Chris Lattner697954c2002-01-20 22:54:45 +000092 << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000093 instrForest.dump();
94 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000095
96 //
97 // Invoke BURG instruction selection for each tree
98 //
Chris Lattner697954c2002-01-20 22:54:45 +000099 const std::hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet();
100 for (std::hash_set<InstructionNode*>::const_iterator
Chris Lattner0e6530e2001-09-14 03:37:52 +0000101 treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000102 ++treeRootIter)
103 {
104 InstrTreeNode* basicNode = *treeRootIter;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000105
Vikram S. Adve6e447182001-09-18 12:56:28 +0000106 // Invoke BURM to label each tree node with a state
107 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000108
Vikram S. Adve6e447182001-09-18 12:56:28 +0000109 if (SelectDebugLevel >= Select_DebugBurgTrees)
110 {
111 printcover(basicNode, 1, 0);
112 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
113 printMatches(basicNode);
114 }
115
116 // Then recursively walk the tree to select instructions
Vikram S. Adve6d353262001-10-17 23:57:50 +0000117 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000118 {
119 failed = true;
120 break;
121 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000122 }
123
Vikram S. Adve76d35202001-07-30 18:48:43 +0000124 //
125 // Record instructions in the vector for each basic block
126 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000127 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI)
128 {
129 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
130 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
131 {
Chris Lattner06cb1b72002-02-03 07:33:46 +0000132 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(*II);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000133 for (unsigned i=0; i < mvec.size(); i++)
134 bbMvec.push_back(mvec[i]);
135 }
Vikram S. Adve76d35202001-07-30 18:48:43 +0000136 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000137
138 // Insert phi elimination code -- added by Ruchira
139 InsertCode4AllPhisInMeth(method, target);
140
Vikram S. Adve76d35202001-07-30 18:48:43 +0000141
Vikram S. Adve6e447182001-09-18 12:56:28 +0000142 if (SelectDebugLevel >= Select_PrintMachineCode)
143 {
Chris Lattner697954c2002-01-20 22:54:45 +0000144 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Vikram S. Advebe495262001-11-08 04:47:06 +0000145 MachineCodeForMethod::get(method).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000146 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000147
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000148 return false;
149}
150
151
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000152//*********************** Private Functions *****************************/
153
154
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000155//-------------------------------------------------------------------------
156// Thid method inserts a copy instruction to a predecessor BB as a result
157// of phi elimination.
158//-------------------------------------------------------------------------
159
Ruchira Sasanka71309382001-11-12 19:42:27 +0000160void InsertPhiElimInst(BasicBlock *BB, MachineInstr *CpMI) {
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000161
Chris Lattner455889a2002-02-12 22:39:50 +0000162 Instruction *TermInst = (Instruction*)BB->getTerminator();
Chris Lattner06cb1b72002-02-03 07:33:46 +0000163 MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000164 MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
165
166 assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
167
168 // get an iterator to machine instructions in the BB
169 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
170 MachineCodeForBasicBlock::iterator MCIt = bbMvec.begin();
171
172 // find the position of first machine instruction generated by the
173 // terminator of this BB
174 for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt ) ;
175
176 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000177
178 // insert the copy instruction just before the first machine instruction
179 // generated for the terminator
Ruchira Sasanka71309382001-11-12 19:42:27 +0000180 bbMvec.insert( MCIt , CpMI );
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000181
Ruchira Sasanka71309382001-11-12 19:42:27 +0000182 //cerr << "\nPhiElimination copy inst: " << *CopyInstVec[0];
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000183
184}
185
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000186#if 0
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000187//-------------------------------------------------------------------------
188// This method inserts phi elimination code for all BBs in a method
189//-------------------------------------------------------------------------
190void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target) {
191
192
193 // for all basic blocks in method
194 //
195 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) {
196
197 BasicBlock *BB = *BI;
198 const BasicBlock::InstListType &InstList = BB->getInstList();
199 BasicBlock::InstListType::const_iterator IIt = InstList.begin();
200
201 // for all instructions in the basic block
202 //
203 for( ; IIt != InstList.end(); ++IIt ) {
204
205 if( (*IIt)->getOpcode() == Instruction::PHINode ) {
206
207 PHINode *PN = (PHINode *) (*IIt);
208
209 // for each incoming value of the phi, insert phi elimination
210 //
211 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
212
213 // insert the copy instruction to the predecessor BB
214
Chris Lattner697954c2002-01-20 22:54:45 +0000215 std::vector<MachineInstr*> CopyInstVec;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000216
Ruchira Sasanka71309382001-11-12 19:42:27 +0000217 MachineInstr *CpMI =
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000218 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PN);
219
Ruchira Sasanka71309382001-11-12 19:42:27 +0000220 InsertPhiElimInst( PN->getIncomingBlock(i), CpMI);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000221 }
222 }
223 else break; // since PHI nodes can only be at the top
224
225 } // for each Phi Instr in BB
226
227 } // for all BBs in method
228
229}
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000230#endif
231
232
233//-------------------------------------------------------------------------
234// This method inserts phi elimination code for all BBs in a method
235//-------------------------------------------------------------------------
236void InsertCode4AllPhisInMeth(Method *method, TargetMachine &target) {
237
238
239 // for all basic blocks in method
240 //
241 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI) {
242
243 BasicBlock *BB = *BI;
244 const BasicBlock::InstListType &InstList = BB->getInstList();
245 BasicBlock::InstListType::const_iterator IIt = InstList.begin();
246
247 // for all instructions in the basic block
248 //
249 for( ; IIt != InstList.end(); ++IIt ) {
250
251 if( (*IIt)->getOpcode() == Instruction::PHINode ) {
252
253 PHINode *PN = (PHINode *) (*IIt);
254
Chris Lattner697954c2002-01-20 22:54:45 +0000255 Value *PhiCpRes = new Value(PN->getType(), PN->getValueType(),"PhiCp:");
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000256
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000257 // for each incoming value of the phi, insert phi elimination
258 //
259 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000260 // insert the copy instruction to the predecessor BB
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000261 MachineInstr *CpMI =
262 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i),
263 PhiCpRes);
264
265 InsertPhiElimInst(PN->getIncomingBlock(i), CpMI);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000266 }
267
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000268 MachineInstr *CpMI2 =
269 target.getRegInfo().cpValue2Value(PhiCpRes, PN);
270
271 // get an iterator to machine instructions in the BB
272 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
273
274 bbMvec.insert( bbMvec.begin(), CpMI2);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000275 }
276 else break; // since PHI nodes can only be at the top
277
278 } // for each Phi Instr in BB
279
280 } // for all BBs in method
281
282}
283
284
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000285//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000286// Function PostprocessMachineCodeForTree
287//
288// Apply any final cleanups to machine code for the root of a subtree
289// after selection for all its children has been completed.
290//---------------------------------------------------------------------------
291
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000292static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000293PostprocessMachineCodeForTree(InstructionNode* instrNode,
294 int ruleForNode,
295 short* nts,
296 TargetMachine &target)
297{
298 // Fix up any constant operands in the machine instructions to either
299 // use an immediate field or to load the constant into a register
300 // Walk backwards and use direct indexes to allow insertion before current
301 //
302 Instruction* vmInstr = instrNode->getInstruction();
Chris Lattner06cb1b72002-02-03 07:33:46 +0000303 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000304 for (int i = (int) mvec.size()-1; i >= 0; i--)
305 {
Chris Lattner697954c2002-01-20 22:54:45 +0000306 std::vector<MachineInstr*> loadConstVec =
Vikram S. Adve6d353262001-10-17 23:57:50 +0000307 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
308
309 if (loadConstVec.size() > 0)
310 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
311 }
312}
313
314//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000315// Function SelectInstructionsForTree
316//
317// Recursively walk the tree to select instructions.
318// Do this top-down so that child instructions can exploit decisions
319// made at the child instructions.
320//
321// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
322// a branch-on-integer-register instruction, then the setle node
323// can use that information to avoid generating the SUBcc instruction.
324//
325// Note that this cannot be done bottom-up because setle must do this
326// only if it is a child of the branch (otherwise, the result of setle
327// may be used by multiple instructions).
328//---------------------------------------------------------------------------
329
Vikram S. Adve6e447182001-09-18 12:56:28 +0000330bool
331SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000332 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000333{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000334 // Get the rule that matches this node.
335 //
336 int ruleForNode = burm_rule(treeRoot->state, goalnt);
337
Vikram S. Adve6e447182001-09-18 12:56:28 +0000338 if (ruleForNode == 0)
339 {
Chris Lattner697954c2002-01-20 22:54:45 +0000340 cerr << "Could not match instruction tree for instr selection\n";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000341 assert(0);
342 return true;
343 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000344
345 // Get this rule's non-terminals and the corresponding child nodes (if any)
346 //
347 short *nts = burm_nts[ruleForNode];
348
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000349 // First, select instructions for the current node and rule.
350 // (If this is a list node, not an instruction, then skip this step).
351 // This function is specific to the target architecture.
352 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000353 if (treeRoot->opLabel != VRegListOp)
354 {
355 InstructionNode* instrNode = (InstructionNode*)treeRoot;
356 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000357
Vikram S. Adve6d353262001-10-17 23:57:50 +0000358 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, target,
Vikram S. Adve6e447182001-09-18 12:56:28 +0000359 minstrVec);
Chris Lattner06cb1b72002-02-03 07:33:46 +0000360 assert(N <= MAX_INSTR_PER_VMINSTR);
361 MachineCodeForInstruction &mvec =
362 MachineCodeForInstruction::get(instrNode->getInstruction());
363 mvec.insert(mvec.end(), minstrVec, minstrVec+N);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000364 }
365
366 // Then, recursively compile the child nodes, if any.
367 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000368 if (nts[0])
369 { // i.e., there is at least one kid
370 InstrTreeNode* kids[2];
371 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000372 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000373
374 // First skip over any chain rules so that we don't visit
375 // the current node again.
376 //
377 while (ThisIsAChainRule(currentRule))
378 {
379 currentRule = burm_rule(treeRoot->state, nts[0]);
380 nts = burm_nts[currentRule];
381 burm_kids(treeRoot, currentRule, kids);
382 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000383
Vikram S. Adve6e447182001-09-18 12:56:28 +0000384 // Now we have the first non-chain rule so we have found
385 // the actual child nodes. Recursively compile them.
386 //
387 for (int i = 0; nts[i]; i++)
388 {
389 assert(i < 2);
390 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
391 if (nodeType == InstrTreeNode::NTVRegListNode ||
392 nodeType == InstrTreeNode::NTInstructionNode)
393 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000394 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000395 return true; // failure
396 }
397 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000398 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000399
Vikram S. Adve6d353262001-10-17 23:57:50 +0000400 // Finally, do any postprocessing on this node after its children
401 // have been translated
402 //
403 if (treeRoot->opLabel != VRegListOp)
404 {
405 InstructionNode* instrNode = (InstructionNode*)treeRoot;
406 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
407 }
408
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000409 return false; // success
410}
411