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