Have MachineFunction cache a pointer to the subtarget to make lookups
[oota-llvm.git] / lib / CodeGen / SelectionDAG / SelectionDAGDumper.cpp
1 //===-- SelectionDAGDumper.cpp - Implement SelectionDAG::dump() -----------===//
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 //
10 // This implements the SelectionDAG::dump method and friends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "ScheduleDAGSDNodes.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/CodeGen/MachineConstantPool.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/GraphWriter.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetIntrinsicInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegisterInfo.h"
30 #include "llvm/Target/TargetSubtargetInfo.h"
31 using namespace llvm;
32
33 std::string SDNode::getOperationName(const SelectionDAG *G) const {
34   switch (getOpcode()) {
35   default:
36     if (getOpcode() < ISD::BUILTIN_OP_END)
37       return "<<Unknown DAG Node>>";
38     if (isMachineOpcode()) {
39       if (G)
40         if (const TargetInstrInfo *TII = G->getSubtarget().getInstrInfo())
41           if (getMachineOpcode() < TII->getNumOpcodes())
42             return TII->getName(getMachineOpcode());
43       return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>";
44     }
45     if (G) {
46       const TargetLowering &TLI = G->getTargetLoweringInfo();
47       const char *Name = TLI.getTargetNodeName(getOpcode());
48       if (Name) return Name;
49       return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>";
50     }
51     return "<<Unknown Node #" + utostr(getOpcode()) + ">>";
52
53 #ifndef NDEBUG
54   case ISD::DELETED_NODE:               return "<<Deleted Node!>>";
55 #endif
56   case ISD::PREFETCH:                   return "Prefetch";
57   case ISD::ATOMIC_FENCE:               return "AtomicFence";
58   case ISD::ATOMIC_CMP_SWAP:            return "AtomicCmpSwap";
59   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: return "AtomicCmpSwapWithSuccess";
60   case ISD::ATOMIC_SWAP:                return "AtomicSwap";
61   case ISD::ATOMIC_LOAD_ADD:            return "AtomicLoadAdd";
62   case ISD::ATOMIC_LOAD_SUB:            return "AtomicLoadSub";
63   case ISD::ATOMIC_LOAD_AND:            return "AtomicLoadAnd";
64   case ISD::ATOMIC_LOAD_OR:             return "AtomicLoadOr";
65   case ISD::ATOMIC_LOAD_XOR:            return "AtomicLoadXor";
66   case ISD::ATOMIC_LOAD_NAND:           return "AtomicLoadNand";
67   case ISD::ATOMIC_LOAD_MIN:            return "AtomicLoadMin";
68   case ISD::ATOMIC_LOAD_MAX:            return "AtomicLoadMax";
69   case ISD::ATOMIC_LOAD_UMIN:           return "AtomicLoadUMin";
70   case ISD::ATOMIC_LOAD_UMAX:           return "AtomicLoadUMax";
71   case ISD::ATOMIC_LOAD:                return "AtomicLoad";
72   case ISD::ATOMIC_STORE:               return "AtomicStore";
73   case ISD::PCMARKER:                   return "PCMarker";
74   case ISD::READCYCLECOUNTER:           return "ReadCycleCounter";
75   case ISD::SRCVALUE:                   return "SrcValue";
76   case ISD::MDNODE_SDNODE:              return "MDNode";
77   case ISD::EntryToken:                 return "EntryToken";
78   case ISD::TokenFactor:                return "TokenFactor";
79   case ISD::AssertSext:                 return "AssertSext";
80   case ISD::AssertZext:                 return "AssertZext";
81
82   case ISD::BasicBlock:                 return "BasicBlock";
83   case ISD::VALUETYPE:                  return "ValueType";
84   case ISD::Register:                   return "Register";
85   case ISD::RegisterMask:               return "RegisterMask";
86   case ISD::Constant:
87     if (cast<ConstantSDNode>(this)->isOpaque())
88       return "OpaqueConstant";
89     return "Constant";
90   case ISD::ConstantFP:                 return "ConstantFP";
91   case ISD::GlobalAddress:              return "GlobalAddress";
92   case ISD::GlobalTLSAddress:           return "GlobalTLSAddress";
93   case ISD::FrameIndex:                 return "FrameIndex";
94   case ISD::JumpTable:                  return "JumpTable";
95   case ISD::GLOBAL_OFFSET_TABLE:        return "GLOBAL_OFFSET_TABLE";
96   case ISD::RETURNADDR:                 return "RETURNADDR";
97   case ISD::FRAMEADDR:                  return "FRAMEADDR";
98   case ISD::READ_REGISTER:              return "READ_REGISTER";
99   case ISD::WRITE_REGISTER:             return "WRITE_REGISTER";
100   case ISD::FRAME_TO_ARGS_OFFSET:       return "FRAME_TO_ARGS_OFFSET";
101   case ISD::EH_RETURN:                  return "EH_RETURN";
102   case ISD::EH_SJLJ_SETJMP:             return "EH_SJLJ_SETJMP";
103   case ISD::EH_SJLJ_LONGJMP:            return "EH_SJLJ_LONGJMP";
104   case ISD::ConstantPool:               return "ConstantPool";
105   case ISD::TargetIndex:                return "TargetIndex";
106   case ISD::ExternalSymbol:             return "ExternalSymbol";
107   case ISD::BlockAddress:               return "BlockAddress";
108   case ISD::INTRINSIC_WO_CHAIN:
109   case ISD::INTRINSIC_VOID:
110   case ISD::INTRINSIC_W_CHAIN: {
111     unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
112     unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
113     if (IID < Intrinsic::num_intrinsics)
114       return Intrinsic::getName((Intrinsic::ID)IID);
115     else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
116       return TII->getName(IID);
117     llvm_unreachable("Invalid intrinsic ID");
118   }
119
120   case ISD::BUILD_VECTOR:               return "BUILD_VECTOR";
121   case ISD::TargetConstant:
122     if (cast<ConstantSDNode>(this)->isOpaque())
123       return "OpaqueTargetConstant";
124     return "TargetConstant";
125   case ISD::TargetConstantFP:           return "TargetConstantFP";
126   case ISD::TargetGlobalAddress:        return "TargetGlobalAddress";
127   case ISD::TargetGlobalTLSAddress:     return "TargetGlobalTLSAddress";
128   case ISD::TargetFrameIndex:           return "TargetFrameIndex";
129   case ISD::TargetJumpTable:            return "TargetJumpTable";
130   case ISD::TargetConstantPool:         return "TargetConstantPool";
131   case ISD::TargetExternalSymbol:       return "TargetExternalSymbol";
132   case ISD::TargetBlockAddress:         return "TargetBlockAddress";
133
134   case ISD::CopyToReg:                  return "CopyToReg";
135   case ISD::CopyFromReg:                return "CopyFromReg";
136   case ISD::UNDEF:                      return "undef";
137   case ISD::MERGE_VALUES:               return "merge_values";
138   case ISD::INLINEASM:                  return "inlineasm";
139   case ISD::EH_LABEL:                   return "eh_label";
140   case ISD::HANDLENODE:                 return "handlenode";
141
142   // Unary operators
143   case ISD::FABS:                       return "fabs";
144   case ISD::FNEG:                       return "fneg";
145   case ISD::FSQRT:                      return "fsqrt";
146   case ISD::FSIN:                       return "fsin";
147   case ISD::FCOS:                       return "fcos";
148   case ISD::FSINCOS:                    return "fsincos";
149   case ISD::FTRUNC:                     return "ftrunc";
150   case ISD::FFLOOR:                     return "ffloor";
151   case ISD::FCEIL:                      return "fceil";
152   case ISD::FRINT:                      return "frint";
153   case ISD::FNEARBYINT:                 return "fnearbyint";
154   case ISD::FROUND:                     return "fround";
155   case ISD::FEXP:                       return "fexp";
156   case ISD::FEXP2:                      return "fexp2";
157   case ISD::FLOG:                       return "flog";
158   case ISD::FLOG2:                      return "flog2";
159   case ISD::FLOG10:                     return "flog10";
160
161   // Binary operators
162   case ISD::ADD:                        return "add";
163   case ISD::SUB:                        return "sub";
164   case ISD::MUL:                        return "mul";
165   case ISD::MULHU:                      return "mulhu";
166   case ISD::MULHS:                      return "mulhs";
167   case ISD::SDIV:                       return "sdiv";
168   case ISD::UDIV:                       return "udiv";
169   case ISD::SREM:                       return "srem";
170   case ISD::UREM:                       return "urem";
171   case ISD::SMUL_LOHI:                  return "smul_lohi";
172   case ISD::UMUL_LOHI:                  return "umul_lohi";
173   case ISD::SDIVREM:                    return "sdivrem";
174   case ISD::UDIVREM:                    return "udivrem";
175   case ISD::AND:                        return "and";
176   case ISD::OR:                         return "or";
177   case ISD::XOR:                        return "xor";
178   case ISD::SHL:                        return "shl";
179   case ISD::SRA:                        return "sra";
180   case ISD::SRL:                        return "srl";
181   case ISD::ROTL:                       return "rotl";
182   case ISD::ROTR:                       return "rotr";
183   case ISD::FADD:                       return "fadd";
184   case ISD::FSUB:                       return "fsub";
185   case ISD::FMUL:                       return "fmul";
186   case ISD::FDIV:                       return "fdiv";
187   case ISD::FMA:                        return "fma";
188   case ISD::FREM:                       return "frem";
189   case ISD::FCOPYSIGN:                  return "fcopysign";
190   case ISD::FGETSIGN:                   return "fgetsign";
191   case ISD::FPOW:                       return "fpow";
192
193   case ISD::FPOWI:                      return "fpowi";
194   case ISD::SETCC:                      return "setcc";
195   case ISD::SELECT:                     return "select";
196   case ISD::VSELECT:                    return "vselect";
197   case ISD::SELECT_CC:                  return "select_cc";
198   case ISD::INSERT_VECTOR_ELT:          return "insert_vector_elt";
199   case ISD::EXTRACT_VECTOR_ELT:         return "extract_vector_elt";
200   case ISD::CONCAT_VECTORS:             return "concat_vectors";
201   case ISD::INSERT_SUBVECTOR:           return "insert_subvector";
202   case ISD::EXTRACT_SUBVECTOR:          return "extract_subvector";
203   case ISD::SCALAR_TO_VECTOR:           return "scalar_to_vector";
204   case ISD::VECTOR_SHUFFLE:             return "vector_shuffle";
205   case ISD::CARRY_FALSE:                return "carry_false";
206   case ISD::ADDC:                       return "addc";
207   case ISD::ADDE:                       return "adde";
208   case ISD::SADDO:                      return "saddo";
209   case ISD::UADDO:                      return "uaddo";
210   case ISD::SSUBO:                      return "ssubo";
211   case ISD::USUBO:                      return "usubo";
212   case ISD::SMULO:                      return "smulo";
213   case ISD::UMULO:                      return "umulo";
214   case ISD::SUBC:                       return "subc";
215   case ISD::SUBE:                       return "sube";
216   case ISD::SHL_PARTS:                  return "shl_parts";
217   case ISD::SRA_PARTS:                  return "sra_parts";
218   case ISD::SRL_PARTS:                  return "srl_parts";
219
220   // Conversion operators.
221   case ISD::SIGN_EXTEND:                return "sign_extend";
222   case ISD::ZERO_EXTEND:                return "zero_extend";
223   case ISD::ANY_EXTEND:                 return "any_extend";
224   case ISD::SIGN_EXTEND_INREG:          return "sign_extend_inreg";
225   case ISD::ANY_EXTEND_VECTOR_INREG:    return "any_extend_vector_inreg";
226   case ISD::SIGN_EXTEND_VECTOR_INREG:   return "sign_extend_vector_inreg";
227   case ISD::ZERO_EXTEND_VECTOR_INREG:   return "zero_extend_vector_inreg";
228   case ISD::TRUNCATE:                   return "truncate";
229   case ISD::FP_ROUND:                   return "fp_round";
230   case ISD::FLT_ROUNDS_:                return "flt_rounds";
231   case ISD::FP_ROUND_INREG:             return "fp_round_inreg";
232   case ISD::FP_EXTEND:                  return "fp_extend";
233
234   case ISD::SINT_TO_FP:                 return "sint_to_fp";
235   case ISD::UINT_TO_FP:                 return "uint_to_fp";
236   case ISD::FP_TO_SINT:                 return "fp_to_sint";
237   case ISD::FP_TO_UINT:                 return "fp_to_uint";
238   case ISD::BITCAST:                    return "bitcast";
239   case ISD::ADDRSPACECAST:              return "addrspacecast";
240   case ISD::FP16_TO_FP:                 return "fp16_to_fp";
241   case ISD::FP_TO_FP16:                 return "fp_to_fp16";
242
243   case ISD::CONVERT_RNDSAT: {
244     switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
245     default: llvm_unreachable("Unknown cvt code!");
246     case ISD::CVT_FF:                   return "cvt_ff";
247     case ISD::CVT_FS:                   return "cvt_fs";
248     case ISD::CVT_FU:                   return "cvt_fu";
249     case ISD::CVT_SF:                   return "cvt_sf";
250     case ISD::CVT_UF:                   return "cvt_uf";
251     case ISD::CVT_SS:                   return "cvt_ss";
252     case ISD::CVT_SU:                   return "cvt_su";
253     case ISD::CVT_US:                   return "cvt_us";
254     case ISD::CVT_UU:                   return "cvt_uu";
255     }
256   }
257
258     // Control flow instructions
259   case ISD::BR:                         return "br";
260   case ISD::BRIND:                      return "brind";
261   case ISD::BR_JT:                      return "br_jt";
262   case ISD::BRCOND:                     return "brcond";
263   case ISD::BR_CC:                      return "br_cc";
264   case ISD::CALLSEQ_START:              return "callseq_start";
265   case ISD::CALLSEQ_END:                return "callseq_end";
266
267     // Other operators
268   case ISD::LOAD:                       return "load";
269   case ISD::STORE:                      return "store";
270   case ISD::VAARG:                      return "vaarg";
271   case ISD::VACOPY:                     return "vacopy";
272   case ISD::VAEND:                      return "vaend";
273   case ISD::VASTART:                    return "vastart";
274   case ISD::DYNAMIC_STACKALLOC:         return "dynamic_stackalloc";
275   case ISD::EXTRACT_ELEMENT:            return "extract_element";
276   case ISD::BUILD_PAIR:                 return "build_pair";
277   case ISD::STACKSAVE:                  return "stacksave";
278   case ISD::STACKRESTORE:               return "stackrestore";
279   case ISD::TRAP:                       return "trap";
280   case ISD::DEBUGTRAP:                  return "debugtrap";
281   case ISD::LIFETIME_START:             return "lifetime.start";
282   case ISD::LIFETIME_END:               return "lifetime.end";
283
284   // Bit manipulation
285   case ISD::BSWAP:                      return "bswap";
286   case ISD::CTPOP:                      return "ctpop";
287   case ISD::CTTZ:                       return "cttz";
288   case ISD::CTTZ_ZERO_UNDEF:            return "cttz_zero_undef";
289   case ISD::CTLZ:                       return "ctlz";
290   case ISD::CTLZ_ZERO_UNDEF:            return "ctlz_zero_undef";
291
292   // Trampolines
293   case ISD::INIT_TRAMPOLINE:            return "init_trampoline";
294   case ISD::ADJUST_TRAMPOLINE:          return "adjust_trampoline";
295
296   case ISD::CONDCODE:
297     switch (cast<CondCodeSDNode>(this)->get()) {
298     default: llvm_unreachable("Unknown setcc condition!");
299     case ISD::SETOEQ:                   return "setoeq";
300     case ISD::SETOGT:                   return "setogt";
301     case ISD::SETOGE:                   return "setoge";
302     case ISD::SETOLT:                   return "setolt";
303     case ISD::SETOLE:                   return "setole";
304     case ISD::SETONE:                   return "setone";
305
306     case ISD::SETO:                     return "seto";
307     case ISD::SETUO:                    return "setuo";
308     case ISD::SETUEQ:                   return "setue";
309     case ISD::SETUGT:                   return "setugt";
310     case ISD::SETUGE:                   return "setuge";
311     case ISD::SETULT:                   return "setult";
312     case ISD::SETULE:                   return "setule";
313     case ISD::SETUNE:                   return "setune";
314
315     case ISD::SETEQ:                    return "seteq";
316     case ISD::SETGT:                    return "setgt";
317     case ISD::SETGE:                    return "setge";
318     case ISD::SETLT:                    return "setlt";
319     case ISD::SETLE:                    return "setle";
320     case ISD::SETNE:                    return "setne";
321
322     case ISD::SETTRUE:                  return "settrue";
323     case ISD::SETTRUE2:                 return "settrue2";
324     case ISD::SETFALSE:                 return "setfalse";
325     case ISD::SETFALSE2:                return "setfalse2";
326     }
327   }
328 }
329
330 const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
331   switch (AM) {
332   default:              return "";
333   case ISD::PRE_INC:    return "<pre-inc>";
334   case ISD::PRE_DEC:    return "<pre-dec>";
335   case ISD::POST_INC:   return "<post-inc>";
336   case ISD::POST_DEC:   return "<post-dec>";
337   }
338 }
339
340 void SDNode::dump() const { dump(nullptr); }
341 void SDNode::dump(const SelectionDAG *G) const {
342   print(dbgs(), G);
343   dbgs() << '\n';
344 }
345
346 void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
347   OS << (const void*)this << ": ";
348
349   for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
350     if (i) OS << ",";
351     if (getValueType(i) == MVT::Other)
352       OS << "ch";
353     else
354       OS << getValueType(i).getEVTString();
355   }
356   OS << " = " << getOperationName(G);
357 }
358
359 void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
360   if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
361     if (!MN->memoperands_empty()) {
362       OS << "<";
363       OS << "Mem:";
364       for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
365            e = MN->memoperands_end(); i != e; ++i) {
366         OS << **i;
367         if (std::next(i) != e)
368           OS << " ";
369       }
370       OS << ">";
371     }
372   } else if (const ShuffleVectorSDNode *SVN =
373                dyn_cast<ShuffleVectorSDNode>(this)) {
374     OS << "<";
375     for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
376       int Idx = SVN->getMaskElt(i);
377       if (i) OS << ",";
378       if (Idx < 0)
379         OS << "u";
380       else
381         OS << Idx;
382     }
383     OS << ">";
384   } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
385     OS << '<' << CSDN->getAPIntValue() << '>';
386   } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
387     if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
388       OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
389     else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
390       OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
391     else {
392       OS << "<APFloat(";
393       CSDN->getValueAPF().bitcastToAPInt().dump();
394       OS << ")>";
395     }
396   } else if (const GlobalAddressSDNode *GADN =
397              dyn_cast<GlobalAddressSDNode>(this)) {
398     int64_t offset = GADN->getOffset();
399     OS << '<';
400     GADN->getGlobal()->printAsOperand(OS);
401     OS << '>';
402     if (offset > 0)
403       OS << " + " << offset;
404     else
405       OS << " " << offset;
406     if (unsigned int TF = GADN->getTargetFlags())
407       OS << " [TF=" << TF << ']';
408   } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
409     OS << "<" << FIDN->getIndex() << ">";
410   } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
411     OS << "<" << JTDN->getIndex() << ">";
412     if (unsigned int TF = JTDN->getTargetFlags())
413       OS << " [TF=" << TF << ']';
414   } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
415     int offset = CP->getOffset();
416     if (CP->isMachineConstantPoolEntry())
417       OS << "<" << *CP->getMachineCPVal() << ">";
418     else
419       OS << "<" << *CP->getConstVal() << ">";
420     if (offset > 0)
421       OS << " + " << offset;
422     else
423       OS << " " << offset;
424     if (unsigned int TF = CP->getTargetFlags())
425       OS << " [TF=" << TF << ']';
426   } else if (const TargetIndexSDNode *TI = dyn_cast<TargetIndexSDNode>(this)) {
427     OS << "<" << TI->getIndex() << '+' << TI->getOffset() << ">";
428     if (unsigned TF = TI->getTargetFlags())
429       OS << " [TF=" << TF << ']';
430   } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
431     OS << "<";
432     const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
433     if (LBB)
434       OS << LBB->getName() << " ";
435     OS << (const void*)BBDN->getBasicBlock() << ">";
436   } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
437     OS << ' ' << PrintReg(R->getReg(),
438                           G ? G->getSubtarget().getRegisterInfo() : nullptr);
439   } else if (const ExternalSymbolSDNode *ES =
440              dyn_cast<ExternalSymbolSDNode>(this)) {
441     OS << "'" << ES->getSymbol() << "'";
442     if (unsigned int TF = ES->getTargetFlags())
443       OS << " [TF=" << TF << ']';
444   } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
445     if (M->getValue())
446       OS << "<" << M->getValue() << ">";
447     else
448       OS << "<null>";
449   } else if (const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(this)) {
450     if (MD->getMD())
451       OS << "<" << MD->getMD() << ">";
452     else
453       OS << "<null>";
454   } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
455     OS << ":" << N->getVT().getEVTString();
456   }
457   else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
458     OS << "<" << *LD->getMemOperand();
459
460     bool doExt = true;
461     switch (LD->getExtensionType()) {
462     default: doExt = false; break;
463     case ISD::EXTLOAD:  OS << ", anyext"; break;
464     case ISD::SEXTLOAD: OS << ", sext"; break;
465     case ISD::ZEXTLOAD: OS << ", zext"; break;
466     }
467     if (doExt)
468       OS << " from " << LD->getMemoryVT().getEVTString();
469
470     const char *AM = getIndexedModeName(LD->getAddressingMode());
471     if (*AM)
472       OS << ", " << AM;
473
474     OS << ">";
475   } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
476     OS << "<" << *ST->getMemOperand();
477
478     if (ST->isTruncatingStore())
479       OS << ", trunc to " << ST->getMemoryVT().getEVTString();
480
481     const char *AM = getIndexedModeName(ST->getAddressingMode());
482     if (*AM)
483       OS << ", " << AM;
484
485     OS << ">";
486   } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
487     OS << "<" << *M->getMemOperand() << ">";
488   } else if (const BlockAddressSDNode *BA =
489                dyn_cast<BlockAddressSDNode>(this)) {
490     int64_t offset = BA->getOffset();
491     OS << "<";
492     BA->getBlockAddress()->getFunction()->printAsOperand(OS, false);
493     OS << ", ";
494     BA->getBlockAddress()->getBasicBlock()->printAsOperand(OS, false);
495     OS << ">";
496     if (offset > 0)
497       OS << " + " << offset;
498     else
499       OS << " " << offset;
500     if (unsigned int TF = BA->getTargetFlags())
501       OS << " [TF=" << TF << ']';
502   } else if (const AddrSpaceCastSDNode *ASC =
503                dyn_cast<AddrSpaceCastSDNode>(this)) {
504     OS << '['
505        << ASC->getSrcAddressSpace()
506        << " -> "
507        << ASC->getDestAddressSpace()
508        << ']';
509   }
510
511   if (unsigned Order = getIROrder())
512       OS << " [ORD=" << Order << ']';
513
514   if (getNodeId() != -1)
515     OS << " [ID=" << getNodeId() << ']';
516
517   DebugLoc dl = getDebugLoc();
518   if (G && !dl.isUnknown()) {
519     DIScope
520       Scope(dl.getScope(G->getMachineFunction().getFunction()->getContext()));
521     OS << " dbg:";
522     assert((!Scope || Scope.isScope()) &&
523       "Scope of a DebugLoc should be null or a DIScope.");
524     // Omit the directory, since it's usually long and uninteresting.
525     if (Scope)
526       OS << Scope.getFilename();
527     else
528       OS << "<unknown>";
529     OS << ':' << dl.getLine();
530     if (dl.getCol() != 0)
531       OS << ':' << dl.getCol();
532   }
533 }
534
535 static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
536   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
537     if (N->getOperand(i).getNode()->hasOneUse())
538       DumpNodes(N->getOperand(i).getNode(), indent+2, G);
539     else
540       dbgs() << "\n" << std::string(indent+2, ' ')
541              << (void*)N->getOperand(i).getNode() << ": <multiple use>";
542
543   dbgs() << '\n';
544   dbgs().indent(indent);
545   N->dump(G);
546 }
547
548 void SelectionDAG::dump() const {
549   dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
550
551   for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
552        I != E; ++I) {
553     const SDNode *N = I;
554     if (!N->hasOneUse() && N != getRoot().getNode())
555       DumpNodes(N, 2, this);
556   }
557
558   if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
559   dbgs() << "\n\n";
560 }
561
562 void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
563   print_types(OS, G);
564   print_details(OS, G);
565 }
566
567 typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
568 static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
569                        const SelectionDAG *G, VisitedSDNodeSet &once) {
570   if (!once.insert(N))          // If we've been here before, return now.
571     return;
572
573   // Dump the current SDNode, but don't end the line yet.
574   OS.indent(indent);
575   N->printr(OS, G);
576
577   // Having printed this SDNode, walk the children:
578   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
579     const SDNode *child = N->getOperand(i).getNode();
580
581     if (i) OS << ",";
582     OS << " ";
583
584     if (child->getNumOperands() == 0) {
585       // This child has no grandchildren; print it inline right here.
586       child->printr(OS, G);
587       once.insert(child);
588     } else {         // Just the address. FIXME: also print the child's opcode.
589       OS << (const void*)child;
590       if (unsigned RN = N->getOperand(i).getResNo())
591         OS << ":" << RN;
592     }
593   }
594
595   OS << "\n";
596
597   // Dump children that have grandchildren on their own line(s).
598   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
599     const SDNode *child = N->getOperand(i).getNode();
600     DumpNodesr(OS, child, indent+2, G, once);
601   }
602 }
603
604 void SDNode::dumpr() const {
605   VisitedSDNodeSet once;
606   DumpNodesr(dbgs(), this, 0, nullptr, once);
607 }
608
609 void SDNode::dumpr(const SelectionDAG *G) const {
610   VisitedSDNodeSet once;
611   DumpNodesr(dbgs(), this, 0, G, once);
612 }
613
614 static void printrWithDepthHelper(raw_ostream &OS, const SDNode *N,
615                                   const SelectionDAG *G, unsigned depth,
616                                   unsigned indent) {
617   if (depth == 0)
618     return;
619
620   OS.indent(indent);
621
622   N->print(OS, G);
623
624   if (depth < 1)
625     return;
626
627   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
628     // Don't follow chain operands.
629     if (N->getOperand(i).getValueType() == MVT::Other)
630       continue;
631     OS << '\n';
632     printrWithDepthHelper(OS, N->getOperand(i).getNode(), G, depth-1, indent+2);
633   }
634 }
635
636 void SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G,
637                             unsigned depth) const {
638   printrWithDepthHelper(OS, this, G, depth, 0);
639 }
640
641 void SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const {
642   // Don't print impossibly deep things.
643   printrWithDepth(OS, G, 10);
644 }
645
646 void SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const {
647   printrWithDepth(dbgs(), G, depth);
648 }
649
650 void SDNode::dumprFull(const SelectionDAG *G) const {
651   // Don't print impossibly deep things.
652   dumprWithDepth(G, 10);
653 }
654
655 void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
656   print_types(OS, G);
657   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
658     if (i) OS << ", "; else OS << " ";
659     OS << (void*)getOperand(i).getNode();
660     if (unsigned RN = getOperand(i).getResNo())
661       OS << ":" << RN;
662   }
663   print_details(OS, G);
664 }