blob: 614c5f67731914aba204c344e207ead7e10712a7 [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"
21#include "llvm/CodeGen/MachineCodeForMethod.h"
22#include "llvm/Target/MachineRegInfo.h"
23#include "llvm/Target/TargetMachine.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000024#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000025#include "llvm/Function.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000028#include <iostream>
29using std::cerr;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000030
Vikram S. Adve7ad10462001-10-22 13:51:09 +000031//******************** Internal Data Declarations ************************/
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 Lattnerad86b742002-05-20 21:39:10 +000042cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::Hidden,
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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000062static void InsertCode4AllPhisInMeth(Function *F, TargetMachine &target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000063
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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000075SelectInstructionsForMethod(Function *F, 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 Lattner2fbfdcf2002-04-07 20:49:59 +000082 InstrForest instrForest(F);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000083
84 if (SelectDebugLevel >= Select_DebugInstTrees)
85 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000086 cerr << "\n\n*** Input to instruction selection for function "
87 << F->getName() << "\n\n";
88 F->dump();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +000089
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 cerr << "\n\n*** Instruction trees for function "
91 << F->getName() << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000092 instrForest.dump();
93 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000094
95 //
96 // Invoke BURG instruction selection for each tree
97 //
Vikram S. Adve4e7bc492002-03-24 03:36:52 +000098 for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
99 RI != instrForest.roots_end(); ++RI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000100 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000101 InstructionNode* basicNode = *RI;
102 assert(basicNode->parent() == NULL && "A `root' node has a parent?");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000103
Vikram S. Adve6e447182001-09-18 12:56:28 +0000104 // Invoke BURM to label each tree node with a state
105 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000106
Vikram S. Adve6e447182001-09-18 12:56:28 +0000107 if (SelectDebugLevel >= Select_DebugBurgTrees)
108 {
109 printcover(basicNode, 1, 0);
110 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
111 printMatches(basicNode);
112 }
113
114 // Then recursively walk the tree to select instructions
Vikram S. Adve6d353262001-10-17 23:57:50 +0000115 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000116 {
117 failed = true;
118 break;
119 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000120 }
121
Vikram S. Adve76d35202001-07-30 18:48:43 +0000122 //
123 // Record instructions in the vector for each basic block
124 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000125 for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000126 {
127 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
128 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
129 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000130 MachineCodeForInstruction &mvec =MachineCodeForInstruction::get(*II);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000131 for (unsigned i=0; i < mvec.size(); i++)
132 bbMvec.push_back(mvec[i]);
133 }
Vikram S. Adve76d35202001-07-30 18:48:43 +0000134 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000135
136 // Insert phi elimination code -- added by Ruchira
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000137 InsertCode4AllPhisInMeth(F, target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000138
Vikram S. Adve76d35202001-07-30 18:48:43 +0000139
Vikram S. Adve6e447182001-09-18 12:56:28 +0000140 if (SelectDebugLevel >= Select_PrintMachineCode)
141 {
Chris Lattner697954c2002-01-20 22:54:45 +0000142 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000143 MachineCodeForMethod::get(F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000144 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000145
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000146 return false;
147}
148
149
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000150//*********************** Private Functions *****************************/
151
152
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000153//-------------------------------------------------------------------------
154// Thid method inserts a copy instruction to a predecessor BB as a result
155// of phi elimination.
156//-------------------------------------------------------------------------
157
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000158void
159InsertPhiElimInstructions(BasicBlock *BB, const vector<MachineInstr*>& CpVec)
160{
Chris Lattner455889a2002-02-12 22:39:50 +0000161 Instruction *TermInst = (Instruction*)BB->getTerminator();
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000162 MachineCodeForInstruction &MC4Term =MachineCodeForInstruction::get(TermInst);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000163 MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000164
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000165 assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000166
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000167 // get an iterator to machine instructions in the BB
168 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
169 MachineCodeForBasicBlock::iterator MCIt = bbMvec.begin();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000170
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000171 // find the position of first machine instruction generated by the
172 // terminator of this BB
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000173 for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt )
174 ;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000175 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000176
177 // insert the copy instructions just before the first machine instruction
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000178 // generated for the terminator
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000179 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
180
Ruchira Sasanka71309382001-11-12 19:42:27 +0000181 //cerr << "\nPhiElimination copy inst: " << *CopyInstVec[0];
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000182}
183
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000184
185//-------------------------------------------------------------------------
186// This method inserts phi elimination code for all BBs in a method
187//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000188
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000189void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000190InsertCode4AllPhisInMeth(Function *F, TargetMachine &target)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000191{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000192 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000193 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000194 for (Function::iterator BI = F->begin(); BI != F->end(); ++BI) {
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000195
196 BasicBlock *BB = *BI;
197 const BasicBlock::InstListType &InstList = BB->getInstList();
198 BasicBlock::InstListType::const_iterator IIt = InstList.begin();
199
200 // for all instructions in the basic block
201 //
202 for( ; IIt != InstList.end(); ++IIt ) {
203
Chris Lattner69a34cd2002-04-08 22:05:54 +0000204 if (PHINode *PN = dyn_cast<PHINode>(*IIt)) {
205 // FIXME: This is probably wrong...
206 Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000207
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000208 // for each incoming value of the phi, insert phi elimination
209 //
Vikram S. Adve629b70f2002-05-19 15:31:08 +0000210 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
211 { // insert the copy instruction to the predecessor BB
212 vector<MachineInstr*> mvec, CpVec;
213 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
214 mvec);
215 for (vector<MachineInstr*>::iterator MI=mvec.begin();
216 MI != mvec.end(); ++MI)
217 {
218 vector<MachineInstr*> CpVec2 =
219 FixConstantOperandsForInstr(PN, *MI, target);
220 CpVec2.push_back(*MI);
221 CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
222 }
223
224 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
225 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000226
Vikram S. Adve629b70f2002-05-19 15:31:08 +0000227 vector<MachineInstr*> mvec;
228 target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
229
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000230 // get an iterator to machine instructions in the BB
231 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
232
Vikram S. Adve629b70f2002-05-19 15:31:08 +0000233 bbMvec.insert( bbMvec.begin(), mvec.begin(), mvec.end());
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000234 }
235 else break; // since PHI nodes can only be at the top
236
237 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000238 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000239}
240
241
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000242//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000243// Function PostprocessMachineCodeForTree
244//
245// Apply any final cleanups to machine code for the root of a subtree
246// after selection for all its children has been completed.
247//---------------------------------------------------------------------------
248
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000249static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000250PostprocessMachineCodeForTree(InstructionNode* instrNode,
251 int ruleForNode,
252 short* nts,
253 TargetMachine &target)
254{
255 // Fix up any constant operands in the machine instructions to either
256 // use an immediate field or to load the constant into a register
257 // Walk backwards and use direct indexes to allow insertion before current
258 //
259 Instruction* vmInstr = instrNode->getInstruction();
Chris Lattner06cb1b72002-02-03 07:33:46 +0000260 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000261 for (int i = (int) mvec.size()-1; i >= 0; i--)
262 {
Chris Lattner697954c2002-01-20 22:54:45 +0000263 std::vector<MachineInstr*> loadConstVec =
Vikram S. Adve6d353262001-10-17 23:57:50 +0000264 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
265
266 if (loadConstVec.size() > 0)
267 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
268 }
269}
270
271//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000272// Function SelectInstructionsForTree
273//
274// Recursively walk the tree to select instructions.
275// Do this top-down so that child instructions can exploit decisions
276// made at the child instructions.
277//
278// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
279// a branch-on-integer-register instruction, then the setle node
280// can use that information to avoid generating the SUBcc instruction.
281//
282// Note that this cannot be done bottom-up because setle must do this
283// only if it is a child of the branch (otherwise, the result of setle
284// may be used by multiple instructions).
285//---------------------------------------------------------------------------
286
Vikram S. Adve6e447182001-09-18 12:56:28 +0000287bool
288SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000289 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000290{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000291 // Get the rule that matches this node.
292 //
293 int ruleForNode = burm_rule(treeRoot->state, goalnt);
294
Vikram S. Adve6e447182001-09-18 12:56:28 +0000295 if (ruleForNode == 0)
296 {
Chris Lattner697954c2002-01-20 22:54:45 +0000297 cerr << "Could not match instruction tree for instr selection\n";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000298 assert(0);
299 return true;
300 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000301
302 // Get this rule's non-terminals and the corresponding child nodes (if any)
303 //
304 short *nts = burm_nts[ruleForNode];
305
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000306 // First, select instructions for the current node and rule.
307 // (If this is a list node, not an instruction, then skip this step).
308 // This function is specific to the target architecture.
309 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000310 if (treeRoot->opLabel != VRegListOp)
311 {
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000312 vector<MachineInstr*> minstrVec;
313
Vikram S. Adve6e447182001-09-18 12:56:28 +0000314 InstructionNode* instrNode = (InstructionNode*)treeRoot;
315 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000316
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000317 GetInstructionsByRule(instrNode, ruleForNode, nts, target, minstrVec);
318
Chris Lattner06cb1b72002-02-03 07:33:46 +0000319 MachineCodeForInstruction &mvec =
320 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000321 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000322 }
323
324 // Then, recursively compile the child nodes, if any.
325 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000326 if (nts[0])
327 { // i.e., there is at least one kid
328 InstrTreeNode* kids[2];
329 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000330 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000331
332 // First skip over any chain rules so that we don't visit
333 // the current node again.
334 //
335 while (ThisIsAChainRule(currentRule))
336 {
337 currentRule = burm_rule(treeRoot->state, nts[0]);
338 nts = burm_nts[currentRule];
339 burm_kids(treeRoot, currentRule, kids);
340 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000341
Vikram S. Adve6e447182001-09-18 12:56:28 +0000342 // Now we have the first non-chain rule so we have found
343 // the actual child nodes. Recursively compile them.
344 //
345 for (int i = 0; nts[i]; i++)
346 {
347 assert(i < 2);
348 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
349 if (nodeType == InstrTreeNode::NTVRegListNode ||
350 nodeType == InstrTreeNode::NTInstructionNode)
351 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000352 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000353 return true; // failure
354 }
355 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000356 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000357
Vikram S. Adve6d353262001-10-17 23:57:50 +0000358 // Finally, do any postprocessing on this node after its children
359 // have been translated
360 //
361 if (treeRoot->opLabel != VRegListOp)
362 {
363 InstructionNode* instrNode = (InstructionNode*)treeRoot;
364 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
365 }
366
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000367 return false; // success
368}
369