blob: 1724ce448968eadfc0502d4053125ab6700d6550 [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 Lattner5f6baf72001-09-12 16:34:03 +000042cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags,
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 //
210 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000211 // insert the copy instruction to the predecessor BB
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000212 MachineInstr *CpMI =
213 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i),
214 PhiCpRes);
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000215
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000216 vector<MachineInstr*> CpVec = FixConstantOperandsForInstr(PN, CpMI,
217 target);
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000218 CpVec.push_back(CpMI);
219
220 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000221 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000222
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000223 MachineInstr *CpMI2 =
224 target.getRegInfo().cpValue2Value(PhiCpRes, PN);
225
226 // get an iterator to machine instructions in the BB
227 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
228
229 bbMvec.insert( bbMvec.begin(), CpMI2);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000230 }
231 else break; // since PHI nodes can only be at the top
232
233 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000234 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000235}
236
237
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000238//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000239// Function PostprocessMachineCodeForTree
240//
241// Apply any final cleanups to machine code for the root of a subtree
242// after selection for all its children has been completed.
243//---------------------------------------------------------------------------
244
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000245static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000246PostprocessMachineCodeForTree(InstructionNode* instrNode,
247 int ruleForNode,
248 short* nts,
249 TargetMachine &target)
250{
251 // Fix up any constant operands in the machine instructions to either
252 // use an immediate field or to load the constant into a register
253 // Walk backwards and use direct indexes to allow insertion before current
254 //
255 Instruction* vmInstr = instrNode->getInstruction();
Chris Lattner06cb1b72002-02-03 07:33:46 +0000256 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000257 for (int i = (int) mvec.size()-1; i >= 0; i--)
258 {
Chris Lattner697954c2002-01-20 22:54:45 +0000259 std::vector<MachineInstr*> loadConstVec =
Vikram S. Adve6d353262001-10-17 23:57:50 +0000260 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
261
262 if (loadConstVec.size() > 0)
263 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
264 }
265}
266
267//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000268// Function SelectInstructionsForTree
269//
270// Recursively walk the tree to select instructions.
271// Do this top-down so that child instructions can exploit decisions
272// made at the child instructions.
273//
274// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
275// a branch-on-integer-register instruction, then the setle node
276// can use that information to avoid generating the SUBcc instruction.
277//
278// Note that this cannot be done bottom-up because setle must do this
279// only if it is a child of the branch (otherwise, the result of setle
280// may be used by multiple instructions).
281//---------------------------------------------------------------------------
282
Vikram S. Adve6e447182001-09-18 12:56:28 +0000283bool
284SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000285 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000286{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000287 // Get the rule that matches this node.
288 //
289 int ruleForNode = burm_rule(treeRoot->state, goalnt);
290
Vikram S. Adve6e447182001-09-18 12:56:28 +0000291 if (ruleForNode == 0)
292 {
Chris Lattner697954c2002-01-20 22:54:45 +0000293 cerr << "Could not match instruction tree for instr selection\n";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000294 assert(0);
295 return true;
296 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000297
298 // Get this rule's non-terminals and the corresponding child nodes (if any)
299 //
300 short *nts = burm_nts[ruleForNode];
301
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000302 // First, select instructions for the current node and rule.
303 // (If this is a list node, not an instruction, then skip this step).
304 // This function is specific to the target architecture.
305 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000306 if (treeRoot->opLabel != VRegListOp)
307 {
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000308 vector<MachineInstr*> minstrVec;
309
Vikram S. Adve6e447182001-09-18 12:56:28 +0000310 InstructionNode* instrNode = (InstructionNode*)treeRoot;
311 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000312
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000313 GetInstructionsByRule(instrNode, ruleForNode, nts, target, minstrVec);
314
Chris Lattner06cb1b72002-02-03 07:33:46 +0000315 MachineCodeForInstruction &mvec =
316 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000317 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000318 }
319
320 // Then, recursively compile the child nodes, if any.
321 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000322 if (nts[0])
323 { // i.e., there is at least one kid
324 InstrTreeNode* kids[2];
325 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000326 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000327
328 // First skip over any chain rules so that we don't visit
329 // the current node again.
330 //
331 while (ThisIsAChainRule(currentRule))
332 {
333 currentRule = burm_rule(treeRoot->state, nts[0]);
334 nts = burm_nts[currentRule];
335 burm_kids(treeRoot, currentRule, kids);
336 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000337
Vikram S. Adve6e447182001-09-18 12:56:28 +0000338 // Now we have the first non-chain rule so we have found
339 // the actual child nodes. Recursively compile them.
340 //
341 for (int i = 0; nts[i]; i++)
342 {
343 assert(i < 2);
344 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
345 if (nodeType == InstrTreeNode::NTVRegListNode ||
346 nodeType == InstrTreeNode::NTInstructionNode)
347 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000348 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000349 return true; // failure
350 }
351 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000352 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000353
Vikram S. Adve6d353262001-10-17 23:57:50 +0000354 // Finally, do any postprocessing on this node after its children
355 // have been translated
356 //
357 if (treeRoot->opLabel != VRegListOp)
358 {
359 InstructionNode* instrNode = (InstructionNode*)treeRoot;
360 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
361 }
362
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000363 return false; // success
364}
365