blob: d61feb47979923ef56cb53984f3de8ce548033b3 [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 Lattner06cb1b72002-02-03 07:33:46 +000019#include "llvm/CodeGen/InstrForest.h"
20#include "llvm/CodeGen/MachineCodeForInstruction.h"
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +000021#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattner06cb1b72002-02-03 07:33:46 +000022#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;
Anand Shuklacfb22d32002-06-25 20:55:50 +000031using std::vector;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000032
Vikram S. Adve7ad10462001-10-22 13:51:09 +000033//******************** Internal Data Declarations ************************/
34
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000035
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000036enum SelectDebugLevel_t {
37 Select_NoDebugInfo,
38 Select_PrintMachineCode,
39 Select_DebugInstTrees,
40 Select_DebugBurgTrees,
41};
42
43// Enable Debug Options to be specified on the command line
Chris Lattnerad86b742002-05-20 21:39:10 +000044cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::Hidden,
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000045 "enable instruction selection debugging information",
46 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
47 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
Vikram S. Adve6e447182001-09-18 12:56:28 +000048 clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "),
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000049 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
50
51
Vikram S. Adve7ad10462001-10-22 13:51:09 +000052//******************** Forward Function Declarations ***********************/
53
54
55static bool SelectInstructionsForTree (InstrTreeNode* treeRoot,
56 int goalnt,
57 TargetMachine &target);
58
59static void PostprocessMachineCodeForTree(InstructionNode* instrNode,
60 int ruleForNode,
61 short* nts,
62 TargetMachine &target);
63
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000064static void InsertCode4AllPhisInMeth(Function *F, TargetMachine &target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000065
66
Vikram S. Adve7ad10462001-10-22 13:51:09 +000067
68//******************* Externally Visible Functions *************************/
69
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000070
71//---------------------------------------------------------------------------
72// Entry point for instruction selection using BURG.
73// Returns true if instruction selection failed, false otherwise.
74//---------------------------------------------------------------------------
75
Vikram S. Adve6e447182001-09-18 12:56:28 +000076bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000077SelectInstructionsForMethod(Function *F, TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +000078{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000079 bool failed = false;
80
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000081 //
82 // Build the instruction trees to be given as inputs to BURG.
83 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000084 InstrForest instrForest(F);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000085
86 if (SelectDebugLevel >= Select_DebugInstTrees)
87 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000088 cerr << "\n\n*** Input to instruction selection for function "
89 << F->getName() << "\n\n";
90 F->dump();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +000091
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000092 cerr << "\n\n*** Instruction trees for function "
93 << F->getName() << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000094 instrForest.dump();
95 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000096
97 //
98 // Invoke BURG instruction selection for each tree
99 //
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000100 for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
101 RI != instrForest.roots_end(); ++RI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000102 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000103 InstructionNode* basicNode = *RI;
104 assert(basicNode->parent() == NULL && "A `root' node has a parent?");
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 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000127 for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000128 for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) {
129 MachineCodeForInstruction &mvec =MachineCodeForInstruction::get(II);
130 for (unsigned i=0; i < mvec.size(); i++)
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000131 MachineCodeForBasicBlock::get(BI).push_back(mvec[i]);
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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000135 InsertCode4AllPhisInMeth(F, target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000136
Vikram S. Adve76d35202001-07-30 18:48:43 +0000137
Vikram S. Adve6e447182001-09-18 12:56:28 +0000138 if (SelectDebugLevel >= Select_PrintMachineCode)
139 {
Chris Lattner697954c2002-01-20 22:54:45 +0000140 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000141 MachineCodeForMethod::get(F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000142 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000143
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000144 return false;
145}
146
147
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000148//*********************** Private Functions *****************************/
149
150
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000151//-------------------------------------------------------------------------
152// Thid method inserts a copy instruction to a predecessor BB as a result
153// of phi elimination.
154//-------------------------------------------------------------------------
155
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000156void
Anand Shuklacfb22d32002-06-25 20:55:50 +0000157InsertPhiElimInstructions(BasicBlock *BB, const std::vector<MachineInstr*>& CpVec)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000158{
Chris Lattner455889a2002-02-12 22:39:50 +0000159 Instruction *TermInst = (Instruction*)BB->getTerminator();
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000160 MachineCodeForInstruction &MC4Term =MachineCodeForInstruction::get(TermInst);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000161 MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000162
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000163 assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000164
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000165 // get an iterator to machine instructions in the BB
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000166 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(BB);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000167 MachineCodeForBasicBlock::iterator MCIt = bbMvec.begin();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000168
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000169 // find the position of first machine instruction generated by the
170 // terminator of this BB
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000171 for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt )
172 ;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000173 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000174
175 // insert the copy instructions just before the first machine instruction
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000176 // generated for the terminator
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000177 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
178
Ruchira Sasanka71309382001-11-12 19:42:27 +0000179 //cerr << "\nPhiElimination copy inst: " << *CopyInstVec[0];
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000180}
181
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000182
183//-------------------------------------------------------------------------
184// This method inserts phi elimination code for all BBs in a method
185//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000186
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000187void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000188InsertCode4AllPhisInMeth(Function *F, TargetMachine &target)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000189{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000190 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000191 //
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000192 for (Function::iterator BB = F->begin(); BB != F->end(); ++BB) {
193 BasicBlock::InstListType &InstList = BB->getInstList();
194 for (BasicBlock::iterator IIt = InstList.begin();
195 PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) {
196 // FIXME: This is probably wrong...
197 Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000198
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000199 // for each incoming value of the phi, insert phi elimination
200 //
201 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
202 // insert the copy instruction to the predecessor BB
203 vector<MachineInstr*> mvec, CpVec;
204 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
205 mvec);
206 for (vector<MachineInstr*>::iterator MI=mvec.begin();
207 MI != mvec.end(); ++MI) {
208 vector<MachineInstr*> CpVec2 =
209 FixConstantOperandsForInstr(PN, *MI, target);
210 CpVec2.push_back(*MI);
211 CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
212 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000213
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000214 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000215 }
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000216
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000217 vector<MachineInstr*> mvec;
218 target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
219
220 // get an iterator to machine instructions in the BB
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000221 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(BB);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000222
223 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000224 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000225 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000226}
227
228
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000229//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000230// Function PostprocessMachineCodeForTree
231//
232// Apply any final cleanups to machine code for the root of a subtree
233// after selection for all its children has been completed.
234//---------------------------------------------------------------------------
235
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000236static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000237PostprocessMachineCodeForTree(InstructionNode* instrNode,
238 int ruleForNode,
239 short* nts,
240 TargetMachine &target)
241{
242 // Fix up any constant operands in the machine instructions to either
243 // use an immediate field or to load the constant into a register
244 // Walk backwards and use direct indexes to allow insertion before current
245 //
246 Instruction* vmInstr = instrNode->getInstruction();
Chris Lattner06cb1b72002-02-03 07:33:46 +0000247 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000248 for (int i = (int) mvec.size()-1; i >= 0; i--)
249 {
Chris Lattner697954c2002-01-20 22:54:45 +0000250 std::vector<MachineInstr*> loadConstVec =
Vikram S. Adve6d353262001-10-17 23:57:50 +0000251 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
252
253 if (loadConstVec.size() > 0)
254 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
255 }
256}
257
258//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000259// Function SelectInstructionsForTree
260//
261// Recursively walk the tree to select instructions.
262// Do this top-down so that child instructions can exploit decisions
263// made at the child instructions.
264//
265// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
266// a branch-on-integer-register instruction, then the setle node
267// can use that information to avoid generating the SUBcc instruction.
268//
269// Note that this cannot be done bottom-up because setle must do this
270// only if it is a child of the branch (otherwise, the result of setle
271// may be used by multiple instructions).
272//---------------------------------------------------------------------------
273
Vikram S. Adve6e447182001-09-18 12:56:28 +0000274bool
275SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000276 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000277{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000278 // Get the rule that matches this node.
279 //
280 int ruleForNode = burm_rule(treeRoot->state, goalnt);
281
Vikram S. Adve6e447182001-09-18 12:56:28 +0000282 if (ruleForNode == 0)
283 {
Chris Lattner697954c2002-01-20 22:54:45 +0000284 cerr << "Could not match instruction tree for instr selection\n";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000285 assert(0);
286 return true;
287 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000288
289 // Get this rule's non-terminals and the corresponding child nodes (if any)
290 //
291 short *nts = burm_nts[ruleForNode];
292
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000293 // First, select instructions for the current node and rule.
294 // (If this is a list node, not an instruction, then skip this step).
295 // This function is specific to the target architecture.
296 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000297 if (treeRoot->opLabel != VRegListOp)
298 {
Anand Shuklacfb22d32002-06-25 20:55:50 +0000299 std::vector<MachineInstr*> minstrVec;
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000300
Vikram S. Adve6e447182001-09-18 12:56:28 +0000301 InstructionNode* instrNode = (InstructionNode*)treeRoot;
302 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000303
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000304 GetInstructionsByRule(instrNode, ruleForNode, nts, target, minstrVec);
305
Chris Lattner06cb1b72002-02-03 07:33:46 +0000306 MachineCodeForInstruction &mvec =
307 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000308 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000309 }
310
311 // Then, recursively compile the child nodes, if any.
312 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000313 if (nts[0])
314 { // i.e., there is at least one kid
315 InstrTreeNode* kids[2];
316 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000317 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000318
319 // First skip over any chain rules so that we don't visit
320 // the current node again.
321 //
322 while (ThisIsAChainRule(currentRule))
323 {
324 currentRule = burm_rule(treeRoot->state, nts[0]);
325 nts = burm_nts[currentRule];
326 burm_kids(treeRoot, currentRule, kids);
327 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000328
Vikram S. Adve6e447182001-09-18 12:56:28 +0000329 // Now we have the first non-chain rule so we have found
330 // the actual child nodes. Recursively compile them.
331 //
332 for (int i = 0; nts[i]; i++)
333 {
334 assert(i < 2);
335 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
336 if (nodeType == InstrTreeNode::NTVRegListNode ||
337 nodeType == InstrTreeNode::NTInstructionNode)
338 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000339 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000340 return true; // failure
341 }
342 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000343 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000344
Vikram S. Adve6d353262001-10-17 23:57:50 +0000345 // Finally, do any postprocessing on this node after its children
346 // have been translated
347 //
348 if (treeRoot->opLabel != VRegListOp)
349 {
350 InstructionNode* instrNode = (InstructionNode*)treeRoot;
351 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
352 }
353
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000354 return false; // success
355}
356