blob: 86e54d0b16582a1c33d1eca7e7bad4f32aad3d7b [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 +000029using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000030using std::vector;
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
Chris Lattner5ff62e92002-07-22 02:10:13 +000042
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000043// Enable Debug Options to be specified on the command line
Chris Lattner5ff62e92002-07-22 02:10:13 +000044static cl::opt<SelectDebugLevel_t>
45SelectDebugLevel("dselect", cl::Hidden,
46 cl::desc("enable instruction selection debugging information"),
47 cl::values(
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000048 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
49 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000050 clEnumValN(Select_DebugInstTrees, "i",
51 "print debugging info for instruction selection"),
52 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"),
53 0));
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000054
55
Vikram S. Adve7ad10462001-10-22 13:51:09 +000056//******************** Forward Function Declarations ***********************/
57
58
59static bool SelectInstructionsForTree (InstrTreeNode* treeRoot,
60 int goalnt,
61 TargetMachine &target);
62
63static void PostprocessMachineCodeForTree(InstructionNode* instrNode,
64 int ruleForNode,
65 short* nts,
66 TargetMachine &target);
67
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000068static void InsertCode4AllPhisInMeth(Function *F, TargetMachine &target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +000069
70
Vikram S. Adve7ad10462001-10-22 13:51:09 +000071
72//******************* Externally Visible Functions *************************/
73
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000074
75//---------------------------------------------------------------------------
76// Entry point for instruction selection using BURG.
77// Returns true if instruction selection failed, false otherwise.
78//---------------------------------------------------------------------------
79
Vikram S. Adve6e447182001-09-18 12:56:28 +000080bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081SelectInstructionsForMethod(Function *F, TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +000082{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000083 bool failed = false;
84
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000085 //
86 // Build the instruction trees to be given as inputs to BURG.
87 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000088 InstrForest instrForest(F);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000089
90 if (SelectDebugLevel >= Select_DebugInstTrees)
91 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000092 cerr << "\n\n*** Input to instruction selection for function "
93 << F->getName() << "\n\n";
94 F->dump();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +000095
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000096 cerr << "\n\n*** Instruction trees for function "
97 << F->getName() << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000098 instrForest.dump();
99 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000100
101 //
102 // Invoke BURG instruction selection for each tree
103 //
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000104 for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
105 RI != instrForest.roots_end(); ++RI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000106 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000107 InstructionNode* basicNode = *RI;
108 assert(basicNode->parent() == NULL && "A `root' node has a parent?");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000109
Vikram S. Adve6e447182001-09-18 12:56:28 +0000110 // Invoke BURM to label each tree node with a state
111 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000112
Vikram S. Adve6e447182001-09-18 12:56:28 +0000113 if (SelectDebugLevel >= Select_DebugBurgTrees)
114 {
115 printcover(basicNode, 1, 0);
116 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
117 printMatches(basicNode);
118 }
119
120 // Then recursively walk the tree to select instructions
Vikram S. Adve6d353262001-10-17 23:57:50 +0000121 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000122 {
123 failed = true;
124 break;
125 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000126 }
127
Vikram S. Adve76d35202001-07-30 18:48:43 +0000128 //
129 // Record instructions in the vector for each basic block
130 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000131 for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000132 for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) {
133 MachineCodeForInstruction &mvec =MachineCodeForInstruction::get(II);
134 for (unsigned i=0; i < mvec.size(); i++)
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000135 MachineCodeForBasicBlock::get(BI).push_back(mvec[i]);
Vikram S. Adve76d35202001-07-30 18:48:43 +0000136 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000137
138 // Insert phi elimination code -- added by Ruchira
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000139 InsertCode4AllPhisInMeth(F, target);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000140
Vikram S. Adve76d35202001-07-30 18:48:43 +0000141
Vikram S. Adve6e447182001-09-18 12:56:28 +0000142 if (SelectDebugLevel >= Select_PrintMachineCode)
143 {
Chris Lattner697954c2002-01-20 22:54:45 +0000144 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000145 MachineCodeForMethod::get(F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000146 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000147
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000148 return false;
149}
150
151
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000152//*********************** Private Functions *****************************/
153
154
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000155//-------------------------------------------------------------------------
156// Thid method inserts a copy instruction to a predecessor BB as a result
157// of phi elimination.
158//-------------------------------------------------------------------------
159
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000160void
Anand Shuklacfb22d32002-06-25 20:55:50 +0000161InsertPhiElimInstructions(BasicBlock *BB, const std::vector<MachineInstr*>& CpVec)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000162{
Chris Lattner455889a2002-02-12 22:39:50 +0000163 Instruction *TermInst = (Instruction*)BB->getTerminator();
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000164 MachineCodeForInstruction &MC4Term =MachineCodeForInstruction::get(TermInst);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000165 MachineInstr *FirstMIOfTerm = *( MC4Term.begin() );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000166
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000167 assert( FirstMIOfTerm && "No Machine Instrs for terminator" );
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000168
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000169 // get an iterator to machine instructions in the BB
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000170 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(BB);
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000171 MachineCodeForBasicBlock::iterator MCIt = bbMvec.begin();
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000172
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000173 // find the position of first machine instruction generated by the
174 // terminator of this BB
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000175 for( ; (MCIt != bbMvec.end()) && (*MCIt != FirstMIOfTerm) ; ++MCIt )
176 ;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000177 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000178
179 // insert the copy instructions just before the first machine instruction
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000180 // generated for the terminator
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000181 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
182
Ruchira Sasanka71309382001-11-12 19:42:27 +0000183 //cerr << "\nPhiElimination copy inst: " << *CopyInstVec[0];
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000184}
185
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000186
187//-------------------------------------------------------------------------
188// This method inserts phi elimination code for all BBs in a method
189//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000190
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000191void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000192InsertCode4AllPhisInMeth(Function *F, TargetMachine &target)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000193{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000194 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000195 //
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000196 for (Function::iterator BB = F->begin(); BB != F->end(); ++BB) {
197 BasicBlock::InstListType &InstList = BB->getInstList();
198 for (BasicBlock::iterator IIt = InstList.begin();
199 PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) {
200 // FIXME: This is probably wrong...
201 Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000202
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000203 // for each incoming value of the phi, insert phi elimination
204 //
205 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
206 // insert the copy instruction to the predecessor BB
207 vector<MachineInstr*> mvec, CpVec;
208 target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
209 mvec);
210 for (vector<MachineInstr*>::iterator MI=mvec.begin();
211 MI != mvec.end(); ++MI) {
212 vector<MachineInstr*> CpVec2 =
213 FixConstantOperandsForInstr(PN, *MI, target);
214 CpVec2.push_back(*MI);
215 CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
216 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000217
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000218 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000219 }
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000220
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000221 vector<MachineInstr*> mvec;
222 target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
223
224 // get an iterator to machine instructions in the BB
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000225 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(BB);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000226
227 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000228 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000229 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000230}
231
232
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000233//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000234// Function PostprocessMachineCodeForTree
235//
236// Apply any final cleanups to machine code for the root of a subtree
237// after selection for all its children has been completed.
238//---------------------------------------------------------------------------
239
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000240static void
Vikram S. Adve6d353262001-10-17 23:57:50 +0000241PostprocessMachineCodeForTree(InstructionNode* instrNode,
242 int ruleForNode,
243 short* nts,
244 TargetMachine &target)
245{
246 // Fix up any constant operands in the machine instructions to either
247 // use an immediate field or to load the constant into a register
248 // Walk backwards and use direct indexes to allow insertion before current
249 //
250 Instruction* vmInstr = instrNode->getInstruction();
Chris Lattner06cb1b72002-02-03 07:33:46 +0000251 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000252 for (int i = (int) mvec.size()-1; i >= 0; i--)
253 {
Chris Lattner697954c2002-01-20 22:54:45 +0000254 std::vector<MachineInstr*> loadConstVec =
Vikram S. Adve6d353262001-10-17 23:57:50 +0000255 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
256
257 if (loadConstVec.size() > 0)
258 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
259 }
260}
261
262//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000263// Function SelectInstructionsForTree
264//
265// Recursively walk the tree to select instructions.
266// Do this top-down so that child instructions can exploit decisions
267// made at the child instructions.
268//
269// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
270// a branch-on-integer-register instruction, then the setle node
271// can use that information to avoid generating the SUBcc instruction.
272//
273// Note that this cannot be done bottom-up because setle must do this
274// only if it is a child of the branch (otherwise, the result of setle
275// may be used by multiple instructions).
276//---------------------------------------------------------------------------
277
Vikram S. Adve6e447182001-09-18 12:56:28 +0000278bool
279SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000280 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000281{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000282 // Get the rule that matches this node.
283 //
284 int ruleForNode = burm_rule(treeRoot->state, goalnt);
285
Vikram S. Adve6e447182001-09-18 12:56:28 +0000286 if (ruleForNode == 0)
287 {
Chris Lattner697954c2002-01-20 22:54:45 +0000288 cerr << "Could not match instruction tree for instr selection\n";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000289 assert(0);
290 return true;
291 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000292
293 // Get this rule's non-terminals and the corresponding child nodes (if any)
294 //
295 short *nts = burm_nts[ruleForNode];
296
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000297 // First, select instructions for the current node and rule.
298 // (If this is a list node, not an instruction, then skip this step).
299 // This function is specific to the target architecture.
300 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000301 if (treeRoot->opLabel != VRegListOp)
302 {
Anand Shuklacfb22d32002-06-25 20:55:50 +0000303 std::vector<MachineInstr*> minstrVec;
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000304
Vikram S. Adve6e447182001-09-18 12:56:28 +0000305 InstructionNode* instrNode = (InstructionNode*)treeRoot;
306 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000307
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000308 GetInstructionsByRule(instrNode, ruleForNode, nts, target, minstrVec);
309
Chris Lattner06cb1b72002-02-03 07:33:46 +0000310 MachineCodeForInstruction &mvec =
311 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000312 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000313 }
314
315 // Then, recursively compile the child nodes, if any.
316 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000317 if (nts[0])
318 { // i.e., there is at least one kid
319 InstrTreeNode* kids[2];
320 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000321 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000322
323 // First skip over any chain rules so that we don't visit
324 // the current node again.
325 //
326 while (ThisIsAChainRule(currentRule))
327 {
328 currentRule = burm_rule(treeRoot->state, nts[0]);
329 nts = burm_nts[currentRule];
330 burm_kids(treeRoot, currentRule, kids);
331 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000332
Vikram S. Adve6e447182001-09-18 12:56:28 +0000333 // Now we have the first non-chain rule so we have found
334 // the actual child nodes. Recursively compile them.
335 //
336 for (int i = 0; nts[i]; i++)
337 {
338 assert(i < 2);
339 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
340 if (nodeType == InstrTreeNode::NTVRegListNode ||
341 nodeType == InstrTreeNode::NTInstructionNode)
342 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000343 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000344 return true; // failure
345 }
346 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000347 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000348
Vikram S. Adve6d353262001-10-17 23:57:50 +0000349 // Finally, do any postprocessing on this node after its children
350 // have been translated
351 //
352 if (treeRoot->opLabel != VRegListOp)
353 {
354 InstructionNode* instrNode = (InstructionNode*)treeRoot;
355 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
356 }
357
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000358 return false; // success
359}
360