llvm.org GIT mirror
[LoopPredication] Support ule, sle latch predicates This is a follow up for the loop predication change 313981 to support ule, sle latch predicates. Reviewed By: mkazantsev Differential Revision: https://reviews.llvm.org/D38177 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315616 91177308-0d34-0410-b5e6-96231b3b80d8 Artur Pilipenko 1 year, 11 months ago
2 changed file(s) with 144 addition(s) and 21 deletion(s).
 99 99 // implies M. 100 100 // 101 101 // For now the transformation is limited to the following case: 102 // * The loop has a single latch with either ult or slt icmp condition.⏎ 102 // * The loop has a single latch with the condition of the form:⏎ 103 // ++i latchLimit, where is u<, u<=, s<, or s<=. 103 104 // * The step of the IV used in the latch condition is 1. 104 105 // * The IV of the latch condition is the same as the post increment IV of the 105 106 // guard condition. 106 // * The guard condition is ult. 107 // 108 // In this case the latch is of the from: 109 // ++i u< latchLimit or ++i s< latchLimit 110 // and the guard is of the form: 111 // i u< guardLimit 112 // 113 // For the unsigned latch comparison case M is:⏎ 107 // * The guard condition is⏎ 108 // i u< guardLimit. 109 // 110 // For the ult latch comparison case M is: 114 111 // forall X . X u< guardLimit && (X + 1) u< latchLimit => 115 112 // (X + 1) u< guardLimit 116 113 // 117 114 // This is true if latchLimit u<= guardLimit since then 118 115 // (X + 1) u< latchLimit u<= guardLimit == (X + 1) u< guardLimit. 119 116 // 120 // So the widened condition is:⏎ 117 // So for ult condition the widened condition is:⏎ 121 118 // i.start u< guardLimit && latchLimit u<= guardLimit 119 // Similarly for ule condition the widened condition is: 120 // i.start u< guardLimit && latchLimit u< guardLimit 122 121 // 123 122 // For the signed latch comparison case M is: 124 123 // forall X . X u< guardLimit && (X + 1) s< latchLimit => 146 145 // 147 146 // So the widened condition is: 148 147 // i.start u< guardLimit && latchLimit s<= guardLimit 148 // Similarly for sle condition the widened condition is: 149 // i.start u< guardLimit && latchLimit s< guardLimit 149 150 // 150 151 //===----------------------------------------------------------------------===// 151 152 302 303 DEBUG(ICI->dump()); 303 304 304 305 // parseLoopStructure guarantees that the latch condition is: 305 // ++i u< latchLimit or ++i s< latchLimit⏎ 306 // ++i latchLimit, where is u<, u<=, s<, or s<=.⏎ 306 307 // We are looking for the range checks of the form: 307 308 // i u< guardLimit 308 309 auto RangeCheck = parseLoopICmp(ICI); 326 327 assert(RangeCheckIV->getStepRecurrence(*SE)->isOne() && "must be one"); 327 328 const SCEV *Start = RangeCheckIV->getStart(); 328 329 329 // Generate the widened condition. See the file header comment for reasoning. 330 // If the latch condition is unsigned: 331 // i.start u< guardLimit && latchLimit u<= guardLimit 332 // If the latch condition is signed: 333 // i.start u< guardLimit && latchLimit s<= guardLimit 334 335 auto LimitCheckPred = ICmpInst::isSigned(LatchCheck.Pred) 336 ? ICmpInst::ICMP_SLE 337 : ICmpInst::ICMP_ULE;⏎ 330 // Generate the widened condition:⏎ 331 // i.start u< guardLimit && latchLimit guardLimit 332 // where depends on the latch condition predicate. See the file 333 // header comment for the reasoning. 334 ICmpInst::Predicate LimitCheckPred; 335 switch (LatchCheck.Pred) { 336 case ICmpInst::ICMP_ULT: 337 LimitCheckPred = ICmpInst::ICMP_ULE; 338 break; 339 case ICmpInst::ICMP_ULE: 340 LimitCheckPred = ICmpInst::ICMP_ULT; 341 break; 342 case ICmpInst::ICMP_SLT: 343 LimitCheckPred = ICmpInst::ICMP_SLE; 344 break; 345 case ICmpInst::ICMP_SLE: 346 LimitCheckPred = ICmpInst::ICMP_SLT; 347 break; 348 default: 349 llvm_unreachable("Unsupported loop latch!"); 350 } 338 351 339 352 auto CanExpand = [this](const SCEV *S) { 340 353 return SE->isLoopInvariant(S, L) && isSafeToExpand(S, *SE); 442 455 } 443 456 444 457 if (Result->Pred != ICmpInst::ICMP_ULT && 445 Result->Pred != ICmpInst::ICMP_SLT) {⏎ 458 Result->Pred != ICmpInst::ICMP_SLT &&⏎ 459 Result->Pred != ICmpInst::ICMP_ULE && 460 Result->Pred != ICmpInst::ICMP_SLE) { 446 461 DEBUG(dbgs() << "Unsupported loop latch predicate(" << Result->Pred 447 462 << ")!\n"); 448 463 return None;