Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 1 | //===-- InstrForest.cpp - Build instruction forest for inst selection -----===// |
| 2 | // |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 3 | // The key goal is to group instructions into a single |
| 4 | // tree if one or more of them might be potentially combined into a single |
| 5 | // complex instruction in the target machine. |
| 6 | // Since this grouping is completely machine-independent, we do it as |
| 7 | // aggressive as possible to exploit any possible taret instructions. |
| 8 | // In particular, we group two instructions O and I if: |
| 9 | // (1) Instruction O computes an operand used by instruction I, |
| 10 | // and (2) O and I are part of the same basic block, |
| 11 | // and (3) O has only a single use, viz., I. |
| 12 | // |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 14 | |
Chris Lattner | 942d99e | 2001-07-21 22:59:56 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/InstrForest.h" |
Chris Lattner | a8bbb6b | 2002-02-03 07:31:41 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 18 | #include "llvm/iTerminators.h" |
| 19 | #include "llvm/iMemory.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 20 | #include "llvm/Constant.h" |
Chris Lattner | 86e9187 | 2002-04-29 18:48:55 +0000 | [diff] [blame] | 21 | #include "llvm/Type.h" |
Chris Lattner | 7e583cf | 2001-07-21 20:58:30 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 23 | #include "Support/STLExtras.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 24 | using std::cerr; |
| 25 | using std::vector; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 26 | |
| 27 | //------------------------------------------------------------------------ |
| 28 | // class InstrTreeNode |
| 29 | //------------------------------------------------------------------------ |
| 30 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 31 | void |
| 32 | InstrTreeNode::dump(int dumpChildren, int indent) const |
| 33 | { |
Chris Lattner | d268ad6 | 2001-09-11 23:52:11 +0000 | [diff] [blame] | 34 | dumpNode(indent); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 35 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 36 | if (dumpChildren) |
| 37 | { |
| 38 | if (LeftChild) |
| 39 | LeftChild->dump(dumpChildren, indent+1); |
| 40 | if (RightChild) |
| 41 | RightChild->dump(dumpChildren, indent+1); |
| 42 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 46 | InstructionNode::InstructionNode(Instruction* I) |
Vikram S. Adve | d95919c | 2002-03-24 03:24:00 +0000 | [diff] [blame] | 47 | : InstrTreeNode(NTInstructionNode, I), |
| 48 | codeIsFoldedIntoParent(false) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 49 | { |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 50 | opLabel = I->getOpcode(); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 51 | |
| 52 | // Distinguish special cases of some instructions such as Ret and Br |
| 53 | // |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 54 | if (opLabel == Instruction::Ret && cast<ReturnInst>(I)->getReturnValue()) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 55 | { |
| 56 | opLabel = RetValueOp; // ret(value) operation |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 57 | } |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 58 | else if (opLabel ==Instruction::Br && !cast<BranchInst>(I)->isUnconditional()) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 59 | { |
| 60 | opLabel = BrCondOp; // br(cond) operation |
| 61 | } |
| 62 | else if (opLabel >= Instruction::SetEQ && opLabel <= Instruction::SetGT) |
| 63 | { |
| 64 | opLabel = SetCCOp; // common label for all SetCC ops |
| 65 | } |
| 66 | else if (opLabel == Instruction::Alloca && I->getNumOperands() > 0) |
| 67 | { |
| 68 | opLabel = AllocaN; // Alloca(ptr, N) operation |
| 69 | } |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame^] | 70 | else if (opLabel == Instruction::GetElementPtr && |
| 71 | cast<GetElementPtrInst>(I)->hasIndices()) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 72 | { |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame^] | 73 | opLabel = opLabel + 100; // getElem with index vector |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 74 | } |
Vikram S. Adve | 85af131 | 2002-08-15 14:19:22 +0000 | [diff] [blame] | 75 | else if (opLabel == Instruction::Xor && |
| 76 | BinaryOperator::isNot(I)) |
| 77 | { |
| 78 | opLabel = (I->getType() == Type::BoolTy)? NotOp // boolean Not operator |
| 79 | : BNotOp; // bitwise Not operator |
| 80 | } |
Vikram S. Adve | be49526 | 2001-11-08 04:47:06 +0000 | [diff] [blame] | 81 | else if (opLabel == Instruction::And || |
| 82 | opLabel == Instruction::Or || |
Vikram S. Adve | 85af131 | 2002-08-15 14:19:22 +0000 | [diff] [blame] | 83 | opLabel == Instruction::Xor) |
Vikram S. Adve | be49526 | 2001-11-08 04:47:06 +0000 | [diff] [blame] | 84 | { |
| 85 | // Distinguish bitwise operators from logical operators! |
| 86 | if (I->getType() != Type::BoolTy) |
| 87 | opLabel = opLabel + 100; // bitwise operator |
| 88 | } |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 89 | else if (opLabel == Instruction::Cast) |
| 90 | { |
| 91 | const Type *ITy = I->getType(); |
| 92 | switch(ITy->getPrimitiveID()) |
| 93 | { |
| 94 | case Type::BoolTyID: opLabel = ToBoolTy; break; |
| 95 | case Type::UByteTyID: opLabel = ToUByteTy; break; |
| 96 | case Type::SByteTyID: opLabel = ToSByteTy; break; |
| 97 | case Type::UShortTyID: opLabel = ToUShortTy; break; |
| 98 | case Type::ShortTyID: opLabel = ToShortTy; break; |
| 99 | case Type::UIntTyID: opLabel = ToUIntTy; break; |
| 100 | case Type::IntTyID: opLabel = ToIntTy; break; |
| 101 | case Type::ULongTyID: opLabel = ToULongTy; break; |
| 102 | case Type::LongTyID: opLabel = ToLongTy; break; |
| 103 | case Type::FloatTyID: opLabel = ToFloatTy; break; |
| 104 | case Type::DoubleTyID: opLabel = ToDoubleTy; break; |
| 105 | case Type::ArrayTyID: opLabel = ToArrayTy; break; |
| 106 | case Type::PointerTyID: opLabel = ToPointerTy; break; |
| 107 | default: |
| 108 | // Just use `Cast' opcode otherwise. It's probably ignored. |
| 109 | break; |
| 110 | } |
| 111 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 114 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 115 | void |
| 116 | InstructionNode::dumpNode(int indent) const |
| 117 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 118 | for (int i=0; i < indent; i++) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 119 | cerr << " "; |
Vikram S. Adve | da920aa | 2002-08-22 02:59:46 +0000 | [diff] [blame] | 120 | cerr << getInstruction()->getOpcodeName() |
| 121 | << " [label " << getOpLabel() << "]" << "\n"; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 125 | void |
| 126 | VRegListNode::dumpNode(int indent) const |
| 127 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 128 | for (int i=0; i < indent; i++) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 129 | cerr << " "; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 131 | cerr << "List" << "\n"; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 135 | void |
| 136 | VRegNode::dumpNode(int indent) const |
| 137 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 138 | for (int i=0; i < indent; i++) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 139 | cerr << " "; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 141 | cerr << "VReg " << getValue() << "\t(type " |
| 142 | << (int) getValue()->getValueType() << ")" << "\n"; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 145 | void |
| 146 | ConstantNode::dumpNode(int indent) const |
| 147 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 148 | for (int i=0; i < indent; i++) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 149 | cerr << " "; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 151 | cerr << "Constant " << getValue() << "\t(type " |
| 152 | << (int) getValue()->getValueType() << ")" << "\n"; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 155 | void |
| 156 | LabelNode::dumpNode(int indent) const |
| 157 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 158 | for (int i=0; i < indent; i++) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 159 | cerr << " "; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 161 | cerr << "Label " << getValue() << "\n"; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | //------------------------------------------------------------------------ |
| 165 | // class InstrForest |
| 166 | // |
| 167 | // A forest of instruction trees, usually for a single method. |
| 168 | //------------------------------------------------------------------------ |
| 169 | |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 170 | InstrForest::InstrForest(Function *F) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 171 | { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 172 | for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) { |
| 173 | for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 174 | buildTreeForInstruction(I); |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 175 | } |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | InstrForest::~InstrForest() |
| 179 | { |
Chris Lattner | 7884cd1 | 2002-04-08 23:09:07 +0000 | [diff] [blame] | 180 | for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>); |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
| 184 | InstrForest::dump() const |
| 185 | { |
Vikram S. Adve | d95919c | 2002-03-24 03:24:00 +0000 | [diff] [blame] | 186 | for (const_root_iterator I = roots_begin(); I != roots_end(); ++I) |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 187 | (*I)->dump(/*dumpChildren*/ 1, /*indent*/ 0); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 190 | inline void |
Vikram S. Adve | d95919c | 2002-03-24 03:24:00 +0000 | [diff] [blame] | 191 | InstrForest::eraseRoot(InstructionNode* node) |
| 192 | { |
| 193 | for (RootSet::reverse_iterator RI=treeRoots.rbegin(), RE=treeRoots.rend(); |
| 194 | RI != RE; ++RI) |
| 195 | if (*RI == node) |
| 196 | treeRoots.erase(RI.base()-1); |
| 197 | } |
| 198 | |
| 199 | inline void |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 200 | InstrForest::noteTreeNodeForInstr(Instruction *instr, |
| 201 | InstructionNode *treeNode) |
| 202 | { |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 203 | assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode); |
| 204 | (*this)[instr] = treeNode; |
Vikram S. Adve | d95919c | 2002-03-24 03:24:00 +0000 | [diff] [blame] | 205 | treeRoots.push_back(treeNode); // mark node as root of a new tree |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 209 | inline void |
Vikram S. Adve | d95919c | 2002-03-24 03:24:00 +0000 | [diff] [blame] | 210 | InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 211 | { |
Vikram S. Adve | d95919c | 2002-03-24 03:24:00 +0000 | [diff] [blame] | 212 | parent->LeftChild = child; |
| 213 | child->Parent = parent; |
| 214 | if (child->getNodeType() == InstrTreeNode::NTInstructionNode) |
| 215 | eraseRoot((InstructionNode*) child); // no longer a tree root |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 218 | inline void |
Vikram S. Adve | d95919c | 2002-03-24 03:24:00 +0000 | [diff] [blame] | 219 | InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 220 | { |
Vikram S. Adve | d95919c | 2002-03-24 03:24:00 +0000 | [diff] [blame] | 221 | parent->RightChild = child; |
| 222 | child->Parent = parent; |
| 223 | if (child->getNodeType() == InstrTreeNode::NTInstructionNode) |
| 224 | eraseRoot((InstructionNode*) child); // no longer a tree root |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 228 | InstructionNode* |
| 229 | InstrForest::buildTreeForInstruction(Instruction *instr) |
| 230 | { |
| 231 | InstructionNode *treeNode = getTreeNodeForInstr(instr); |
| 232 | if (treeNode) |
| 233 | { |
| 234 | // treeNode has already been constructed for this instruction |
| 235 | assert(treeNode->getInstruction() == instr); |
| 236 | return treeNode; |
| 237 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 238 | |
| 239 | // Otherwise, create a new tree node for this instruction. |
| 240 | // |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 241 | treeNode = new InstructionNode(instr); |
| 242 | noteTreeNodeForInstr(instr, treeNode); |
| 243 | |
| 244 | if (instr->getOpcode() == Instruction::Call) |
| 245 | { // Operands of call instruction |
| 246 | return treeNode; |
| 247 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 248 | |
| 249 | // If the instruction has more than 2 instruction operands, |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 250 | // then we need to create artificial list nodes to hold them. |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 251 | // (Note that we only count operands that get tree nodes, and not |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 252 | // others such as branch labels for a branch or switch instruction.) |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 253 | // |
| 254 | // To do this efficiently, we'll walk all operands, build treeNodes |
Vikram S. Adve | e4e77f9 | 2001-07-31 21:49:53 +0000 | [diff] [blame] | 255 | // for all appropriate operands and save them in an array. We then |
| 256 | // insert children at the end, creating list nodes where needed. |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 257 | // As a performance optimization, allocate a child array only |
| 258 | // if a fixed array is too small. |
| 259 | // |
| 260 | int numChildren = 0; |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 261 | InstrTreeNode **childArray = |
Chris Lattner | 7884cd1 | 2002-04-08 23:09:07 +0000 | [diff] [blame] | 262 | (InstrTreeNode **)alloca(instr->getNumOperands()*sizeof(InstrTreeNode *)); |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 263 | |
| 264 | // |
| 265 | // Walk the operands of the instruction |
| 266 | // |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 267 | for (Instruction::op_iterator O = instr->op_begin(); O!=instr->op_end(); ++O) |
| 268 | { |
| 269 | Value* operand = *O; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 270 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 271 | // Check if the operand is a data value, not an branch label, type, |
| 272 | // method or module. If the operand is an address type (i.e., label |
| 273 | // or method) that is used in an non-branching operation, e.g., `add'. |
| 274 | // that should be considered a data value. |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 275 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 276 | // Check latter condition here just to simplify the next IF. |
| 277 | bool includeAddressOperand = |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 278 | (isa<BasicBlock>(operand) || isa<Function>(operand)) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 279 | && !instr->isTerminator(); |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 281 | if (includeAddressOperand || isa<Instruction>(operand) || |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 282 | isa<Constant>(operand) || isa<Argument>(operand) || |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 283 | isa<GlobalVariable>(operand)) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 284 | { |
| 285 | // This operand is a data value |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 286 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 287 | // An instruction that computes the incoming value is added as a |
| 288 | // child of the current instruction if: |
| 289 | // the value has only a single use |
| 290 | // AND both instructions are in the same basic block. |
| 291 | // AND the current instruction is not a PHI (because the incoming |
| 292 | // value is conceptually in a predecessor block, |
| 293 | // even though it may be in the same static block) |
| 294 | // |
| 295 | // (Note that if the value has only a single use (viz., `instr'), |
| 296 | // the def of the value can be safely moved just before instr |
| 297 | // and therefore it is safe to combine these two instructions.) |
| 298 | // |
| 299 | // In all other cases, the virtual register holding the value |
| 300 | // is used directly, i.e., made a child of the instruction node. |
| 301 | // |
| 302 | InstrTreeNode* opTreeNode; |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 303 | if (isa<Instruction>(operand) && operand->use_size() == 1 && |
| 304 | cast<Instruction>(operand)->getParent() == instr->getParent() && |
Chris Lattner | 7884cd1 | 2002-04-08 23:09:07 +0000 | [diff] [blame] | 305 | instr->getOpcode() != Instruction::PHINode && |
Vikram S. Adve | 64c2ced | 2001-09-30 23:45:08 +0000 | [diff] [blame] | 306 | instr->getOpcode() != Instruction::Call) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 307 | { |
| 308 | // Recursively create a treeNode for it. |
| 309 | opTreeNode = buildTreeForInstruction((Instruction*)operand); |
| 310 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 311 | else if (Constant *CPV = dyn_cast<Constant>(operand)) |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 312 | { |
| 313 | // Create a leaf node for a constant |
| 314 | opTreeNode = new ConstantNode(CPV); |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | // Create a leaf node for the virtual register |
| 319 | opTreeNode = new VRegNode(operand); |
| 320 | } |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 321 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 322 | childArray[numChildren++] = opTreeNode; |
| 323 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | //-------------------------------------------------------------------- |
| 327 | // Add any selected operands as children in the tree. |
| 328 | // Certain instructions can have more than 2 in some instances (viz., |
| 329 | // a CALL or a memory access -- LOAD, STORE, and GetElemPtr -- to an |
| 330 | // array or struct). Make the operands of every such instruction into |
| 331 | // a right-leaning binary tree with the operand nodes at the leaves |
| 332 | // and VRegList nodes as internal nodes. |
| 333 | //-------------------------------------------------------------------- |
| 334 | |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 335 | InstrTreeNode *parent = treeNode; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 336 | |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 337 | if (numChildren > 2) |
| 338 | { |
| 339 | unsigned instrOpcode = treeNode->getInstruction()->getOpcode(); |
| 340 | assert(instrOpcode == Instruction::PHINode || |
| 341 | instrOpcode == Instruction::Call || |
| 342 | instrOpcode == Instruction::Load || |
| 343 | instrOpcode == Instruction::Store || |
| 344 | instrOpcode == Instruction::GetElementPtr); |
| 345 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 346 | |
| 347 | // Insert the first child as a direct child |
| 348 | if (numChildren >= 1) |
Chris Lattner | 4ddb4c8 | 2001-09-12 01:28:49 +0000 | [diff] [blame] | 349 | setLeftChild(parent, childArray[0]); |
| 350 | |
| 351 | int n; |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 352 | |
| 353 | // Create a list node for children 2 .. N-1, if any |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 354 | for (n = numChildren-1; n >= 2; n--) |
| 355 | { |
| 356 | // We have more than two children |
| 357 | InstrTreeNode *listNode = new VRegListNode(); |
| 358 | setRightChild(parent, listNode); |
| 359 | setLeftChild(listNode, childArray[numChildren - n]); |
| 360 | parent = listNode; |
| 361 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 362 | |
| 363 | // Now insert the last remaining child (if any). |
Vikram S. Adve | 4c31fb5 | 2001-09-18 12:54:27 +0000 | [diff] [blame] | 364 | if (numChildren >= 2) |
| 365 | { |
| 366 | assert(n == 1); |
| 367 | setRightChild(parent, childArray[numChildren - 1]); |
| 368 | } |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 369 | |
Vikram S. Adve | 70bc4b5 | 2001-07-21 12:41:50 +0000 | [diff] [blame] | 370 | return treeNode; |
| 371 | } |