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