blob: a9d1331c94c83e12129cc334a8fb06190430d484 [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) {}
Chris Lattnera0877722002-10-23 03:30:47 +000066
67 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.setPreservesCFG();
69 }
Chris Lattner2e1749b2002-07-30 03:57:36 +000070
71 bool runOnFunction(Function &F);
72 };
73}
74
75// Register the pass...
76static RegisterLLC<InstructionSelection>
77X("instselect", "Instruction Selection", createInstructionSelectionPass);
78
Chris Lattnera175ed42002-09-08 21:08:43 +000079TmpInstruction::TmpInstruction(Value *s1, Value *s2, const std::string &name)
80 : Instruction(s1->getType(), Instruction::UserOp1, name) {
81 Operands.push_back(Use(s1, this)); // s1 must be nonnull
82 if (s2) {
83 Operands.push_back(Use(s2, this));
84 }
85
86 // TmpInstructions should not be garbage checked.
87 LeakDetector::removeGarbageObject(this);
88}
89
90// Constructor that requires the type of the temporary to be specified.
91// Both S1 and S2 may be NULL.(
92TmpInstruction::TmpInstruction(const Type *Ty, Value *s1, Value* s2,
93 const std::string &name)
94 : Instruction(Ty, Instruction::UserOp1, name) {
95 if (s1) { Operands.push_back(Use(s1, this)); }
96 if (s2) { Operands.push_back(Use(s2, this)); }
97
98 // TmpInstructions should not be garbage checked.
99 LeakDetector::removeGarbageObject(this);
100}
101
Chris Lattner2e1749b2002-07-30 03:57:36 +0000102
103bool InstructionSelection::runOnFunction(Function &F)
104{
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000105 //
106 // Build the instruction trees to be given as inputs to BURG.
107 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000108 InstrForest instrForest(&F);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000109
110 if (SelectDebugLevel >= Select_DebugInstTrees)
111 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000112 cerr << "\n\n*** Input to instruction selection for function "
Chris Lattner2e1749b2002-07-30 03:57:36 +0000113 << F.getName() << "\n\n" << F
114 << "\n\n*** Instruction trees for function "
115 << F.getName() << "\n\n";
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000116 instrForest.dump();
117 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000118
119 //
120 // Invoke BURG instruction selection for each tree
121 //
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000122 for (InstrForest::const_root_iterator RI = instrForest.roots_begin();
123 RI != instrForest.roots_end(); ++RI)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000124 {
Vikram S. Adve4e7bc492002-03-24 03:36:52 +0000125 InstructionNode* basicNode = *RI;
126 assert(basicNode->parent() == NULL && "A `root' node has a parent?");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000127
Vikram S. Adve6e447182001-09-18 12:56:28 +0000128 // Invoke BURM to label each tree node with a state
129 burm_label(basicNode);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000130
Vikram S. Adve6e447182001-09-18 12:56:28 +0000131 if (SelectDebugLevel >= Select_DebugBurgTrees)
132 {
133 printcover(basicNode, 1, 0);
134 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
135 printMatches(basicNode);
136 }
137
138 // Then recursively walk the tree to select instructions
Chris Lattner2e1749b2002-07-30 03:57:36 +0000139 SelectInstructionsForTree(basicNode, /*goalnt*/1);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000140 }
141
Vikram S. Adve76d35202001-07-30 18:48:43 +0000142 //
143 // Record instructions in the vector for each basic block
144 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000145 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI)
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000146 for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) {
Chris Lattner2e1749b2002-07-30 03:57:36 +0000147 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(II);
148 MachineCodeForBasicBlock &MCBB = MachineCodeForBasicBlock::get(BI);
149 MCBB.insert(MCBB.end(), mvec.begin(), mvec.end());
Vikram S. Adve76d35202001-07-30 18:48:43 +0000150 }
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000151
Chris Lattner2e1749b2002-07-30 03:57:36 +0000152 // Insert phi elimination code
153 InsertCodeForPhis(F);
Vikram S. Adve76d35202001-07-30 18:48:43 +0000154
Vikram S. Adve6e447182001-09-18 12:56:28 +0000155 if (SelectDebugLevel >= Select_PrintMachineCode)
156 {
Chris Lattner697954c2002-01-20 22:54:45 +0000157 cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
Chris Lattner2e1749b2002-07-30 03:57:36 +0000158 MachineCodeForMethod::get(&F).dump();
Vikram S. Adve6e447182001-09-18 12:56:28 +0000159 }
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000160
Chris Lattner2e1749b2002-07-30 03:57:36 +0000161 return true;
Ruchira Sasankab2490fc2001-11-12 14:44:50 +0000162}
163
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000164
165//-------------------------------------------------------------------------
166// This method inserts phi elimination code for all BBs in a method
167//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000168
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000169void
Chris Lattner2e1749b2002-07-30 03:57:36 +0000170InstructionSelection::InsertCodeForPhis(Function &F)
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000171{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000172 // for all basic blocks in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000173 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000174 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000175 BasicBlock::InstListType &InstList = BB->getInstList();
176 for (BasicBlock::iterator IIt = InstList.begin();
177 PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) {
178 // FIXME: This is probably wrong...
179 Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
Chris Lattner823c4ab2002-09-08 21:19:29 +0000180
181 // The leak detector shouldn't track these nodes. They are not garbage,
182 // even though their parent field is never filled in.
183 //
184 LeakDetector::removeGarbageObject(PhiCpRes);
185
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000186 // for each incoming value of the phi, insert phi elimination
187 //
188 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
189 // insert the copy instruction to the predecessor BB
190 vector<MachineInstr*> mvec, CpVec;
Chris Lattner2e1749b2002-07-30 03:57:36 +0000191 Target.getRegInfo().cpValue2Value(PN->getIncomingValue(i), PhiCpRes,
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000192 mvec);
193 for (vector<MachineInstr*>::iterator MI=mvec.begin();
194 MI != mvec.end(); ++MI) {
195 vector<MachineInstr*> CpVec2 =
Chris Lattner2e1749b2002-07-30 03:57:36 +0000196 FixConstantOperandsForInstr(PN, *MI, Target);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000197 CpVec2.push_back(*MI);
198 CpVec.insert(CpVec.end(), CpVec2.begin(), CpVec2.end());
199 }
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000200
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000201 InsertPhiElimInstructions(PN->getIncomingBlock(i), CpVec);
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000202 }
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000203
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000204 vector<MachineInstr*> mvec;
Chris Lattner2e1749b2002-07-30 03:57:36 +0000205 Target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000206
207 // get an iterator to machine instructions in the BB
Vikram S. Adve1dcfd3c2002-07-08 23:03:10 +0000208 MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(BB);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000209
210 bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000211 } // for each Phi Instr in BB
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000212 } // for all BBs in function
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000213}
214
Chris Lattner2e1749b2002-07-30 03:57:36 +0000215//-------------------------------------------------------------------------
216// Thid method inserts a copy instruction to a predecessor BB as a result
217// of phi elimination.
218//-------------------------------------------------------------------------
Ruchira Sasanka20ac79e2001-11-15 00:27:14 +0000219
Chris Lattner2e1749b2002-07-30 03:57:36 +0000220void
221InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB,
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000222 const vector<MachineInstr*>& CpVec)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000223{
224 Instruction *TermInst = (Instruction*)BB->getTerminator();
225 MachineCodeForInstruction &MC4Term = MachineCodeForInstruction::get(TermInst);
226 MachineInstr *FirstMIOfTerm = MC4Term.front();
227
228 assert (FirstMIOfTerm && "No Machine Instrs for terminator");
229
230 MachineCodeForBasicBlock &bbMvec = MachineCodeForBasicBlock::get(BB);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000231
Chris Lattner2e1749b2002-07-30 03:57:36 +0000232 // find the position of first machine instruction generated by the
233 // terminator of this BB
234 MachineCodeForBasicBlock::iterator MCIt =
235 std::find(bbMvec.begin(), bbMvec.end(), FirstMIOfTerm);
236
237 assert( MCIt != bbMvec.end() && "Start inst of terminator not found");
238
239 // insert the copy instructions just before the first machine instruction
240 // generated for the terminator
241 bbMvec.insert(MCIt, CpVec.begin(), CpVec.end());
Vikram S. Adve6d353262001-10-17 23:57:50 +0000242}
243
Chris Lattner2e1749b2002-07-30 03:57:36 +0000244
Vikram S. Adve6d353262001-10-17 23:57:50 +0000245//---------------------------------------------------------------------------
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000246// Function SelectInstructionsForTree
247//
248// Recursively walk the tree to select instructions.
249// Do this top-down so that child instructions can exploit decisions
250// made at the child instructions.
251//
252// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
253// a branch-on-integer-register instruction, then the setle node
254// can use that information to avoid generating the SUBcc instruction.
255//
256// Note that this cannot be done bottom-up because setle must do this
257// only if it is a child of the branch (otherwise, the result of setle
258// may be used by multiple instructions).
259//---------------------------------------------------------------------------
260
Chris Lattner2e1749b2002-07-30 03:57:36 +0000261void
262InstructionSelection::SelectInstructionsForTree(InstrTreeNode* treeRoot,
263 int goalnt)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000264{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000265 // Get the rule that matches this node.
266 //
267 int ruleForNode = burm_rule(treeRoot->state, goalnt);
268
Chris Lattner2e1749b2002-07-30 03:57:36 +0000269 if (ruleForNode == 0) {
270 cerr << "Could not match instruction tree for instr selection\n";
271 abort();
272 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000273
274 // Get this rule's non-terminals and the corresponding child nodes (if any)
275 //
276 short *nts = burm_nts[ruleForNode];
277
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000278 // First, select instructions for the current node and rule.
279 // (If this is a list node, not an instruction, then skip this step).
280 // This function is specific to the target architecture.
281 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000282 if (treeRoot->opLabel != VRegListOp)
283 {
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000284 vector<MachineInstr*> minstrVec;
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000285
Vikram S. Adve6e447182001-09-18 12:56:28 +0000286 InstructionNode* instrNode = (InstructionNode*)treeRoot;
287 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
Vikram S. Adve7ad10462001-10-22 13:51:09 +0000288
Chris Lattner2e1749b2002-07-30 03:57:36 +0000289 GetInstructionsByRule(instrNode, ruleForNode, nts, Target, minstrVec);
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000290
Chris Lattner06cb1b72002-02-03 07:33:46 +0000291 MachineCodeForInstruction &mvec =
292 MachineCodeForInstruction::get(instrNode->getInstruction());
Vikram S. Adve1ed009f2002-03-18 03:31:54 +0000293 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000294 }
295
296 // Then, recursively compile the child nodes, if any.
297 //
Vikram S. Adve6e447182001-09-18 12:56:28 +0000298 if (nts[0])
299 { // i.e., there is at least one kid
300 InstrTreeNode* kids[2];
301 int currentRule = ruleForNode;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000302 burm_kids(treeRoot, currentRule, kids);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000303
304 // First skip over any chain rules so that we don't visit
305 // the current node again.
306 //
307 while (ThisIsAChainRule(currentRule))
308 {
309 currentRule = burm_rule(treeRoot->state, nts[0]);
310 nts = burm_nts[currentRule];
311 burm_kids(treeRoot, currentRule, kids);
312 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000313
Vikram S. Adve6e447182001-09-18 12:56:28 +0000314 // Now we have the first non-chain rule so we have found
315 // the actual child nodes. Recursively compile them.
316 //
Chris Lattner2e1749b2002-07-30 03:57:36 +0000317 for (unsigned i = 0; nts[i]; i++)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000318 {
319 assert(i < 2);
320 InstrTreeNode::InstrTreeNodeType nodeType = kids[i]->getNodeType();
321 if (nodeType == InstrTreeNode::NTVRegListNode ||
322 nodeType == InstrTreeNode::NTInstructionNode)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000323 SelectInstructionsForTree(kids[i], nts[i]);
Vikram S. Adve6e447182001-09-18 12:56:28 +0000324 }
Chris Lattner0e6530e2001-09-14 03:37:52 +0000325 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000326
Vikram S. Adve6d353262001-10-17 23:57:50 +0000327 // Finally, do any postprocessing on this node after its children
328 // have been translated
329 //
330 if (treeRoot->opLabel != VRegListOp)
Chris Lattner2e1749b2002-07-30 03:57:36 +0000331 PostprocessMachineCodeForTree((InstructionNode*)treeRoot, ruleForNode, nts);
332}
333
334//---------------------------------------------------------------------------
335// Function PostprocessMachineCodeForTree
336//
337// Apply any final cleanups to machine code for the root of a subtree
338// after selection for all its children has been completed.
339//
340void
341InstructionSelection::PostprocessMachineCodeForTree(InstructionNode* instrNode,
342 int ruleForNode,
343 short* nts)
344{
345 // Fix up any constant operands in the machine instructions to either
346 // use an immediate field or to load the constant into a register
347 // Walk backwards and use direct indexes to allow insertion before current
348 //
349 Instruction* vmInstr = instrNode->getInstruction();
350 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(vmInstr);
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000351 for (unsigned i = mvec.size(); i != 0; --i)
Vikram S. Adve6d353262001-10-17 23:57:50 +0000352 {
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000353 vector<MachineInstr*> loadConstVec =
354 FixConstantOperandsForInstr(vmInstr, mvec[i-1], Target);
Chris Lattner2e1749b2002-07-30 03:57:36 +0000355
Chris Lattnerb91b31c2002-08-09 20:05:34 +0000356 mvec.insert(mvec.begin()+i-1, loadConstVec.begin(), loadConstVec.end());
Vikram S. Adve6d353262001-10-17 23:57:50 +0000357 }
Chris Lattner2e1749b2002-07-30 03:57:36 +0000358}
359
360
361
362//===----------------------------------------------------------------------===//
363// createInstructionSelectionPass - Public entrypoint for instruction selection
364// and this file as a whole...
365//
366Pass *createInstructionSelectionPass(TargetMachine &T) {
367 return new InstructionSelection(T);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000368}
369