blob: dbb0f8672a63156fab76c97faefd9becd242eb2d [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:
7//
8// History:
9// 7/02/01 - Vikram Adve - Created
Vikram S. Adve960066a2001-07-31 21:53:25 +000010//**************************************************************************/
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000011
12
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000013//************************** System Include Files ***************************/
14
15
16//*************************** User Include Files ***************************/
17
Chris Lattnerfeb60592001-09-07 17:15:18 +000018#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000019#include "llvm/Support/CommandLine.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000020#include "llvm/Type.h"
21#include "llvm/iMemory.h"
22#include "llvm/Instruction.h"
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000023#include "llvm/BasicBlock.h"
24#include "llvm/Method.h"
Chris Lattner7e583cf2001-07-21 20:58:30 +000025#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000026
27
28//************************* Forward Declarations ***************************/
29
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000030static bool SelectInstructionsForTree(BasicTreeNode* treeRoot,
31 int goalnt,
Chris Lattner6c5a32d2001-07-23 03:09:03 +000032 TargetMachine &Target);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000033
34
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000035//************************* Internal Data Types *****************************/
36
37enum SelectDebugLevel_t {
38 Select_NoDebugInfo,
39 Select_PrintMachineCode,
40 Select_DebugInstTrees,
41 Select_DebugBurgTrees,
42};
43
44// Enable Debug Options to be specified on the command line
45cl::Enum<enum SelectDebugLevel_t> SelectDebugLevel("dselect", cl::NoFlags, // cl::Hidden
46 "enable instruction selection debugging information",
47 clEnumValN(Select_NoDebugInfo, "n", "disable debug output"),
48 clEnumValN(Select_PrintMachineCode, "y", "print generated machine code"),
49 clEnumValN(Select_DebugInstTrees, "i", "print instr. selection debugging info"),
50 clEnumValN(Select_DebugBurgTrees, "b", "print burg trees"), 0);
51
52
53//************************** External Functions ****************************/
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000054
55
56//---------------------------------------------------------------------------
57// Entry point for instruction selection using BURG.
58// Returns true if instruction selection failed, false otherwise.
59//---------------------------------------------------------------------------
60
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000061bool
62SelectInstructionsForMethod(Method* method,
63 TargetMachine &Target)
64{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000065 bool failed = false;
66
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000067 //
68 // Build the instruction trees to be given as inputs to BURG.
69 //
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000070 InstrForest instrForest;
71 instrForest.buildTreesForMethod(method);
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000072
73 if (SelectDebugLevel >= Select_DebugInstTrees)
74 {
75 cout << "\n\n*** Instruction trees for method "
76 << (method->hasName()? method->getName() : "")
77 << endl << endl;
78 instrForest.dump();
79 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000080
81 //
82 // Invoke BURG instruction selection for each tree
83 //
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000084 const hash_set<InstructionNode*> &treeRoots = instrForest.getRootSet();
Chris Lattner75279cc2001-07-23 03:50:57 +000085 for (hash_set<InstructionNode*>::const_iterator
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000086 treeRootIter = treeRoots.begin();
87 treeRootIter != treeRoots.end();
88 ++treeRootIter)
89 {
90 BasicTreeNode* basicNode = (*treeRootIter)->getBasicNode();
91
92 // Invoke BURM to label each tree node with a state
93 (void) burm_label(basicNode);
94
Vikram S. Adve89df1ae2001-08-28 23:04:38 +000095 if (SelectDebugLevel >= Select_DebugBurgTrees)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000096 {
97 printcover(basicNode, 1, 0);
Chris Lattner36765b02001-07-21 22:53:35 +000098 cerr << "\nCover cost == " << treecost(basicNode, 1, 0) << "\n\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000099 printMatches(basicNode);
100 }
101
102 // Then recursively walk the tree to select instructions
Chris Lattner6c5a32d2001-07-23 03:09:03 +0000103 if (SelectInstructionsForTree(basicNode, /*goalnt*/1, Target))
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000104 {
105 failed = true;
106 break;
107 }
108 }
109
Vikram S. Adve76d35202001-07-30 18:48:43 +0000110 //
111 // Record instructions in the vector for each basic block
112 //
113 for (Method::iterator BI = method->begin(); BI != method->end(); ++BI)
114 {
115 MachineCodeForBasicBlock& bbMvec = (*BI)->getMachineInstrVec();
116 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
117 {
118 MachineCodeForVMInstr& mvec = (*II)->getMachineInstrVec();
119 for (unsigned i=0; i < mvec.size(); i++)
120 bbMvec.push_back(mvec[i]);
121 }
122 }
123
Vikram S. Adve89df1ae2001-08-28 23:04:38 +0000124 if (SelectDebugLevel >= Select_PrintMachineCode)
125 {
126 cout << endl << "*** Machine instructions after INSTRUCTION SELECTION" << endl;
127 PrintMachineInstructions(method);
128 }
129
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000130 return false;
131}
132
133
134//---------------------------------------------------------------------------
135// Function: FoldGetElemChain
136//
137// Purpose:
138// Fold a chain of GetElementPtr instructions into an equivalent
139// (Pointer, IndexVector) pair. Returns the pointer Value, and
140// stores the resulting IndexVector in argument chainIdxVec.
141//---------------------------------------------------------------------------
142
143Value*
144FoldGetElemChain(const InstructionNode* getElemInstrNode,
145 vector<ConstPoolVal*>& chainIdxVec)
146{
147 MemAccessInst* getElemInst = (MemAccessInst*)
148 getElemInstrNode->getInstruction();
149
150 // Initialize return values from the incoming instruction
151 Value* ptrVal = getElemInst->getPtrOperand();
152 chainIdxVec = getElemInst->getIndexVec(); // copies index vector values
153
154 // Now chase the chain of getElementInstr instructions, if any
155 InstrTreeNode* ptrChild = getElemInstrNode->leftChild();
156 while (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
157 ptrChild->getOpLabel() == GetElemPtrIdx)
158 {
159 // Child is a GetElemPtr instruction
160 getElemInst = (MemAccessInst*)
161 ((InstructionNode*) ptrChild)->getInstruction();
162 const vector<ConstPoolVal*>& idxVec = getElemInst->getIndexVec();
163
164 // Get the pointer value out of ptrChild and *prepend* its index vector
165 ptrVal = getElemInst->getPtrOperand();
166 chainIdxVec.insert(chainIdxVec.begin(), idxVec.begin(), idxVec.end());
167
168 ptrChild = ptrChild->leftChild();
169 }
170
171 return ptrVal;
172}
173
174
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000175//*********************** Private Functions *****************************/
176
177
178//---------------------------------------------------------------------------
179// Function SelectInstructionsForTree
180//
181// Recursively walk the tree to select instructions.
182// Do this top-down so that child instructions can exploit decisions
183// made at the child instructions.
184//
185// E.g., if br(setle(reg,const)) decides the constant is 0 and uses
186// a branch-on-integer-register instruction, then the setle node
187// can use that information to avoid generating the SUBcc instruction.
188//
189// Note that this cannot be done bottom-up because setle must do this
190// only if it is a child of the branch (otherwise, the result of setle
191// may be used by multiple instructions).
192//---------------------------------------------------------------------------
193
194bool
195SelectInstructionsForTree(BasicTreeNode* treeRoot,
196 int goalnt,
Chris Lattner6c5a32d2001-07-23 03:09:03 +0000197 TargetMachine &Target)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000198{
199 // Use a static vector to avoid allocating a new one per VM instruction
200 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
201
202 // Get the rule that matches this node.
203 //
204 int ruleForNode = burm_rule(treeRoot->state, goalnt);
205
206 if (ruleForNode == 0)
207 {
208 cerr << "Could not match instruction tree for instr selection" << endl;
209 return true;
210 }
211
212 // Get this rule's non-terminals and the corresponding child nodes (if any)
213 //
214 short *nts = burm_nts[ruleForNode];
215
216
217 // First, select instructions for the current node and rule.
218 // (If this is a list node, not an instruction, then skip this step).
219 // This function is specific to the target architecture.
220 //
221 if (treeRoot->opLabel != VRegListOp)
222 {
223 InstructionNode* instrNode = (InstructionNode*) MainTreeNode(treeRoot);
224 assert(instrNode->getNodeType() == InstrTreeNode::NTInstructionNode);
225
Chris Lattner6c5a32d2001-07-23 03:09:03 +0000226 unsigned N = GetInstructionsByRule(instrNode, ruleForNode, nts, Target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000227 minstrVec);
228 assert(N <= MAX_INSTR_PER_VMINSTR);
229 for (unsigned i=0; i < N; i++)
230 {
231 assert(minstrVec[i] != NULL);
232 instrNode->getInstruction()->addMachineInstruction(minstrVec[i]);
233 }
234 }
235
236 // Then, recursively compile the child nodes, if any.
237 //
238 if (nts[0])
239 { // i.e., there is at least one kid
240
241 BasicTreeNode* kids[2];
242 int currentRule = ruleForNode;
243 burm_kids(treeRoot, currentRule, kids);
244
245 // First skip over any chain rules so that we don't visit
246 // the current node again.
247 //
248 while (ThisIsAChainRule(currentRule))
249 {
250 currentRule = burm_rule(treeRoot->state, nts[0]);
251 nts = burm_nts[currentRule];
252 burm_kids(treeRoot, currentRule, kids);
253 }
254
255 // Now we have the first non-chain rule so we have found
256 // the actual child nodes. Recursively compile them.
257 //
258 for (int i = 0; nts[i]; i++)
259 {
260 assert(i < 2);
261 InstrTreeNode::InstrTreeNodeType
262 nodeType = MainTreeNode(kids[i])->getNodeType();
263 if (nodeType == InstrTreeNode::NTVRegListNode ||
264 nodeType == InstrTreeNode::NTInstructionNode)
265 {
Chris Lattner6c5a32d2001-07-23 03:09:03 +0000266 if (SelectInstructionsForTree(kids[i], nts[i], Target))
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000267 return true; // failure
268 }
269 }
270 }
271
272 return false; // success
273}
274