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