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