Skip to content

Commit fe84df1

Browse files
Refactoring crc32 pattern matcher!
1 parent 4bc052e commit fe84df1

File tree

1 file changed

+133
-69
lines changed

1 file changed

+133
-69
lines changed

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 133 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ static bool tryToRecognizeReverseFunction(Instruction &I){
305305
APInt Mask0F = APInt::getSplat(Len, APInt(8, 0x0F));
306306
APInt Mask01 = APInt::getSplat(Len, APInt(8, 0x01));
307307
APInt MaskShift = APInt(Len, Len - 8);
308-
309-
Value *Op0 = I.getOperand(0);
310-
Value *Op1 = I.getOperand(1);
308+
309+
//Value *Op0 = I.getOperand(0);
310+
//Value *Op1 = I.getOperand(1);
311311
Value *MulOp0;
312312

313313
//Petar's insertion! Aditional variables!
@@ -698,23 +698,41 @@ static bool tryToRecognizeTableBasedCRC32(Function &F){
698698
//auto it=BB.getInstList().begin();
699699
int count=1;
700700
for(Instruction& I: BB){
701-
if(count<=4 && !dyn_cast<AllocaInst>I)
702-
return false;
701+
if(count<=4 ){
702+
AllocaInst *II= dyn_cast<AllocaInst>(&I);
703+
if(!II)
704+
return false;
705+
}
703706

704-
if(count>=5 && count<=7 && !dyn_cast<StoreInst>I)
705-
return false;
706-
707-
if(count==8 && !dyn_cast<LoadInst>I)
708-
return false;
709-
710-
if(count==9 && !dyn_cast<BitCastInst>I)
711-
return false;
712-
713-
if(count==10 && !dyn_cast<StoreInst>I)
714-
return false;
715-
716-
if(count==11 && !dyn_cast<BranchInst>I)
717-
return false;
707+
if(count>=5 && count<=7){
708+
StoreInst* II=dyn_cast<StoreInst>(&I);
709+
if(!II)
710+
return false;
711+
}
712+
713+
if(count==8){
714+
LoadInst* II=dyn_cast<LoadInst>(&I);
715+
if(!II)
716+
return false;
717+
}
718+
719+
if(count==9){
720+
BitCastInst *II=dyn_cast<BitCastInst>(&I);
721+
if(!II)
722+
return false;
723+
}
724+
725+
if(count==10){
726+
StoreInst *II=dyn_cast<StoreInst>(&I);
727+
if(!II)
728+
return false;
729+
}
730+
731+
if(count==11){
732+
BranchInst *II=dyn_cast<BranchInst>(&I);
733+
if(!II)
734+
return false;
735+
}
718736
count++;
719737
}
720738
} else if(step==2){
@@ -724,24 +742,36 @@ static bool tryToRecognizeTableBasedCRC32(Function &F){
724742
*/
725743
int count=1;
726744
for(Instruction& I: BB){
727-
Value *help1;
728-
Value *help2;
729-
if(count==1 && !dyn_cast<LoadInst>I)
730-
return false;
745+
Value *help1;
746+
Value *help2;
747+
if(count==1){
748+
LoadInst *II=dyn_cast<LoadInst>(&I);
749+
if(!II)
750+
return false;
751+
}
731752

732-
if(count==2 && !match(help1, m_Add(m_Value(help2), m_SpecificInt(-1))))
733-
return false;
753+
if(count==2 && !match(help1, m_Add(m_Value(help2), m_SpecificInt(-1))))
754+
return false;
734755

735-
if(count==3 && !dyn_cast<StoreInst>I)
736-
return false;
756+
if(count==3){
757+
StoreInst *II=dyn_cast<StoreInst>(&I);
758+
if(!II)
759+
return false;
760+
}
737761

738-
if(count==4 && !dyn_cast<ICmpInst>I)
739-
return false;
762+
if(count==4){
763+
ICmpInst *II=dyn_cast<ICmpInst>(&I);
764+
if(!II)
765+
return false;
766+
}
740767

741-
if(count==5 && !dyn_cast<BranchInst>I)
742-
return false;
768+
if(count==5){
769+
BranchInst *II=dyn_cast<BranchInst>(&I);
770+
if(!II)
771+
return false;
772+
}
743773

744-
count++;
774+
count++;
745775
}
746776
} else if(step==3){
747777
int count=1;
@@ -751,61 +781,95 @@ static bool tryToRecognizeTableBasedCRC32(Function &F){
751781
*/
752782

753783
for(Instruction& I: BB){
754-
Value *help1;
755-
Value *help2;
756-
Value *help3;
784+
Value *help1;
785+
Value *help2;
786+
Value *help3;
757787

758-
if(count<=2 && !dyn_cast<LoadInst>I)
759-
return false;
788+
if(count<=2){
789+
LoadInst *II=dyn_cast<LoadInst>(&I);
790+
if(!II)
791+
return false;
792+
}
760793

761-
if(count==3 && !dyn_cast<GetElementPtrInst>I)
762-
return false;
794+
if(count==3){
795+
GetElementPtrInst *II=dyn_cast<GetElementPtrInst>(&I);
796+
if(!II)
797+
return false;
798+
}
763799

764-
if(count==4 && !dyn_cast<StoreInst>I)
765-
return false;
800+
if(count==4){
801+
StoreInst *II=dyn_cast<StoreInst>(&I);
802+
if(!II)
803+
return false;
804+
}
766805

767-
if(count==5 && !dyn_cast<LoadInst>I)
768-
return false;
806+
if(count==5){
807+
LoadInst *II=dyn_cast<LoadInst>(&I);
808+
if(!II)
809+
return false;
810+
}
769811

770-
if(count==6 && !match(help1, m_Xor(m_Value(help2), m_Value(help3))))
771-
return false;
812+
if(count==6 && !match(help1, m_Xor(m_Value(help2), m_Value(help3))))
813+
return false;
772814

773-
if(count==7 && !match(help1, m_And(m_Value(help2), m_Value(help3))))
774-
return false;
815+
if(count==7 && !match(help1, m_And(m_Value(help2), m_Value(help3))))
816+
return false;
775817

776-
if(count==8 && !dyn_cast<ZExtInst>I)
777-
return false;
818+
if(count==8){
819+
ZExtInst *II=dyn_cast<ZExtInst>(&I);
820+
if(!II)
821+
return false;
822+
}
778823

779-
if(count==9 && !dyn_cast<GetElementPtrInst>I)
780-
return false;
824+
if(count==9){
825+
GetElementPtrInst *II=dyn_cast<GetElementPtrInst>(&I);
826+
if(!II)
827+
return false;
828+
}
781829

782-
if(count>=10 && count<=11 && !dyn_cast<LoadInst>I)
783-
return false;
830+
if(count>=10 && count<=11){
831+
LoadInst *II=dyn_cast<LoadInst>(&I);
832+
if(!II)
833+
return false;
834+
}
784835

785-
if(count==12 && !match(help1, m_LShr(m_Value(help2), m_Value(help3))))
786-
return false;
836+
if(count==12 && !match(help1, m_LShr(m_Value(help2), m_Value(help3))))
837+
return false;
787838

788-
if(count==13 && !match(help1, m_Xor(m_Value(help2), m_Value(help3))))
789-
return false;
839+
if(count==13 && !match(help1, m_Xor(m_Value(help2), m_Value(help3))))
840+
return false;
790841

791-
if(count==14 && !dyn_cast<StoreInst>I)
792-
return false;
842+
if(count==14){
843+
StoreInst *II=dyn_cast<StoreInst>(&I);
844+
if(!II)
845+
return false;
846+
}
793847

794-
if(count==15 && !dyn_cast<BranchInst>I)
795-
return false;
848+
if(count==15){
849+
BranchInst *II=dyn_cast<BranchInst>(&I);
850+
if(!II)
851+
return false;
852+
}
796853

797-
count++;
854+
count++;
798855
}
799856
} else {
800857
int count=1;
801858

802-
if(count==1 && !dyn_cast<LoadInst>I)
803-
return false;
804-
805-
if(count==2 && !dyn_cast<ReturnInst>I)
806-
return false;
807-
808-
count++;
859+
for(Instruction& I: BB){
860+
if(count==1){
861+
LoadInst *II=dyn_cast<LoadInst>(&I);
862+
if(!II)
863+
return false;
864+
}
865+
866+
if(count==2){
867+
ReturnInst *II=dyn_cast<ReturnInst>(&I);
868+
if(!II)
869+
return false;
870+
}
871+
count++;
872+
}
809873
}
810874
step++;
811875
}

0 commit comments

Comments
 (0)