blob: d8f8981362285b8ca64594154874002f0a68287e [file] [log] [blame]
Chris Lattner2e1749b2002-07-30 03:57:36 +00001//===- InstrSelection.cpp - Machine Independant Inst Selection Driver -----===//
2//
3// Machine-independent driver file for instruction selection. This file
4// constructs a forest of BURG instruction trees and then uses the
5// BURG-generated tree grammar (BURM) to find the optimal instruction sequences
6// for a given machine.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00007//
Chris Lattner2e1749b2002-07-30 03:57:36 +00008//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00009
Chris Lattnerfeb60592001-09-07 17:15:18 +000010#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve6d353262001-10-17 23:57:50 +000011#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattner06cb1b72002-02-03 07:33:46 +000012#include "llvm/CodeGen/InstrForest.h"
13#include "llvm/CodeGen/MachineCodeForInstruction.h"
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +000014#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattner06cb1b72002-02-03 07:33:46 +000015#include "llvm/CodeGen/MachineCodeForMethod.h"
16#include "llvm/Target/MachineRegInfo.h"
17#include "llvm/Target/TargetMachine.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000018#include "llvm/Function.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000019#include "llvm/iPHINode.h"
Chris Lattner2e1749b2002-07-30 03:57:36 +000020#include "llvm/Pass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000022using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000023using std::vector;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000024
Chris Lattner2e1749b2002-07-30 03:57:36 +000025namespace {
26 //===--------------------------------------------------------------------===//
27 // SelectDebugLevel - Allow command line control over debugging.
28 //
29 enum SelectDebugLevel_t {
30 Select_NoDebugInfo,
31 Select_PrintMachineCode,
32 Select_DebugInstTrees,
33 Select_DebugBurgTrees,
34 };
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000035
Chris Lattner2e1749b2002-07-30 03:57:36 +000036 // Enable Debug Options to be specified on the command line
37 cl::opt<SelectDebugLevel_t>
38 SelectDebugLevel("dselect", cl::Hidden,
39 cl::desc("enable instruction selection debug information"),
40 cl::values(
41 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
42 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
43 clEnumValN(Select_DebugInstTrees, "i",
44 "print debugging info for instruction selection"),
45 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"),
46 0));
47
48
49 //===--------------------------------------------------------------------===//
50 // InstructionSelection Pass
51 //
52 // This is the actual pass object that drives the instruction selection
53 // process.
54 //
55 class InstructionSelection : public FunctionPass {
56 TargetMachine &Target;
57 void InsertCodeForPhis(Function &F);
58 void InsertPhiElimInstructions(BasicBlock *BB,
Chris Lattnerb91b31c2002-08-09 20:05:34 +000059 const vector<MachineInstr*>& CpVec);
Chris Lattner2e1749b2002-07-30 03:57:36 +000060 void SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt);
61 void PostprocessMachineCodeForTree(InstructionNode* instrNode,
62 int ruleForNode, short* nts);
63 public:
64 InstructionSelection(TargetMachine &T) : Target(T) {}
65
66 bool runOnFunction(Function &F);
67 };
68}
69
70// Register the pass...
71static RegisterLLC<InstructionSelection>
72X("instselect", "Instruction Selection", createInstructionSelectionPass);
73
74
75bool InstructionSelection::runOnFunction(Function &F)
76{
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000077 //
78 // Build the instruction trees to be given as inputs to BURG.
79 //
Chris Lattner2e1749b2002-07-30 03:57:36 +000080 InstrForest instrForest(&F);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000081
82 if (SelectDebugLevel >= Select_DebugInstTrees)
83 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000084 cerr << "\n\n*** Input to instruction selection for function "
Chris Lattner2e1749b2002-07-30 03:57:36 +000085 << F.getName() << "\n\n" << F
86 << "\n\n*** Instruction trees for function "
87 << F.getName() << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000088 instrForest.dump();
89 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000090
91 //
92 // Invoke BURG instruction selection for each tree
93 //
Vikram S. Adve4e7bc492002-03-24 03:36:52 +000094 for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
95 RI != instrForest.roots_end(); ++RI)
Vikram S. Adve6e447182001-09-18 12:56:28 +000096 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +000097 InstructionNode* basicNode = *RI;
98 assert(basicNode->parent() == NULL && "A `root' node has a parent?");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000099
Vikram S. Adve6e447182001-09-18 12:56:28 +0000100 // Invoke BURM to label each tree node with a state
101 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000102
Vikram S. Adve6e447182001-09-18 12:56:28 +0000103 if (SelectDebugLevel >= Select_DebugBurgTrees)
104 {
105 printcover(basicNode, 1, 0);
106 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
107 printMatches(basicNode);
108 }
109
110 // Then recursively walk the tree to select instructions
Chris Lattner2e1749b2002-07-30 03:57:36 +0000111 SelectInstructionsForTree(basicNode, /*goalnt*/1);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000112 }
113
Vikram S. Adve76d35202001-07-30 18:48:43 +0000114 //
115 // Record instructions in the vector for each basic block
116 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000117 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000118 for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) {
Chris Lattner2e1749b2002-07-30 03:57:36 +0000119 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(II);
120 MachineCodeForBasicBlock &MCBB = MachineCodeForBasicBlock::get(BI);
121 MCBB.insert(MCBB.end(), mvec.begin(), mvec.end());
Vikram S. Adve76d35202001-07-30 18:48:43 +0000122 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000123
Chris Lattner2e1749b2002-07-30 03:57:36 +0000124 // Insert phi elimination code
125 InsertCodeForPhis(F);
Vikram S. Adve76d35202001-07-30 18:48:43 +0000126
Vikram S. Adve6e447182001-09-18 12:56:28 +0000127 if (SelectDebugLevel >= Select_PrintMachineCode)
128 {
Chris Lattner697954c2002-01-20 22:54:45 +0000129 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Chris Lattner2e1749b2002-07-30 03:57:36 +0000130 MachineCodeForMethod::get(&F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000131 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000132
Chris Lattner2e1749b2002-07-30 03:57:36 +0000133 return true;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000134}
135
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000136
137//-------------------------------------------------------------------------
138// This method inserts phi elimination code for all BBs in a method
139//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000140
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000141void
Chris Lattner2e1749b2002-07-30 03:57:36 +0000142InstructionSelection::InsertCodeForPhis(Function &F)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000143{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000144 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000145 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000146 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000147 BasicBlock::InstListType &InstList = BB->getInstList();
148 for (BasicBlock::iterator IIt = InstList.begin();
149 PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) {
150 // FIXME: This is probably wrong...
151 Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000152
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000153 // for each incoming value of the phi, insert phi elimination
154 //
155 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
156 // insert the copy instruction to the predecessor BB
157 vector<MachineInstr*> mvec, CpVec;
Chris Lattner2e1749b2002-07-30 03:57:36 +0000158 Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000159 mvec);
160 for (vector<MachineInstr*>::iterator MI=mvec.begin();
161 MI != mvec.end(); ++MI) {
162 vector<MachineInstr*> CpVec2 =
Chris Lattner2e1749b2002-07-30 03:57:36 +0000163 FixConstantOperandsForInstr(PN, *MI, Target);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000164 CpVec2.push_back(*MI);
165 CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
166 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000167
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000168 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000169 }
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000170
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000171 vector<MachineInstr*> mvec;
Chris Lattner2e1749b2002-07-30 03:57:36 +0000172 Target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000173
174 // get an iterator to machine instructions in the BB
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000175 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(BB);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000176
177 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000178 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000179 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000180}
181
Chris Lattner2e1749b2002-07-30 03:57:36 +0000182//-------------------------------------------------------------------------
183// Thid method inserts a copy instruction to a predecessor BB as a result
184// of phi elimination.
185//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000186
Chris Lattner2e1749b2002-07-30 03:57:36 +0000187void
188InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB,
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000189 const vector<MachineInstr*>& CpVec)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000190{
191 Instruction *TermInst = (Instruction*)BB->getTerminator();
192 MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst);
193 MachineInstr *FirstMIOfTerm = MC4Term.front();
194
195 assert (FirstMIOfTerm && "No Machine Instrs for terminator");
196
197 MachineCodeForBasicBlock &bbMvec = MachineCodeForBasicBlock::get(BB);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000198
Chris Lattner2e1749b2002-07-30 03:57:36 +0000199 // find the position of first machine instruction generated by the
200 // terminator of this BB
201 MachineCodeForBasicBlock::iterator MCIt =
202 std::find(bbMvec.begin(), bbMvec.end(), FirstMIOfTerm);
203
204 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
205
206 // insert the copy instructions just before the first machine instruction
207 // generated for the terminator
208 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
Vikram S. Adve6d353262001-10-17 23:57:50 +0000209}
210
Chris Lattner2e1749b2002-07-30 03:57:36 +0000211
Vikram S. Adve6d353262001-10-17 23:57:50 +0000212//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000213// Function SelectInstructionsForTree
214//
215// Recursively walk the tree to select instructions.
216// Do this top-down so that child instructions can exploit decisions
217// made at the child instructions.
218//
219// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
220// a branch-on-integer-register instruction, then the setle node
221// can use that information to avoid generating the SUBcc instruction.
222//
223// Note that this cannot be done bottom-up because setle must do this
224// only if it is a child of the branch (otherwise, the result of setle
225// may be used by multiple instructions).
226//---------------------------------------------------------------------------
227
Chris Lattner2e1749b2002-07-30 03:57:36 +0000228void
229InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
230 int goalnt)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000231{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000232 // Get the rule that matches this node.
233 //
234 int ruleForNode = burm_rule(treeRoot->state, goalnt);
235
Chris Lattner2e1749b2002-07-30 03:57:36 +0000236 if (ruleForNode == 0) {
237 cerr << "Could not match instruction tree for instr selection\n";
238 abort();
239 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000240
241 // Get this rule's non-terminals and the corresponding child nodes (if any)
242 //
243 short *nts = burm_nts[ruleForNode];
244
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000245 // First, select instructions for the current node and rule.
246 // (If this is a list node, not an instruction, then skip this step).
247 // This function is specific to the target architecture.
248 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000249 if (treeRoot->opLabel != VRegListOp)
250 {
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000251 vector<MachineInstr*> minstrVec;
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000252
Vikram S. Adve6e447182001-09-18 12:56:28 +0000253 InstructionNode* instrNode = (InstructionNode*)treeRoot;
254 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000255
Chris Lattner2e1749b2002-07-30 03:57:36 +0000256 GetInstructionsByRule(instrNode, ruleForNode, nts, Target, minstrVec);
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000257
Chris Lattner06cb1b72002-02-03 07:33:46 +0000258 MachineCodeForInstruction &mvec =
259 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000260 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000261 }
262
263 // Then, recursively compile the child nodes, if any.
264 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000265 if (nts[0])
266 { // i.e., there is at least one kid
267 InstrTreeNode* kids[2];
268 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000269 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000270
271 // First skip over any chain rules so that we don't visit
272 // the current node again.
273 //
274 while (ThisIsAChainRule(currentRule))
275 {
276 currentRule = burm_rule(treeRoot->state, nts[0]);
277 nts = burm_nts[currentRule];
278 burm_kids(treeRoot, currentRule, kids);
279 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000280
Vikram S. Adve6e447182001-09-18 12:56:28 +0000281 // Now we have the first non-chain rule so we have found
282 // the actual child nodes. Recursively compile them.
283 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000284 for (unsigned i = 0; nts[i]; i++)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000285 {
286 assert(i < 2);
287 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
288 if (nodeType == InstrTreeNode::NTVRegListNode ||
289 nodeType == InstrTreeNode::NTInstructionNode)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000290 SelectInstructionsForTree(kids[i], nts[i]);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000291 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000292 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000293
Vikram S. Adve6d353262001-10-17 23:57:50 +0000294 // Finally, do any postprocessing on this node after its children
295 // have been translated
296 //
297 if (treeRoot->opLabel != VRegListOp)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000298 PostprocessMachineCodeForTree((InstructionNode*)treeRoot, ruleForNode, nts);
299}
300
301//---------------------------------------------------------------------------
302// Function PostprocessMachineCodeForTree
303//
304// Apply any final cleanups to machine code for the root of a subtree
305// after selection for all its children has been completed.
306//
307void
308InstructionSelection::PostprocessMachineCodeForTree(InstructionNode* instrNode,
309 int ruleForNode,
310 short* nts)
311{
312 // Fix up any constant operands in the machine instructions to either
313 // use an immediate field or to load the constant into a register
314 // Walk backwards and use direct indexes to allow insertion before current
315 //
316 Instruction* vmInstr = instrNode->getInstruction();
317 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000318 for (unsigned i = mvec.size(); i != 0; --i)
Vikram S. Adve6d353262001-10-17 23:57:50 +0000319 {
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000320 vector<MachineInstr*> loadConstVec =
321 FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target);
Chris Lattner2e1749b2002-07-30 03:57:36 +0000322
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000323 mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end());
Vikram S. Adve6d353262001-10-17 23:57:50 +0000324 }
Chris Lattner2e1749b2002-07-30 03:57:36 +0000325}
326
327
328
329//===----------------------------------------------------------------------===//
330// createInstructionSelectionPass - Public entrypoint for instruction selection
331// and this file as a whole...
332//
333Pass *createInstructionSelectionPass(TargetMachine &T) {
334 return new InstructionSelection(T);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000335}
336