[Polly] Avoid "using namespace llvm" in public headers. NFC.

"using namespace" pollutes the namespace of every file that includes
such a header and universally considered a bad thing. Even the variant

    namespace polly {
      using namespace llvm;
    }

(previously used by LoopGenerators.h) imports more symbols than the file
is in control of. The header may include a fixed set of files from LLVM,
but the header itself may by be included together with other headers
from LLVM. For instance, LLVM's MemorySSA.h and Polly's ScopInfo.h both
declare a class 'MemoryAccess' which may conflict.

Instead of prefixing everything in Polly's header files, this patch adds
'using' statements to import only the symbols that are actually
referenced in Polly. This approach is also used by MLIR to import
commonly used symbols into the mlir namespace.

This patch also puts the symbols declared in IslNodeBuilder.h into the
Polly namespace to also be able to use the imported symbols.

GitOrigin-RevId: 91ca9adc9edfba164b579d02c5fe0a7a24cfdd4e
diff --git a/include/polly/CodeGen/BlockGenerators.h b/include/polly/CodeGen/BlockGenerators.h
index f2c52c8..f02524a 100644
--- a/include/polly/CodeGen/BlockGenerators.h
+++ b/include/polly/CodeGen/BlockGenerators.h
@@ -21,7 +21,32 @@
 #include "isl/isl-noexceptions.h"
 
 namespace polly {
-using namespace llvm;
+using llvm::AllocaInst;
+using llvm::ArrayRef;
+using llvm::AssertingVH;
+using llvm::BasicBlock;
+using llvm::BinaryOperator;
+using llvm::CmpInst;
+using llvm::DataLayout;
+using llvm::DenseMap;
+using llvm::DominatorTree;
+using llvm::Function;
+using llvm::Instruction;
+using llvm::LoadInst;
+using llvm::Loop;
+using llvm::LoopInfo;
+using llvm::LoopToScevMapT;
+using llvm::MapVector;
+using llvm::PHINode;
+using llvm::ScalarEvolution;
+using llvm::SetVector;
+using llvm::SmallVector;
+using llvm::StoreInst;
+using llvm::StringRef;
+using llvm::Type;
+using llvm::UnaryInstruction;
+using llvm::Value;
+
 class MemoryAccess;
 class ScopArrayInfo;
 class IslExprBuilder;
diff --git a/include/polly/CodeGen/IslAst.h b/include/polly/CodeGen/IslAst.h
index 1a842b8..fc2a5e4 100644
--- a/include/polly/CodeGen/IslAst.h
+++ b/include/polly/CodeGen/IslAst.h
@@ -27,6 +27,7 @@
 #include "isl/ctx.h"
 
 namespace polly {
+using llvm::SmallPtrSet;
 
 struct Dependences;
 
diff --git a/include/polly/CodeGen/IslNodeBuilder.h b/include/polly/CodeGen/IslNodeBuilder.h
index ac28af2..3177762 100644
--- a/include/polly/CodeGen/IslNodeBuilder.h
+++ b/include/polly/CodeGen/IslNodeBuilder.h
@@ -23,13 +23,11 @@
 #include "isl/ctx.h"
 #include "isl/isl-noexceptions.h"
 
-using namespace llvm;
-using namespace polly;
-
 namespace polly {
+using llvm::LoopInfo;
+using llvm::SmallSet;
 
 struct InvariantEquivClassTy;
-} // namespace polly
 
 struct SubtreeReferences {
   LoopInfo &LI;
@@ -429,4 +427,6 @@
   Value *materializeNonScopLoopInductionVariable(const Loop *L);
 };
 
+} // namespace polly
+
 #endif // POLLY_ISLNODEBUILDER_H
diff --git a/include/polly/CodeGen/LoopGenerators.h b/include/polly/CodeGen/LoopGenerators.h
index 09a0424..bb73f22 100644
--- a/include/polly/CodeGen/LoopGenerators.h
+++ b/include/polly/CodeGen/LoopGenerators.h
@@ -18,7 +18,17 @@
 #include "llvm/ADT/SetVector.h"
 
 namespace polly {
-using namespace llvm;
+using llvm::AllocaInst;
+using llvm::BasicBlock;
+using llvm::DataLayout;
+using llvm::DominatorTree;
+using llvm::Function;
+using llvm::ICmpInst;
+using llvm::LoopInfo;
+using llvm::Module;
+using llvm::SetVector;
+using llvm::Type;
+using llvm::Value;
 
 /// General scheduling types of parallel OpenMP for loops.
 /// Initialization values taken from OpenMP's enum in kmp.h: sched_type.
diff --git a/include/polly/CodeGen/LoopGeneratorsGOMP.h b/include/polly/CodeGen/LoopGeneratorsGOMP.h
index b3ff982..1f47d38 100644
--- a/include/polly/CodeGen/LoopGeneratorsGOMP.h
+++ b/include/polly/CodeGen/LoopGeneratorsGOMP.h
@@ -19,7 +19,6 @@
 #include "llvm/ADT/SetVector.h"
 
 namespace polly {
-using namespace llvm;
 
 /// This ParallelLoopGenerator subclass handles the generation of parallelized
 /// code, utilizing the GNU OpenMP library.
diff --git a/include/polly/CodeGen/LoopGeneratorsKMP.h b/include/polly/CodeGen/LoopGeneratorsKMP.h
index 470df60..c4921ac 100644
--- a/include/polly/CodeGen/LoopGeneratorsKMP.h
+++ b/include/polly/CodeGen/LoopGeneratorsKMP.h
@@ -19,7 +19,8 @@
 #include "llvm/ADT/SetVector.h"
 
 namespace polly {
-using namespace llvm;
+using llvm::GlobalValue;
+using llvm::GlobalVariable;
 
 /// This ParallelLoopGenerator subclass handles the generation of parallelized
 /// code, utilizing the LLVM OpenMP library.
diff --git a/include/polly/DependenceInfo.h b/include/polly/DependenceInfo.h
index d66be81..a8b1119 100644
--- a/include/polly/DependenceInfo.h
+++ b/include/polly/DependenceInfo.h
@@ -26,8 +26,6 @@
 #include "isl/ctx.h"
 #include "isl/isl-noexceptions.h"
 
-using namespace llvm;
-
 namespace polly {
 
 /// The accumulated dependence information for a SCoP.
diff --git a/include/polly/ForwardOpTree.h b/include/polly/ForwardOpTree.h
index dfa11be..72c77c3 100644
--- a/include/polly/ForwardOpTree.h
+++ b/include/polly/ForwardOpTree.h
@@ -22,7 +22,7 @@
 } // namespace llvm
 
 namespace polly {
-Pass *createForwardOpTreeWrapperPass();
+llvm::Pass *createForwardOpTreeWrapperPass();
 
 struct ForwardOpTreePass : llvm::PassInfoMixin<ForwardOpTreePass> {
   ForwardOpTreePass() {}
diff --git a/include/polly/ScopBuilder.h b/include/polly/ScopBuilder.h
index 7bed07c..7c3a944 100644
--- a/include/polly/ScopBuilder.h
+++ b/include/polly/ScopBuilder.h
@@ -22,6 +22,7 @@
 #include "llvm/ADT/SetVector.h"
 
 namespace polly {
+using llvm::SmallSetVector;
 
 class ScopDetection;
 
diff --git a/include/polly/ScopDetection.h b/include/polly/ScopDetection.h
index f28ab49..835dd13 100644
--- a/include/polly/ScopDetection.h
+++ b/include/polly/ScopDetection.h
@@ -54,8 +54,6 @@
 #include "llvm/Pass.h"
 #include <set>
 
-using namespace llvm;
-
 namespace llvm {
 class AAResults;
 
@@ -63,6 +61,32 @@
 } // namespace llvm
 
 namespace polly {
+using llvm::AAResults;
+using llvm::AliasSetTracker;
+using llvm::AnalysisInfoMixin;
+using llvm::AnalysisKey;
+using llvm::AnalysisUsage;
+using llvm::BranchInst;
+using llvm::CallInst;
+using llvm::DenseMap;
+using llvm::DominatorTree;
+using llvm::Function;
+using llvm::FunctionAnalysisManager;
+using llvm::FunctionPass;
+using llvm::IntrinsicInst;
+using llvm::LoopInfo;
+using llvm::Module;
+using llvm::OptimizationRemarkEmitter;
+using llvm::PassInfoMixin;
+using llvm::PreservedAnalyses;
+using llvm::RegionInfo;
+using llvm::ScalarEvolution;
+using llvm::SCEVUnknown;
+using llvm::SetVector;
+using llvm::SmallSetVector;
+using llvm::SmallVectorImpl;
+using llvm::StringRef;
+using llvm::SwitchInst;
 
 using ParamSetType = std::set<const SCEV *>;
 
@@ -136,7 +160,7 @@
     ///
     /// This set contains all base pointers and the locations where they are
     /// used for memory accesses that can not be detected as affine accesses.
-    SetVector<std::pair<const SCEVUnknown *, Loop *>> NonAffineAccesses;
+    llvm::SetVector<std::pair<const SCEVUnknown *, Loop *>> NonAffineAccesses;
     BaseToElSize ElementSize;
 
     /// The region has at least one load instruction.
diff --git a/include/polly/ScopDetectionDiagnostic.h b/include/polly/ScopDetectionDiagnostic.h
index 6824615..200fb8f 100644
--- a/include/polly/ScopDetectionDiagnostic.h
+++ b/include/polly/ScopDetectionDiagnostic.h
@@ -25,10 +25,7 @@
 #include "llvm/IR/Instruction.h"
 #include <cstddef>
 
-using namespace llvm;
-
 namespace llvm {
-
 class AliasSet;
 class BasicBlock;
 class OptimizationRemarkEmitter;
@@ -37,6 +34,17 @@
 } // namespace llvm
 
 namespace polly {
+using llvm::AliasSet;
+using llvm::BasicBlock;
+using llvm::DebugLoc;
+using llvm::Instruction;
+using llvm::Loop;
+using llvm::OptimizationRemarkEmitter;
+using llvm::raw_ostream;
+using llvm::Region;
+using llvm::SCEV;
+using llvm::SmallVector;
+using llvm::Value;
 
 /// Type to hold region delimiters (entry & exit block).
 using BBPair = std::pair<BasicBlock *, BasicBlock *>;
diff --git a/include/polly/ScopInfo.h b/include/polly/ScopInfo.h
index dbebfb2..6ac029c 100644
--- a/include/polly/ScopInfo.h
+++ b/include/polly/ScopInfo.h
@@ -35,14 +35,42 @@
 #include <cstddef>
 #include <forward_list>
 
-using namespace llvm;
-
 namespace llvm {
 void initializeScopInfoRegionPassPass(PassRegistry &);
 void initializeScopInfoWrapperPassPass(PassRegistry &);
 } // end namespace llvm
 
 namespace polly {
+using llvm::AnalysisInfoMixin;
+using llvm::ArrayRef;
+using llvm::AssertingVH;
+using llvm::AssumptionCache;
+using llvm::cast;
+using llvm::DataLayout;
+using llvm::DenseMap;
+using llvm::DenseSet;
+using llvm::function_ref;
+using llvm::isa;
+using llvm::iterator_range;
+using llvm::LoadInst;
+using llvm::make_range;
+using llvm::MapVector;
+using llvm::MemIntrinsic;
+using llvm::Optional;
+using llvm::PassInfoMixin;
+using llvm::PHINode;
+using llvm::RegionNode;
+using llvm::RegionPass;
+using llvm::RGPassManager;
+using llvm::SetVector;
+using llvm::SmallPtrSetImpl;
+using llvm::SmallVector;
+using llvm::SmallVectorImpl;
+using llvm::StringMap;
+using llvm::Type;
+using llvm::Use;
+using llvm::Value;
+using llvm::ValueToValueMap;
 
 class MemoryAccess;
 
@@ -1212,7 +1240,7 @@
   /// The memory accesses of this statement.
   ///
   /// The only side effects of a statement are its memory accesses.
-  using MemoryAccessVec = SmallVector<MemoryAccess *, 8>;
+  using MemoryAccessVec = llvm::SmallVector<MemoryAccess *, 8>;
   MemoryAccessVec MemAccs;
 
   /// Mapping from instructions to (scalar) memory accesses.
diff --git a/include/polly/ScopPass.h b/include/polly/ScopPass.h
index e091a39..ccd6da1 100644
--- a/include/polly/ScopPass.h
+++ b/include/polly/ScopPass.h
@@ -24,9 +24,20 @@
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/PassManagerImpl.h"
 
-using namespace llvm;
-
 namespace polly {
+using llvm::AllAnalysesOn;
+using llvm::AnalysisManager;
+using llvm::DominatorTreeAnalysis;
+using llvm::InnerAnalysisManagerProxy;
+using llvm::LoopAnalysis;
+using llvm::OuterAnalysisManagerProxy;
+using llvm::PassManager;
+using llvm::RegionInfoAnalysis;
+using llvm::ScalarEvolutionAnalysis;
+using llvm::SmallPriorityWorklist;
+using llvm::TargetIRAnalysis;
+using llvm::TargetTransformInfo;
+
 class Scop;
 class SPMUpdater;
 struct ScopStandardAnalysisResults;
diff --git a/include/polly/Support/VirtualInstruction.h b/include/polly/Support/VirtualInstruction.h
index 90bf04f..4faf66e 100644
--- a/include/polly/Support/VirtualInstruction.h
+++ b/include/polly/Support/VirtualInstruction.h
@@ -17,6 +17,7 @@
 #include "polly/ScopInfo.h"
 
 namespace polly {
+using llvm::User;
 
 /// Determine the nature of a value's use within a statement.
 ///
diff --git a/lib/CodeGen/IslNodeBuilder.cpp b/lib/CodeGen/IslNodeBuilder.cpp
index a329661..688618e 100644
--- a/lib/CodeGen/IslNodeBuilder.cpp
+++ b/lib/CodeGen/IslNodeBuilder.cpp
@@ -225,8 +225,8 @@
   return 0;
 }
 
-void addReferencesFromStmt(const ScopStmt *Stmt, void *UserPtr,
-                           bool CreateScalarRefs) {
+void polly::addReferencesFromStmt(const ScopStmt *Stmt, void *UserPtr,
+                                  bool CreateScalarRefs) {
   auto &References = *static_cast<struct SubtreeReferences *>(UserPtr);
 
   if (Stmt->isBlockStmt())
diff --git a/lib/CodeGen/ManagedMemoryRewrite.cpp b/lib/CodeGen/ManagedMemoryRewrite.cpp
index 72ad026..64bfa8d 100644
--- a/lib/CodeGen/ManagedMemoryRewrite.cpp
+++ b/lib/CodeGen/ManagedMemoryRewrite.cpp
@@ -26,6 +26,7 @@
 #include "llvm/InitializePasses.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
 
+using namespace llvm;
 using namespace polly;
 
 static cl::opt<bool> RewriteAllocas(
diff --git a/lib/Transform/ScopInliner.cpp b/lib/Transform/ScopInliner.cpp
index 3d0737f..83c3b8d 100644
--- a/lib/Transform/ScopInliner.cpp
+++ b/lib/Transform/ScopInliner.cpp
@@ -23,7 +23,9 @@
 
 #define DEBUG_TYPE "polly-scop-inliner"
 
+using namespace llvm;
 using namespace polly;
+
 extern bool polly::PollyAllowFullFunction;
 
 namespace {