llvm-objdump: Ignore unreachable blocks when printing the CFG.
[oota-llvm.git] / tools / llvm-objdump / MCFunction.cpp
1 //===-- MCFunction.cpp ----------------------------------------------------===//
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 file defines the algorithm to break down a region of machine code
11 // into basic blocks and try to reconstruct a CFG from it.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "MCFunction.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/MC/MCDisassembler.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstPrinter.h"
20 #include "llvm/MC/MCInstrDesc.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/Support/MemoryObject.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Support/system_error.h"
25 #include <set>
26 using namespace llvm;
27
28 MCFunction
29 MCFunction::createFunctionFromMC(StringRef Name, const MCDisassembler *DisAsm,
30                                  const MemoryObject &Region, uint64_t Start,
31                                  uint64_t End, const MCInstrInfo *InstrInfo,
32                                  raw_ostream &DebugOut) {
33   std::set<uint64_t> Splits;
34   Splits.insert(Start);
35   std::vector<MCDecodedInst> Instructions;
36   uint64_t Size;
37
38   // Disassemble code and gather basic block split points.
39   for (uint64_t Index = Start; Index < End; Index += Size) {
40     MCInst Inst;
41
42     if (DisAsm->getInstruction(Inst, Size, Region, Index, DebugOut)) {
43       const MCInstrDesc &Desc = InstrInfo->get(Inst.getOpcode());
44       if (Desc.isBranch()) {
45         if (Desc.OpInfo[0].OperandType == MCOI::OPERAND_PCREL) {
46           int64_t Imm = Inst.getOperand(0).getImm();
47           // FIXME: Distinguish relocations from nop jumps.
48           if (Imm != 0) {
49             if (Index+Imm+Size >= End) {
50               Instructions.push_back(MCDecodedInst(Index, Size, Inst));
51               continue; // Skip branches that leave the function.
52             }
53             Splits.insert(Index+Imm+Size);
54           }
55         }
56         Splits.insert(Index+Size);
57       } else if (Desc.isReturn()) {
58         Splits.insert(Index+Size);
59       }
60
61       Instructions.push_back(MCDecodedInst(Index, Size, Inst));
62     } else {
63       errs() << "warning: invalid instruction encoding\n";
64       if (Size == 0)
65         Size = 1; // skip illegible bytes
66     }
67
68   }
69
70   MCFunction f(Name);
71
72   // Create basic blocks.
73   unsigned ii = 0, ie = Instructions.size();
74   for (std::set<uint64_t>::iterator spi = Splits.begin(),
75        spe = Splits.end(); spi != spe; ++spi) {
76     MCBasicBlock BB;
77     uint64_t BlockEnd = llvm::next(spi) == spe ? End : *llvm::next(spi);
78     // Add instructions to the BB.
79     for (; ii != ie; ++ii) {
80       if (Instructions[ii].Address < *spi ||
81           Instructions[ii].Address >= BlockEnd)
82         break;
83       BB.addInst(Instructions[ii]);
84     }
85     f.addBlock(*spi, BB);
86   }
87
88   // Calculate successors of each block.
89   for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i) {
90     MCBasicBlock &BB = i->second;
91     if (BB.getInsts().empty()) continue;
92     const MCDecodedInst &Inst = BB.getInsts().back();
93     const MCInstrDesc &Desc = InstrInfo->get(Inst.Inst.getOpcode());
94
95     if (Desc.isBranch()) {
96       // PCRel branch, we know the destination.
97       if (Desc.OpInfo[0].OperandType == MCOI::OPERAND_PCREL) {
98         int64_t Imm = Inst.Inst.getOperand(0).getImm();
99         if (Imm != 0)
100           BB.addSucc(&f.getBlockAtAddress(Inst.Address+Inst.Size+Imm));
101         // Conditional branches can also fall through to the next block.
102         if (Desc.isConditionalBranch() && llvm::next(i) != e)
103           BB.addSucc(&llvm::next(i)->second);
104       } else {
105         // Indirect branch. Bail and add all blocks of the function as a
106         // successor.
107         for (MCFunction::iterator i = f.begin(), e = f.end(); i != e; ++i)
108           BB.addSucc(&i->second);
109       }
110     } else {
111       // No branch. Fall through to the next block.
112       if (!Desc.isReturn() && llvm::next(i) != e)
113         BB.addSucc(&llvm::next(i)->second);
114     }
115   }
116
117   return f;
118 }