2 //---------------------------------------------------------------------------
7 // Convert SSA graph to instruction trees for instruction selection.
10 // The key goal is to group instructions into a single
11 // tree if one or more of them might be potentially combined into a single
12 // complex instruction in the target machine.
13 // Since this grouping is completely machine-independent, we do it as
14 // aggressive as possible to exploit any possible taret instructions.
15 // In particular, we group two instructions O and I if:
16 // (1) Instruction O computes an operand used by instruction I,
17 // and (2) O and I are part of the same basic block,
18 // and (3) O has only a single use, viz., I.
21 // 6/28/01 - Vikram Adve - Created
23 //---------------------------------------------------------------------------
25 #include "llvm/CodeGen/InstrForest.h"
26 #include "llvm/Method.h"
27 #include "llvm/iTerminators.h"
28 #include "llvm/iMemory.h"
29 #include "llvm/ConstPoolVals.h"
30 #include "llvm/BasicBlock.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/Support/STLExtras.h"
34 //------------------------------------------------------------------------
35 // class InstrTreeNode
36 //------------------------------------------------------------------------
38 void InstrTreeNode::dump(int dumpChildren, int indent) const {
43 leftChild()->dump(dumpChildren, indent+1);
45 rightChild()->dump(dumpChildren, indent+1);
50 InstructionNode::InstructionNode(Instruction* I)
51 : InstrTreeNode(NTInstructionNode, I) {
52 opLabel = I->getOpcode();
54 // Distinguish special cases of some instructions such as Ret and Br
56 if (opLabel == Instruction::Ret && ((ReturnInst*)I)->getReturnValue()) {
57 opLabel = RetValueOp; // ret(value) operation
58 } else if (opLabel == Instruction::Br &&
59 !((BranchInst*)I)->isUnconditional()) {
60 opLabel = BrCondOp; // br(cond) operation
61 } else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT) {
62 opLabel = SetCCOp; // common label for all SetCC ops
63 } else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0) {
64 opLabel = AllocaN; // Alloca(ptr, N) operation
65 } else if ((opLabel == Instruction::Load ||
66 opLabel == Instruction::GetElementPtr) &&
67 ((MemAccessInst*)I)->getFirstOffsetIdx() > 0) {
68 opLabel = opLabel + 100; // load/getElem with index vector
69 } else if (opLabel == Instruction::Cast) {
70 const Type *ITy = I->getType();
71 switch(ITy->getPrimitiveID()) {
72 case Type::BoolTyID: opLabel = ToBoolTy; break;
73 case Type::UByteTyID: opLabel = ToUByteTy; break;
74 case Type::SByteTyID: opLabel = ToSByteTy; break;
75 case Type::UShortTyID: opLabel = ToUShortTy; break;
76 case Type::ShortTyID: opLabel = ToShortTy; break;
77 case Type::UIntTyID: opLabel = ToUIntTy; break;
78 case Type::IntTyID: opLabel = ToIntTy; break;
79 case Type::ULongTyID: opLabel = ToULongTy; break;
80 case Type::LongTyID: opLabel = ToLongTy; break;
81 case Type::FloatTyID: opLabel = ToFloatTy; break;
82 case Type::DoubleTyID: opLabel = ToDoubleTy; break;
83 case Type::ArrayTyID: opLabel = ToArrayTy; break;
84 case Type::PointerTyID: opLabel = ToPointerTy; break;
86 // Just use `Cast' opcode otherwise. It's probably ignored.
93 void InstructionNode::dumpNode(int indent) const {
94 for (int i=0; i < indent; i++)
97 cout << getInstruction()->getOpcodeName();
99 const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec();
101 cout << "\tMachine Instructions: ";
102 for (unsigned int i=0; i < mvec.size(); i++) {
104 if (i < mvec.size() - 1)
112 void VRegListNode::dumpNode(int indent) const {
113 for (int i=0; i < indent; i++)
116 cout << "List" << endl;
120 void VRegNode::dumpNode(int indent) const {
121 for (int i=0; i < indent; i++)
124 cout << "VReg " << getValue() << "\t(type "
125 << (int) getValue()->getValueType() << ")" << endl;
128 void ConstantNode::dumpNode(int indent) const {
129 for (int i=0; i < indent; i++)
132 cout << "Constant " << getValue() << "\t(type "
133 << (int) getValue()->getValueType() << ")" << endl;
136 void LabelNode::dumpNode(int indent) const {
137 for (int i=0; i < indent; i++)
140 cout << "Label " << getValue() << endl;
143 //------------------------------------------------------------------------
146 // A forest of instruction trees, usually for a single method.
147 //------------------------------------------------------------------------
149 void InstrForest::dump() const {
150 for (hash_set<InstructionNode*>::const_iterator I = treeRoots.begin();
151 I != treeRoots.end(); ++I)
152 (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0);
155 inline void InstrForest::noteTreeNodeForInstr(Instruction *instr,
156 InstructionNode *treeNode) {
157 assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
158 (*this)[instr] = treeNode;
159 treeRoots.insert(treeNode); // mark node as root of a new tree
163 inline void InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld) {
164 Par->LeftChild = Chld;
166 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
167 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
171 inline void InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld){
172 Par->RightChild = Chld;
174 if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode)
175 treeRoots.erase((InstructionNode*)Chld); // no longer a tree root
179 void InstrForest::buildTreesForMethod(Method *M) {
180 for_each(M->inst_begin(), M->inst_end(),
181 bind_obj(this, &InstrForest::buildTreeForInstruction));
184 InstructionNode *InstrForest::buildTreeForInstruction(Instruction *Inst) {
185 InstructionNode *treeNode = getTreeNodeForInstr(Inst);
187 // treeNode has already been constructed for this instruction
188 assert(treeNode->getInstruction() == Inst);
192 // Otherwise, create a new tree node for this instruction.
194 treeNode = new InstructionNode(Inst);
195 noteTreeNodeForInstr(Inst, treeNode);
197 // If the instruction has more than 2 instruction operands,
198 // then we need to create artificial list nodes to hold them.
199 // (Note that we only not count operands that get tree nodes, and not
200 // others such as branch labels for a branch or switch instruction.)
202 // To do this efficiently, we'll walk all operands, build treeNodes
203 // for all appropriate operands and save them in an array. We then
204 // insert children at the end, creating list nodes where needed.
205 // As a performance optimization, allocate a child array only
206 // if a fixed array is too small.
209 const unsigned int MAX_CHILD = 8;
210 static InstrTreeNode *fixedChildArray[MAX_CHILD];
211 InstrTreeNode **childArray =
212 (Inst->getNumOperands() > MAX_CHILD)
213 ? new (InstrTreeNode*)[Inst->getNumOperands()]
217 // Walk the operands of the instruction
219 for (Instruction::op_iterator O = Inst->op_begin(); O != Inst->op_end(); ++O){
222 // Check if the operand is a data value, not an branch label, type,
223 // method or module. If the operand is an address type (i.e., label
224 // or method) that is used in an non-branching operation, e.g., `add'.
225 // that should be considered a data value.
227 // Check latter condition here just to simplify the next IF.
228 bool includeAddressOperand =
229 (operand->isBasicBlock() || operand->isMethod())
230 && !Inst->isTerminator();
232 if (includeAddressOperand || operand->isInstruction() ||
233 operand->isConstant() || operand->isMethodArgument()) {
234 // This operand is a data value
236 // An instruction that computes the incoming value is added as a
237 // child of the current instruction if:
238 // the value has only a single use
239 // AND both instructions are in the same basic block.
241 // (Note that if the value has only a single use (viz., `instr'),
242 // the def of the value can be safely moved just before instr
243 // and therefore it is safe to combine these two instructions.)
245 // In all other cases, the virtual register holding the value
246 // is used directly, i.e., made a child of the instruction node.
248 InstrTreeNode* opTreeNode;
249 if (operand->isInstruction() && operand->use_size() == 1 &&
250 ((Instruction*)operand)->getParent() == Inst->getParent()) {
251 // Recursively create a treeNode for it.
252 opTreeNode = buildTreeForInstruction((Instruction*)operand);
253 } else if (ConstPoolVal *CPV = operand->castConstant()) {
254 // Create a leaf node for a constant
255 opTreeNode = new ConstantNode(CPV);
257 // Create a leaf node for the virtual register
258 opTreeNode = new VRegNode(operand);
261 childArray[numChildren++] = opTreeNode;
265 //--------------------------------------------------------------------
266 // Add any selected operands as children in the tree.
267 // Certain instructions can have more than 2 in some instances (viz.,
268 // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an
269 // array or struct). Make the operands of every such instruction into
270 // a right-leaning binary tree with the operand nodes at the leaves
271 // and VRegList nodes as internal nodes.
272 //--------------------------------------------------------------------
274 InstrTreeNode *parent = treeNode;
276 if (numChildren > 2) {
277 unsigned instrOpcode = treeNode->getInstruction()->getOpcode();
278 assert(instrOpcode == Instruction::PHINode ||
279 instrOpcode == Instruction::Call ||
280 instrOpcode == Instruction::Load ||
281 instrOpcode == Instruction::Store ||
282 instrOpcode == Instruction::GetElementPtr);
285 // Insert the first child as a direct child
286 if (numChildren >= 1)
287 setLeftChild(parent, childArray[0]);
291 // Create a list node for children 2 .. N-1, if any
292 for (n = numChildren-1; n >= 2; n--) {
293 // We have more than two children
294 InstrTreeNode *listNode = new VRegListNode();
295 setRightChild(parent, listNode);
296 setLeftChild(listNode, childArray[numChildren - n]);
300 // Now insert the last remaining child (if any).
301 if (numChildren >= 2) {
303 setRightChild(parent, childArray[numChildren - 1]);
306 if (childArray != fixedChildArray)
307 delete [] childArray;