blob: 677bef7d04ad14741819d5fcdae80f1a810379f1 [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"
Misha Brukmanfce11432002-10-28 00:28:31 +000014#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner06cb1b72002-02-03 07:33:46 +000015#include "llvm/Target/MachineRegInfo.h"
16#include "llvm/Target/TargetMachine.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000017#include "llvm/Function.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/iPHINode.h"
Chris Lattner2e1749b2002-07-30 03:57:36 +000019#include "llvm/Pass.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/CommandLine.h"
Chris Lattnera175ed42002-09-08 21:08:43 +000021#include "Support/LeakDetector.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) {}
Chris Lattnera0877722002-10-23 03:30:47 +000065
66 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.setPreservesCFG();
68 }
Chris Lattner2e1749b2002-07-30 03:57:36 +000069
70 bool runOnFunction(Function &F);
71 };
72}
73
74// Register the pass...
75static RegisterLLC<InstructionSelection>
76X("instselect", "Instruction Selection", createInstructionSelectionPass);
77
Chris Lattnera175ed42002-09-08 21:08:43 +000078TmpInstruction::TmpInstruction(Value *s1, Value *s2, const std::string &name)
79 : Instruction(s1->getType(), Instruction::UserOp1, name) {
80 Operands.push_back(Use(s1, this)); // s1 must be nonnull
81 if (s2) {
82 Operands.push_back(Use(s2, this));
83 }
84
85 // TmpInstructions should not be garbage checked.
86 LeakDetector::removeGarbageObject(this);
87}
88
89// Constructor that requires the type of the temporary to be specified.
90// Both S1 and S2 may be NULL.(
91TmpInstruction::TmpInstruction(const Type *Ty, Value *s1, Value* s2,
92 const std::string &name)
93 : Instruction(Ty, Instruction::UserOp1, name) {
94 if (s1) { Operands.push_back(Use(s1, this)); }
95 if (s2) { Operands.push_back(Use(s2, this)); }
96
97 // TmpInstructions should not be garbage checked.
98 LeakDetector::removeGarbageObject(this);
99}
100
Chris Lattner2e1749b2002-07-30 03:57:36 +0000101
102bool InstructionSelection::runOnFunction(Function &F)
103{
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000104 //
105 // Build the instruction trees to be given as inputs to BURG.
106 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000107 InstrForest instrForest(&F);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000108
109 if (SelectDebugLevel >= Select_DebugInstTrees)
110 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000111 cerr << "\n\n*** Input to instruction selection for function "
Chris Lattner2e1749b2002-07-30 03:57:36 +0000112 << F.getName() << "\n\n" << F
113 << "\n\n*** Instruction trees for function "
114 << F.getName() << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000115 instrForest.dump();
116 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000117
118 //
119 // Invoke BURG instruction selection for each tree
120 //
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000121 for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
122 RI != instrForest.roots_end(); ++RI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000123 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000124 InstructionNode* basicNode = *RI;
125 assert(basicNode->parent() == NULL && "A `root' node has a parent?");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000126
Vikram S. Adve6e447182001-09-18 12:56:28 +0000127 // Invoke BURM to label each tree node with a state
128 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000129
Vikram S. Adve6e447182001-09-18 12:56:28 +0000130 if (SelectDebugLevel >= Select_DebugBurgTrees)
131 {
132 printcover(basicNode, 1, 0);
133 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
134 printMatches(basicNode);
135 }
136
137 // Then recursively walk the tree to select instructions
Chris Lattner2e1749b2002-07-30 03:57:36 +0000138 SelectInstructionsForTree(basicNode, /*goalnt*/1);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000139 }
140
Vikram S. Adve76d35202001-07-30 18:48:43 +0000141 //
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +0000142 // Create the MachineBasicBlock records and add all of the MachineInstrs
143 // defined in the MachineCodeForInstruction objects to also live in the
144 // MachineBasicBlock objects.
Vikram S. Adve76d35202001-07-30 18:48:43 +0000145 //
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +0000146 MachineFunction &MF = MachineFunction::get(&F);
147 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
148 MachineBasicBlock *MCBB = new MachineBasicBlock(BI);
149 MF.getBasicBlockList().push_back(MCBB);
150
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000151 for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) {
Chris Lattner2e1749b2002-07-30 03:57:36 +0000152 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(II);
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +0000153 MCBB->insert(MCBB->end(), mvec.begin(), mvec.end());
Vikram S. Adve76d35202001-07-30 18:48:43 +0000154 }
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +0000155 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000156
Chris Lattner2e1749b2002-07-30 03:57:36 +0000157 // Insert phi elimination code
158 InsertCodeForPhis(F);
Vikram S. Adve76d35202001-07-30 18:48:43 +0000159
Vikram S. Adve6e447182001-09-18 12:56:28 +0000160 if (SelectDebugLevel >= Select_PrintMachineCode)
161 {
Chris Lattner697954c2002-01-20 22:54:45 +0000162 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Misha Brukmanfce11432002-10-28 00:28:31 +0000163 MachineFunction::get(&F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000164 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000165
Chris Lattner2e1749b2002-07-30 03:57:36 +0000166 return true;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000167}
168
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000169
170//-------------------------------------------------------------------------
171// This method inserts phi elimination code for all BBs in a method
172//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000173
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000174void
Chris Lattner2e1749b2002-07-30 03:57:36 +0000175InstructionSelection::InsertCodeForPhis(Function &F)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000176{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000177 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000178 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000179 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000180 BasicBlock::InstListType &InstList = BB->getInstList();
181 for (BasicBlock::iterator IIt = InstList.begin();
182 PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) {
183 // FIXME: This is probably wrong...
184 Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
Chris Lattner823c4ab2002-09-08 21:19:29 +0000185
186 // The leak detector shouldn't track these nodes. They are not garbage,
187 // even though their parent field is never filled in.
188 //
189 LeakDetector::removeGarbageObject(PhiCpRes);
190
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000191 // for each incoming value of the phi, insert phi elimination
192 //
193 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
194 // insert the copy instruction to the predecessor BB
195 vector<MachineInstr*> mvec, CpVec;
Chris Lattner2e1749b2002-07-30 03:57:36 +0000196 Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000197 mvec);
198 for (vector<MachineInstr*>::iterator MI=mvec.begin();
199 MI != mvec.end(); ++MI) {
200 vector<MachineInstr*> CpVec2 =
Chris Lattner2e1749b2002-07-30 03:57:36 +0000201 FixConstantOperandsForInstr(PN, *MI, Target);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000202 CpVec2.push_back(*MI);
203 CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
204 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000205
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000206 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000207 }
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000208
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000209 vector<MachineInstr*> mvec;
Chris Lattner2e1749b2002-07-30 03:57:36 +0000210 Target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000211
212 // get an iterator to machine instructions in the BB
Chris Lattner55291ea2002-10-28 01:41:47 +0000213 MachineBasicBlock& bbMvec = MachineBasicBlock::get(BB);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000214
215 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000216 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000217 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000218}
219
Chris Lattner2e1749b2002-07-30 03:57:36 +0000220//-------------------------------------------------------------------------
221// Thid method inserts a copy instruction to a predecessor BB as a result
222// of phi elimination.
223//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000224
Chris Lattner2e1749b2002-07-30 03:57:36 +0000225void
226InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB,
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000227 const vector<MachineInstr*>& CpVec)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000228{
229 Instruction *TermInst = (Instruction*)BB->getTerminator();
230 MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst);
231 MachineInstr *FirstMIOfTerm = MC4Term.front();
232
233 assert (FirstMIOfTerm && "No Machine Instrs for terminator");
234
Chris Lattner55291ea2002-10-28 01:41:47 +0000235 MachineBasicBlock &bbMvec = MachineBasicBlock::get(BB);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000236
Chris Lattner2e1749b2002-07-30 03:57:36 +0000237 // find the position of first machine instruction generated by the
238 // terminator of this BB
Chris Lattner55291ea2002-10-28 01:41:47 +0000239 MachineBasicBlock::iterator MCIt =
Chris Lattner2e1749b2002-07-30 03:57:36 +0000240 std::find(bbMvec.begin(), bbMvec.end(), FirstMIOfTerm);
241
242 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
243
244 // insert the copy instructions just before the first machine instruction
245 // generated for the terminator
246 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
Vikram S. Adve6d353262001-10-17 23:57:50 +0000247}
248
Chris Lattner2e1749b2002-07-30 03:57:36 +0000249
Vikram S. Adve6d353262001-10-17 23:57:50 +0000250//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000251// Function SelectInstructionsForTree
252//
253// Recursively walk the tree to select instructions.
254// Do this top-down so that child instructions can exploit decisions
255// made at the child instructions.
256//
257// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
258// a branch-on-integer-register instruction, then the setle node
259// can use that information to avoid generating the SUBcc instruction.
260//
261// Note that this cannot be done bottom-up because setle must do this
262// only if it is a child of the branch (otherwise, the result of setle
263// may be used by multiple instructions).
264//---------------------------------------------------------------------------
265
Chris Lattner2e1749b2002-07-30 03:57:36 +0000266void
267InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
268 int goalnt)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000269{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000270 // Get the rule that matches this node.
271 //
272 int ruleForNode = burm_rule(treeRoot->state, goalnt);
273
Chris Lattner2e1749b2002-07-30 03:57:36 +0000274 if (ruleForNode == 0) {
275 cerr << "Could not match instruction tree for instr selection\n";
276 abort();
277 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000278
279 // Get this rule's non-terminals and the corresponding child nodes (if any)
280 //
281 short *nts = burm_nts[ruleForNode];
282
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000283 // First, select instructions for the current node and rule.
284 // (If this is a list node, not an instruction, then skip this step).
285 // This function is specific to the target architecture.
286 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000287 if (treeRoot->opLabel != VRegListOp)
288 {
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000289 vector<MachineInstr*> minstrVec;
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000290
Vikram S. Adve6e447182001-09-18 12:56:28 +0000291 InstructionNode* instrNode = (InstructionNode*)treeRoot;
292 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000293
Chris Lattner2e1749b2002-07-30 03:57:36 +0000294 GetInstructionsByRule(instrNode, ruleForNode, nts, Target, minstrVec);
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000295
Chris Lattner06cb1b72002-02-03 07:33:46 +0000296 MachineCodeForInstruction &mvec =
297 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000298 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000299 }
300
301 // Then, recursively compile the child nodes, if any.
302 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000303 if (nts[0])
304 { // i.e., there is at least one kid
305 InstrTreeNode* kids[2];
306 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000307 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000308
309 // First skip over any chain rules so that we don't visit
310 // the current node again.
311 //
312 while (ThisIsAChainRule(currentRule))
313 {
314 currentRule = burm_rule(treeRoot->state, nts[0]);
315 nts = burm_nts[currentRule];
316 burm_kids(treeRoot, currentRule, kids);
317 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000318
Vikram S. Adve6e447182001-09-18 12:56:28 +0000319 // Now we have the first non-chain rule so we have found
320 // the actual child nodes. Recursively compile them.
321 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000322 for (unsigned i = 0; nts[i]; i++)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000323 {
324 assert(i < 2);
325 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
326 if (nodeType == InstrTreeNode::NTVRegListNode ||
327 nodeType == InstrTreeNode::NTInstructionNode)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000328 SelectInstructionsForTree(kids[i], nts[i]);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000329 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000330 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000331
Vikram S. Adve6d353262001-10-17 23:57:50 +0000332 // Finally, do any postprocessing on this node after its children
333 // have been translated
334 //
335 if (treeRoot->opLabel != VRegListOp)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000336 PostprocessMachineCodeForTree((InstructionNode*)treeRoot, ruleForNode, nts);
337}
338
339//---------------------------------------------------------------------------
340// Function PostprocessMachineCodeForTree
341//
342// Apply any final cleanups to machine code for the root of a subtree
343// after selection for all its children has been completed.
344//
345void
346InstructionSelection::PostprocessMachineCodeForTree(InstructionNode* instrNode,
347 int ruleForNode,
348 short* nts)
349{
350 // Fix up any constant operands in the machine instructions to either
351 // use an immediate field or to load the constant into a register
352 // Walk backwards and use direct indexes to allow insertion before current
353 //
354 Instruction* vmInstr = instrNode->getInstruction();
355 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000356 for (unsigned i = mvec.size(); i != 0; --i)
Vikram S. Adve6d353262001-10-17 23:57:50 +0000357 {
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000358 vector<MachineInstr*> loadConstVec =
359 FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target);
Chris Lattner2e1749b2002-07-30 03:57:36 +0000360
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000361 mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end());
Vikram S. Adve6d353262001-10-17 23:57:50 +0000362 }
Chris Lattner2e1749b2002-07-30 03:57:36 +0000363}
364
365
366
367//===----------------------------------------------------------------------===//
368// createInstructionSelectionPass - Public entrypoint for instruction selection
369// and this file as a whole...
370//
371Pass *createInstructionSelectionPass(TargetMachine &T) {
372 return new InstructionSelection(T);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000373}
374