blob: b27f9022bba8d01c42d91ce1d7de57372dacc72f [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)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000126 for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) {
127 MachineCodeForInstruction &mvec =MachineCodeForInstruction::get(II);
128 for (unsigned i=0; i < mvec.size(); i++)
129 BI->getMachineInstrVec().push_back(mvec[i]);
Vikram S. Adve76d35202001-07-30 18:48:43 +0000130 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000131
132 // Insert phi elimination code -- added by Ruchira
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000133 InsertCode4AllPhisInMeth(F, target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000134
Vikram S. Adve76d35202001-07-30 18:48:43 +0000135
Vikram S. Adve6e447182001-09-18 12:56:28 +0000136 if (SelectDebugLevel >= Select_PrintMachineCode)
137 {
Chris Lattner697954c2002-01-20 22:54:45 +0000138 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000139 MachineCodeForMethod::get(F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000140 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000141
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000142 return false;
143}
144
145
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000146//*********************** Private Functions *****************************/
147
148
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000149//-------------------------------------------------------------------------
150// Thid method inserts a copy instruction to a predecessor BB as a result
151// of phi elimination.
152//-------------------------------------------------------------------------
153
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000154void
155InsertPhiElimInstructions(BasicBlock *BB, const vector<MachineInstr*>& CpVec)
156{
Chris Lattner455889a2002-02-12 22:39:50 +0000157 Instruction *TermInst = (Instruction*)BB->getTerminator();
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000158 MachineCodeForInstruction &MC4Term =MachineCodeForInstruction::get(TermInst);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000159 MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000160
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000161 assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000162
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000163 // get an iterator to machine instructions in the BB
164 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
165 MachineCodeForBasicBlock::iterator MCIt = bbMvec.begin();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000166
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000167 // find the position of first machine instruction generated by the
168 // terminator of this BB
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000169 for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt )
170 ;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000171 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000172
173 // insert the copy instructions just before the first machine instruction
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000174 // generated for the terminator
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000175 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
176
Ruchira Sasanka71309382001-11-12 19:42:27 +0000177 //cerr << "\nPhiElimination copy inst: " << *CopyInstVec[0];
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000178}
179
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000180
181//-------------------------------------------------------------------------
182// This method inserts phi elimination code for all BBs in a method
183//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000184
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000185void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000186InsertCode4AllPhisInMeth(Function *F, TargetMachine &target)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000187{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000188 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000189 //
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000190 for (Function::iterator BB = F->begin(); BB != F->end(); ++BB) {
191 BasicBlock::InstListType &InstList = BB->getInstList();
192 for (BasicBlock::iterator IIt = InstList.begin();
193 PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) {
194 // FIXME: This is probably wrong...
195 Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000196
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000197 // for each incoming value of the phi, insert phi elimination
198 //
199 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
200 // insert the copy instruction to the predecessor BB
201 vector<MachineInstr*> mvec, CpVec;
202 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
203 mvec);
204 for (vector<MachineInstr*>::iterator MI=mvec.begin();
205 MI != mvec.end(); ++MI) {
206 vector<MachineInstr*> CpVec2 =
207 FixConstantOperandsForInstr(PN, *MI, target);
208 CpVec2.push_back(*MI);
209 CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
210 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000211
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000212 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000213 }
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000214
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000215 vector<MachineInstr*> mvec;
216 target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
217
218 // get an iterator to machine instructions in the BB
219 MachineCodeForBasicBlock& bbMvec = BB->getMachineInstrVec();
220
221 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000222 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000223 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000224}
225
226
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000227//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000228// Function PostprocessMachineCodeForTree
229//
230// Apply any final cleanups to machine code for the root of a subtree
231// after selection for all its children has been completed.
232//---------------------------------------------------------------------------
233
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000234static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000235PostprocessMachineCodeForTree(InstructionNode* instrNode,
236 int ruleForNode,
237 short* nts,
238 TargetMachine &target)
239{
240 // Fix up any constant operands in the machine instructions to either
241 // use an immediate field or to load the constant into a register
242 // Walk backwards and use direct indexes to allow insertion before current
243 //
244 Instruction* vmInstr = instrNode->getInstruction();
Chris Lattner06cb1b72002-02-03 07:33:46 +0000245 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000246 for (int i = (int) mvec.size()-1; i >= 0; i--)
247 {
Chris Lattner697954c2002-01-20 22:54:45 +0000248 std::vector<MachineInstr*> loadConstVec =
Vikram S. Adve6d353262001-10-17 23:57:50 +0000249 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
250
251 if (loadConstVec.size() > 0)
252 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
253 }
254}
255
256//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000257// Function SelectInstructionsForTree
258//
259// Recursively walk the tree to select instructions.
260// Do this top-down so that child instructions can exploit decisions
261// made at the child instructions.
262//
263// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
264// a branch-on-integer-register instruction, then the setle node
265// can use that information to avoid generating the SUBcc instruction.
266//
267// Note that this cannot be done bottom-up because setle must do this
268// only if it is a child of the branch (otherwise, the result of setle
269// may be used by multiple instructions).
270//---------------------------------------------------------------------------
271
Vikram S. Adve6e447182001-09-18 12:56:28 +0000272bool
273SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000274 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000275{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000276 // Get the rule that matches this node.
277 //
278 int ruleForNode = burm_rule(treeRoot->state, goalnt);
279
Vikram S. Adve6e447182001-09-18 12:56:28 +0000280 if (ruleForNode == 0)
281 {
Chris Lattner697954c2002-01-20 22:54:45 +0000282 cerr << "Could not match instruction tree for instr selection\n";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000283 assert(0);
284 return true;
285 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000286
287 // Get this rule's non-terminals and the corresponding child nodes (if any)
288 //
289 short *nts = burm_nts[ruleForNode];
290
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000291 // First, select instructions for the current node and rule.
292 // (If this is a list node, not an instruction, then skip this step).
293 // This function is specific to the target architecture.
294 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000295 if (treeRoot->opLabel != VRegListOp)
296 {
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000297 vector<MachineInstr*> minstrVec;
298
Vikram S. Adve6e447182001-09-18 12:56:28 +0000299 InstructionNode* instrNode = (InstructionNode*)treeRoot;
300 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000301
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000302 GetInstructionsByRule(instrNode, ruleForNode, nts, target, minstrVec);
303
Chris Lattner06cb1b72002-02-03 07:33:46 +0000304 MachineCodeForInstruction &mvec =
305 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000306 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000307 }
308
309 // Then, recursively compile the child nodes, if any.
310 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000311 if (nts[0])
312 { // i.e., there is at least one kid
313 InstrTreeNode* kids[2];
314 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000315 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000316
317 // First skip over any chain rules so that we don't visit
318 // the current node again.
319 //
320 while (ThisIsAChainRule(currentRule))
321 {
322 currentRule = burm_rule(treeRoot->state, nts[0]);
323 nts = burm_nts[currentRule];
324 burm_kids(treeRoot, currentRule, kids);
325 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000326
Vikram S. Adve6e447182001-09-18 12:56:28 +0000327 // Now we have the first non-chain rule so we have found
328 // the actual child nodes. Recursively compile them.
329 //
330 for (int i = 0; nts[i]; i++)
331 {
332 assert(i < 2);
333 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
334 if (nodeType == InstrTreeNode::NTVRegListNode ||
335 nodeType == InstrTreeNode::NTInstructionNode)
336 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000337 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000338 return true; // failure
339 }
340 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000341 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000342
Vikram S. Adve6d353262001-10-17 23:57:50 +0000343 // Finally, do any postprocessing on this node after its children
344 // have been translated
345 //
346 if (treeRoot->opLabel != VRegListOp)
347 {
348 InstructionNode* instrNode = (InstructionNode*)treeRoot;
349 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
350 }
351
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000352 return false; // success
353}
354