41
41
//
42
42
// ===----------------------------------------------------------------------===//
43
43
44
+ #include " llvm/Transforms/Scalar/MergeICmps.h"
44
45
#include " llvm/Analysis/DomTreeUpdater.h"
45
46
#include " llvm/Analysis/GlobalsModRef.h"
46
47
#include " llvm/Analysis/Loads.h"
@@ -214,19 +215,19 @@ class BCECmpBlock {
214
215
215
216
// Returns true if the non-BCE-cmp instructions can be separated from BCE-cmp
216
217
// instructions in the block.
217
- bool canSplit (AliasAnalysis * AA) const ;
218
+ bool canSplit (AliasAnalysis & AA) const ;
218
219
219
220
// Return true if this all the relevant instructions in the BCE-cmp-block can
220
221
// be sunk below this instruction. By doing this, we know we can separate the
221
222
// BCE-cmp-block instructions from the non-BCE-cmp-block instructions in the
222
223
// block.
223
224
bool canSinkBCECmpInst (const Instruction *, DenseSet<Instruction *> &,
224
- AliasAnalysis * AA) const ;
225
+ AliasAnalysis & AA) const ;
225
226
226
227
// We can separate the BCE-cmp-block instructions and the non-BCE-cmp-block
227
228
// instructions. Split the old block and move all non-BCE-cmp-insts into the
228
229
// new parent block.
229
- void split (BasicBlock *NewParent, AliasAnalysis * AA) const ;
230
+ void split (BasicBlock *NewParent, AliasAnalysis & AA) const ;
230
231
231
232
// The basic block where this comparison happens.
232
233
BasicBlock *BB = nullptr ;
@@ -245,7 +246,7 @@ class BCECmpBlock {
245
246
246
247
bool BCECmpBlock::canSinkBCECmpInst (const Instruction *Inst,
247
248
DenseSet<Instruction *> &BlockInsts,
248
- AliasAnalysis * AA) const {
249
+ AliasAnalysis & AA) const {
249
250
// If this instruction has side effects and its in middle of the BCE cmp block
250
251
// instructions, then bail for now.
251
252
if (Inst->mayHaveSideEffects ()) {
@@ -255,9 +256,9 @@ bool BCECmpBlock::canSinkBCECmpInst(const Instruction *Inst,
255
256
// Disallow stores that might alias the BCE operands
256
257
MemoryLocation LLoc = MemoryLocation::get (Lhs_.LoadI );
257
258
MemoryLocation RLoc = MemoryLocation::get (Rhs_.LoadI );
258
- if (isModSet (AA-> getModRefInfo (Inst, LLoc)) ||
259
- isModSet (AA-> getModRefInfo (Inst, RLoc)))
260
- return false ;
259
+ if (isModSet (AA. getModRefInfo (Inst, LLoc)) ||
260
+ isModSet (AA. getModRefInfo (Inst, RLoc)))
261
+ return false ;
261
262
}
262
263
// Make sure this instruction does not use any of the BCE cmp block
263
264
// instructions as operand.
@@ -268,7 +269,7 @@ bool BCECmpBlock::canSinkBCECmpInst(const Instruction *Inst,
268
269
return true ;
269
270
}
270
271
271
- void BCECmpBlock::split (BasicBlock *NewParent, AliasAnalysis * AA) const {
272
+ void BCECmpBlock::split (BasicBlock *NewParent, AliasAnalysis & AA) const {
272
273
DenseSet<Instruction *> BlockInsts (
273
274
{Lhs_.GEP , Rhs_.GEP , Lhs_.LoadI , Rhs_.LoadI , CmpI, BranchI});
274
275
llvm::SmallVector<Instruction *, 4 > OtherInsts;
@@ -288,7 +289,7 @@ void BCECmpBlock::split(BasicBlock *NewParent, AliasAnalysis *AA) const {
288
289
}
289
290
}
290
291
291
- bool BCECmpBlock::canSplit (AliasAnalysis * AA) const {
292
+ bool BCECmpBlock::canSplit (AliasAnalysis & AA) const {
292
293
DenseSet<Instruction *> BlockInsts (
293
294
{Lhs_.GEP , Rhs_.GEP , Lhs_.LoadI , Rhs_.LoadI , CmpI, BranchI});
294
295
for (Instruction &Inst : *BB) {
@@ -404,16 +405,16 @@ static inline void enqueueBlock(std::vector<BCECmpBlock> &Comparisons,
404
405
// A chain of comparisons.
405
406
class BCECmpChain {
406
407
public:
407
- BCECmpChain (const std::vector<BasicBlock *> &Blocks, PHINode &Phi,
408
- AliasAnalysis * AA);
408
+ BCECmpChain (const std::vector<BasicBlock *> &Blocks, PHINode &Phi,
409
+ AliasAnalysis & AA);
409
410
410
- int size () const { return Comparisons_.size (); }
411
+ int size () const { return Comparisons_.size (); }
411
412
412
413
#ifdef MERGEICMPS_DOT_ON
413
414
void dump () const ;
414
415
#endif // MERGEICMPS_DOT_ON
415
416
416
- bool simplify (const TargetLibraryInfo * const TLI, AliasAnalysis * AA,
417
+ bool simplify (const TargetLibraryInfo & TLI, AliasAnalysis & AA,
417
418
DomTreeUpdater &DTU);
418
419
419
420
private:
@@ -432,7 +433,7 @@ class BCECmpChain {
432
433
};
433
434
434
435
BCECmpChain::BCECmpChain (const std::vector<BasicBlock *> &Blocks, PHINode &Phi,
435
- AliasAnalysis * AA)
436
+ AliasAnalysis & AA)
436
437
: Phi_(Phi) {
437
438
assert (!Blocks.empty () && " a chain should have at least one block" );
438
439
// Now look inside blocks to check for BCE comparisons.
@@ -604,9 +605,8 @@ class MergedBlockName {
604
605
static BasicBlock *mergeComparisons (ArrayRef<BCECmpBlock> Comparisons,
605
606
BasicBlock *const InsertBefore,
606
607
BasicBlock *const NextCmpBlock,
607
- PHINode &Phi,
608
- const TargetLibraryInfo *const TLI,
609
- AliasAnalysis *AA, DomTreeUpdater &DTU) {
608
+ PHINode &Phi, const TargetLibraryInfo &TLI,
609
+ AliasAnalysis &AA, DomTreeUpdater &DTU) {
610
610
assert (!Comparisons.empty () && " merging zero comparisons" );
611
611
LLVMContext &Context = NextCmpBlock->getContext ();
612
612
const BCECmpBlock &FirstCmp = Comparisons[0 ];
@@ -652,7 +652,7 @@ static BasicBlock *mergeComparisons(ArrayRef<BCECmpBlock> Comparisons,
652
652
Value *const MemCmpCall = emitMemCmp (
653
653
Lhs, Rhs,
654
654
ConstantInt::get (DL.getIntPtrType (Context), TotalSizeBits / 8 ), Builder,
655
- DL, TLI);
655
+ DL, & TLI);
656
656
IsEqual = Builder.CreateICmpEQ (
657
657
MemCmpCall, ConstantInt::get (Type::getInt32Ty (Context), 0 ));
658
658
}
@@ -674,8 +674,8 @@ static BasicBlock *mergeComparisons(ArrayRef<BCECmpBlock> Comparisons,
674
674
return BB;
675
675
}
676
676
677
- bool BCECmpChain::simplify (const TargetLibraryInfo * const TLI,
678
- AliasAnalysis *AA, DomTreeUpdater &DTU) {
677
+ bool BCECmpChain::simplify (const TargetLibraryInfo & TLI, AliasAnalysis &AA ,
678
+ DomTreeUpdater &DTU) {
679
679
assert (Comparisons_.size () >= 2 && " simplifying trivial BCECmpChain" );
680
680
// First pass to check if there is at least one merge. If not, we don't do
681
681
// anything and we keep analysis passes intact.
@@ -694,9 +694,9 @@ bool BCECmpChain::simplify(const TargetLibraryInfo *const TLI,
694
694
695
695
// Effectively merge blocks. We go in the reverse direction from the phi block
696
696
// so that the next block is always available to branch to.
697
- const auto mergeRange = [this , TLI, AA, &DTU](int I, int Num,
698
- BasicBlock *InsertBefore,
699
- BasicBlock *Next) {
697
+ const auto mergeRange = [this , & TLI, & AA, &DTU](int I, int Num,
698
+ BasicBlock *InsertBefore,
699
+ BasicBlock *Next) {
700
700
return mergeComparisons (makeArrayRef (Comparisons_).slice (I, Num),
701
701
InsertBefore, Next, Phi_, TLI, AA, DTU);
702
702
};
@@ -790,8 +790,8 @@ std::vector<BasicBlock *> getOrderedBlocks(PHINode &Phi,
790
790
return Blocks;
791
791
}
792
792
793
- bool processPhi (PHINode &Phi, const TargetLibraryInfo * const TLI,
794
- AliasAnalysis *AA, DomTreeUpdater &DTU) {
793
+ bool processPhi (PHINode &Phi, const TargetLibraryInfo & TLI, AliasAnalysis &AA ,
794
+ DomTreeUpdater &DTU) {
795
795
LLVM_DEBUG (dbgs () << " processPhi()\n " );
796
796
if (Phi.getNumIncomingValues () <= 1 ) {
797
797
LLVM_DEBUG (dbgs () << " skip: only one incoming value in phi\n " );
@@ -859,12 +859,40 @@ bool processPhi(PHINode &Phi, const TargetLibraryInfo *const TLI,
859
859
return CmpChain.simplify (TLI, AA, DTU);
860
860
}
861
861
862
- class MergeICmps : public FunctionPass {
863
- public:
862
+ static bool runImpl (Function &F, const TargetLibraryInfo &TLI,
863
+ const TargetTransformInfo &TTI, AliasAnalysis &AA,
864
+ DominatorTree *DT) {
865
+ LLVM_DEBUG (dbgs () << " MergeICmpsLegacyPass: " << F.getName () << " \n " );
866
+
867
+ // We only try merging comparisons if the target wants to expand memcmp later.
868
+ // The rationale is to avoid turning small chains into memcmp calls.
869
+ if (!TTI.enableMemCmpExpansion (true ))
870
+ return false ;
871
+
872
+ // If we don't have memcmp avaiable we can't emit calls to it.
873
+ if (!TLI.has (LibFunc_memcmp))
874
+ return false ;
875
+
876
+ DomTreeUpdater DTU (DT, /* PostDominatorTree*/ nullptr ,
877
+ DomTreeUpdater::UpdateStrategy::Eager);
878
+
879
+ bool MadeChange = false ;
880
+
881
+ for (auto BBIt = ++F.begin (); BBIt != F.end (); ++BBIt) {
882
+ // A Phi operation is always first in a basic block.
883
+ if (auto *const Phi = dyn_cast<PHINode>(&*BBIt->begin ()))
884
+ MadeChange |= processPhi (*Phi, TLI, AA, DTU);
885
+ }
886
+
887
+ return MadeChange;
888
+ }
889
+
890
+ class MergeICmpsLegacyPass : public FunctionPass {
891
+ public:
864
892
static char ID;
865
893
866
- MergeICmps () : FunctionPass(ID) {
867
- initializeMergeICmpsPass (*PassRegistry::getPassRegistry ());
894
+ MergeICmpsLegacyPass () : FunctionPass(ID) {
895
+ initializeMergeICmpsLegacyPassPass (*PassRegistry::getPassRegistry ());
868
896
}
869
897
870
898
bool runOnFunction (Function &F) override {
@@ -874,12 +902,8 @@ class MergeICmps : public FunctionPass {
874
902
// MergeICmps does not need the DominatorTree, but we update it if it's
875
903
// already available.
876
904
auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
877
- DomTreeUpdater DTU (DTWP ? &DTWP->getDomTree () : nullptr ,
878
- /* PostDominatorTree*/ nullptr ,
879
- DomTreeUpdater::UpdateStrategy::Eager);
880
- AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults ();
881
- auto PA = runImpl (F, &TLI, &TTI, AA, DTU);
882
- return !PA.areAllPreserved ();
905
+ auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults ();
906
+ return runImpl (F, TLI, TTI, AA, DTWP ? &DTWP->getDomTree () : nullptr );
883
907
}
884
908
885
909
private:
@@ -890,50 +914,32 @@ class MergeICmps : public FunctionPass {
890
914
AU.addPreserved <GlobalsAAWrapperPass>();
891
915
AU.addPreserved <DominatorTreeWrapperPass>();
892
916
}
893
-
894
- PreservedAnalyses runImpl (Function &F, const TargetLibraryInfo *TLI,
895
- const TargetTransformInfo *TTI, AliasAnalysis *AA,
896
- DomTreeUpdater &DTU);
897
917
};
898
918
899
- PreservedAnalyses MergeICmps::runImpl (Function &F, const TargetLibraryInfo *TLI,
900
- const TargetTransformInfo *TTI,
901
- AliasAnalysis *AA, DomTreeUpdater &DTU) {
902
- LLVM_DEBUG (dbgs () << " MergeICmpsPass: " << F.getName () << " \n " );
903
-
904
- // We only try merging comparisons if the target wants to expand memcmp later.
905
- // The rationale is to avoid turning small chains into memcmp calls.
906
- if (!TTI->enableMemCmpExpansion (true )) return PreservedAnalyses::all ();
919
+ } // namespace
907
920
908
- // If we don't have memcmp avaiable we can't emit calls to it.
909
- if (!TLI->has (LibFunc_memcmp))
910
- return PreservedAnalyses::all ();
921
+ char MergeICmpsLegacyPass::ID = 0 ;
922
+ INITIALIZE_PASS_BEGIN (MergeICmpsLegacyPass, " mergeicmps" ,
923
+ " Merge contiguous icmps into a memcmp" , false , false )
924
+ INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
925
+ INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
926
+ INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
927
+ INITIALIZE_PASS_END(MergeICmpsLegacyPass, " mergeicmps" ,
928
+ " Merge contiguous icmps into a memcmp" , false , false )
911
929
912
- bool MadeChange = false ;
930
+ Pass *llvm::createMergeICmpsLegacyPass() { return new MergeICmpsLegacyPass (); }
913
931
914
- for (auto BBIt = ++F.begin (); BBIt != F.end (); ++BBIt) {
915
- // A Phi operation is always first in a basic block.
916
- if (auto *const Phi = dyn_cast<PHINode>(&*BBIt->begin ()))
917
- MadeChange |= processPhi (*Phi, TLI, AA, DTU);
918
- }
919
-
920
- if (!MadeChange)
932
+ PreservedAnalyses MergeICmpsPass::run (Function &F,
933
+ FunctionAnalysisManager &AM) {
934
+ auto &TLI = AM.getResult <TargetLibraryAnalysis>(F);
935
+ auto &TTI = AM.getResult <TargetIRAnalysis>(F);
936
+ auto &AA = AM.getResult <AAManager>(F);
937
+ auto *DT = AM.getCachedResult <DominatorTreeAnalysis>(F);
938
+ const bool MadeChanges = runImpl (F, TLI, TTI, AA, DT);
939
+ if (!MadeChanges)
921
940
return PreservedAnalyses::all ();
922
941
PreservedAnalyses PA;
923
942
PA.preserve <GlobalsAA>();
924
943
PA.preserve <DominatorTreeAnalysis>();
925
944
return PA;
926
945
}
927
-
928
- } // namespace
929
-
930
- char MergeICmps::ID = 0 ;
931
- INITIALIZE_PASS_BEGIN (MergeICmps, " mergeicmps" ,
932
- " Merge contiguous icmps into a memcmp" , false , false )
933
- INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
934
- INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
935
- INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
936
- INITIALIZE_PASS_END(MergeICmps, " mergeicmps" ,
937
- " Merge contiguous icmps into a memcmp" , false , false )
938
-
939
- Pass *llvm::createMergeICmpsPass() { return new MergeICmps (); }
0 commit comments