blob: df9098f21918322b23974d87a2810b22e12e782f [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 Lattnerd268ad62001-09-11 23:52:11 +000019#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000020#include "llvm/Support/CommandLine.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000021#include "llvm/Instruction.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000022#include "llvm/BasicBlock.h"
23#include "llvm/Method.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000024
Chris Lattnerd268ad62001-09-11 23:52:11 +000025static bool SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +000026 TargetMachine &target);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000027
28
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000029enum SelectDebugLevel_t {
30 Select_NoDebugInfo,
31 Select_PrintMachineCode,
32 Select_DebugInstTrees,
33 Select_DebugBurgTrees,
34};
35
36// Enable Debug Options to be specified on the command line
Chris Lattner5f6baf72001-09-12 16:34:03 +000037cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags,
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000038 "enable instruction selection debugging information",
39 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
40 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
Vikram S. Adve6e447182001-09-18 12:56:28 +000041 clEnumValN(Select_DebugInstTrees, "i", "print debugging info for instruction selection "),
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000042 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
43
44
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000045
46//---------------------------------------------------------------------------
47// Entry point for instruction selection using BURG.
48// Returns true if instruction selection failed, false otherwise.
49//---------------------------------------------------------------------------
50
Vikram S. Adve6e447182001-09-18 12:56:28 +000051bool
Vikram S. Adve6d353262001-10-17 23:57:50 +000052SelectInstructionsForMethod(Method* method, TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +000053{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000054 bool failed = false;
55
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000056 //
57 // Build the instruction trees to be given as inputs to BURG.
58 //
Chris Lattner5f6baf72001-09-12 16:34:03 +000059 InstrForest instrForest(method);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000060
61 if (SelectDebugLevel >= Select_DebugInstTrees)
62 {
63 cout << "\n\n*** Instruction trees for method "
64 << (method->hasName()? method->getName() : "")
65 << endl << endl;
66 instrForest.dump();
67 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000068
69 //
70 // Invoke BURG instruction selection for each tree
71 //
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000072 const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet();
Chris Lattner75279cc2001-07-23 03:50:57 +000073 for (hash_set<InstructionNode*>::const_iterator
Chris Lattner0e6530e2001-09-14 03:37:52 +000074 treeRootIter = treeRoots.begin(); treeRootIter != treeRoots.end();
Vikram S. Adve6e447182001-09-18 12:56:28 +000075 ++treeRootIter)
76 {
77 InstrTreeNode* basicNode = *treeRootIter;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000078
Vikram S. Adve6e447182001-09-18 12:56:28 +000079 // Invoke BURM to label each tree node with a state
80 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000081
Vikram S. Adve6e447182001-09-18 12:56:28 +000082 if (SelectDebugLevel >= Select_DebugBurgTrees)
83 {
84 printcover(basicNode, 1, 0);
85 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
86 printMatches(basicNode);
87 }
88
89 // Then recursively walk the tree to select instructions
Vikram S. Adve6d353262001-10-17 23:57:50 +000090 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, target))
Vikram S. Adve6e447182001-09-18 12:56:28 +000091 {
92 failed = true;
93 break;
94 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000095 }
96
Vikram S. Adve76d35202001-07-30 18:48:43 +000097 //
98 // Record instructions in the vector for each basic block
99 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000100 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI)
101 {
102 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
103 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
104 {
105 MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
106 for (unsigned i=0; i < mvec.size(); i++)
107 bbMvec.push_back(mvec[i]);
108 }
Vikram S. Adve76d35202001-07-30 18:48:43 +0000109 }
110
Vikram S. Adve6e447182001-09-18 12:56:28 +0000111 if (SelectDebugLevel >= Select_PrintMachineCode)
112 {
113 cout << endl << "*** Machine instructions after INSTRUCTION SELECTION" << endl;
114 PrintMachineInstructions(method);
115 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000116
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000117 return false;
118}
119
120
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000121//*********************** Private Functions *****************************/
122
123
124//---------------------------------------------------------------------------
Vikram S. Adve6d353262001-10-17 23:57:50 +0000125// Function PostprocessMachineCodeForTree
126//
127// Apply any final cleanups to machine code for the root of a subtree
128// after selection for all its children has been completed.
129//---------------------------------------------------------------------------
130
131void
132PostprocessMachineCodeForTree(InstructionNode* instrNode,
133 int ruleForNode,
134 short* nts,
135 TargetMachine &target)
136{
137 // Fix up any constant operands in the machine instructions to either
138 // use an immediate field or to load the constant into a register
139 // Walk backwards and use direct indexes to allow insertion before current
140 //
141 Instruction* vmInstr = instrNode->getInstruction();
142 MachineCodeForVMInstr& mvec = vmInstr->getMachineInstrVec();
143 for (int i = (int) mvec.size()-1; i >= 0; i--)
144 {
145 vector<MachineInstr*> loadConstVec =
146 FixConstantOperandsForInstr(vmInstr, mvec[i], target);
147
148 if (loadConstVec.size() > 0)
149 mvec.insert(mvec.begin()+i, loadConstVec.begin(), loadConstVec.end());
150 }
151}
152
153//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000154// Function SelectInstructionsForTree
155//
156// Recursively walk the tree to select instructions.
157// Do this top-down so that child instructions can exploit decisions
158// made at the child instructions.
159//
160// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
161// a branch-on-integer-register instruction, then the setle node
162// can use that information to avoid generating the SUBcc instruction.
163//
164// Note that this cannot be done bottom-up because setle must do this
165// only if it is a child of the branch (otherwise, the result of setle
166// may be used by multiple instructions).
167//---------------------------------------------------------------------------
168
Vikram S. Adve6e447182001-09-18 12:56:28 +0000169bool
170SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt,
Vikram S. Adve6d353262001-10-17 23:57:50 +0000171 TargetMachine &target)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000172{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000173 // Use a static vector to avoid allocating a new one per VM instruction
174 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
175
176 // Get the rule that matches this node.
177 //
178 int ruleForNode = burm_rule(treeRoot->state, goalnt);
179
Vikram S. Adve6e447182001-09-18 12:56:28 +0000180 if (ruleForNode == 0)
181 {
182 cerr << "Could not match instruction tree for instr selection" << endl;
183 assert(0);
184 return true;
185 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000186
187 // Get this rule's non-terminals and the corresponding child nodes (if any)
188 //
189 short *nts = burm_nts[ruleForNode];
190
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000191 // First, select instructions for the current node and rule.
192 // (If this is a list node, not an instruction, then skip this step).
193 // This function is specific to the target architecture.
194 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000195 if (treeRoot->opLabel != VRegListOp)
196 {
197 InstructionNode* instrNode = (InstructionNode*)treeRoot;
198 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Chris Lattner0e6530e2001-09-14 03:37:52 +0000199
Vikram S. Adve6d353262001-10-17 23:57:50 +0000200 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, target,
Vikram S. Adve6e447182001-09-18 12:56:28 +0000201 minstrVec);
202 assert(N <= MAX_INSTR_PER_VMINSTR);
203 for (unsigned i=0; i < N; i++)
204 {
205 assert(minstrVec[i] != NULL);
206 instrNode->getInstruction()->addMachineInstruction(minstrVec[i]);
207 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000208 }
209
210 // Then, recursively compile the child nodes, if any.
211 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000212 if (nts[0])
213 { // i.e., there is at least one kid
214 InstrTreeNode* kids[2];
215 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000216 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000217
218 // First skip over any chain rules so that we don't visit
219 // the current node again.
220 //
221 while (ThisIsAChainRule(currentRule))
222 {
223 currentRule = burm_rule(treeRoot->state, nts[0]);
224 nts = burm_nts[currentRule];
225 burm_kids(treeRoot, currentRule, kids);
226 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000227
Vikram S. Adve6e447182001-09-18 12:56:28 +0000228 // Now we have the first non-chain rule so we have found
229 // the actual child nodes. Recursively compile them.
230 //
231 for (int i = 0; nts[i]; i++)
232 {
233 assert(i < 2);
234 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
235 if (nodeType == InstrTreeNode::NTVRegListNode ||
236 nodeType == InstrTreeNode::NTInstructionNode)
237 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000238 if (SelectInstructionsForTree(kids[i], nts[i], target))
Vikram S. Adve6e447182001-09-18 12:56:28 +0000239 return true; // failure
240 }
241 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000242 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000243
Vikram S. Adve6d353262001-10-17 23:57:50 +0000244 // Finally, do any postprocessing on this node after its children
245 // have been translated
246 //
247 if (treeRoot->opLabel != VRegListOp)
248 {
249 InstructionNode* instrNode = (InstructionNode*)treeRoot;
250 PostprocessMachineCodeForTree(instrNode, ruleForNode, nts, target);
251 }
252
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000253 return false; // success
254}
255