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