blob: 1eb507a695b1ca97b499cd7c0d3f9ed4bcc31b5c [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"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000026#include "llvm/Function.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
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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000063static void InsertCode4AllPhisInMeth(Function *F, TargetMachine &target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000064
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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000076SelectInstructionsForMethod(Function *F, 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 Lattner2fbfdcf2002-04-07 20:49:59 +000083 InstrForest instrForest(F);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000084
85 if (SelectDebugLevel >= Select_DebugInstTrees)
86 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000087 cerr << "\n\n*** Input to instruction selection for function "
88 << F->getName() << "\n\n";
89 F->dump();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +000090
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000091 cerr << "\n\n*** Instruction trees for function "
92 << F->getName() << "\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 //
Vikram S. Adve4e7bc492002-03-24 03:36:52 +000099 for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
100 RI != instrForest.roots_end(); ++RI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000101 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000102 InstructionNode* basicNode = *RI;
103 assert(basicNode->parent() == NULL && "A `root' node has a parent?");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000104
Vikram S. Adve6e447182001-09-18 12:56:28 +0000105 // Invoke BURM to label each tree node with a state
106 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000107
Vikram S. Adve6e447182001-09-18 12:56:28 +0000108 if (SelectDebugLevel >= Select_DebugBurgTrees)
109 {
110 printcover(basicNode, 1, 0);
111 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
112 printMatches(basicNode);
113 }
114
115 // Then recursively walk the tree to select instructions
Vikram S. Adve6d353262001-10-17 23:57:50 +0000116 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000117 {
118 failed = true;
119 break;
120 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000121 }
122
Vikram S. Adve76d35202001-07-30 18:48:43 +0000123 //
124 // Record instructions in the vector for each basic block
125 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000126 for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000127 {
128 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
129 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
130 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000131 MachineCodeForInstruction &mvec =MachineCodeForInstruction::get(*II);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000132 for (unsigned i=0; i < mvec.size(); i++)
133 bbMvec.push_back(mvec[i]);
134 }
Vikram S. Adve76d35202001-07-30 18:48:43 +0000135 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000136
137 // Insert phi elimination code -- added by Ruchira
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000138 InsertCode4AllPhisInMeth(F, target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000139
Vikram S. Adve76d35202001-07-30 18:48:43 +0000140
Vikram S. Adve6e447182001-09-18 12:56:28 +0000141 if (SelectDebugLevel >= Select_PrintMachineCode)
142 {
Chris Lattner697954c2002-01-20 22:54:45 +0000143 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000144 MachineCodeForMethod::get(F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000145 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000146
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000147 return false;
148}
149
150
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000151//*********************** Private Functions *****************************/
152
153
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000154//-------------------------------------------------------------------------
155// Thid method inserts a copy instruction to a predecessor BB as a result
156// of phi elimination.
157//-------------------------------------------------------------------------
158
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000159void
160InsertPhiElimInstructions(BasicBlock *BB, const vector<MachineInstr*>& CpVec)
161{
Chris Lattner455889a2002-02-12 22:39:50 +0000162 Instruction *TermInst = (Instruction*)BB->getTerminator();
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000163 MachineCodeForInstruction &MC4Term =MachineCodeForInstruction::get(TermInst);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000164 MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000165
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000166 assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000167
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000168 // get an iterator to machine instructions in the BB
169 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
170 MachineCodeForBasicBlock::iterator MCIt = bbMvec.begin();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000171
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000172 // find the position of first machine instruction generated by the
173 // terminator of this BB
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000174 for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt )
175 ;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000176 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000177
178 // insert the copy instructions just before the first machine instruction
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000179 // generated for the terminator
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000180 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
181
Ruchira Sasanka71309382001-11-12 19:42:27 +0000182 //cerr << "\nPhiElimination copy inst: " << *CopyInstVec[0];
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000183}
184
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000185
186//-------------------------------------------------------------------------
187// This method inserts phi elimination code for all BBs in a method
188//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000189
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000190void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000191InsertCode4AllPhisInMeth(Function *F, TargetMachine &target)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000192{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000193 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000194 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000195 for (Function::iterator BI = F->begin(); BI != F->end(); ++BI) {
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000196
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
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000209 Value *PhiCpRes = new Value(PN->getType(),PN->getValueType(),"PhiCp:");
210
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000211 // for each incoming value of the phi, insert phi elimination
212 //
213 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000214 // insert the copy instruction to the predecessor BB
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000215 MachineInstr *CpMI =
216 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i),
217 PhiCpRes);
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000218
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000219 vector<MachineInstr*> CpVec = FixConstantOperandsForInstr(PN, CpMI,
220 target);
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000221 CpVec.push_back(CpMI);
222
223 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000224 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000225
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000226 MachineInstr *CpMI2 =
227 target.getRegInfo().cpValue2Value(PhiCpRes, PN);
228
229 // get an iterator to machine instructions in the BB
230 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
231
232 bbMvec.insert( bbMvec.begin(), CpMI2);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000233 }
234 else break; // since PHI nodes can only be at the top
235
236 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000237 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000238}
239
240
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000241//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000242// Function PostprocessMachineCodeForTree
243//
244// Apply any final cleanups to machine code for the root of a subtree
245// after selection for all its children has been completed.
246//---------------------------------------------------------------------------
247
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000248static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000249PostprocessMachineCodeForTree(InstructionNode* instrNode,
250 int ruleForNode,
251 short* nts,
252 TargetMachine &target)
253{
254 // Fix up any constant operands in the machine instructions to either
255 // use an immediate field or to load the constant into a register
256 // Walk backwards and use direct indexes to allow insertion before current
257 //
258 Instruction* vmInstr = instrNode->getInstruction();
Chris Lattner06cb1b72002-02-03 07:33:46 +0000259 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000260 for (int i = (int) mvec.size()-1; i >= 0; i--)
261 {
Chris Lattner697954c2002-01-20 22:54:45 +0000262 std::vector<MachineInstr*> loadConstVec =
Vikram S. Adve6d353262001-10-17 23:57:50 +0000263 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
264
265 if (loadConstVec.size() > 0)
266 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
267 }
268}
269
270//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000271// Function SelectInstructionsForTree
272//
273// Recursively walk the tree to select instructions.
274// Do this top-down so that child instructions can exploit decisions
275// made at the child instructions.
276//
277// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
278// a branch-on-integer-register instruction, then the setle node
279// can use that information to avoid generating the SUBcc instruction.
280//
281// Note that this cannot be done bottom-up because setle must do this
282// only if it is a child of the branch (otherwise, the result of setle
283// may be used by multiple instructions).
284//---------------------------------------------------------------------------
285
Vikram S. Adve6e447182001-09-18 12:56:28 +0000286bool
287SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000288 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000289{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000290 // Get the rule that matches this node.
291 //
292 int ruleForNode = burm_rule(treeRoot->state, goalnt);
293
Vikram S. Adve6e447182001-09-18 12:56:28 +0000294 if (ruleForNode == 0)
295 {
Chris Lattner697954c2002-01-20 22:54:45 +0000296 cerr << "Could not match instruction tree for instr selection\n";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000297 assert(0);
298 return true;
299 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000300
301 // Get this rule's non-terminals and the corresponding child nodes (if any)
302 //
303 short *nts = burm_nts[ruleForNode];
304
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000305 // First, select instructions for the current node and rule.
306 // (If this is a list node, not an instruction, then skip this step).
307 // This function is specific to the target architecture.
308 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000309 if (treeRoot->opLabel != VRegListOp)
310 {
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000311 vector<MachineInstr*> minstrVec;
312
Vikram S. Adve6e447182001-09-18 12:56:28 +0000313 InstructionNode* instrNode = (InstructionNode*)treeRoot;
314 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000315
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000316 GetInstructionsByRule(instrNode, ruleForNode, nts, target, minstrVec);
317
Chris Lattner06cb1b72002-02-03 07:33:46 +0000318 MachineCodeForInstruction &mvec =
319 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000320 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000321 }
322
323 // Then, recursively compile the child nodes, if any.
324 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000325 if (nts[0])
326 { // i.e., there is at least one kid
327 InstrTreeNode* kids[2];
328 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000329 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000330
331 // First skip over any chain rules so that we don't visit
332 // the current node again.
333 //
334 while (ThisIsAChainRule(currentRule))
335 {
336 currentRule = burm_rule(treeRoot->state, nts[0]);
337 nts = burm_nts[currentRule];
338 burm_kids(treeRoot, currentRule, kids);
339 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000340
Vikram S. Adve6e447182001-09-18 12:56:28 +0000341 // Now we have the first non-chain rule so we have found
342 // the actual child nodes. Recursively compile them.
343 //
344 for (int i = 0; nts[i]; i++)
345 {
346 assert(i < 2);
347 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
348 if (nodeType == InstrTreeNode::NTVRegListNode ||
349 nodeType == InstrTreeNode::NTInstructionNode)
350 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000351 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000352 return true; // failure
353 }
354 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000355 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000356
Vikram S. Adve6d353262001-10-17 23:57:50 +0000357 // Finally, do any postprocessing on this node after its children
358 // have been translated
359 //
360 if (treeRoot->opLabel != VRegListOp)
361 {
362 InstructionNode* instrNode = (InstructionNode*)treeRoot;
363 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
364 }
365
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000366 return false; // success
367}
368