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