6464
6565using namespace llvm ;
6666
67- namespace {
68-
6967#define DEBUG_TYPE " mergeicmps"
7068
69+ namespace {
70+
7171// A BCE atom "Binary Compare Expression Atom" represents an integer load
7272// that is a constant offset from a base value, e.g. `a` or `o.c` in the example
7373// at the top.
@@ -128,11 +128,12 @@ class BaseIdentifier {
128128 unsigned Order = 1 ;
129129 DenseMap<const Value*, int > BaseToIndex;
130130};
131+ } // namespace
131132
132133// If this value is a load from a constant offset w.r.t. a base address, and
133134// there are no other users of the load or address, returns the base address and
134135// the offset.
135- BCEAtom visitICmpLoadOperand (Value *const Val, BaseIdentifier &BaseId) {
136+ static BCEAtom visitICmpLoadOperand (Value *const Val, BaseIdentifier &BaseId) {
136137 auto *const LoadI = dyn_cast<LoadInst>(Val);
137138 if (!LoadI)
138139 return {};
@@ -175,6 +176,7 @@ BCEAtom visitICmpLoadOperand(Value *const Val, BaseIdentifier &BaseId) {
175176 return BCEAtom (GEP, LoadI, BaseId.getBaseId (Base), Offset);
176177}
177178
179+ namespace {
178180// A comparison between two BCE atoms, e.g. `a == o.a` in the example at the
179181// top.
180182// Note: the terminology is misleading: the comparison is symmetric, so there
@@ -239,6 +241,7 @@ class BCECmpBlock {
239241private:
240242 BCECmp Cmp;
241243};
244+ } // namespace
242245
243246bool BCECmpBlock::canSinkBCECmpInst (const Instruction *Inst,
244247 AliasAnalysis &AA) const {
@@ -302,9 +305,9 @@ bool BCECmpBlock::doesOtherWork() const {
302305
303306// Visit the given comparison. If this is a comparison between two valid
304307// BCE atoms, returns the comparison.
305- std::optional<BCECmp> visitICmp ( const ICmpInst * const CmpI,
306- const ICmpInst::Predicate ExpectedPredicate ,
307- BaseIdentifier &BaseId) {
308+ static std::optional<BCECmp>
309+ visitICmp ( const ICmpInst * const CmpI ,
310+ const ICmpInst::Predicate ExpectedPredicate, BaseIdentifier &BaseId) {
308311 // The comparison can only be used once:
309312 // - For intermediate blocks, as a branch condition.
310313 // - For the final block, as an incoming value for the Phi.
@@ -332,10 +335,9 @@ std::optional<BCECmp> visitICmp(const ICmpInst *const CmpI,
332335
333336// Visit the given comparison block. If this is a comparison between two valid
334337// BCE atoms, returns the comparison.
335- std::optional<BCECmpBlock> visitCmpBlock (Value *const Val,
336- BasicBlock *const Block,
337- const BasicBlock *const PhiBlock,
338- BaseIdentifier &BaseId) {
338+ static std::optional<BCECmpBlock>
339+ visitCmpBlock (Value *const Val, BasicBlock *const Block,
340+ const BasicBlock *const PhiBlock, BaseIdentifier &BaseId) {
339341 if (Block->empty ())
340342 return std::nullopt ;
341343 auto *const BranchI = dyn_cast<BranchInst>(Block->getTerminator ());
@@ -397,6 +399,7 @@ static inline void enqueueBlock(std::vector<BCECmpBlock> &Comparisons,
397399 Comparisons.push_back (std::move (Comparison));
398400}
399401
402+ namespace {
400403// A chain of comparisons.
401404class BCECmpChain {
402405public:
@@ -420,6 +423,7 @@ class BCECmpChain {
420423 // The original entry block (before sorting);
421424 BasicBlock *EntryBlock_;
422425};
426+ } // namespace
423427
424428static bool areContiguous (const BCECmpBlock &First, const BCECmpBlock &Second) {
425429 return First.Lhs ().BaseId == Second.Lhs ().BaseId &&
@@ -742,9 +746,8 @@ bool BCECmpChain::simplify(const TargetLibraryInfo &TLI, AliasAnalysis &AA,
742746 return true ;
743747}
744748
745- std::vector<BasicBlock *> getOrderedBlocks (PHINode &Phi,
746- BasicBlock *const LastBlock,
747- int NumBlocks) {
749+ static std::vector<BasicBlock *>
750+ getOrderedBlocks (PHINode &Phi, BasicBlock *const LastBlock, int NumBlocks) {
748751 // Walk up from the last block to find other blocks.
749752 std::vector<BasicBlock *> Blocks (NumBlocks);
750753 assert (LastBlock && " invalid last block" );
@@ -777,8 +780,8 @@ std::vector<BasicBlock *> getOrderedBlocks(PHINode &Phi,
777780 return Blocks;
778781}
779782
780- bool processPhi (PHINode &Phi, const TargetLibraryInfo &TLI, AliasAnalysis &AA ,
781- DomTreeUpdater &DTU) {
783+ static bool processPhi (PHINode &Phi, const TargetLibraryInfo &TLI,
784+ AliasAnalysis &AA, DomTreeUpdater &DTU) {
782785 LLVM_DEBUG (dbgs () << " processPhi()\n " );
783786 if (Phi.getNumIncomingValues () <= 1 ) {
784787 LLVM_DEBUG (dbgs () << " skip: only one incoming value in phi\n " );
@@ -874,6 +877,7 @@ static bool runImpl(Function &F, const TargetLibraryInfo &TLI,
874877 return MadeChange;
875878}
876879
880+ namespace {
877881class MergeICmpsLegacyPass : public FunctionPass {
878882public:
879883 static char ID;
0 commit comments