blob: 28bfb74666d87d96eba944b4787a53d481d0ae58 [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 Lattnera175ed42002-09-08 21:08:43 +000022#include "Support/LeakDetector.h"
Chris Lattner697954c2002-01-20 22:54:45 +000023using std::cerr;
Anand Shuklacfb22d32002-06-25 20:55:50 +000024using std::vector;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000025
Chris Lattner2e1749b2002-07-30 03:57:36 +000026namespace {
27 //===--------------------------------------------------------------------===//
28 // SelectDebugLevel - Allow command line control over debugging.
29 //
30 enum SelectDebugLevel_t {
31 Select_NoDebugInfo,
32 Select_PrintMachineCode,
33 Select_DebugInstTrees,
34 Select_DebugBurgTrees,
35 };
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000036
Chris Lattner2e1749b2002-07-30 03:57:36 +000037 // Enable Debug Options to be specified on the command line
38 cl::opt<SelectDebugLevel_t>
39 SelectDebugLevel("dselect", cl::Hidden,
40 cl::desc("enable instruction selection debug information"),
41 cl::values(
42 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
43 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
44 clEnumValN(Select_DebugInstTrees, "i",
45 "print debugging info for instruction selection"),
46 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"),
47 0));
48
49
50 //===--------------------------------------------------------------------===//
51 // InstructionSelection Pass
52 //
53 // This is the actual pass object that drives the instruction selection
54 // process.
55 //
56 class InstructionSelection : public FunctionPass {
57 TargetMachine &Target;
58 void InsertCodeForPhis(Function &F);
59 void InsertPhiElimInstructions(BasicBlock *BB,
Chris Lattnerb91b31c2002-08-09 20:05:34 +000060 const vector<MachineInstr*>& CpVec);
Chris Lattner2e1749b2002-07-30 03:57:36 +000061 void SelectInstructionsForTree(InstrTreeNode* treeRoot, int goalnt);
62 void PostprocessMachineCodeForTree(InstructionNode* instrNode,
63 int ruleForNode, short* nts);
64 public:
65 InstructionSelection(TargetMachine &T) : Target(T) {}
66
67 bool runOnFunction(Function &F);
68 };
69}
70
71// Register the pass...
72static RegisterLLC<InstructionSelection>
73X("instselect", "Instruction Selection", createInstructionSelectionPass);
74
Chris Lattnera175ed42002-09-08 21:08:43 +000075TmpInstruction::TmpInstruction(Value *s1, Value *s2, const std::string &name)
76 : Instruction(s1->getType(), Instruction::UserOp1, name) {
77 Operands.push_back(Use(s1, this)); // s1 must be nonnull
78 if (s2) {
79 Operands.push_back(Use(s2, this));
80 }
81
82 // TmpInstructions should not be garbage checked.
83 LeakDetector::removeGarbageObject(this);
84}
85
86// Constructor that requires the type of the temporary to be specified.
87// Both S1 and S2 may be NULL.(
88TmpInstruction::TmpInstruction(const Type *Ty, Value *s1, Value* s2,
89 const std::string &name)
90 : Instruction(Ty, Instruction::UserOp1, name) {
91 if (s1) { Operands.push_back(Use(s1, this)); }
92 if (s2) { Operands.push_back(Use(s2, this)); }
93
94 // TmpInstructions should not be garbage checked.
95 LeakDetector::removeGarbageObject(this);
96}
97
Chris Lattner2e1749b2002-07-30 03:57:36 +000098
99bool InstructionSelection::runOnFunction(Function &F)
100{
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000101 //
102 // Build the instruction trees to be given as inputs to BURG.
103 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000104 InstrForest instrForest(&F);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000105
106 if (SelectDebugLevel >= Select_DebugInstTrees)
107 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000108 cerr << "\n\n*** Input to instruction selection for function "
Chris Lattner2e1749b2002-07-30 03:57:36 +0000109 << F.getName() << "\n\n" << F
110 << "\n\n*** Instruction trees for function "
111 << F.getName() << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000112 instrForest.dump();
113 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000114
115 //
116 // Invoke BURG instruction selection for each tree
117 //
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000118 for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
119 RI != instrForest.roots_end(); ++RI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000120 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000121 InstructionNode* basicNode = *RI;
122 assert(basicNode->parent() == NULL && "A `root' node has a parent?");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000123
Vikram S. Adve6e447182001-09-18 12:56:28 +0000124 // Invoke BURM to label each tree node with a state
125 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000126
Vikram S. Adve6e447182001-09-18 12:56:28 +0000127 if (SelectDebugLevel >= Select_DebugBurgTrees)
128 {
129 printcover(basicNode, 1, 0);
130 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
131 printMatches(basicNode);
132 }
133
134 // Then recursively walk the tree to select instructions
Chris Lattner2e1749b2002-07-30 03:57:36 +0000135 SelectInstructionsForTree(basicNode, /*goalnt*/1);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000136 }
137
Vikram S. Adve76d35202001-07-30 18:48:43 +0000138 //
139 // Record instructions in the vector for each basic block
140 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000141 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000142 for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) {
Chris Lattner2e1749b2002-07-30 03:57:36 +0000143 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(II);
144 MachineCodeForBasicBlock &MCBB = MachineCodeForBasicBlock::get(BI);
145 MCBB.insert(MCBB.end(), mvec.begin(), mvec.end());
Vikram S. Adve76d35202001-07-30 18:48:43 +0000146 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000147
Chris Lattner2e1749b2002-07-30 03:57:36 +0000148 // Insert phi elimination code
149 InsertCodeForPhis(F);
Vikram S. Adve76d35202001-07-30 18:48:43 +0000150
Vikram S. Adve6e447182001-09-18 12:56:28 +0000151 if (SelectDebugLevel >= Select_PrintMachineCode)
152 {
Chris Lattner697954c2002-01-20 22:54:45 +0000153 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Chris Lattner2e1749b2002-07-30 03:57:36 +0000154 MachineCodeForMethod::get(&F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000155 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000156
Chris Lattner2e1749b2002-07-30 03:57:36 +0000157 return true;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000158}
159
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000160
161//-------------------------------------------------------------------------
162// This method inserts phi elimination code for all BBs in a method
163//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000164
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000165void
Chris Lattner2e1749b2002-07-30 03:57:36 +0000166InstructionSelection::InsertCodeForPhis(Function &F)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000167{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000168 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000169 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000170 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000171 BasicBlock::InstListType &InstList = BB->getInstList();
172 for (BasicBlock::iterator IIt = InstList.begin();
173 PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) {
174 // FIXME: This is probably wrong...
175 Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
Chris Lattner823c4ab2002-09-08 21:19:29 +0000176
177 // The leak detector shouldn't track these nodes. They are not garbage,
178 // even though their parent field is never filled in.
179 //
180 LeakDetector::removeGarbageObject(PhiCpRes);
181
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000182 // for each incoming value of the phi, insert phi elimination
183 //
184 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
185 // insert the copy instruction to the predecessor BB
186 vector<MachineInstr*> mvec, CpVec;
Chris Lattner2e1749b2002-07-30 03:57:36 +0000187 Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000188 mvec);
189 for (vector<MachineInstr*>::iterator MI=mvec.begin();
190 MI != mvec.end(); ++MI) {
191 vector<MachineInstr*> CpVec2 =
Chris Lattner2e1749b2002-07-30 03:57:36 +0000192 FixConstantOperandsForInstr(PN, *MI, Target);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000193 CpVec2.push_back(*MI);
194 CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
195 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000196
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000197 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000198 }
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000199
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000200 vector<MachineInstr*> mvec;
Chris Lattner2e1749b2002-07-30 03:57:36 +0000201 Target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000202
203 // get an iterator to machine instructions in the BB
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000204 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(BB);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000205
206 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000207 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000208 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000209}
210
Chris Lattner2e1749b2002-07-30 03:57:36 +0000211//-------------------------------------------------------------------------
212// Thid method inserts a copy instruction to a predecessor BB as a result
213// of phi elimination.
214//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000215
Chris Lattner2e1749b2002-07-30 03:57:36 +0000216void
217InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB,
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000218 const vector<MachineInstr*>& CpVec)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000219{
220 Instruction *TermInst = (Instruction*)BB->getTerminator();
221 MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst);
222 MachineInstr *FirstMIOfTerm = MC4Term.front();
223
224 assert (FirstMIOfTerm && "No Machine Instrs for terminator");
225
226 MachineCodeForBasicBlock &bbMvec = MachineCodeForBasicBlock::get(BB);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000227
Chris Lattner2e1749b2002-07-30 03:57:36 +0000228 // find the position of first machine instruction generated by the
229 // terminator of this BB
230 MachineCodeForBasicBlock::iterator MCIt =
231 std::find(bbMvec.begin(), bbMvec.end(), FirstMIOfTerm);
232
233 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
234
235 // insert the copy instructions just before the first machine instruction
236 // generated for the terminator
237 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
Vikram S. Adve6d353262001-10-17 23:57:50 +0000238}
239
Chris Lattner2e1749b2002-07-30 03:57:36 +0000240
Vikram S. Adve6d353262001-10-17 23:57:50 +0000241//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000242// Function SelectInstructionsForTree
243//
244// Recursively walk the tree to select instructions.
245// Do this top-down so that child instructions can exploit decisions
246// made at the child instructions.
247//
248// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
249// a branch-on-integer-register instruction, then the setle node
250// can use that information to avoid generating the SUBcc instruction.
251//
252// Note that this cannot be done bottom-up because setle must do this
253// only if it is a child of the branch (otherwise, the result of setle
254// may be used by multiple instructions).
255//---------------------------------------------------------------------------
256
Chris Lattner2e1749b2002-07-30 03:57:36 +0000257void
258InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
259 int goalnt)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000260{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000261 // Get the rule that matches this node.
262 //
263 int ruleForNode = burm_rule(treeRoot->state, goalnt);
264
Chris Lattner2e1749b2002-07-30 03:57:36 +0000265 if (ruleForNode == 0) {
266 cerr << "Could not match instruction tree for instr selection\n";
267 abort();
268 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000269
270 // Get this rule's non-terminals and the corresponding child nodes (if any)
271 //
272 short *nts = burm_nts[ruleForNode];
273
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000274 // First, select instructions for the current node and rule.
275 // (If this is a list node, not an instruction, then skip this step).
276 // This function is specific to the target architecture.
277 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000278 if (treeRoot->opLabel != VRegListOp)
279 {
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000280 vector<MachineInstr*> minstrVec;
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000281
Vikram S. Adve6e447182001-09-18 12:56:28 +0000282 InstructionNode* instrNode = (InstructionNode*)treeRoot;
283 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000284
Chris Lattner2e1749b2002-07-30 03:57:36 +0000285 GetInstructionsByRule(instrNode, ruleForNode, nts, Target, minstrVec);
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000286
Chris Lattner06cb1b72002-02-03 07:33:46 +0000287 MachineCodeForInstruction &mvec =
288 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000289 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000290 }
291
292 // Then, recursively compile the child nodes, if any.
293 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000294 if (nts[0])
295 { // i.e., there is at least one kid
296 InstrTreeNode* kids[2];
297 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000298 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000299
300 // First skip over any chain rules so that we don't visit
301 // the current node again.
302 //
303 while (ThisIsAChainRule(currentRule))
304 {
305 currentRule = burm_rule(treeRoot->state, nts[0]);
306 nts = burm_nts[currentRule];
307 burm_kids(treeRoot, currentRule, kids);
308 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000309
Vikram S. Adve6e447182001-09-18 12:56:28 +0000310 // Now we have the first non-chain rule so we have found
311 // the actual child nodes. Recursively compile them.
312 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000313 for (unsigned i = 0; nts[i]; i++)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000314 {
315 assert(i < 2);
316 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
317 if (nodeType == InstrTreeNode::NTVRegListNode ||
318 nodeType == InstrTreeNode::NTInstructionNode)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000319 SelectInstructionsForTree(kids[i], nts[i]);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000320 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000321 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000322
Vikram S. Adve6d353262001-10-17 23:57:50 +0000323 // Finally, do any postprocessing on this node after its children
324 // have been translated
325 //
326 if (treeRoot->opLabel != VRegListOp)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000327 PostprocessMachineCodeForTree((InstructionNode*)treeRoot, ruleForNode, nts);
328}
329
330//---------------------------------------------------------------------------
331// Function PostprocessMachineCodeForTree
332//
333// Apply any final cleanups to machine code for the root of a subtree
334// after selection for all its children has been completed.
335//
336void
337InstructionSelection::PostprocessMachineCodeForTree(InstructionNode* instrNode,
338 int ruleForNode,
339 short* nts)
340{
341 // Fix up any constant operands in the machine instructions to either
342 // use an immediate field or to load the constant into a register
343 // Walk backwards and use direct indexes to allow insertion before current
344 //
345 Instruction* vmInstr = instrNode->getInstruction();
346 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000347 for (unsigned i = mvec.size(); i != 0; --i)
Vikram S. Adve6d353262001-10-17 23:57:50 +0000348 {
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000349 vector<MachineInstr*> loadConstVec =
350 FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target);
Chris Lattner2e1749b2002-07-30 03:57:36 +0000351
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000352 mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end());
Vikram S. Adve6d353262001-10-17 23:57:50 +0000353 }
Chris Lattner2e1749b2002-07-30 03:57:36 +0000354}
355
356
357
358//===----------------------------------------------------------------------===//
359// createInstructionSelectionPass - Public entrypoint for instruction selection
360// and this file as a whole...
361//
362Pass *createInstructionSelectionPass(TargetMachine &T) {
363 return new InstructionSelection(T);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000364}
365