1 //===-- DiffConsumer.cpp - Difference Consumer ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This files implements the the LLVM difference Consumer
12 //===----------------------------------------------------------------------===//
14 #include "DiffConsumer.h"
16 #include "llvm/Module.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Support/ErrorHandling.h"
22 static void ComputeNumbering(Function *F, DenseMap<Value*,unsigned> &Numbering){
25 // Arguments get the first numbers.
26 for (Function::arg_iterator
27 AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI)
29 Numbering[&*AI] = IN++;
31 // Walk the basic blocks in order.
32 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) {
34 Numbering[&*FI] = IN++;
36 // Walk the instructions in order.
37 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
38 // void instructions don't get numbers.
39 if (!BI->hasName() && !BI->getType()->isVoidTy())
40 Numbering[&*BI] = IN++;
43 assert(!Numbering.empty() && "asked for numbering but numbering was no-op");
47 void Consumer::anchor() { }
49 void DiffConsumer::printValue(Value *V, bool isL) {
51 out << (isa<GlobalValue>(V) ? '@' : '%') << V->getName();
54 if (V->getType()->isVoidTy()) {
55 if (isa<StoreInst>(V)) {
57 printValue(cast<StoreInst>(V)->getPointerOperand(), isL);
58 } else if (isa<CallInst>(V)) {
60 printValue(cast<CallInst>(V)->getCalledValue(), isL);
61 } else if (isa<InvokeInst>(V)) {
63 printValue(cast<InvokeInst>(V)->getCalledValue(), isL);
69 if (isa<Constant>(V)) {
74 unsigned N = contexts.size();
77 DiffContext &ctxt = contexts[N];
78 if (!ctxt.IsFunction) continue;
80 if (ctxt.LNumbering.empty())
81 ComputeNumbering(cast<Function>(ctxt.L), ctxt.LNumbering);
82 out << '%' << ctxt.LNumbering[V];
85 if (ctxt.RNumbering.empty())
86 ComputeNumbering(cast<Function>(ctxt.R), ctxt.RNumbering);
87 out << '%' << ctxt.RNumbering[V];
95 void DiffConsumer::header() {
96 if (contexts.empty()) return;
97 for (SmallVectorImpl<DiffContext>::iterator
98 I = contexts.begin(), E = contexts.end(); I != E; ++I) {
99 if (I->Differences) continue;
100 if (isa<Function>(I->L)) {
101 // Extra newline between functions.
102 if (Differences) out << "\n";
104 Function *L = cast<Function>(I->L);
105 Function *R = cast<Function>(I->R);
106 if (L->getName() != R->getName())
107 out << "in function " << L->getName()
108 << " / " << R->getName() << ":\n";
110 out << "in function " << L->getName() << ":\n";
111 } else if (isa<BasicBlock>(I->L)) {
112 BasicBlock *L = cast<BasicBlock>(I->L);
113 BasicBlock *R = cast<BasicBlock>(I->R);
114 if (L->hasName() && R->hasName() && L->getName() == R->getName())
115 out << " in block %" << L->getName() << ":\n";
120 printValue(R, false);
123 } else if (isa<Instruction>(I->L)) {
124 out << " in instruction ";
125 printValue(I->L, true);
127 printValue(I->R, false);
131 I->Differences = true;
135 void DiffConsumer::indent() {
137 while (N--) out << ' ';
140 bool DiffConsumer::hadDifferences() const {
144 void DiffConsumer::enterContext(Value *L, Value *R) {
145 contexts.push_back(DiffContext(L, R));
149 void DiffConsumer::exitContext() {
150 Differences |= contexts.back().Differences;
155 void DiffConsumer::log(StringRef text) {
161 void DiffConsumer::logf(const LogBuilder &Log) {
167 StringRef format = Log.getFormat();
169 size_t percent = format.find('%');
170 if (percent == StringRef::npos) {
174 assert(format[percent] == '%');
176 if (percent > 0) out << format.substr(0, percent);
178 switch (format[percent+1]) {
179 case '%': out << '%'; break;
180 case 'l': printValue(Log.getArgument(arg++), true); break;
181 case 'r': printValue(Log.getArgument(arg++), false); break;
182 default: llvm_unreachable("unknown format character");
185 format = format.substr(percent+2);
191 void DiffConsumer::logd(const DiffLogBuilder &Log) {
194 for (unsigned I = 0, E = Log.getNumLines(); I != E; ++I) {
196 switch (Log.getLineKind(I)) {
199 Log.getLeft(I)->dump();
200 //printValue(Log.getLeft(I), true);
204 Log.getLeft(I)->dump();
205 //printValue(Log.getLeft(I), true);
209 Log.getRight(I)->dump();
210 //printValue(Log.getRight(I), false);