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; |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame^] | 251 | if (OpLeafRec->isSubClassOf("RegisterOperand")) |
| 252 | OpLeafRec = OpLeafRec->getValueAsDef("RegClass"); |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 253 | if (OpLeafRec->isSubClassOf("RegisterClass")) |
| 254 | RC = &Target.getRegisterClass(OpLeafRec); |
| 255 | else if (OpLeafRec->isSubClassOf("Register")) |
Jakob Stoklund Olesen | 7b9cafd | 2011-06-15 00:20:40 +0000 | [diff] [blame] | 256 | RC = Target.getRegBank().getRegClassForRegister(OpLeafRec); |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 257 | else |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 258 | return false; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 259 | |
Eric Christopher | 2cfcad9 | 2010-08-24 23:21:59 +0000 | [diff] [blame] | 260 | // For now, this needs to be a register class of some sort. |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 261 | if (!RC) |
| 262 | return false; |
Eric Christopher | 2cfcad9 | 2010-08-24 23:21:59 +0000 | [diff] [blame] | 263 | |
Eric Christopher | 5345260 | 2010-08-25 04:58:56 +0000 | [diff] [blame] | 264 | // For now, all the operands must have the same register class or be |
| 265 | // a strict subclass of the destination. |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 266 | if (DstRC) { |
Eric Christopher | 5345260 | 2010-08-25 04:58:56 +0000 | [diff] [blame] | 267 | if (DstRC != RC && !DstRC->hasSubClass(RC)) |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 268 | return false; |
| 269 | } else |
| 270 | DstRC = RC; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 271 | Operands.push_back(OpKind::getReg()); |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 276 | void PrintParameters(raw_ostream &OS) const { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 277 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 278 | if (Operands[i].isReg()) { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 279 | OS << "unsigned Op" << i << ", bool Op" << i << "IsKill"; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 280 | } else if (Operands[i].isImm()) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 281 | OS << "uint64_t imm" << i; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 282 | } else if (Operands[i].isFP()) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 283 | OS << "ConstantFP *f" << i; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 284 | } else { |
Chad Rosier | 36a300a | 2011-06-07 20:41:31 +0000 | [diff] [blame] | 285 | llvm_unreachable("Unknown operand kind!"); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 286 | } |
| 287 | if (i + 1 != e) |
| 288 | OS << ", "; |
| 289 | } |
| 290 | } |
| 291 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 292 | void PrintArguments(raw_ostream &OS, |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 293 | const std::vector<std::string> &PR) const { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 294 | assert(PR.size() == Operands.size()); |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 295 | bool PrintedArg = false; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 296 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 297 | if (PR[i] != "") |
| 298 | // Implicit physical register operand. |
| 299 | continue; |
| 300 | |
| 301 | if (PrintedArg) |
| 302 | OS << ", "; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 303 | if (Operands[i].isReg()) { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 304 | OS << "Op" << i << ", Op" << i << "IsKill"; |
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].isImm()) { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 307 | OS << "imm" << i; |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 308 | PrintedArg = true; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 309 | } else if (Operands[i].isFP()) { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 310 | OS << "f" << i; |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 311 | PrintedArg = true; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 312 | } else { |
Chad Rosier | 36a300a | 2011-06-07 20:41:31 +0000 | [diff] [blame] | 313 | llvm_unreachable("Unknown operand kind!"); |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 314 | } |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 318 | void PrintArguments(raw_ostream &OS) const { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 319 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 320 | if (Operands[i].isReg()) { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 321 | OS << "Op" << i << ", Op" << i << "IsKill"; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 322 | } else if (Operands[i].isImm()) { |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 323 | OS << "imm" << i; |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 324 | } else if (Operands[i].isFP()) { |
Dan Gohman | 10df0fa | 2008-08-27 01:09:54 +0000 | [diff] [blame] | 325 | OS << "f" << i; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 326 | } else { |
Chad Rosier | 36a300a | 2011-06-07 20:41:31 +0000 | [diff] [blame] | 327 | llvm_unreachable("Unknown operand kind!"); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 328 | } |
| 329 | if (i + 1 != e) |
| 330 | OS << ", "; |
| 331 | } |
| 332 | } |
| 333 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 335 | void PrintManglingSuffix(raw_ostream &OS, const std::vector<std::string> &PR, |
| 336 | ImmPredicateSet &ImmPredicates, |
| 337 | bool StripImmCodes = false) const { |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 338 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) { |
| 339 | if (PR[i] != "") |
| 340 | // Implicit physical register operand. e.g. Instruction::Mul expect to |
| 341 | // select to a binary op. On x86, mul may take a single operand with |
| 342 | // the other operand being implicit. We must emit something that looks |
| 343 | // like a binary instruction except for the very inner FastEmitInst_* |
| 344 | // call. |
| 345 | continue; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 346 | Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes); |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 350 | void PrintManglingSuffix(raw_ostream &OS, ImmPredicateSet &ImmPredicates, |
| 351 | bool StripImmCodes = false) const { |
Chris Lattner | 9bfd5f3 | 2011-04-17 23:29:05 +0000 | [diff] [blame] | 352 | for (unsigned i = 0, e = Operands.size(); i != e; ++i) |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 353 | Operands[i].printManglingSuffix(OS, ImmPredicates, StripImmCodes); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 354 | } |
| 355 | }; |
| 356 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 357 | class FastISelMap { |
| 358 | typedef std::map<std::string, InstructionMemo> PredMap; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 359 | typedef std::map<MVT::SimpleValueType, PredMap> RetPredMap; |
| 360 | typedef std::map<MVT::SimpleValueType, RetPredMap> TypeRetPredMap; |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 361 | typedef std::map<std::string, TypeRetPredMap> OpcodeTypeRetPredMap; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 362 | typedef std::map<OperandsSignature, OpcodeTypeRetPredMap> |
Eric Christopher | ecfa079 | 2010-07-26 17:53:07 +0000 | [diff] [blame] | 363 | OperandsOpcodeTypeRetPredMap; |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 364 | |
| 365 | OperandsOpcodeTypeRetPredMap SimplePatterns; |
| 366 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 367 | std::map<OperandsSignature, std::vector<OperandsSignature> > |
| 368 | SignaturesWithConstantForms; |
| 369 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 370 | std::string InstNS; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 371 | ImmPredicateSet ImmediatePredicates; |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 372 | public: |
| 373 | explicit FastISelMap(std::string InstNS); |
| 374 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 375 | void collectPatterns(CodeGenDAGPatterns &CGP); |
| 376 | void printImmediatePredicates(raw_ostream &OS); |
| 377 | void printFunctionDefinitions(raw_ostream &OS); |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 378 | }; |
| 379 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) { |
| 383 | return CGP.getSDNodeInfo(Op).getEnumName(); |
| 384 | } |
| 385 | |
| 386 | static std::string getLegalCName(std::string OpName) { |
| 387 | std::string::size_type pos = OpName.find("::"); |
| 388 | if (pos != std::string::npos) |
| 389 | OpName.replace(pos, 2, "_"); |
| 390 | return OpName; |
| 391 | } |
| 392 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 393 | FastISelMap::FastISelMap(std::string instns) |
| 394 | : InstNS(instns) { |
| 395 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 396 | |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 397 | static std::string PhyRegForNode(TreePatternNode *Op, |
| 398 | const CodeGenTarget &Target) { |
| 399 | std::string PhysReg; |
| 400 | |
| 401 | if (!Op->isLeaf()) |
| 402 | return PhysReg; |
| 403 | |
| 404 | DefInit *OpDI = dynamic_cast<DefInit*>(Op->getLeafValue()); |
| 405 | Record *OpLeafRec = OpDI->getDef(); |
| 406 | if (!OpLeafRec->isSubClassOf("Register")) |
| 407 | return PhysReg; |
| 408 | |
| 409 | PhysReg += static_cast<StringInit*>(OpLeafRec->getValue( \ |
| 410 | "Namespace")->getValue())->getValue(); |
| 411 | PhysReg += "::"; |
Jakob Stoklund Olesen | abdbc84 | 2011-06-18 04:26:06 +0000 | [diff] [blame] | 412 | PhysReg += Target.getRegBank().getReg(OpLeafRec)->getName(); |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 413 | return PhysReg; |
| 414 | } |
| 415 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 416 | void FastISelMap::collectPatterns(CodeGenDAGPatterns &CGP) { |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 417 | const CodeGenTarget &Target = CGP.getTargetInfo(); |
| 418 | |
| 419 | // Determine the target's namespace name. |
| 420 | InstNS = Target.getInstNamespace() + "::"; |
| 421 | assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 422 | |
Dan Gohman | 0bfb752 | 2008-08-22 00:28:15 +0000 | [diff] [blame] | 423 | // Scan through all the patterns and record the simple ones. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 424 | for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), |
| 425 | E = CGP.ptm_end(); I != E; ++I) { |
| 426 | const PatternToMatch &Pattern = *I; |
| 427 | |
| 428 | // For now, just look at Instructions, so that we don't have to worry |
| 429 | // about emitting multiple instructions for a pattern. |
| 430 | TreePatternNode *Dst = Pattern.getDstPattern(); |
| 431 | if (Dst->isLeaf()) continue; |
| 432 | Record *Op = Dst->getOperator(); |
| 433 | if (!Op->isSubClassOf("Instruction")) |
| 434 | continue; |
Chris Lattner | f30187a | 2010-03-19 00:07:20 +0000 | [diff] [blame] | 435 | CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op); |
Chris Lattner | a90dbc1 | 2011-04-17 22:24:13 +0000 | [diff] [blame] | 436 | if (II.Operands.empty()) |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 437 | continue; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 438 | |
Evan Cheng | 34fc6ce | 2008-09-07 08:19:51 +0000 | [diff] [blame] | 439 | // For now, ignore multi-instruction patterns. |
| 440 | bool MultiInsts = false; |
| 441 | for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) { |
| 442 | TreePatternNode *ChildOp = Dst->getChild(i); |
| 443 | if (ChildOp->isLeaf()) |
| 444 | continue; |
| 445 | if (ChildOp->getOperator()->isSubClassOf("Instruction")) { |
| 446 | MultiInsts = true; |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | if (MultiInsts) |
| 451 | continue; |
| 452 | |
Dan Gohman | 379cad4 | 2008-08-19 20:36:33 +0000 | [diff] [blame] | 453 | // For now, ignore instructions where the first operand is not an |
| 454 | // output register. |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 455 | const CodeGenRegisterClass *DstRC = 0; |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 456 | std::string SubRegNo; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 457 | if (Op->getName() != "EXTRACT_SUBREG") { |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 458 | Record *Op0Rec = II.Operands[0].Rec; |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame^] | 459 | if (Op0Rec->isSubClassOf("RegisterOperand")) |
| 460 | Op0Rec = Op0Rec->getValueAsDef("RegClass"); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 461 | if (!Op0Rec->isSubClassOf("RegisterClass")) |
| 462 | continue; |
| 463 | DstRC = &Target.getRegisterClass(Op0Rec); |
| 464 | if (!DstRC) |
| 465 | continue; |
| 466 | } else { |
Eric Christopher | 07fdd89 | 2010-07-21 22:07:19 +0000 | [diff] [blame] | 467 | // If this isn't a leaf, then continue since the register classes are |
| 468 | // a bit too complicated for now. |
| 469 | if (!Dst->getChild(1)->isLeaf()) continue; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 470 | |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 471 | DefInit *SR = dynamic_cast<DefInit*>(Dst->getChild(1)->getLeafValue()); |
| 472 | if (SR) |
| 473 | SubRegNo = getQualifiedName(SR->getDef()); |
| 474 | else |
| 475 | SubRegNo = Dst->getChild(1)->getLeafValue()->getAsString(); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 476 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 477 | |
| 478 | // Inspect the pattern. |
| 479 | TreePatternNode *InstPatNode = Pattern.getSrcPattern(); |
| 480 | if (!InstPatNode) continue; |
| 481 | if (InstPatNode->isLeaf()) continue; |
| 482 | |
Chris Lattner | 084df62 | 2010-03-24 00:41:19 +0000 | [diff] [blame] | 483 | // Ignore multiple result nodes for now. |
| 484 | if (InstPatNode->getNumTypes() > 1) continue; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 485 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 486 | Record *InstPatOp = InstPatNode->getOperator(); |
| 487 | std::string OpcodeName = getOpcodeName(InstPatOp, CGP); |
Chris Lattner | d734919 | 2010-03-19 21:37:09 +0000 | [diff] [blame] | 488 | MVT::SimpleValueType RetVT = MVT::isVoid; |
| 489 | if (InstPatNode->getNumTypes()) RetVT = InstPatNode->getType(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 490 | MVT::SimpleValueType VT = RetVT; |
Chris Lattner | d734919 | 2010-03-19 21:37:09 +0000 | [diff] [blame] | 491 | if (InstPatNode->getNumChildren()) { |
| 492 | assert(InstPatNode->getChild(0)->getNumTypes() == 1); |
| 493 | VT = InstPatNode->getChild(0)->getType(0); |
| 494 | } |
Dan Gohman | f4137b5 | 2008-08-19 20:30:54 +0000 | [diff] [blame] | 495 | |
| 496 | // For now, filter out any instructions with predicates. |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 497 | if (!InstPatNode->getPredicateFns().empty()) |
Dan Gohman | f4137b5 | 2008-08-19 20:30:54 +0000 | [diff] [blame] | 498 | continue; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 499 | |
Dan Gohman | 379cad4 | 2008-08-19 20:36:33 +0000 | [diff] [blame] | 500 | // Check all the operands. |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 501 | OperandsSignature Operands; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 502 | if (!Operands.initialize(InstPatNode, Target, VT, ImmediatePredicates)) |
Dan Gohman | d1d2ee8 | 2008-08-19 20:56:30 +0000 | [diff] [blame] | 503 | continue; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 504 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 505 | std::vector<std::string>* PhysRegInputs = new std::vector<std::string>(); |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 506 | if (InstPatNode->getOperator()->getName() == "imm" || |
| 507 | InstPatNode->getOperator()->getName() == "fpimmm") |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 508 | PhysRegInputs->push_back(""); |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 509 | else { |
| 510 | // Compute the PhysRegs used by the given pattern, and check that |
| 511 | // the mapping from the src to dst patterns is simple. |
| 512 | bool FoundNonSimplePattern = false; |
| 513 | unsigned DstIndex = 0; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 514 | for (unsigned i = 0, e = InstPatNode->getNumChildren(); i != e; ++i) { |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 515 | std::string PhysReg = PhyRegForNode(InstPatNode->getChild(i), Target); |
| 516 | if (PhysReg.empty()) { |
| 517 | if (DstIndex >= Dst->getNumChildren() || |
| 518 | Dst->getChild(DstIndex)->getName() != |
| 519 | InstPatNode->getChild(i)->getName()) { |
| 520 | FoundNonSimplePattern = true; |
| 521 | break; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 522 | } |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 523 | ++DstIndex; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 524 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 525 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 526 | PhysRegInputs->push_back(PhysReg); |
| 527 | } |
Eli Friedman | 206a10c | 2011-04-29 21:58:31 +0000 | [diff] [blame] | 528 | |
| 529 | if (Op->getName() != "EXTRACT_SUBREG" && DstIndex < Dst->getNumChildren()) |
| 530 | FoundNonSimplePattern = true; |
| 531 | |
| 532 | if (FoundNonSimplePattern) |
| 533 | continue; |
| 534 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 535 | |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 536 | // Get the predicate that guards this pattern. |
| 537 | std::string PredicateCheck = Pattern.getPredicateCheck(); |
| 538 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 539 | // Ok, we found a pattern that we can handle. Remember it. |
Dan Gohman | 520b50c | 2008-08-21 00:35:26 +0000 | [diff] [blame] | 540 | InstructionMemo Memo = { |
| 541 | Pattern.getDstPattern()->getOperator()->getName(), |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 542 | DstRC, |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 543 | SubRegNo, |
| 544 | PhysRegInputs |
Dan Gohman | 520b50c | 2008-08-21 00:35:26 +0000 | [diff] [blame] | 545 | }; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 546 | |
| 547 | if (SimplePatterns[Operands][OpcodeName][VT][RetVT].count(PredicateCheck)) |
| 548 | throw TGError(Pattern.getSrcRecord()->getLoc(), |
| 549 | "Duplicate record in FastISel table!"); |
Jim Grosbach | 997759a | 2010-12-07 23:05:49 +0000 | [diff] [blame] | 550 | |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 551 | SimplePatterns[Operands][OpcodeName][VT][RetVT][PredicateCheck] = Memo; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 552 | |
| 553 | // If any of the operands were immediates with predicates on them, strip |
| 554 | // them down to a signature that doesn't have predicates so that we can |
| 555 | // associate them with the stripped predicate version. |
| 556 | if (Operands.hasAnyImmediateCodes()) { |
| 557 | SignaturesWithConstantForms[Operands.getWithoutImmCodes()] |
| 558 | .push_back(Operands); |
| 559 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 560 | } |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 561 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 563 | void FastISelMap::printImmediatePredicates(raw_ostream &OS) { |
| 564 | if (ImmediatePredicates.begin() == ImmediatePredicates.end()) |
| 565 | return; |
| 566 | |
| 567 | OS << "\n// FastEmit Immediate Predicate functions.\n"; |
| 568 | for (ImmPredicateSet::iterator I = ImmediatePredicates.begin(), |
| 569 | E = ImmediatePredicates.end(); I != E; ++I) { |
| 570 | OS << "static bool " << I->getFnName() << "(int64_t Imm) {\n"; |
| 571 | OS << I->getImmediatePredicateCode() << "\n}\n"; |
| 572 | } |
| 573 | |
| 574 | OS << "\n\n"; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | void FastISelMap::printFunctionDefinitions(raw_ostream &OS) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 579 | // Now emit code for all the patterns that we collected. |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 580 | for (OperandsOpcodeTypeRetPredMap::const_iterator OI = SimplePatterns.begin(), |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 581 | OE = SimplePatterns.end(); OI != OE; ++OI) { |
| 582 | const OperandsSignature &Operands = OI->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 583 | const OpcodeTypeRetPredMap &OTM = OI->second; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 584 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 585 | for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 586 | I != E; ++I) { |
| 587 | const std::string &Opcode = I->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 588 | const TypeRetPredMap &TM = I->second; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 589 | |
| 590 | OS << "// FastEmit functions for " << Opcode << ".\n"; |
| 591 | OS << "\n"; |
| 592 | |
| 593 | // Emit one function for each opcode,type pair. |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 594 | for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 595 | TI != TE; ++TI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 596 | MVT::SimpleValueType VT = TI->first; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 597 | const RetPredMap &RM = TI->second; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 598 | if (RM.size() != 1) { |
| 599 | for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); |
| 600 | RI != RE; ++RI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 601 | MVT::SimpleValueType RetVT = RI->first; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 602 | const PredMap &PM = RI->second; |
| 603 | bool HasPred = false; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 604 | |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 605 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 606 | << getLegalCName(Opcode) |
| 607 | << "_" << getLegalCName(getName(VT)) |
| 608 | << "_" << getLegalCName(getName(RetVT)) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 609 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 610 | OS << "("; |
| 611 | Operands.PrintParameters(OS); |
| 612 | OS << ") {\n"; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 613 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 614 | // Emit code for each possible instruction. There may be |
| 615 | // multiple if there are subtarget concerns. |
| 616 | for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); |
| 617 | PI != PE; ++PI) { |
| 618 | std::string PredicateCheck = PI->first; |
| 619 | const InstructionMemo &Memo = PI->second; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 620 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 621 | if (PredicateCheck.empty()) { |
| 622 | assert(!HasPred && |
| 623 | "Multiple instructions match, at least one has " |
| 624 | "a predicate and at least one doesn't!"); |
| 625 | } else { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 626 | OS << " if (" + PredicateCheck + ") {\n"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 627 | OS << " "; |
| 628 | HasPred = true; |
| 629 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 630 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 631 | for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { |
| 632 | if ((*Memo.PhysRegs)[i] != "") |
Jakob Stoklund Olesen | 4f8e771 | 2010-07-11 03:53:50 +0000 | [diff] [blame] | 633 | OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, " |
| 634 | << "TII.get(TargetOpcode::COPY), " |
| 635 | << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n"; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 636 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 637 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 638 | OS << " return FastEmitInst_"; |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 639 | if (Memo.SubRegNo.empty()) { |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 640 | Operands.PrintManglingSuffix(OS, *Memo.PhysRegs, |
| 641 | ImmediatePredicates, true); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 642 | OS << "(" << InstNS << Memo.Name << ", "; |
| 643 | OS << InstNS << Memo.RC->getName() << "RegisterClass"; |
| 644 | if (!Operands.empty()) |
| 645 | OS << ", "; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 646 | Operands.PrintArguments(OS, *Memo.PhysRegs); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 647 | OS << ");\n"; |
| 648 | } else { |
Evan Cheng | 536ab13 | 2009-01-22 09:10:11 +0000 | [diff] [blame] | 649 | OS << "extractsubreg(" << getName(RetVT); |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 650 | OS << ", Op0, Op0IsKill, " << Memo.SubRegNo << ");\n"; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 651 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 652 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 653 | if (HasPred) |
Evan Cheng | d07b46e | 2008-09-07 08:23:06 +0000 | [diff] [blame] | 654 | OS << " }\n"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 655 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 656 | } |
| 657 | // Return 0 if none of the predicates were satisfied. |
| 658 | if (HasPred) |
| 659 | OS << " return 0;\n"; |
| 660 | OS << "}\n"; |
| 661 | OS << "\n"; |
| 662 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 663 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 664 | // Emit one function for the type that demultiplexes on return type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 665 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 666 | << getLegalCName(Opcode) << "_" |
Owen Anderson | abb1f16 | 2008-08-26 01:22:59 +0000 | [diff] [blame] | 667 | << getLegalCName(getName(VT)) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 668 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 669 | OS << "(MVT RetVT"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 670 | if (!Operands.empty()) |
| 671 | OS << ", "; |
| 672 | Operands.PrintParameters(OS); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 673 | OS << ") {\nswitch (RetVT.SimpleTy) {\n"; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 674 | for (RetPredMap::const_iterator RI = RM.begin(), RE = RM.end(); |
| 675 | RI != RE; ++RI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 676 | MVT::SimpleValueType RetVT = RI->first; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 677 | OS << " case " << getName(RetVT) << ": return FastEmit_" |
| 678 | << getLegalCName(Opcode) << "_" << getLegalCName(getName(VT)) |
| 679 | << "_" << getLegalCName(getName(RetVT)) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 680 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 681 | OS << "("; |
| 682 | Operands.PrintArguments(OS); |
| 683 | OS << ");\n"; |
| 684 | } |
| 685 | OS << " default: return 0;\n}\n}\n\n"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 686 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 687 | } else { |
| 688 | // Non-variadic return type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 689 | OS << "unsigned FastEmit_" |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 690 | << getLegalCName(Opcode) << "_" |
| 691 | << getLegalCName(getName(VT)) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 692 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 693 | OS << "(MVT RetVT"; |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 694 | if (!Operands.empty()) |
| 695 | OS << ", "; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 696 | Operands.PrintParameters(OS); |
| 697 | OS << ") {\n"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 698 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 699 | OS << " if (RetVT.SimpleTy != " << getName(RM.begin()->first) |
Owen Anderson | 70647e8 | 2008-08-26 18:50:00 +0000 | [diff] [blame] | 700 | << ")\n return 0;\n"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 701 | |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 702 | const PredMap &PM = RM.begin()->second; |
| 703 | bool HasPred = false; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 704 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 705 | // Emit code for each possible instruction. There may be |
| 706 | // multiple if there are subtarget concerns. |
Evan Cheng | 98d2d07 | 2008-09-08 08:39:33 +0000 | [diff] [blame] | 707 | for (PredMap::const_iterator PI = PM.begin(), PE = PM.end(); PI != PE; |
| 708 | ++PI) { |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 709 | std::string PredicateCheck = PI->first; |
| 710 | const InstructionMemo &Memo = PI->second; |
Owen Anderson | 71669e5 | 2008-08-26 00:42:26 +0000 | [diff] [blame] | 711 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 712 | if (PredicateCheck.empty()) { |
| 713 | assert(!HasPred && |
| 714 | "Multiple instructions match, at least one has " |
| 715 | "a predicate and at least one doesn't!"); |
| 716 | } else { |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 717 | OS << " if (" + PredicateCheck + ") {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 718 | OS << " "; |
| 719 | HasPred = true; |
| 720 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 721 | |
Jakob Stoklund Olesen | 4f8e771 | 2010-07-11 03:53:50 +0000 | [diff] [blame] | 722 | for (unsigned i = 0; i < Memo.PhysRegs->size(); ++i) { |
| 723 | if ((*Memo.PhysRegs)[i] != "") |
| 724 | OS << " BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, " |
| 725 | << "TII.get(TargetOpcode::COPY), " |
| 726 | << (*Memo.PhysRegs)[i] << ").addReg(Op" << i << ");\n"; |
| 727 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 728 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 729 | OS << " return FastEmitInst_"; |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 730 | |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 731 | if (Memo.SubRegNo.empty()) { |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 732 | Operands.PrintManglingSuffix(OS, *Memo.PhysRegs, |
| 733 | ImmediatePredicates, true); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 734 | OS << "(" << InstNS << Memo.Name << ", "; |
| 735 | OS << InstNS << Memo.RC->getName() << "RegisterClass"; |
| 736 | if (!Operands.empty()) |
| 737 | OS << ", "; |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 738 | Operands.PrintArguments(OS, *Memo.PhysRegs); |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 739 | OS << ");\n"; |
| 740 | } else { |
Dan Gohman | a6cb641 | 2010-05-11 23:54:07 +0000 | [diff] [blame] | 741 | OS << "extractsubreg(RetVT, Op0, Op0IsKill, "; |
Jakob Stoklund Olesen | 73ea7bf | 2010-05-24 14:48:12 +0000 | [diff] [blame] | 742 | OS << Memo.SubRegNo; |
Owen Anderson | b5dbcb5 | 2008-08-28 18:06:12 +0000 | [diff] [blame] | 743 | OS << ");\n"; |
| 744 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 745 | |
Owen Anderson | 667d8f7 | 2008-08-29 17:45:56 +0000 | [diff] [blame] | 746 | if (HasPred) |
| 747 | OS << " }\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 748 | } |
Jim Grosbach | 45258f5 | 2010-12-07 19:36:07 +0000 | [diff] [blame] | 749 | |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 750 | // Return 0 if none of the predicates were satisfied. |
| 751 | if (HasPred) |
| 752 | OS << " return 0;\n"; |
| 753 | OS << "}\n"; |
| 754 | OS << "\n"; |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 755 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | // Emit one function for the opcode that demultiplexes based on the type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 759 | OS << "unsigned FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 760 | << getLegalCName(Opcode) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 761 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 762 | OS << "(MVT VT, MVT RetVT"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 763 | if (!Operands.empty()) |
| 764 | OS << ", "; |
| 765 | Operands.PrintParameters(OS); |
| 766 | OS << ") {\n"; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 767 | OS << " switch (VT.SimpleTy) {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 768 | for (TypeRetPredMap::const_iterator TI = TM.begin(), TE = TM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 769 | TI != TE; ++TI) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 770 | MVT::SimpleValueType VT = TI->first; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 771 | std::string TypeName = getName(VT); |
| 772 | OS << " case " << TypeName << ": return FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 773 | << getLegalCName(Opcode) << "_" << getLegalCName(TypeName) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 774 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 775 | OS << "(RetVT"; |
| 776 | if (!Operands.empty()) |
| 777 | OS << ", "; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 778 | Operands.PrintArguments(OS); |
| 779 | OS << ");\n"; |
| 780 | } |
| 781 | OS << " default: return 0;\n"; |
| 782 | OS << " }\n"; |
| 783 | OS << "}\n"; |
| 784 | OS << "\n"; |
| 785 | } |
| 786 | |
Dan Gohman | 0bfb752 | 2008-08-22 00:28:15 +0000 | [diff] [blame] | 787 | OS << "// Top-level FastEmit function.\n"; |
| 788 | OS << "\n"; |
| 789 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 790 | // Emit one function for the operand signature that demultiplexes based |
| 791 | // on opcode and type. |
Evan Cheng | c3f44b0 | 2008-09-03 00:03:49 +0000 | [diff] [blame] | 792 | OS << "unsigned FastEmit_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 793 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Dan Gohman | 7c3ecb6 | 2010-01-05 22:26:32 +0000 | [diff] [blame] | 794 | OS << "(MVT VT, MVT RetVT, unsigned Opcode"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 795 | if (!Operands.empty()) |
| 796 | OS << ", "; |
| 797 | Operands.PrintParameters(OS); |
| 798 | OS << ") {\n"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 799 | |
| 800 | // If there are any forms of this signature available that operand on |
| 801 | // constrained forms of the immediate (e.g. 32-bit sext immediate in a |
| 802 | // 64-bit operand), check them first. |
| 803 | |
| 804 | std::map<OperandsSignature, std::vector<OperandsSignature> >::iterator MI |
| 805 | = SignaturesWithConstantForms.find(Operands); |
| 806 | if (MI != SignaturesWithConstantForms.end()) { |
| 807 | // Unique any duplicates out of the list. |
| 808 | std::sort(MI->second.begin(), MI->second.end()); |
| 809 | MI->second.erase(std::unique(MI->second.begin(), MI->second.end()), |
| 810 | MI->second.end()); |
| 811 | |
| 812 | // Check each in order it was seen. It would be nice to have a good |
| 813 | // relative ordering between them, but we're not going for optimality |
| 814 | // here. |
| 815 | for (unsigned i = 0, e = MI->second.size(); i != e; ++i) { |
| 816 | OS << " if ("; |
| 817 | MI->second[i].emitImmediatePredicate(OS, ImmediatePredicates); |
| 818 | OS << ")\n if (unsigned Reg = FastEmit_"; |
| 819 | MI->second[i].PrintManglingSuffix(OS, ImmediatePredicates); |
| 820 | OS << "(VT, RetVT, Opcode"; |
| 821 | if (!MI->second[i].empty()) |
| 822 | OS << ", "; |
| 823 | MI->second[i].PrintArguments(OS); |
| 824 | OS << "))\n return Reg;\n\n"; |
| 825 | } |
| 826 | |
| 827 | // Done with this, remove it. |
| 828 | SignaturesWithConstantForms.erase(MI); |
| 829 | } |
| 830 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 831 | OS << " switch (Opcode) {\n"; |
Owen Anderson | 7b2e579 | 2008-08-25 23:43:09 +0000 | [diff] [blame] | 832 | for (OpcodeTypeRetPredMap::const_iterator I = OTM.begin(), E = OTM.end(); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 833 | I != E; ++I) { |
| 834 | const std::string &Opcode = I->first; |
| 835 | |
| 836 | OS << " case " << Opcode << ": return FastEmit_" |
Dan Gohman | d5fe57d | 2008-08-21 01:41:07 +0000 | [diff] [blame] | 837 | << getLegalCName(Opcode) << "_"; |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 838 | Operands.PrintManglingSuffix(OS, ImmediatePredicates); |
Owen Anderson | 0f84e4e | 2008-08-25 23:58:18 +0000 | [diff] [blame] | 839 | OS << "(VT, RetVT"; |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 840 | if (!Operands.empty()) |
| 841 | OS << ", "; |
| 842 | Operands.PrintArguments(OS); |
| 843 | OS << ");\n"; |
| 844 | } |
| 845 | OS << " default: return 0;\n"; |
| 846 | OS << " }\n"; |
| 847 | OS << "}\n"; |
| 848 | OS << "\n"; |
| 849 | } |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 850 | |
| 851 | // TODO: SignaturesWithConstantForms should be empty here. |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 854 | void FastISelEmitter::run(raw_ostream &OS) { |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 855 | const CodeGenTarget &Target = CGP.getTargetInfo(); |
| 856 | |
| 857 | // Determine the target's namespace name. |
| 858 | std::string InstNS = Target.getInstNamespace() + "::"; |
| 859 | assert(InstNS.size() > 2 && "Can't determine target-specific namespace!"); |
| 860 | |
| 861 | EmitSourceFileHeader("\"Fast\" Instruction Selector for the " + |
| 862 | Target.getName() + " target", OS); |
| 863 | |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 864 | FastISelMap F(InstNS); |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 865 | F.collectPatterns(CGP); |
| 866 | F.printImmediatePredicates(OS); |
| 867 | F.printFunctionDefinitions(OS); |
Dan Gohman | c7f72de | 2008-08-21 00:19:05 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | FastISelEmitter::FastISelEmitter(RecordKeeper &R) |
Chris Lattner | 1518afd | 2011-04-18 06:22:33 +0000 | [diff] [blame] | 871 | : Records(R), CGP(R) { |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 872 | } |
Dan Gohman | 72d63af | 2008-08-26 21:21:20 +0000 | [diff] [blame] | 873 | |