Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1 | //===- FastISelEmitter.cpp - Generate an instruction selector -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 10 | // This tablegen backend emits code for use by the "fast" instruction |
| 11 | // selection algorithm. See the comments at the top of |
| 12 | // lib/CodeGen/SelectionDAG/FastISel.cpp for background. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 13 | // |
Dan Gohman | 5ec9efd | 2008-09-30 20:48:29 +0000 | [diff] [blame] | 14 | // This file scans through the target's tablegen instruction-info files |
| 15 | // and extracts instructions with obvious-looking patterns, and it emits |
| 16 | // code to look up these instructions by type and operator. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 17 | // |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "FastISelEmitter.h" |
Jim Grosbach | 0b6a44a | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 21 | #include "Error.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 22 | #include "Record.h" |
Jim Grosbach | 76612b5 | 2010-12-07 19:35:36 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/VectorExtras.h" |
Chad Rosier | 36a300a | 2011-06-07 20:41:31 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 31 | /// InstructionMemo - This class holds additional information about an |
| 32 | /// instruction needed to emit code for it. |
| 33 | /// |
| 34 | struct InstructionMemo { |
| 35 | std::string Name; |
| 36 | const CodeGenRegisterClass *RC; |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 37 | std::string SubRegNo; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 38 | std::vector<std::string>* PhysRegs; |
| 39 | }; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 40 | |
| 41 | /// ImmPredicateSet - This uniques predicates (represented as a string) and |
| 42 | /// gives them unique (small) integer ID's that start at 0. |
| 43 | class ImmPredicateSet { |
| 44 | DenseMap<TreePattern *, unsigned> ImmIDs; |
| 45 | std::vector<TreePredicateFn> PredsByName; |
| 46 | public: |
| 47 | |
| 48 | unsigned getIDFor(TreePredicateFn Pred) { |
| 49 | unsigned &Entry = ImmIDs[Pred.getOrigPatFragRecord()]; |
| 50 | if (Entry == 0) { |
| 51 | PredsByName.push_back(Pred); |
| 52 | Entry = PredsByName.size(); |
| 53 | } |
| 54 | return Entry-1; |
| 55 | } |
| 56 | |
| 57 | const TreePredicateFn &getPredicate(unsigned i) { |
| 58 | assert(i < PredsByName.size()); |
| 59 | return PredsByName[i]; |
| 60 | } |
| 61 | |
| 62 | typedef std::vector<TreePredicateFn>::const_iterator iterator; |
| 63 | iterator begin() const { return PredsByName.begin(); } |
| 64 | iterator end() const { return PredsByName.end(); } |
| 65 | |
| 66 | }; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 67 | |
Dan Gohman | 04b7dfb | 2008-08-19 18:06:12 +0000 | [diff] [blame] | 68 | /// OperandsSignature - This class holds a description of a list of operand |
| 69 | /// types. It has utility methods for emitting text based on the operands. |
| 70 | /// |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 71 | struct OperandsSignature { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 72 | class OpKind { |
| 73 | enum { OK_Reg, OK_FP, OK_Imm, OK_Invalid = -1 }; |
| 74 | char Repr; |
| 75 | public: |
| 76 | |
| 77 | OpKind() : Repr(OK_Invalid) {} |
| 78 | |
| 79 | bool operator<(OpKind RHS) const { return Repr < RHS.Repr; } |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 80 | bool operator==(OpKind RHS) const { return Repr == RHS.Repr; } |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 81 | |
| 82 | static OpKind getReg() { OpKind K; K.Repr = OK_Reg; return K; } |
| 83 | static OpKind getFP() { OpKind K; K.Repr = OK_FP; return K; } |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 84 | static OpKind getImm(unsigned V) { |
| 85 | assert((unsigned)OK_Imm+V < 128 && |
| 86 | "Too many integer predicates for the 'Repr' char"); |
| 87 | OpKind K; K.Repr = OK_Imm+V; return K; |
| 88 | } |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 89 | |
| 90 | bool isReg() const { return Repr == OK_Reg; } |
| 91 | bool isFP() const { return Repr == OK_FP; } |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 92 | bool isImm() const { return Repr >= OK_Imm; } |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 94 | unsigned getImmCode() const { assert(isImm()); return Repr-OK_Imm; } |
| 95 | |
| 96 | void printManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates, |
| 97 | bool StripImmCodes) const { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 98 | if (isReg()) |
| 99 | OS << 'r'; |
| 100 | else if (isFP()) |
| 101 | OS << 'f'; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 102 | else { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 103 | OS << 'i'; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 104 | if (!StripImmCodes) |
| 105 | if (unsigned Code = getImmCode()) |
| 106 | OS << "_" << ImmPredicates.getPredicate(Code-1).getFnName(); |
| 107 | } |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 112 | SmallVector<OpKind, 3> Operands; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 113 | |
| 114 | bool operator<(const OperandsSignature &O) const { |
| 115 | return Operands < O.Operands; |
| 116 | } |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 117 | bool operator==(const OperandsSignature &O) const { |
| 118 | return Operands == O.Operands; |
| 119 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 120 | |
| 121 | bool empty() const { return Operands.empty(); } |
| 122 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 123 | bool hasAnyImmediateCodes() const { |
| 124 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 125 | if (Operands[i].isImm() && Operands[i].getImmCode() != 0) |
| 126 | return true; |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | /// getWithoutImmCodes - Return a copy of this with any immediate codes forced |
| 131 | /// to zero. |
| 132 | OperandsSignature getWithoutImmCodes() const { |
| 133 | OperandsSignature Result; |
| 134 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
| 135 | if (!Operands[i].isImm()) |
| 136 | Result.Operands.push_back(Operands[i]); |
| 137 | else |
| 138 | Result.Operands.push_back(OpKind::getImm(0)); |
| 139 | return Result; |
| 140 | } |
| 141 | |
| 142 | void emitImmediatePredicate(raw_ostream &OS, ImmPredicateSet &ImmPredicates) { |
| 143 | bool EmittedAnything = false; |
| 144 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 145 | if (!Operands[i].isImm()) continue; |
| 146 | |
| 147 | unsigned Code = Operands[i].getImmCode(); |
| 148 | if (Code == 0) continue; |
| 149 | |
| 150 | if (EmittedAnything) |
| 151 | OS << " &&\n "; |
| 152 | |
| 153 | TreePredicateFn PredFn = ImmPredicates.getPredicate(Code-1); |
| 154 | |
| 155 | // Emit the type check. |
| 156 | OS << "VT == " |
| 157 | << getEnumName(PredFn.getOrigPatFragRecord()->getTree(0)->getType(0)) |
| 158 | << " && "; |
| 159 | |
| 160 | |
| 161 | OS << PredFn.getFnName() << "(imm" << i <<')'; |
| 162 | EmittedAnything = true; |
| 163 | } |
| 164 | } |
| 165 | |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 166 | /// initialize - Examine the given pattern and initialize the contents |
| 167 | /// of the Operands array accordingly. Return true if all the operands |
| 168 | /// are supported, false otherwise. |
| 169 | /// |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 170 | bool initialize(TreePatternNode *InstPatNode, const CodeGenTarget &Target, |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 171 | MVT::SimpleValueType VT, |
| 172 | ImmPredicateSet &ImmediatePredicates) { |
| 173 | if (InstPatNode->isLeaf()) |
| 174 | return false; |
| 175 | |
| 176 | if (InstPatNode->getOperator()->getName() == "imm") { |
| 177 | Operands.push_back(OpKind::getImm(0)); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | if (InstPatNode->getOperator()->getName() == "fpimm") { |
| 182 | Operands.push_back(OpKind::getFP()); |
| 183 | return true; |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 184 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 185 | |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 186 | const CodeGenRegisterClass *DstRC = 0; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 187 | |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 188 | for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { |
| 189 | TreePatternNode *Op = InstPatNode->getChild(i); |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 191 | // Handle imm operands specially. |
| 192 | if (!Op->isLeaf() && Op->getOperator()->getName() == "imm") { |
| 193 | unsigned PredNo = 0; |
| 194 | if (!Op->getPredicateFns().empty()) { |
Chris Lattner | 202a7a1 | 2011-04-18 06:36:55 +0000 | [diff] [blame] | 195 | TreePredicateFn PredFn = Op->getPredicateFns()[0]; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 196 | // If there is more than one predicate weighing in on this operand |
| 197 | // then we don't handle it. This doesn't typically happen for |
| 198 | // immediates anyway. |
| 199 | if (Op->getPredicateFns().size() > 1 || |
Chris Lattner | 202a7a1 | 2011-04-18 06:36:55 +0000 | [diff] [blame] | 200 | !PredFn.isImmediatePattern()) |
| 201 | return false; |
| 202 | // Ignore any instruction with 'FastIselShouldIgnore', these are |
| 203 | // not needed and just bloat the fast instruction selector. For |
| 204 | // example, X86 doesn't need to generate code to match ADD16ri8 since |
| 205 | // ADD16ri will do just fine. |
| 206 | Record *Rec = PredFn.getOrigPatFragRecord()->getRecord(); |
| 207 | if (Rec->getValueAsBit("FastIselShouldIgnore")) |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 208 | return false; |
| 209 | |
Chris Lattner | 202a7a1 | 2011-04-18 06:36:55 +0000 | [diff] [blame] | 210 | PredNo = ImmediatePredicates.getIDFor(PredFn)+1; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Handle unmatched immediate sizes here. |
| 214 | //if (Op->getType(0) != VT) |
| 215 | // return false; |
| 216 | |
| 217 | Operands.push_back(OpKind::getImm(PredNo)); |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 222 | // For now, filter out any operand with a predicate. |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 223 | // For now, filter out any operand with multiple values. |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 224 | if (!Op->getPredicateFns().empty() || Op->getNumTypes() != 1) |
Chris Lattner | d734919 | 2010-03-19 21:37:09 +0000 | [diff] [blame] | 225 | return false; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 226 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 227 | if (!Op->isLeaf()) { |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 228 | if (Op->getOperator()->getName() == "fpimm") { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 229 | Operands.push_back(OpKind::getFP()); |
Dale Johannesen | edc8774 | 2009-05-21 22:25:49 +0000 | [diff] [blame] | 230 | continue; |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 231 | } |
Dan Gohman | 833ddf8 | 2008-08-27 16:18:22 +0000 | [diff] [blame] | 232 | // For now, ignore other non-leaf nodes. |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 233 | return false; |
| 234 | } |
Chris Lattner | 602fc06 | 2011-04-17 20:23:29 +0000 | [diff] [blame] | 235 | |
| 236 | assert(Op->hasTypeSet(0) && "Type infererence not done?"); |
| 237 | |
| 238 | // For now, all the operands must have the same type (if they aren't |
| 239 | // immediates). Note that this causes us to reject variable sized shifts |
| 240 | // on X86. |
| 241 | if (Op->getType(0) != VT) |
| 242 | return false; |
| 243 | |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 244 | DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue()); |
| 245 | if (!OpDI) |
| 246 | return false; |
| 247 | Record *OpLeafRec = OpDI->getDef(); |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 248 | |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 249 | // For now, the only other thing we accept is register operands. |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 250 | const CodeGenRegisterClass *RC = 0; |
| 251 | if (OpLeafRec->isSubClassOf("RegisterClass")) |
| 252 | RC = &Target.getRegisterClass(OpLeafRec); |
| 253 | else if (OpLeafRec->isSubClassOf("Register")) |
Jakob Stoklund Olesen | 7b9cafd | 2011-06-15 00:20:40 +0000 | [diff] [blame] | 254 | RC = Target.getRegBank().getRegClassForRegister(OpLeafRec); |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 255 | else |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 256 | return false; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 257 | |
Eric Christopher | 2cfcad9 | 2010-08-24 23:21:59 +0000 | [diff] [blame] | 258 | // For now, this needs to be a register class of some sort. |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 259 | if (!RC) |
| 260 | return false; |
Eric Christopher | 2cfcad9 | 2010-08-24 23:21:59 +0000 | [diff] [blame] | 261 | |
Eric Christopher | 5345260 | 2010-08-25 04:58:56 +0000 | [diff] [blame] | 262 | // For now, all the operands must have the same register class or be |
| 263 | // a strict subclass of the destination. |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 264 | if (DstRC) { |
Eric Christopher | 5345260 | 2010-08-25 04:58:56 +0000 | [diff] [blame] | 265 | if (DstRC != RC && !DstRC->hasSubClass(RC)) |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 266 | return false; |
| 267 | } else |
| 268 | DstRC = RC; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 269 | Operands.push_back(OpKind::getReg()); |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 274 | void PrintParameters(raw_ostream &OS) const { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 275 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 276 | if (Operands[i].isReg()) { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 277 | OS << "unsigned Op" << i << ", bool Op" << i << "IsKill"; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 278 | } else if (Operands[i].isImm()) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 279 | OS << "uint64_t imm" << i; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 280 | } else if (Operands[i].isFP()) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 281 | OS << "ConstantFP *f" << i; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 282 | } else { |
Chad Rosier | 36a300a | 2011-06-07 20:41:31 +0000 | [diff] [blame] | 283 | llvm_unreachable("Unknown operand kind!"); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 284 | } |
| 285 | if (i + 1 != e) |
| 286 | OS << ", "; |
| 287 | } |
| 288 | } |
| 289 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 290 | void PrintArguments(raw_ostream &OS, |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 291 | const std::vector<std::string> &PR) const { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 292 | assert(PR.size() == Operands.size()); |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 293 | bool PrintedArg = false; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 294 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 295 | if (PR[i] != "") |
| 296 | // Implicit physical register operand. |
| 297 | continue; |
| 298 | |
| 299 | if (PrintedArg) |
| 300 | OS << ", "; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 301 | if (Operands[i].isReg()) { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 302 | OS << "Op" << i << ", Op" << i << "IsKill"; |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 303 | PrintedArg = true; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 304 | } else if (Operands[i].isImm()) { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 305 | OS << "imm" << i; |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 306 | PrintedArg = true; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 307 | } else if (Operands[i].isFP()) { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 308 | OS << "f" << i; |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 309 | PrintedArg = true; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 310 | } else { |
Chad Rosier | 36a300a | 2011-06-07 20:41:31 +0000 | [diff] [blame] | 311 | llvm_unreachable("Unknown operand kind!"); |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 312 | } |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 316 | void PrintArguments(raw_ostream &OS) const { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 317 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 318 | if (Operands[i].isReg()) { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 319 | OS << "Op" << i << ", Op" << i << "IsKill"; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 320 | } else if (Operands[i].isImm()) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 321 | OS << "imm" << i; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 322 | } else if (Operands[i].isFP()) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 323 | OS << "f" << i; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 324 | } else { |
Chad Rosier | 36a300a | 2011-06-07 20:41:31 +0000 | [diff] [blame] | 325 | llvm_unreachable("Unknown operand kind!"); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 326 | } |
| 327 | if (i + 1 != e) |
| 328 | OS << ", "; |
| 329 | } |
| 330 | } |
| 331 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 333 | void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR, |
| 334 | ImmPredicateSet &ImmPredicates, |
| 335 | bool StripImmCodes = false) const { |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 336 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 337 | if (PR[i] != "") |
| 338 | // Implicit physical register operand. e.g. Instruction::Mul expect to |
| 339 | // select to a binary op. On x86, mul may take a single operand with |
| 340 | // the other operand being implicit. We must emit something that looks |
| 341 | // like a binary instruction except for the very inner FastEmitInst_* |
| 342 | // call. |
| 343 | continue; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 344 | Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes); |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 348 | void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates, |
| 349 | bool StripImmCodes = false) const { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 350 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 351 | Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 352 | } |
| 353 | }; |
| 354 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 355 | class FastISelMap { |
| 356 | typedef std::map<std::string, InstructionMemo> PredMap; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 357 | typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap; |
| 358 | typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap; |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 359 | typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 360 | typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> |
Eric Christopher | ecfa079 | 2010-07-26 17:53:07 +0000 | [diff] [blame] | 361 | OperandsOpcodeTypeRetPredMap; |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 362 | |
| 363 | OperandsOpcodeTypeRetPredMap SimplePatterns; |
| 364 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 365 | std::map<OperandsSignature, std::vector<OperandsSignature> > |
| 366 | SignaturesWithConstantForms; |
| 367 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 368 | std::string InstNS; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 369 | ImmPredicateSet ImmediatePredicates; |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 370 | public: |
| 371 | explicit FastISelMap(std::string InstNS); |
| 372 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 373 | void collectPatterns(CodeGenDAGPatterns &CGP); |
| 374 | void printImmediatePredicates(raw_ostream &OS); |
| 375 | void printFunctionDefinitions(raw_ostream &OS); |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 376 | }; |
| 377 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) { |
| 381 | return CGP.getSDNodeInfo(Op).getEnumName(); |
| 382 | } |
| 383 | |
| 384 | static std::string getLegalCName(std::string OpName) { |
| 385 | std::string::size_type pos = OpName.find("::"); |
| 386 | if (pos != std::string::npos) |
| 387 | OpName.replace(pos, 2, "_"); |
| 388 | return OpName; |
| 389 | } |
| 390 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 391 | FastISelMap::FastISelMap(std::string instns) |
| 392 | : InstNS(instns) { |
| 393 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 394 | |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 395 | static std::string PhyRegForNode(TreePatternNode *Op, |
| 396 | const CodeGenTarget &Target) { |
| 397 | std::string PhysReg; |
| 398 | |
| 399 | if (!Op->isLeaf()) |
| 400 | return PhysReg; |
| 401 | |
| 402 | DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue()); |
| 403 | Record *OpLeafRec = OpDI->getDef(); |
| 404 | if (!OpLeafRec->isSubClassOf("Register")) |
| 405 | return PhysReg; |
| 406 | |
| 407 | PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \ |
| 408 | "Namespace")->getValue())->getValue(); |
| 409 | PhysReg += "::"; |
Jakob Stoklund Olesen | abdbc84 | 2011-06-18 04:26:06 +0000 | [diff] [blame] | 410 | PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName(); |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 411 | return PhysReg; |
| 412 | } |
| 413 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 414 | void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) { |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 415 | const CodeGenTarget &Target = CGP.getTargetInfo(); |
| 416 | |
| 417 | // Determine the target's namespace name. |
| 418 | InstNS = Target.getInstNamespace() + "::"; |
| 419 | assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 420 | |
Dan Gohman | 0bfb752 | 2008-08-22 00:28:15 +0000 | [diff] [blame] | 421 | // Scan through all the patterns and record the simple ones. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 422 | for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), |
| 423 | E = CGP.ptm_end(); I != E; ++I) { |
| 424 | const PatternToMatch &Pattern = *I; |
| 425 | |
| 426 | // For now, just look at Instructions, so that we don't have to worry |
| 427 | // about emitting multiple instructions for a pattern. |
| 428 | TreePatternNode *Dst = Pattern.getDstPattern(); |
| 429 | if (Dst->isLeaf()) continue; |
| 430 | Record *Op = Dst->getOperator(); |
| 431 | if (!Op->isSubClassOf("Instruction")) |
| 432 | continue; |
Chris Lattner | f30187a | 2010-03-19 00:07:20 +0000 | [diff] [blame] | 433 | CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op); |
Chris Lattner | a90dbc1 | 2011-04-17 22:24:13 +0000 | [diff] [blame] | 434 | if (II.Operands.empty()) |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 435 | continue; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 436 | |
Evan Cheng | 34fc6ce | 2008-09-07 08:19:51 +0000 | [diff] [blame] | 437 | // For now, ignore multi-instruction patterns. |
| 438 | bool MultiInsts = false; |
| 439 | for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) { |
| 440 | TreePatternNode *ChildOp = Dst->getChild(i); |
| 441 | if (ChildOp->isLeaf()) |
| 442 | continue; |
| 443 | if (ChildOp->getOperator()->isSubClassOf("Instruction")) { |
| 444 | MultiInsts = true; |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | if (MultiInsts) |
| 449 | continue; |
| 450 | |
Dan Gohman | 379cad4 | 2008-08-19 20:36:33 +0000 | [diff] [blame] | 451 | // For now, ignore instructions where the first operand is not an |
| 452 | // output register. |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 453 | const CodeGenRegisterClass *DstRC = 0; |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 454 | std::string SubRegNo; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 455 | if (Op->getName() != "EXTRACT_SUBREG") { |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 456 | Record *Op0Rec = II.Operands[0].Rec; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 457 | if (!Op0Rec->isSubClassOf("RegisterClass")) |
| 458 | continue; |
| 459 | DstRC = &Target.getRegisterClass(Op0Rec); |
| 460 | if (!DstRC) |
| 461 | continue; |
| 462 | } else { |
Eric Christopher | 07fdd89 | 2010-07-21 22:07:19 +0000 | [diff] [blame] | 463 | // If this isn't a leaf, then continue since the register classes are |
| 464 | // a bit too complicated for now. |
| 465 | if (!Dst->getChild(1)->isLeaf()) continue; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 466 | |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 467 | DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue()); |
| 468 | if (SR) |
| 469 | SubRegNo = getQualifiedName(SR->getDef()); |
| 470 | else |
| 471 | SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString(); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 472 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 473 | |
| 474 | // Inspect the pattern. |
| 475 | TreePatternNode *InstPatNode = Pattern.getSrcPattern(); |
| 476 | if (!InstPatNode) continue; |
| 477 | if (InstPatNode->isLeaf()) continue; |
| 478 | |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 479 | // Ignore multiple result nodes for now. |
| 480 | if (InstPatNode->getNumTypes() > 1) continue; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 481 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 482 | Record *InstPatOp = InstPatNode->getOperator(); |
| 483 | std::string OpcodeName = getOpcodeName(InstPatOp, CGP); |
Chris Lattner | d734919 | 2010-03-19 21:37:09 +0000 | [diff] [blame] | 484 | MVT::SimpleValueType RetVT = MVT::isVoid; |
| 485 | if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 486 | MVT::SimpleValueType VT = RetVT; |
Chris Lattner | d734919 | 2010-03-19 21:37:09 +0000 | [diff] [blame] | 487 | if (InstPatNode->getNumChildren()) { |
| 488 | assert(InstPatNode->getChild(0)->getNumTypes() == 1); |
| 489 | VT = InstPatNode->getChild(0)->getType(0); |
| 490 | } |
Dan Gohman | f4137b5 | 2008-08-19 20:30:54 +0000 | [diff] [blame] | 491 | |
| 492 | // For now, filter out any instructions with predicates. |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 493 | if (!InstPatNode->getPredicateFns().empty()) |
Dan Gohman | f4137b5 | 2008-08-19 20:30:54 +0000 | [diff] [blame] | 494 | continue; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 495 | |
Dan Gohman | 379cad4 | 2008-08-19 20:36:33 +0000 | [diff] [blame] | 496 | // Check all the operands. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 497 | OperandsSignature Operands; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 498 | if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates)) |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 499 | continue; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 500 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 501 | std::vector<std::string>* PhysRegInputs = new std::vector<std::string>(); |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 502 | if (InstPatNode->getOperator()->getName() == "imm" || |
| 503 | InstPatNode->getOperator()->getName() == "fpimmm") |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 504 | PhysRegInputs->push_back(""); |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 505 | else { |
| 506 | // Compute the PhysRegs used by the given pattern, and check that |
| 507 | // the mapping from the src to dst patterns is simple. |
| 508 | bool FoundNonSimplePattern = false; |
| 509 | unsigned DstIndex = 0; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 510 | for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 511 | std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target); |
| 512 | if (PhysReg.empty()) { |
| 513 | if (DstIndex >= Dst->getNumChildren() || |
| 514 | Dst->getChild(DstIndex)->getName() != |
| 515 | InstPatNode->getChild(i)->getName()) { |
| 516 | FoundNonSimplePattern = true; |
| 517 | break; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 518 | } |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 519 | ++DstIndex; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 520 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 521 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 522 | PhysRegInputs->push_back(PhysReg); |
| 523 | } |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 524 | |
| 525 | if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren()) |
| 526 | FoundNonSimplePattern = true; |
| 527 | |
| 528 | if (FoundNonSimplePattern) |
| 529 | continue; |
| 530 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 531 | |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 532 | // Get the predicate that guards this pattern. |
| 533 | std::string PredicateCheck = Pattern.getPredicateCheck(); |
| 534 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 535 | // Ok, we found a pattern that we can handle. Remember it. |
Dan Gohman | 520b50c | 2008-08-21 00:35:26 +0000 | [diff] [blame] | 536 | InstructionMemo Memo = { |
| 537 | Pattern.getDstPattern()->getOperator()->getName(), |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 538 | DstRC, |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 539 | SubRegNo, |
| 540 | PhysRegInputs |
Dan Gohman | 520b50c | 2008-08-21 00:35:26 +0000 | [diff] [blame] | 541 | }; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 542 | |
| 543 | if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck)) |
| 544 | throw TGError(Pattern.getSrcRecord()->getLoc(), |
| 545 | "Duplicate record in FastISel table!"); |
Jim Grosbach | 997759a | 2010-12-07 23:05:49 +0000 | [diff] [blame] | 546 | |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 547 | SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 548 | |
| 549 | // If any of the operands were immediates with predicates on them, strip |
| 550 | // them down to a signature that doesn't have predicates so that we can |
| 551 | // associate them with the stripped predicate version. |
| 552 | if (Operands.hasAnyImmediateCodes()) { |
| 553 | SignaturesWithConstantForms[Operands.getWithoutImmCodes()] |
| 554 | .push_back(Operands); |
| 555 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 556 | } |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 557 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 559 | void FastISelMap::printImmediatePredicates(raw_ostream &OS) { |
| 560 | if (ImmediatePredicates.begin() == ImmediatePredicates.end()) |
| 561 | return; |
| 562 | |
| 563 | OS << "\n// FastEmit Immediate Predicate functions.\n"; |
| 564 | for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(), |
| 565 | E = ImmediatePredicates.end(); I != E; ++I) { |
| 566 | OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n"; |
| 567 | OS << I->getImmediatePredicateCode() << "\n}\n"; |
| 568 | } |
| 569 | |
| 570 | OS << "\n\n"; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | void FastISelMap::printFunctionDefinitions(raw_ostream &OS) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 575 | // Now emit code for all the patterns that we collected. |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 576 | for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(), |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 577 | OE = SimplePatterns.end(); OI != OE; ++OI) { |
| 578 | const OperandsSignature &Operands = OI->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 579 | const OpcodeTypeRetPredMap &OTM = OI->second; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 580 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 581 | for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 582 | I != E; ++I) { |
| 583 | const std::string &Opcode = I->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 584 | const TypeRetPredMap &TM = I->second; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 585 | |
| 586 | OS << "// FastEmit functions for " << Opcode << ".\n"; |
| 587 | OS << "\n"; |
| 588 | |
| 589 | // Emit one function for each opcode,type pair. |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 590 | for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 591 | TI != TE; ++TI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 592 | MVT::SimpleValueType VT = TI->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 593 | const RetPredMap &RM = TI->second; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 594 | if (RM.size() != 1) { |
| 595 | for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); |
| 596 | RI != RE; ++RI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 597 | MVT::SimpleValueType RetVT = RI->first; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 598 | const PredMap &PM = RI->second; |
| 599 | bool HasPred = false; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 600 | |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 601 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 602 | << getLegalCName(Opcode) |
| 603 | << "_" << getLegalCName(getName(VT)) |
| 604 | << "_" << getLegalCName(getName(RetVT)) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 605 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 606 | OS << "("; |
| 607 | Operands.PrintParameters(OS); |
| 608 | OS << ") {\n"; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 609 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 610 | // Emit code for each possible instruction. There may be |
| 611 | // multiple if there are subtarget concerns. |
| 612 | for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); |
| 613 | PI != PE; ++PI) { |
| 614 | std::string PredicateCheck = PI->first; |
| 615 | const InstructionMemo &Memo = PI->second; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 616 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 617 | if (PredicateCheck.empty()) { |
| 618 | assert(!HasPred && |
| 619 | "Multiple instructions match, at least one has " |
| 620 | "a predicate and at least one doesn't!"); |
| 621 | } else { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 622 | OS << " if (" + PredicateCheck + ") {\n"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 623 | OS << " "; |
| 624 | HasPred = true; |
| 625 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 626 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 627 | for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { |
| 628 | if ((*Memo.PhysRegs)[i] != "") |
Jakob Stoklund Olesen | 4f8e771 | 2010-07-11 03:53:50 +0000 | [diff] [blame] | 629 | OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, " |
| 630 | << "TII.get(TargetOpcode::COPY), " |
| 631 | << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n"; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 632 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 633 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 634 | OS << " return FastEmitInst_"; |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 635 | if (Memo.SubRegNo.empty()) { |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 636 | Operands.PrintManglingSuffix(OS, *Memo.PhysRegs, |
| 637 | ImmediatePredicates, true); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 638 | OS << "(" << InstNS << Memo.Name << ", "; |
| 639 | OS << InstNS << Memo.RC->getName() << "RegisterClass"; |
| 640 | if (!Operands.empty()) |
| 641 | OS << ", "; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 642 | Operands.PrintArguments(OS, *Memo.PhysRegs); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 643 | OS << ");\n"; |
| 644 | } else { |
Evan Cheng | 536ab13 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 645 | OS << "extractsubreg(" << getName(RetVT); |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 646 | OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n"; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 647 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 648 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 649 | if (HasPred) |
Evan Cheng | d07b46e | 2008-09-07 08:23:06 +0000 | [diff] [blame] | 650 | OS << " }\n"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 651 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 652 | } |
| 653 | // Return 0 if none of the predicates were satisfied. |
| 654 | if (HasPred) |
| 655 | OS << " return 0;\n"; |
| 656 | OS << "}\n"; |
| 657 | OS << "\n"; |
| 658 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 659 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 660 | // Emit one function for the type that demultiplexes on return type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 661 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 662 | << getLegalCName(Opcode) << "_" |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 663 | << getLegalCName(getName(VT)) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 664 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 665 | OS << "(MVT RetVT"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 666 | if (!Operands.empty()) |
| 667 | OS << ", "; |
| 668 | Operands.PrintParameters(OS); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 669 | OS << ") {\nswitch (RetVT.SimpleTy) {\n"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 670 | for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); |
| 671 | RI != RE; ++RI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 672 | MVT::SimpleValueType RetVT = RI->first; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 673 | OS << " case " << getName(RetVT) << ": return FastEmit_" |
| 674 | << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT)) |
| 675 | << "_" << getLegalCName(getName(RetVT)) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 676 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 677 | OS << "("; |
| 678 | Operands.PrintArguments(OS); |
| 679 | OS << ");\n"; |
| 680 | } |
| 681 | OS << " default: return 0;\n}\n}\n\n"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 682 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 683 | } else { |
| 684 | // Non-variadic return type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 685 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 686 | << getLegalCName(Opcode) << "_" |
| 687 | << getLegalCName(getName(VT)) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 688 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 689 | OS << "(MVT RetVT"; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 690 | if (!Operands.empty()) |
| 691 | OS << ", "; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 692 | Operands.PrintParameters(OS); |
| 693 | OS << ") {\n"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 694 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 695 | OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first) |
Owen Anderson | 70647e8 | 2008-08-26 18:50:00 +0000 | [diff] [blame] | 696 | << ")\n return 0;\n"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 697 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 698 | const PredMap &PM = RM.begin()->second; |
| 699 | bool HasPred = false; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 700 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 701 | // Emit code for each possible instruction. There may be |
| 702 | // multiple if there are subtarget concerns. |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 703 | for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; |
| 704 | ++PI) { |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 705 | std::string PredicateCheck = PI->first; |
| 706 | const InstructionMemo &Memo = PI->second; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 707 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 708 | if (PredicateCheck.empty()) { |
| 709 | assert(!HasPred && |
| 710 | "Multiple instructions match, at least one has " |
| 711 | "a predicate and at least one doesn't!"); |
| 712 | } else { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 713 | OS << " if (" + PredicateCheck + ") {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 714 | OS << " "; |
| 715 | HasPred = true; |
| 716 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 717 | |
Jakob Stoklund Olesen | 4f8e771 | 2010-07-11 03:53:50 +0000 | [diff] [blame] | 718 | for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { |
| 719 | if ((*Memo.PhysRegs)[i] != "") |
| 720 | OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, " |
| 721 | << "TII.get(TargetOpcode::COPY), " |
| 722 | << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n"; |
| 723 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 724 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 725 | OS << " return FastEmitInst_"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 726 | |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 727 | if (Memo.SubRegNo.empty()) { |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 728 | Operands.PrintManglingSuffix(OS, *Memo.PhysRegs, |
| 729 | ImmediatePredicates, true); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 730 | OS << "(" << InstNS << Memo.Name << ", "; |
| 731 | OS << InstNS << Memo.RC->getName() << "RegisterClass"; |
| 732 | if (!Operands.empty()) |
| 733 | OS << ", "; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 734 | Operands.PrintArguments(OS, *Memo.PhysRegs); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 735 | OS << ");\n"; |
| 736 | } else { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 737 | OS << "extractsubreg(RetVT, Op0, Op0IsKill, "; |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 738 | OS << Memo.SubRegNo; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 739 | OS << ");\n"; |
| 740 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 741 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 742 | if (HasPred) |
| 743 | OS << " }\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 744 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 745 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 746 | // Return 0 if none of the predicates were satisfied. |
| 747 | if (HasPred) |
| 748 | OS << " return 0;\n"; |
| 749 | OS << "}\n"; |
| 750 | OS << "\n"; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 751 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // Emit one function for the opcode that demultiplexes based on the type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 755 | OS << "unsigned FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 756 | << getLegalCName(Opcode) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 757 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 758 | OS << "(MVT VT, MVT RetVT"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 759 | if (!Operands.empty()) |
| 760 | OS << ", "; |
| 761 | Operands.PrintParameters(OS); |
| 762 | OS << ") {\n"; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 763 | OS << " switch (VT.SimpleTy) {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 764 | for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 765 | TI != TE; ++TI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 766 | MVT::SimpleValueType VT = TI->first; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 767 | std::string TypeName = getName(VT); |
| 768 | OS << " case " << TypeName << ": return FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 769 | << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 770 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 771 | OS << "(RetVT"; |
| 772 | if (!Operands.empty()) |
| 773 | OS << ", "; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 774 | Operands.PrintArguments(OS); |
| 775 | OS << ");\n"; |
| 776 | } |
| 777 | OS << " default: return 0;\n"; |
| 778 | OS << " }\n"; |
| 779 | OS << "}\n"; |
| 780 | OS << "\n"; |
| 781 | } |
| 782 | |
Dan Gohman | 0bfb752 | 2008-08-22 00:28:15 +0000 | [diff] [blame] | 783 | OS << "// Top-level FastEmit function.\n"; |
| 784 | OS << "\n"; |
| 785 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 786 | // Emit one function for the operand signature that demultiplexes based |
| 787 | // on opcode and type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 788 | OS << "unsigned FastEmit_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 789 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 790 | OS << "(MVT VT, MVT RetVT, unsigned Opcode"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 791 | if (!Operands.empty()) |
| 792 | OS << ", "; |
| 793 | Operands.PrintParameters(OS); |
| 794 | OS << ") {\n"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 795 | |
| 796 | // If there are any forms of this signature available that operand on |
| 797 | // constrained forms of the immediate (e.g. 32-bit sext immediate in a |
| 798 | // 64-bit operand), check them first. |
| 799 | |
| 800 | std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI |
| 801 | = SignaturesWithConstantForms.find(Operands); |
| 802 | if (MI != SignaturesWithConstantForms.end()) { |
| 803 | // Unique any duplicates out of the list. |
| 804 | std::sort(MI->second.begin(), MI->second.end()); |
| 805 | MI->second.erase(std::unique(MI->second.begin(), MI->second.end()), |
| 806 | MI->second.end()); |
| 807 | |
| 808 | // Check each in order it was seen. It would be nice to have a good |
| 809 | // relative ordering between them, but we're not going for optimality |
| 810 | // here. |
| 811 | for (unsigned i = 0, e = MI->second.size(); i != e; ++i) { |
| 812 | OS << " if ("; |
| 813 | MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates); |
| 814 | OS << ")\n if (unsigned Reg = FastEmit_"; |
| 815 | MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates); |
| 816 | OS << "(VT, RetVT, Opcode"; |
| 817 | if (!MI->second[i].empty()) |
| 818 | OS << ", "; |
| 819 | MI->second[i].PrintArguments(OS); |
| 820 | OS << "))\n return Reg;\n\n"; |
| 821 | } |
| 822 | |
| 823 | // Done with this, remove it. |
| 824 | SignaturesWithConstantForms.erase(MI); |
| 825 | } |
| 826 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 827 | OS << " switch (Opcode) {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 828 | for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 829 | I != E; ++I) { |
| 830 | const std::string &Opcode = I->first; |
| 831 | |
| 832 | OS << " case " << Opcode << ": return FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 833 | << getLegalCName(Opcode) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 834 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 835 | OS << "(VT, RetVT"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 836 | if (!Operands.empty()) |
| 837 | OS << ", "; |
| 838 | Operands.PrintArguments(OS); |
| 839 | OS << ");\n"; |
| 840 | } |
| 841 | OS << " default: return 0;\n"; |
| 842 | OS << " }\n"; |
| 843 | OS << "}\n"; |
| 844 | OS << "\n"; |
| 845 | } |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 846 | |
| 847 | // TODO: SignaturesWithConstantForms should be empty here. |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 850 | void FastISelEmitter::run(raw_ostream &OS) { |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 851 | const CodeGenTarget &Target = CGP.getTargetInfo(); |
| 852 | |
| 853 | // Determine the target's namespace name. |
| 854 | std::string InstNS = Target.getInstNamespace() + "::"; |
| 855 | assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); |
| 856 | |
| 857 | EmitSourceFileHeader("\"Fast\" Instruction Selector for the " + |
| 858 | Target.getName() + " target", OS); |
| 859 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 860 | FastISelMap F(InstNS); |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 861 | F.collectPatterns(CGP); |
| 862 | F.printImmediatePredicates(OS); |
| 863 | F.printFunctionDefinitions(OS); |
Dan Gohman | c7f72de | 2008-08-21 00:19:05 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | FastISelEmitter::FastISelEmitter(RecordKeeper &R) |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 867 | : Records(R), CGP(R) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 868 | } |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 869 | |