@@ -120,79 +120,6 @@ void CustomSafeOptPass::visitInstruction(Instruction& I)
120120 // nothing
121121}
122122
123- // Searches the following pattern
124- // %1 = icmp eq i32 %cmpop1, %cmpop2
125- // %2 = xor i1 %1, true
126- // ...
127- // %3 = select i1 %1, i8 0, i8 1
128- //
129- // And changes it to:
130- // %1 = icmp ne i32 %cmpop1, %cmpop2
131- // ...
132- // %3 = select i1 %1, i8 1, i8 0
133- //
134- // and
135- //
136- // Searches the following pattern
137- // %1 = icmp ule i32 %cmpop1, %cmpop2
138- // %2 = xor i1 %1, true
139- // br i1 %1, label %3, label %4
140- //
141- // And changes it to:
142- // %1 = icmp ugt i32 %cmpop1, %cmpop2
143- // br i1 %1, label %4, label %3
144- //
145- // This optimization combines statements regardless of the predicate.
146- // It will also work if the icmp instruction does not have users, except for the xor, select or branch instruction.
147- void CustomSafeOptPass::visitXor (Instruction& XorInstr) {
148- using namespace llvm ::PatternMatch;
149-
150- CmpInst::Predicate Pred;
151- auto XorPattern = m_c_Xor (m_ICmp (Pred, m_Value (), m_Value ()), m_SpecificInt (1 ));
152- if (!match (&XorInstr, XorPattern)) {
153- return ;
154- }
155-
156- Value* XorOp0 = XorInstr.getOperand (0 );
157- Value* XorOp1 = XorInstr.getOperand (1 );
158- auto ICmpInstr = cast<Instruction>(isa<ICmpInst>(XorOp0) ? XorOp0 : XorOp1);
159-
160- llvm::SmallVector<Instruction*, 4 > UsersList;
161-
162- for (auto U : ICmpInstr->users ()) {
163- if (isa<SelectInst>(U) || isa<BranchInst>(U)) {
164- UsersList.push_back (cast<Instruction>(U));
165- }
166- else if (U != &XorInstr) {
167- return ;
168- }
169- }
170-
171- IRBuilder<> builder (ICmpInstr);
172- auto NegatedCmpPred = cast<ICmpInst>(ICmpInstr)->getInversePredicate ();
173- auto NewCmp = cast<ICmpInst>(builder.CreateICmp (NegatedCmpPred, ICmpInstr->getOperand (0 ), ICmpInstr->getOperand (1 )));
174-
175- for (auto I : UsersList) {
176- if (SelectInst* S = dyn_cast<SelectInst>(I)) {
177- S->swapProfMetadata ();
178- Value* TrueVal = S->getTrueValue ();
179- Value* FalseVal = S->getFalseValue ();
180- S->setTrueValue (FalseVal);
181- S->setFalseValue (TrueVal);
182- }
183- else {
184- IGC_ASSERT (isa<BranchInst>(I));
185- BranchInst* B = cast<BranchInst>(I);
186- B->swapSuccessors ();
187- }
188- }
189-
190- XorInstr.replaceAllUsesWith (NewCmp);
191- ICmpInstr->replaceAllUsesWith (NewCmp);
192- XorInstr.eraseFromParent ();
193- ICmpInstr->eraseFromParent ();
194- }
195-
196123// Searches for following pattern:
197124// %cmp = icmp slt i32 %x, %y
198125// %cond.not = xor i1 %cond, true
0 commit comments