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 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 25 | //*************************** User Include Files ***************************/ |
| 26 | |
Chris Lattner | 942d99e | 2001-07-21 22:59:56 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/InstrForest.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 28 | #include "llvm/Method.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 29 | #include "llvm/iTerminators.h" |
| 30 | #include "llvm/iMemory.h" |
| 31 | #include "llvm/ConstPoolVals.h" |
| 32 | #include "llvm/BasicBlock.h" |
Chris Lattner | 7e583cf | 2001-07-21 20:58:30 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineInstr.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 34 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 35 | |
| 36 | //------------------------------------------------------------------------ |
| 37 | // class InstrTreeNode |
| 38 | //------------------------------------------------------------------------ |
| 39 | |
| 40 | |
| 41 | InstrTreeNode::InstrTreeNode(InstrTreeNodeType nodeType, |
| 42 | Value* _val) |
| 43 | : treeNodeType(nodeType), |
| 44 | val(_val) |
| 45 | { |
| 46 | basicNode.leftChild = NULL; |
| 47 | basicNode.rightChild = NULL; |
| 48 | basicNode.parent = NULL; |
| 49 | basicNode.opLabel = InvalidOp; |
| 50 | basicNode.treeNodePtr = this; |
| 51 | } |
| 52 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 53 | void |
| 54 | InstrTreeNode::dump(int dumpChildren, |
| 55 | int indent) const |
| 56 | { |
| 57 | this->dumpNode(indent); |
| 58 | |
| 59 | if (dumpChildren) |
| 60 | { |
| 61 | if (leftChild()) |
| 62 | leftChild()->dump(dumpChildren, indent+1); |
| 63 | if (rightChild()) |
| 64 | rightChild()->dump(dumpChildren, indent+1); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | InstructionNode::InstructionNode(Instruction* _instr) |
| 70 | : InstrTreeNode(NTInstructionNode, _instr) |
| 71 | { |
| 72 | OpLabel opLabel = _instr->getOpcode(); |
| 73 | |
| 74 | // Distinguish special cases of some instructions such as Ret and Br |
| 75 | // |
| 76 | if (opLabel == Instruction::Ret && ((ReturnInst*) _instr)->getReturnValue()) |
| 77 | { |
| 78 | opLabel = RetValueOp; // ret(value) operation |
| 79 | } |
| 80 | else if (opLabel == Instruction::Br && ! ((BranchInst*) _instr)->isUnconditional()) |
| 81 | { |
| 82 | opLabel = BrCondOp; // br(cond) operation |
| 83 | } |
| 84 | else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT) |
| 85 | { |
| 86 | opLabel = SetCCOp; // common label for all SetCC ops |
| 87 | } |
| 88 | else if (opLabel == Instruction::Alloca && _instr->getNumOperands() > 0) |
| 89 | { |
| 90 | opLabel = AllocaN; // Alloca(ptr, N) operation |
| 91 | } |
| 92 | else if ((opLabel == Instruction::Load || |
| 93 | opLabel == Instruction::GetElementPtr) |
| 94 | && ((MemAccessInst*)_instr)->getFirstOffsetIdx() > 0) |
| 95 | { |
| 96 | opLabel = opLabel + 100; // load/getElem with index vector |
| 97 | } |
| 98 | else if (opLabel == Instruction::Cast) |
| 99 | { |
| 100 | const Type* instrValueType = _instr->getType(); |
| 101 | switch(instrValueType->getPrimitiveID()) |
| 102 | { |
| 103 | case Type::BoolTyID: opLabel = ToBoolTy; break; |
| 104 | case Type::UByteTyID: opLabel = ToUByteTy; break; |
| 105 | case Type::SByteTyID: opLabel = ToSByteTy; break; |
| 106 | case Type::UShortTyID: opLabel = ToUShortTy; break; |
| 107 | case Type::ShortTyID: opLabel = ToShortTy; break; |
| 108 | case Type::UIntTyID: opLabel = ToUIntTy; break; |
| 109 | case Type::IntTyID: opLabel = ToIntTy; break; |
| 110 | case Type::ULongTyID: opLabel = ToULongTy; break; |
| 111 | case Type::LongTyID: opLabel = ToLongTy; break; |
| 112 | case Type::FloatTyID: opLabel = ToFloatTy; break; |
| 113 | case Type::DoubleTyID: opLabel = ToDoubleTy; break; |
| 114 | default: |
| 115 | if (instrValueType->isArrayType()) |
| 116 | opLabel = ToArrayTy; |
| 117 | else if (instrValueType->isPointerType()) |
| 118 | opLabel = ToPointerTy; |
| 119 | else |
| 120 | ; // Just use `Cast' opcode otherwise. It's probably ignored. |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | basicNode.opLabel = opLabel; |
| 126 | } |
| 127 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 128 | |
| 129 | void |
| 130 | InstructionNode::dumpNode(int indent) const |
| 131 | { |
| 132 | for (int i=0; i < indent; i++) |
| 133 | cout << " "; |
| 134 | |
| 135 | cout << getInstruction()->getOpcodeName(); |
| 136 | |
| 137 | const vector<MachineInstr*>& mvec = getInstruction()->getMachineInstrVec(); |
| 138 | if (mvec.size() > 0) |
| 139 | cout << "\tMachine Instructions: "; |
| 140 | for (unsigned int i=0; i < mvec.size(); i++) |
| 141 | { |
| 142 | mvec[i]->dump(0); |
| 143 | if (i < mvec.size() - 1) |
| 144 | cout << "; "; |
| 145 | } |
| 146 | |
| 147 | cout << endl; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | VRegListNode::VRegListNode() |
| 152 | : InstrTreeNode(NTVRegListNode, NULL) |
| 153 | { |
| 154 | basicNode.opLabel = VRegListOp; |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | VRegListNode::dumpNode(int indent) const |
| 159 | { |
| 160 | for (int i=0; i < indent; i++) |
| 161 | cout << " "; |
| 162 | |
| 163 | cout << "List" << endl; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | VRegNode::VRegNode(Value* _val) |
| 168 | : InstrTreeNode(NTVRegNode, _val) |
| 169 | { |
| 170 | basicNode.opLabel = VRegNodeOp; |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | VRegNode::dumpNode(int indent) const |
| 175 | { |
| 176 | for (int i=0; i < indent; i++) |
| 177 | cout << " "; |
| 178 | |
| 179 | cout << "VReg " << getValue() << "\t(type " |
| 180 | << (int) getValue()->getValueType() << ")" << endl; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | ConstantNode::ConstantNode(ConstPoolVal* constVal) |
| 185 | : InstrTreeNode(NTConstNode, constVal) |
| 186 | { |
| 187 | basicNode.opLabel = ConstantNodeOp; |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | ConstantNode::dumpNode(int indent) const |
| 192 | { |
| 193 | for (int i=0; i < indent; i++) |
| 194 | cout << " "; |
| 195 | |
| 196 | cout << "Constant " << getValue() << "\t(type " |
| 197 | << (int) getValue()->getValueType() << ")" << endl; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | LabelNode::LabelNode(BasicBlock* _bblock) |
| 202 | : InstrTreeNode(NTLabelNode, _bblock) |
| 203 | { |
| 204 | basicNode.opLabel = LabelNodeOp; |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | LabelNode::dumpNode(int indent) const |
| 209 | { |
| 210 | for (int i=0; i < indent; i++) |
| 211 | cout << " "; |
| 212 | |
| 213 | cout << "Label " << getValue() << endl; |
| 214 | } |
| 215 | |
| 216 | //------------------------------------------------------------------------ |
| 217 | // class InstrForest |
| 218 | // |
| 219 | // A forest of instruction trees, usually for a single method. |
| 220 | //------------------------------------------------------------------------ |
| 221 | |
| 222 | void |
| 223 | InstrForest::buildTreesForMethod(Method *method) |
| 224 | { |
| 225 | for (Method::inst_iterator instrIter = method->inst_begin(); |
| 226 | instrIter != method->inst_end(); |
| 227 | ++instrIter) |
| 228 | { |
| 229 | Instruction *instr = *instrIter; |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 230 | (void) this->buildTreeForInstruction(instr); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | void |
| 236 | InstrForest::dump() const |
| 237 | { |
Chris Lattner | 75279cc | 2001-07-23 03:50:57 +0000 | [diff] [blame] | 238 | for (hash_set<InstructionNode*>::const_iterator |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 239 | treeRootIter = treeRoots.begin(); |
| 240 | treeRootIter != treeRoots.end(); |
| 241 | ++treeRootIter) |
| 242 | { |
| 243 | (*treeRootIter)->dump(/*dumpChildren*/ 1, /*indent*/ 0); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | inline void |
| 248 | InstrForest::noteTreeNodeForInstr(Instruction* instr, |
| 249 | InstructionNode* treeNode) |
| 250 | { |
| 251 | assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode); |
| 252 | (*this)[instr] = treeNode; |
| 253 | treeRoots.insert(treeNode); // mark node as root of a new tree |
| 254 | } |
| 255 | |
| 256 | |
| 257 | inline void |
| 258 | InstrForest::setLeftChild(InstrTreeNode* parent, InstrTreeNode* child) |
| 259 | { |
| 260 | parent->basicNode.leftChild = & child->basicNode; |
| 261 | child->basicNode.parent = & parent->basicNode; |
| 262 | if (child->getNodeType() == InstrTreeNode::NTInstructionNode) |
| 263 | treeRoots.erase((InstructionNode*) child); // no longer a tree root |
| 264 | } |
| 265 | |
| 266 | |
| 267 | inline void |
| 268 | InstrForest::setRightChild(InstrTreeNode* parent, InstrTreeNode* child) |
| 269 | { |
| 270 | parent->basicNode.rightChild = & child->basicNode; |
| 271 | child->basicNode.parent = & parent->basicNode; |
| 272 | if (child->getNodeType() == InstrTreeNode::NTInstructionNode) |
| 273 | treeRoots.erase((InstructionNode*) child); // no longer a tree root |
| 274 | } |
| 275 | |
| 276 | |
| 277 | InstructionNode* |
| 278 | InstrForest::buildTreeForInstruction(Instruction* instr) |
| 279 | { |
| 280 | InstructionNode* treeNode = this->getTreeNodeForInstr(instr); |
| 281 | if (treeNode != NULL) |
| 282 | {// treeNode has already been constructed for this instruction |
| 283 | assert(treeNode->getInstruction() == instr); |
| 284 | return treeNode; |
| 285 | } |
| 286 | |
| 287 | // Otherwise, create a new tree node for this instruction. |
| 288 | // |
| 289 | treeNode = new InstructionNode(instr); |
| 290 | this->noteTreeNodeForInstr(instr, treeNode); |
| 291 | |
| 292 | // If the instruction has more than 2 instruction operands, |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 293 | // then we need to create artificial list nodes to hold them. |
| 294 | // (Note that we only not count operands that get tree nodes, and not |
| 295 | // others such as branch labels for a branch or switch instruction.) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 296 | // |
| 297 | // To do this efficiently, we'll walk all operands, build treeNodes |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 298 | // for all appropriate operands and save them in an array. We then |
| 299 | // insert children at the end, creating list nodes where needed. |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 300 | // As a performance optimization, allocate a child array only |
| 301 | // if a fixed array is too small. |
| 302 | // |
| 303 | int numChildren = 0; |
| 304 | const unsigned int MAX_CHILD = 8; |
| 305 | static InstrTreeNode* fixedChildArray[MAX_CHILD]; |
| 306 | InstrTreeNode** childArray = |
| 307 | (instr->getNumOperands() > MAX_CHILD) |
| 308 | ? new (InstrTreeNode*)[instr->getNumOperands()] |
| 309 | : fixedChildArray; |
| 310 | |
| 311 | // |
| 312 | // Walk the operands of the instruction |
| 313 | // |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 314 | for (Instruction::op_iterator O=instr->op_begin(); O != instr->op_end(); ++O) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 315 | { |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 316 | Value* operand = *O; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 317 | |
| 318 | // Check if the operand is a data value, not an branch label, type, |
| 319 | // method or module. If the operand is an address type (i.e., label |
| 320 | // or method) that is used in an non-branching operation, e.g., `add'. |
| 321 | // that should be considered a data value. |
| 322 | |
| 323 | // Check latter condition here just to simplify the next IF. |
| 324 | bool includeAddressOperand = |
| 325 | ((operand->getValueType() == Value::BasicBlockVal |
| 326 | || operand->getValueType() == Value::MethodVal) |
| 327 | && ! instr->isTerminator()); |
| 328 | |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 329 | if ( includeAddressOperand |
| 330 | || operand->getValueType() == Value::InstructionVal |
| 331 | || operand->getValueType() == Value::ConstantVal |
| 332 | || operand->getValueType() == Value::MethodArgumentVal) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 333 | {// This operand is a data value |
| 334 | |
| 335 | // An instruction that computes the incoming value is added as a |
| 336 | // child of the current instruction if: |
| 337 | // the value has only a single use |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 338 | // AND both instructions are in the same basic block. |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 339 | // |
| 340 | // (Note that if the value has only a single use (viz., `instr'), |
| 341 | // the def of the value can be safely moved just before instr |
| 342 | // and therefore it is safe to combine these two instructions.) |
| 343 | // |
| 344 | // In all other cases, the virtual register holding the value |
| 345 | // is used directly, i.e., made a child of the instruction node. |
| 346 | // |
| 347 | InstrTreeNode* opTreeNode; |
| 348 | if (operand->getValueType() == Value::InstructionVal |
| 349 | && operand->use_size() == 1 |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 350 | && ((Instruction*)operand)->getParent() == instr->getParent()) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 351 | { |
| 352 | // Recursively create a treeNode for it. |
| 353 | opTreeNode =this->buildTreeForInstruction((Instruction*)operand); |
| 354 | } |
| 355 | else if (operand->getValueType() == Value::ConstantVal) |
| 356 | { |
| 357 | // Create a leaf node for a constant |
| 358 | opTreeNode = new ConstantNode((ConstPoolVal*) operand); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | // Create a leaf node for the virtual register |
| 363 | opTreeNode = new VRegNode(operand); |
| 364 | } |
| 365 | |
| 366 | childArray[numChildren] = opTreeNode; |
| 367 | numChildren++; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | //-------------------------------------------------------------------- |
| 372 | // Add any selected operands as children in the tree. |
| 373 | // Certain instructions can have more than 2 in some instances (viz., |
| 374 | // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an |
| 375 | // array or struct). Make the operands of every such instruction into |
| 376 | // a right-leaning binary tree with the operand nodes at the leaves |
| 377 | // and VRegList nodes as internal nodes. |
| 378 | //-------------------------------------------------------------------- |
| 379 | |
| 380 | InstrTreeNode* parent = treeNode; // new VRegListNode(); |
| 381 | int n; |
| 382 | |
| 383 | if (numChildren > 2) |
| 384 | { |
| 385 | unsigned instrOpcode = treeNode->getInstruction()->getOpcode(); |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 386 | assert(instrOpcode == Instruction::PHINode || |
| 387 | instrOpcode == Instruction::Call || |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 388 | instrOpcode == Instruction::Load || |
| 389 | instrOpcode == Instruction::Store || |
| 390 | instrOpcode == Instruction::GetElementPtr); |
| 391 | } |
| 392 | |
| 393 | // Insert the first child as a direct child |
| 394 | if (numChildren >= 1) |
| 395 | this->setLeftChild(parent, childArray[0]); |
| 396 | |
| 397 | // Create a list node for children 2 .. N-1, if any |
| 398 | for (n = numChildren-1; n >= 2; n--) |
| 399 | { // We have more than two children |
| 400 | InstrTreeNode* listNode = new VRegListNode(); |
| 401 | this->setRightChild(parent, listNode); |
| 402 | this->setLeftChild(listNode, childArray[numChildren - n]); |
| 403 | parent = listNode; |
| 404 | } |
| 405 | |
| 406 | // Now insert the last remaining child (if any). |
| 407 | if (numChildren >= 2) |
| 408 | { |
| 409 | assert(n == 1); |
| 410 | this->setRightChild(parent, childArray[numChildren - 1]); |
| 411 | } |
| 412 | |
| 413 | if (childArray != fixedChildArray) |
| 414 | { |
| 415 | delete[] childArray; |
| 416 | } |
| 417 | |
| 418 | return treeNode; |
| 419 | } |
| 420 | |