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