diff --git a/llvm/tools/llvm-diff/DiffConsumer.h b/llvm/tools/llvm-diff/DiffConsumer.h --- a/llvm/tools/llvm-diff/DiffConsumer.h +++ b/llvm/tools/llvm-diff/DiffConsumer.h @@ -34,7 +34,7 @@ /// Right are IR "containers" of some sort which are being /// considered for structural equivalence: global variables, /// functions, blocks, instructions, etc. - virtual void enterContext(Value *Left, Value *Right) = 0; + virtual void enterContext(const Value *Left, const Value *Right) = 0; /// Record that a local context has been exited. virtual void exitContext() = 0; @@ -55,14 +55,14 @@ class DiffConsumer : public Consumer { private: struct DiffContext { - DiffContext(Value *L, Value *R) - : L(L), R(R), Differences(false), IsFunction(isa(L)) {} - Value *L; - Value *R; + DiffContext(const Value *L, const Value *R) + : L(L), R(R), Differences(false), IsFunction(isa(L)) {} + const Value *L; + const Value *R; bool Differences; bool IsFunction; - DenseMap LNumbering; - DenseMap RNumbering; + DenseMap LNumbering; + DenseMap RNumbering; }; raw_ostream &out; @@ -70,7 +70,7 @@ bool Differences; unsigned Indent; - void printValue(Value *V, bool isL); + void printValue(const Value *V, bool isL); void header(); void indent(); @@ -79,7 +79,7 @@ : out(errs()), Differences(false), Indent(0) {} bool hadDifferences() const; - void enterContext(Value *L, Value *R) override; + void enterContext(const Value *L, const Value *R) override; void exitContext() override; void log(StringRef text) override; void logf(const LogBuilder &Log) override; diff --git a/llvm/tools/llvm-diff/DiffConsumer.cpp b/llvm/tools/llvm-diff/DiffConsumer.cpp --- a/llvm/tools/llvm-diff/DiffConsumer.cpp +++ b/llvm/tools/llvm-diff/DiffConsumer.cpp @@ -17,34 +17,33 @@ using namespace llvm; -static void ComputeNumbering(Function *F, DenseMap &Numbering){ +static void ComputeNumbering(const Function *F, + DenseMap &Numbering) { unsigned IN = 0; // Arguments get the first numbers. - for (Function::arg_iterator - AI = F->arg_begin(), AE = F->arg_end(); AI != AE; ++AI) - if (!AI->hasName()) - Numbering[&*AI] = IN++; + for (const auto &Arg : F->args()) + if (!Arg.hasName()) + Numbering[&Arg] = IN++; // Walk the basic blocks in order. - for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) { - if (!FI->hasName()) - Numbering[&*FI] = IN++; + for (const auto &Func : *F) { + if (!Func.hasName()) + Numbering[&Func] = IN++; // Walk the instructions in order. - for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) + for (const auto &BB : Func) // void instructions don't get numbers. - if (!BI->hasName() && !BI->getType()->isVoidTy()) - Numbering[&*BI] = IN++; + if (!BB.hasName() && !BB.getType()->isVoidTy()) + Numbering[&BB] = IN++; } assert(!Numbering.empty() && "asked for numbering but numbering was no-op"); } - void Consumer::anchor() { } -void DiffConsumer::printValue(Value *V, bool isL) { +void DiffConsumer::printValue(const Value *V, bool isL) { if (V->hasName()) { out << (isa(V) ? '@' : '%') << V->getName(); return; @@ -99,16 +98,16 @@ // Extra newline between functions. if (Differences) out << "\n"; - Function *L = cast(I->L); - Function *R = cast(I->R); + const Function *L = cast(I->L); + const Function *R = cast(I->R); if (L->getName() != R->getName()) out << "in function " << L->getName() << " / " << R->getName() << ":\n"; else out << "in function " << L->getName() << ":\n"; } else if (isa(I->L)) { - BasicBlock *L = cast(I->L); - BasicBlock *R = cast(I->R); + const BasicBlock *L = cast(I->L); + const BasicBlock *R = cast(I->R); if (L->hasName() && R->hasName() && L->getName() == R->getName()) out << " in block %" << L->getName() << ":\n"; else { @@ -139,7 +138,7 @@ return Differences; } -void DiffConsumer::enterContext(Value *L, Value *R) { +void DiffConsumer::enterContext(const Value *L, const Value *R) { contexts.push_back(DiffContext(L, R)); Indent += 2; } diff --git a/llvm/tools/llvm-diff/DiffLog.h b/llvm/tools/llvm-diff/DiffLog.h --- a/llvm/tools/llvm-diff/DiffLog.h +++ b/llvm/tools/llvm-diff/DiffLog.h @@ -34,7 +34,7 @@ /// might be initializing this format. StringRef Format; - SmallVector Arguments; + SmallVector Arguments; public: LogBuilder(Consumer &c, StringRef Format) : consumer(&c), Format(Format) {} @@ -44,7 +44,7 @@ L.consumer = nullptr; } - LogBuilder &operator<<(Value *V) { + LogBuilder &operator<<(const Value *V) { Arguments.push_back(V); return *this; } @@ -53,12 +53,12 @@ StringRef getFormat() const; unsigned getNumArguments() const; - Value *getArgument(unsigned I) const; + const Value *getArgument(unsigned I) const; }; /// A temporary-object class for building up diff messages. class DiffLogBuilder { - typedef std::pair DiffRecord; + typedef std::pair DiffRecord; SmallVector Diff; Consumer &consumer; @@ -67,15 +67,15 @@ DiffLogBuilder(Consumer &c) : consumer(c) {} ~DiffLogBuilder(); - void addMatch(Instruction *L, Instruction *R); + void addMatch(const Instruction *L, const Instruction *R); // HACK: VS 2010 has a bug in the stdlib that requires this. - void addLeft(Instruction *L); - void addRight(Instruction *R); + void addLeft(const Instruction *L); + void addRight(const Instruction *R); unsigned getNumLines() const; DiffChange getLineKind(unsigned I) const; - Instruction *getLeft(unsigned I) const; - Instruction *getRight(unsigned I) const; + const Instruction *getLeft(unsigned I) const; + const Instruction *getRight(unsigned I) const; }; } diff --git a/llvm/tools/llvm-diff/DiffLog.cpp b/llvm/tools/llvm-diff/DiffLog.cpp --- a/llvm/tools/llvm-diff/DiffLog.cpp +++ b/llvm/tools/llvm-diff/DiffLog.cpp @@ -24,18 +24,18 @@ StringRef LogBuilder::getFormat() const { return Format; } unsigned LogBuilder::getNumArguments() const { return Arguments.size(); } -Value *LogBuilder::getArgument(unsigned I) const { return Arguments[I]; } +const Value *LogBuilder::getArgument(unsigned I) const { return Arguments[I]; } DiffLogBuilder::~DiffLogBuilder() { consumer.logd(*this); } -void DiffLogBuilder::addMatch(Instruction *L, Instruction *R) { +void DiffLogBuilder::addMatch(const Instruction *L, const Instruction *R) { Diff.push_back(DiffRecord(L, R)); } -void DiffLogBuilder::addLeft(Instruction *L) { +void DiffLogBuilder::addLeft(const Instruction *L) { // HACK: VS 2010 has a bug in the stdlib that requires this. Diff.push_back(DiffRecord(L, DiffRecord::second_type(nullptr))); } -void DiffLogBuilder::addRight(Instruction *R) { +void DiffLogBuilder::addRight(const Instruction *R) { // HACK: VS 2010 has a bug in the stdlib that requires this. Diff.push_back(DiffRecord(DiffRecord::first_type(nullptr), R)); } @@ -46,5 +46,9 @@ return (Diff[I].first ? (Diff[I].second ? DC_match : DC_left) : DC_right); } -Instruction *DiffLogBuilder::getLeft(unsigned I) const { return Diff[I].first; } -Instruction *DiffLogBuilder::getRight(unsigned I) const { return Diff[I].second; } +const Instruction *DiffLogBuilder::getLeft(unsigned I) const { + return Diff[I].first; +} +const Instruction *DiffLogBuilder::getRight(unsigned I) const { + return Diff[I].second; +} diff --git a/llvm/tools/llvm-diff/DifferenceEngine.h b/llvm/tools/llvm-diff/DifferenceEngine.h --- a/llvm/tools/llvm-diff/DifferenceEngine.h +++ b/llvm/tools/llvm-diff/DifferenceEngine.h @@ -33,7 +33,8 @@ public: /// A RAII object for recording the current context. struct Context { - Context(DifferenceEngine &Engine, Value *L, Value *R) : Engine(Engine) { + Context(DifferenceEngine &Engine, const Value *L, const Value *R) + : Engine(Engine) { Engine.consumer.enterContext(L, R); } @@ -50,7 +51,7 @@ class Oracle { virtual void anchor(); public: - virtual bool operator()(Value *L, Value *R) = 0; + virtual bool operator()(const Value *L, const Value *R) = 0; protected: virtual ~Oracle() {} @@ -59,8 +60,8 @@ DifferenceEngine(Consumer &consumer) : consumer(consumer), globalValueOracle(nullptr) {} - void diff(Module *L, Module *R); - void diff(Function *L, Function *R); + void diff(const Module *L, const Module *R); + void diff(const Function *L, const Function *R); void log(StringRef text) { consumer.log(text); } @@ -78,7 +79,7 @@ } /// Determines whether two global values are equivalent. - bool equivalentAsOperands(GlobalValue *L, GlobalValue *R); + bool equivalentAsOperands(const GlobalValue *L, const GlobalValue *R); private: Consumer &consumer; diff --git a/llvm/tools/llvm-diff/DifferenceEngine.cpp b/llvm/tools/llvm-diff/DifferenceEngine.cpp --- a/llvm/tools/llvm-diff/DifferenceEngine.cpp +++ b/llvm/tools/llvm-diff/DifferenceEngine.cpp @@ -114,21 +114,22 @@ DifferenceEngine &Engine; /// The current mapping from old local values to new local values. - DenseMap Values; + DenseMap Values; /// The current mapping from old blocks to new blocks. - DenseMap Blocks; + DenseMap Blocks; - DenseSet > TentativeValues; + DenseSet> TentativeValues; - unsigned getUnprocPredCount(BasicBlock *Block) const { + unsigned getUnprocPredCount(const BasicBlock *Block) const { unsigned Count = 0; - for (pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; ++I) + for (const_pred_iterator I = pred_begin(Block), E = pred_end(Block); I != E; + ++I) if (!Blocks.count(*I)) Count++; return Count; } - typedef std::pair BlockPair; + typedef std::pair BlockPair; /// A type which sorts a priority queue by the number of unprocessed /// predecessor blocks it has remaining. @@ -138,7 +139,7 @@ const FunctionDifferenceEngine &fde; explicit QueueSorter(const FunctionDifferenceEngine &fde) : fde(fde) {} - bool operator()(const BlockPair &Old, const BlockPair &New) { + bool operator()(BlockPair &Old, BlockPair &New) { return fde.getUnprocPredCount(Old.first) < fde.getUnprocPredCount(New.first); } @@ -151,8 +152,8 @@ /// if they haven't already been processed. /// /// Returns true if there was a problem unifying them. - bool tryUnify(BasicBlock *L, BasicBlock *R) { - BasicBlock *&Ref = Blocks[L]; + bool tryUnify(const BasicBlock *L, const BasicBlock *R) { + const BasicBlock *&Ref = Blocks[L]; if (Ref) { if (Ref == R) return false; @@ -167,10 +168,10 @@ Queue.insert(BlockPair(L, R)); return false; } - + /// Unifies two instructions, given that they're known not to have /// structural differences. - void unify(Instruction *L, Instruction *R) { + void unify(const Instruction *L, const Instruction *R) { DifferenceEngine::Context C(Engine, L, R); bool Result = diff(L, R, true, true); @@ -187,15 +188,15 @@ } } - void diff(BasicBlock *L, BasicBlock *R) { + void diff(const BasicBlock *L, const BasicBlock *R) { DifferenceEngine::Context C(Engine, L, R); - BasicBlock::iterator LI = L->begin(), LE = L->end(); - BasicBlock::iterator RI = R->begin(); + BasicBlock::const_iterator LI = L->begin(), LE = L->end(); + BasicBlock::const_iterator RI = R->begin(); do { assert(LI != LE && RI != R->end()); - Instruction *LeftI = &*LI, *RightI = &*RI; + const Instruction *LeftI = &*LI, *RightI = &*RI; // If the instructions differ, start the more sophisticated diff // algorithm at the start of the block. @@ -219,10 +220,11 @@ unify(&*LI, &*RI); } - bool matchForBlockDiff(Instruction *L, Instruction *R); - void runBlockDiff(BasicBlock::iterator LI, BasicBlock::iterator RI); + bool matchForBlockDiff(const Instruction *L, const Instruction *R); + void runBlockDiff(BasicBlock::const_iterator LI, + BasicBlock::const_iterator RI); - bool diffCallSites(CallBase &L, CallBase &R, bool Complain) { + bool diffCallSites(const CallBase &L, const CallBase &R, bool Complain) { // FIXME: call attributes if (!equivalentAsOperands(L.getCalledOperand(), R.getCalledOperand())) { if (Complain) Engine.log("called functions differ"); @@ -242,7 +244,8 @@ return false; } - bool diff(Instruction *L, Instruction *R, bool Complain, bool TryUnify) { + bool diff(const Instruction *L, const Instruction *R, bool Complain, + bool TryUnify) { // FIXME: metadata (if Complain is set) // Different opcodes always imply different operations. @@ -273,8 +276,8 @@ // Terminators. } else if (isa(L)) { - InvokeInst &LI = cast(*L); - InvokeInst &RI = cast(*R); + const InvokeInst &LI = cast(*L); + const InvokeInst &RI = cast(*R); if (diffCallSites(LI, RI, Complain)) return true; @@ -285,8 +288,8 @@ return false; } else if (isa(L)) { - BranchInst *LI = cast(L); - BranchInst *RI = cast(R); + const BranchInst *LI = cast(L); + const BranchInst *RI = cast(R); if (LI->isConditional() != RI->isConditional()) { if (Complain) Engine.log("branch conditionality differs"); return true; @@ -303,8 +306,8 @@ return false; } else if (isa(L)) { - IndirectBrInst *LI = cast(L); - IndirectBrInst *RI = cast(R); + const IndirectBrInst *LI = cast(L); + const IndirectBrInst *RI = cast(R); if (LI->getNumDestinations() != RI->getNumDestinations()) { if (Complain) Engine.log("indirectbr # of destinations differ"); return true; @@ -323,8 +326,8 @@ return false; } else if (isa(L)) { - SwitchInst *LI = cast(L); - SwitchInst *RI = cast(R); + const SwitchInst *LI = cast(L); + const SwitchInst *RI = cast(R); if (!equivalentAsOperands(LI->getCondition(), RI->getCondition())) { if (Complain) Engine.log("switch conditions differ"); return true; @@ -333,13 +336,13 @@ bool Difference = false; - DenseMap LCases; + DenseMap LCases; for (auto Case : LI->cases()) LCases[Case.getCaseValue()] = Case.getCaseSuccessor(); for (auto Case : RI->cases()) { - ConstantInt *CaseValue = Case.getCaseValue(); - BasicBlock *LCase = LCases[CaseValue]; + const ConstantInt *CaseValue = Case.getCaseValue(); + const BasicBlock *LCase = LCases[CaseValue]; if (LCase) { if (TryUnify) tryUnify(LCase, Case.getCaseSuccessor()); @@ -351,8 +354,10 @@ } } if (!Difference) - for (DenseMap::iterator - I = LCases.begin(), E = LCases.end(); I != E; ++I) { + for (DenseMap::iterator + I = LCases.begin(), + E = LCases.end(); + I != E; ++I) { if (Complain) Engine.logf("left switch has extra case %l") << I->first; Difference = true; @@ -378,7 +383,7 @@ return false; } - bool equivalentAsOperands(Constant *L, Constant *R) { + bool equivalentAsOperands(const Constant *L, const Constant *R) { // Use equality as a preliminary filter. if (L == R) return true; @@ -409,8 +414,8 @@ // If L and R are ConstantVectors, compare each element if (isa(L)) { - ConstantVector *CVL = cast(L); - ConstantVector *CVR = cast(R); + const ConstantVector *CVL = cast(L); + const ConstantVector *CVR = cast(R); if (CVL->getType()->getNumElements() != CVR->getType()->getNumElements()) return false; for (unsigned i = 0; i < CVL->getType()->getNumElements(); i++) { @@ -423,7 +428,7 @@ return false; } - bool equivalentAsOperands(ConstantExpr *L, ConstantExpr *R) { + bool equivalentAsOperands(const ConstantExpr *L, const ConstantExpr *R) { if (L == R) return true; if (L->getOpcode() != R->getOpcode()) @@ -454,7 +459,7 @@ return true; } - bool equivalentAsOperands(Value *L, Value *R) { + bool equivalentAsOperands(const Value *L, const Value *R) { // Fall out if the values have different kind. // This possibly shouldn't take priority over oracles. if (L->getValueID() != R->getValueID()) @@ -486,14 +491,13 @@ FunctionDifferenceEngine(DifferenceEngine &Engine) : Engine(Engine), Queue(QueueSorter(*this_())) {} - void diff(Function *L, Function *R) { + void diff(const Function *L, const Function *R) { if (L->arg_size() != R->arg_size()) Engine.log("different argument counts"); // Map the arguments. - for (Function::arg_iterator - LI = L->arg_begin(), LE = L->arg_end(), - RI = R->arg_begin(), RE = R->arg_end(); + for (Function::const_arg_iterator LI = L->arg_begin(), LE = L->arg_end(), + RI = R->arg_begin(), RE = R->arg_end(); LI != LE && RI != RE; ++LI, ++RI) Values[&*LI] = &*RI; @@ -509,15 +513,15 @@ llvm::SmallVector Path; // actually of DifferenceEngine::DiffChange }; -bool FunctionDifferenceEngine::matchForBlockDiff(Instruction *L, - Instruction *R) { +bool FunctionDifferenceEngine::matchForBlockDiff(const Instruction *L, + const Instruction *R) { return !diff(L, R, false, false); } -void FunctionDifferenceEngine::runBlockDiff(BasicBlock::iterator LStart, - BasicBlock::iterator RStart) { - BasicBlock::iterator LE = LStart->getParent()->end(); - BasicBlock::iterator RE = RStart->getParent()->end(); +void FunctionDifferenceEngine::runBlockDiff(BasicBlock::const_iterator LStart, + BasicBlock::const_iterator RStart) { + BasicBlock::const_iterator LE = LStart->getParent()->end(); + BasicBlock::const_iterator RE = RStart->getParent()->end(); unsigned NL = std::distance(LStart, LE); @@ -540,14 +544,14 @@ Cur[I].Path.push_back(DC_left); } - for (BasicBlock::iterator RI = RStart; RI != RE; ++RI) { + for (BasicBlock::const_iterator RI = RStart; RI != RE; ++RI) { // Initialize the first row. Next[0] = Cur[0]; Next[0].Cost += RightCost; Next[0].Path.push_back(DC_right); unsigned Index = 1; - for (BasicBlock::iterator LI = LStart; LI != LE; ++LI, ++Index) { + for (BasicBlock::const_iterator LI = LStart; LI != LE; ++LI, ++Index) { if (matchForBlockDiff(&*LI, &*RI)) { Next[Index] = Cur[Index-1]; Next[Index].Cost += MatchCost; @@ -572,7 +576,7 @@ TentativeValues.clear(); SmallVectorImpl &Path = Cur[NL].Path; - BasicBlock::iterator LI = LStart, RI = RStart; + BasicBlock::const_iterator LI = LStart, RI = RStart; DiffLogBuilder Diff(Engine.getConsumer()); @@ -595,7 +599,7 @@ case DC_match: assert(LI != LE && RI != RE); { - Instruction *L = &*LI, *R = &*RI; + const Instruction *L = &*LI, *R = &*RI; unify(L, R); Diff.addMatch(L, R); } @@ -628,16 +632,16 @@ // If the terminators have different kinds, but one is an invoke and the // other is an unconditional branch immediately following a call, unify // the results and the destinations. - Instruction *LTerm = LStart->getParent()->getTerminator(); - Instruction *RTerm = RStart->getParent()->getTerminator(); + const Instruction *LTerm = LStart->getParent()->getTerminator(); + const Instruction *RTerm = RStart->getParent()->getTerminator(); if (isa(LTerm) && isa(RTerm)) { if (cast(LTerm)->isConditional()) return; - BasicBlock::iterator I = LTerm->getIterator(); + BasicBlock::const_iterator I = LTerm->getIterator(); if (I == LStart->getParent()->begin()) return; --I; if (!isa(*I)) return; - CallInst *LCall = cast(&*I); - InvokeInst *RInvoke = cast(RTerm); + const CallInst *LCall = cast(&*I); + const InvokeInst *RInvoke = cast(RTerm); if (!equivalentAsOperands(LCall->getCalledOperand(), RInvoke->getCalledOperand())) return; @@ -646,12 +650,12 @@ tryUnify(LTerm->getSuccessor(0), RInvoke->getNormalDest()); } else if (isa(LTerm) && isa(RTerm)) { if (cast(RTerm)->isConditional()) return; - BasicBlock::iterator I = RTerm->getIterator(); + BasicBlock::const_iterator I = RTerm->getIterator(); if (I == RStart->getParent()->begin()) return; --I; if (!isa(*I)) return; - CallInst *RCall = cast(I); - InvokeInst *LInvoke = cast(LTerm); + const CallInst *RCall = cast(I); + const InvokeInst *LInvoke = cast(LTerm); if (!equivalentAsOperands(LInvoke->getCalledOperand(), RCall->getCalledOperand())) return; @@ -660,12 +664,11 @@ tryUnify(LInvoke->getNormalDest(), RTerm->getSuccessor(0)); } } - } void DifferenceEngine::Oracle::anchor() { } -void DifferenceEngine::diff(Function *L, Function *R) { +void DifferenceEngine::diff(const Function *L, const Function *R) { Context C(*this, L, R); // FIXME: types @@ -683,15 +686,15 @@ FunctionDifferenceEngine(*this).diff(L, R); } -void DifferenceEngine::diff(Module *L, Module *R) { +void DifferenceEngine::diff(const Module *L, const Module *R) { StringSet<> LNames; - SmallVector, 20> Queue; + SmallVector, 20> Queue; unsigned LeftAnonCount = 0; unsigned RightAnonCount = 0; - for (Module::iterator I = L->begin(), E = L->end(); I != E; ++I) { - Function *LFn = &*I; + for (Module::const_iterator I = L->begin(), E = L->end(); I != E; ++I) { + const Function *LFn = &*I; StringRef Name = LFn->getName(); if (Name.empty()) { ++LeftAnonCount; @@ -706,8 +709,8 @@ logf("function %l exists only in left module") << LFn; } - for (Module::iterator I = R->begin(), E = R->end(); I != E; ++I) { - Function *RFn = &*I; + for (Module::const_iterator I = R->begin(), E = R->end(); I != E; ++I) { + const Function *RFn = &*I; StringRef Name = RFn->getName(); if (Name.empty()) { ++RightAnonCount; @@ -718,7 +721,6 @@ logf("function %r exists only in right module") << RFn; } - if (LeftAnonCount != 0 || RightAnonCount != 0) { SmallString<32> Tmp; logf(("not comparing " + Twine(LeftAnonCount) + @@ -727,17 +729,20 @@ .toStringRef(Tmp)); } - for (SmallVectorImpl >::iterator - I = Queue.begin(), E = Queue.end(); I != E; ++I) + for (SmallVectorImpl>::iterator + I = Queue.begin(), + E = Queue.end(); + I != E; ++I) diff(I->first, I->second); } -bool DifferenceEngine::equivalentAsOperands(GlobalValue *L, GlobalValue *R) { +bool DifferenceEngine::equivalentAsOperands(const GlobalValue *L, + const GlobalValue *R) { if (globalValueOracle) return (*globalValueOracle)(L, R); if (isa(L) && isa(R)) { - GlobalVariable *GVL = cast(L); - GlobalVariable *GVR = cast(R); + const GlobalVariable *GVL = cast(L); + const GlobalVariable *GVR = cast(R); if (GVL->hasLocalLinkage() && GVL->hasUniqueInitializer() && GVR->hasLocalLinkage() && GVR->hasUniqueInitializer()) return GVL->getInitializer() == GVR->getInitializer();