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