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