From 55cfc6f78e567dec35665892da11db4e64cdd360 Mon Sep 17 00:00:00 2001 From: Vsevolod Livinskiy Date: Fri, 20 Mar 2015 08:48:24 +0300 Subject: [PATCH] Fix for trunk revision #232397 --- cbackend.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 5 deletions(-) diff --git a/cbackend.cpp b/cbackend.cpp index b641d5ad..a90f251b 100644 --- a/cbackend.cpp +++ b/cbackend.cpp @@ -65,7 +65,11 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/Analysis/ConstantsScanner.h" +#if defined(LLVM_3_4) + #include "llvm/Support/InstIterator.h" +#else + #include "llvm/IR/InstIterator.h" +#endif #if defined(LLVM_3_2) || defined(LLVM_3_3) || defined(LLVM_3_4) || defined(LLVM_3_5) #include "llvm/Analysis/FindUsedTypes.h" #endif @@ -128,6 +132,72 @@ #undef setjmp #define snprintf _snprintf #endif +/////////////////////////////////////////////////////////////////////////////// +// This part of code was in LLVM's ConstantsScanner.h, +// but it was removed in revision #232397 + +namespace constant_scanner { +class constant_iterator : public std::iterator { + llvm::const_inst_iterator InstI; // Method instruction iterator + unsigned OpIdx; // Operand index + + bool isAtConstant() const { + assert(!InstI.atEnd() && OpIdx < InstI->getNumOperands() && + "isAtConstant called with invalid arguments!"); + return llvm::isa(InstI->getOperand(OpIdx)); + } + +public: + constant_iterator(const llvm::Function *F) : InstI(llvm::inst_begin(F)), OpIdx(0) { + // Advance to first constant... if we are not already at constant or end + if (InstI != llvm::inst_end(F) && // InstI is valid? + (InstI->getNumOperands() == 0 || !isAtConstant())) // Not at constant? + operator++(); + } + + constant_iterator(const llvm::Function *F, bool) // end ctor + : InstI(llvm::inst_end(F)), + OpIdx(0) {} + + bool operator==(const constant_iterator &x) const { + return OpIdx == x.OpIdx && InstI == x.InstI; + } + bool operator!=(const constant_iterator &x) const { return !(*this == x); } + + pointer operator*() const { + assert(isAtConstant() && "Dereferenced an iterator at the end!"); + return llvm::cast(InstI->getOperand(OpIdx)); + } + + constant_iterator &operator++() { // Preincrement implementation + ++OpIdx; + do { + unsigned NumOperands = InstI->getNumOperands(); + while (OpIdx < NumOperands && !isAtConstant()) { + ++OpIdx; + } + + if (OpIdx < NumOperands) return *this; // Found a constant! + ++InstI; + OpIdx = 0; + } while (!InstI.atEnd()); + + return *this; // At the end of the method + } +}; + +inline constant_iterator constant_begin(const llvm::Function *F) { + return constant_iterator(F); +} + +inline constant_iterator constant_end(const llvm::Function *F) { + return constant_iterator(F, true); +} + +} + +/////////////////////////////////////////////////////////////////////////////// // FIXME: namespace { @@ -2687,8 +2757,8 @@ void CWriter::printFloatingPointConstants(llvm::Function &F) { // the precision of the printed form, unless the printed form preserves // precision. // - for (llvm::constant_iterator I = constant_begin(&F), E = constant_end(&F); - I != E; ++I) + for (constant_scanner::constant_iterator I = constant_scanner::constant_begin(&F), + E = constant_scanner::constant_end(&F); I != E; ++I) printFloatingPointConstants(*I); Out << '\n'; @@ -2754,8 +2824,8 @@ void CWriter::printFloatingPointConstants(const llvm::Constant *C) { // loads to get their values, rather than tediously inserting the // individual values into the vector. void CWriter::printVectorConstants(llvm::Function &F) { - for (llvm::constant_iterator I = constant_begin(&F), E = constant_end(&F); - I != E; ++I) { + for (constant_scanner::constant_iterator I = constant_scanner::constant_begin(&F), + E = constant_scanner::constant_end(&F); I != E; ++I) { const llvm::ConstantDataVector *CDV = llvm::dyn_cast(*I); if (CDV == NULL) continue;