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