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