Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 1 | // $Id$ |
| 2 | //--------------------------------------------------------------------------- |
| 3 | // File: |
| 4 | // InstrForest.cpp |
| 5 | // |
| 6 | // Purpose: |
| 7 | // Convert SSA graph to instruction trees for instruction selection. |
| 8 | // |
| 9 | // Strategy: |
| 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. |
| 19 | // |
| 20 | // History: |
| 21 | // 6/28/01 - Vikram Adve - Created |
| 22 | // |
| 23 | //--------------------------------------------------------------------------- |
| 24 | |
Chris Lattner | 942d99e | 2001-07-21 22:59:56 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/InstrForest.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 26 | #include "llvm/Method.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 27 | #include "llvm/iTerminators.h" |
| 28 | #include "llvm/iMemory.h" |
| 29 | #include "llvm/ConstPoolVals.h" |
| 30 | #include "llvm/BasicBlock.h" |
Chris Lattner | 7e583cf | 2001-07-21 20:58:30 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 32 | #include "llvm/Support/STLExtras.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 33 | |
| 34 | //------------------------------------------------------------------------ |
| 35 | // class InstrTreeNode |
| 36 | //------------------------------------------------------------------------ |
| 37 | |
Chris Lattner | d268ad6 | 2001-09-11 23:52:11 +0000 | [diff] [blame] | 38 | void InstrTreeNode::dump(int dumpChildren, int indent) const { |
| 39 | dumpNode(indent); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 41 | if (dumpChildren) { |
| 42 | if (leftChild()) |
| 43 | leftChild()->dump(dumpChildren, indent+1); |
| 44 | if (rightChild()) |
| 45 | rightChild()->dump(dumpChildren, indent+1); |
| 46 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 50 | InstructionNode::InstructionNode(Instruction* I) |
| 51 | : InstrTreeNode(NTInstructionNode, I) { |
| 52 | opLabel = I->getOpcode(); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 53 | |
| 54 | // Distinguish special cases of some instructions such as Ret and Br |
| 55 | // |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 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; |
| 85 | default: |
| 86 | // Just use `Cast' opcode otherwise. It's probably ignored. |
| 87 | break; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 89 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 93 | void InstructionNode::dumpNode(int indent) const { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 94 | for (int i=0; i < indent; i++) |
| 95 | cout << " "; |
| 96 | |
| 97 | cout << getInstruction()->getOpcodeName(); |
| 98 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 99 | const vector<MachineInstr*> &mvec = getInstruction()->getMachineInstrVec(); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 100 | if (mvec.size() > 0) |
| 101 | cout << "\tMachine Instructions: "; |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 102 | for (unsigned int i=0; i < mvec.size(); i++) { |
| 103 | mvec[i]->dump(0); |
| 104 | if (i < mvec.size() - 1) |
| 105 | cout << "; "; |
| 106 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 107 | |
| 108 | cout << endl; |
| 109 | } |
| 110 | |
| 111 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 112 | void VRegListNode::dumpNode(int indent) const { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 113 | for (int i=0; i < indent; i++) |
| 114 | cout << " "; |
| 115 | |
| 116 | cout << "List" << endl; |
| 117 | } |
| 118 | |
| 119 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 120 | void VRegNode::dumpNode(int indent) const { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 121 | for (int i=0; i < indent; i++) |
| 122 | cout << " "; |
| 123 | |
| 124 | cout << "VReg " << getValue() << "\t(type " |
| 125 | << (int) getValue()->getValueType() << ")" << endl; |
| 126 | } |
| 127 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 128 | void ConstantNode::dumpNode(int indent) const { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 129 | for (int i=0; i < indent; i++) |
| 130 | cout << " "; |
| 131 | |
| 132 | cout << "Constant " << getValue() << "\t(type " |
| 133 | << (int) getValue()->getValueType() << ")" << endl; |
| 134 | } |
| 135 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 136 | void LabelNode::dumpNode(int indent) const { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 137 | for (int i=0; i < indent; i++) |
| 138 | cout << " "; |
| 139 | |
| 140 | cout << "Label " << getValue() << endl; |
| 141 | } |
| 142 | |
| 143 | //------------------------------------------------------------------------ |
| 144 | // class InstrForest |
| 145 | // |
| 146 | // A forest of instruction trees, usually for a single method. |
| 147 | //------------------------------------------------------------------------ |
| 148 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 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); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 155 | inline void InstrForest::noteTreeNodeForInstr(Instruction *instr, |
| 156 | InstructionNode *treeNode) { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 157 | assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode); |
| 158 | (*this)[instr] = treeNode; |
| 159 | treeRoots.insert(treeNode); // mark node as root of a new tree |
| 160 | } |
| 161 | |
| 162 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 163 | inline void InstrForest::setLeftChild(InstrTreeNode *Par, InstrTreeNode *Chld) { |
| 164 | Par->LeftChild = Chld; |
| 165 | Chld->Parent = Par; |
| 166 | if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode) |
| 167 | treeRoots.erase((InstructionNode*)Chld); // no longer a tree root |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 171 | inline void InstrForest::setRightChild(InstrTreeNode *Par, InstrTreeNode *Chld){ |
| 172 | Par->RightChild = Chld; |
| 173 | Chld->Parent = Par; |
| 174 | if (Chld->getNodeType() == InstrTreeNode::NTInstructionNode) |
| 175 | treeRoots.erase((InstructionNode*)Chld); // no longer a tree root |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 179 | void InstrForest::buildTreesForMethod(Method *M) { |
| 180 | for_each(M->inst_begin(), M->inst_end(), |
| 181 | bind_obj(this, &InstrForest::buildTreeForInstruction)); |
| 182 | } |
| 183 | |
| 184 | InstructionNode *InstrForest::buildTreeForInstruction(Instruction *Inst) { |
| 185 | InstructionNode *treeNode = getTreeNodeForInstr(Inst); |
| 186 | if (treeNode) { |
| 187 | // treeNode has already been constructed for this instruction |
| 188 | assert(treeNode->getInstruction() == Inst); |
| 189 | return treeNode; |
| 190 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 191 | |
| 192 | // Otherwise, create a new tree node for this instruction. |
| 193 | // |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 194 | treeNode = new InstructionNode(Inst); |
| 195 | noteTreeNodeForInstr(Inst, treeNode); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 196 | |
| 197 | // If the instruction has more than 2 instruction operands, |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 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.) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 201 | // |
| 202 | // To do this efficiently, we'll walk all operands, build treeNodes |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 203 | // for all appropriate operands and save them in an array. We then |
| 204 | // insert children at the end, creating list nodes where needed. |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 205 | // As a performance optimization, allocate a child array only |
| 206 | // if a fixed array is too small. |
| 207 | // |
| 208 | int numChildren = 0; |
| 209 | const unsigned int MAX_CHILD = 8; |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 210 | static InstrTreeNode *fixedChildArray[MAX_CHILD]; |
| 211 | InstrTreeNode **childArray = |
| 212 | (Inst->getNumOperands() > MAX_CHILD) |
| 213 | ? new (InstrTreeNode*)[Inst->getNumOperands()] |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 214 | : fixedChildArray; |
| 215 | |
| 216 | // |
| 217 | // Walk the operands of the instruction |
| 218 | // |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 219 | for (Instruction::op_iterator O = Inst->op_begin(); O != Inst->op_end(); ++O){ |
| 220 | Value* operand = *O; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 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. |
| 226 | |
| 227 | // Check latter condition here just to simplify the next IF. |
| 228 | bool includeAddressOperand = |
| 229 | (operand->isBasicBlock() || operand->isMethod()) |
| 230 | && !Inst->isTerminator(); |
| 231 | |
| 232 | if (includeAddressOperand || operand->isInstruction() || |
| 233 | operand->isConstant() || operand->isMethodArgument()) { |
| 234 | // This operand is a data value |
| 235 | |
| 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. |
| 240 | // |
| 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.) |
| 244 | // |
| 245 | // In all other cases, the virtual register holding the value |
| 246 | // is used directly, i.e., made a child of the instruction node. |
| 247 | // |
| 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); |
| 256 | } else { |
| 257 | // Create a leaf node for the virtual register |
| 258 | opTreeNode = new VRegNode(operand); |
| 259 | } |
| 260 | |
| 261 | childArray[numChildren++] = opTreeNode; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 262 | } |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 263 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 264 | |
| 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 | //-------------------------------------------------------------------- |
| 273 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 274 | InstrTreeNode *parent = treeNode; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 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); |
| 283 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 284 | |
| 285 | // Insert the first child as a direct child |
| 286 | if (numChildren >= 1) |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 287 | setLeftChild(parent, childArray[0]); |
| 288 | |
| 289 | int n; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 290 | |
| 291 | // Create a list node for children 2 .. N-1, if any |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 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]); |
| 297 | parent = listNode; |
| 298 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 299 | |
| 300 | // Now insert the last remaining child (if any). |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 301 | if (numChildren >= 2) { |
| 302 | assert(n == 1); |
| 303 | setRightChild(parent, childArray[numChildren - 1]); |
| 304 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 305 | |
| 306 | if (childArray != fixedChildArray) |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame^] | 307 | delete [] childArray; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 308 | |
| 309 | return treeNode; |
| 310 | } |
| 311 | |