55 #define DEBUG_TYPE "loop-predication"
60 class LoopPredication {
69 : Pred(Pred), IV(IV), Limit(Limit) {}
91 bool runOnLoop(
Loop *
L);
94 class LoopPredicationLegacyPass :
public LoopPass {
108 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
109 LoopPredication LP(SE);
110 return LP.runOnLoop(L);
118 "Loop predication",
false,
false)
124 return new LoopPredicationLegacyPass();
130 LoopPredication LP(&AR.
SE);
131 if (!LP.runOnLoop(&L))
138 LoopPredication::parseLoopICmp(
ICmpInst *ICI) {
143 const SCEV *LHSS = SE->getSCEV(LHS);
144 if (isa<SCEVCouldNotCompute>(LHSS))
146 const SCEV *RHSS = SE->getSCEV(RHS);
147 if (isa<SCEVCouldNotCompute>(RHSS))
151 if (SE->isLoopInvariant(LHSS,
L)) {
161 return LoopICmp(Pred, AR, RHSS);
169 assert(Ty == RHS->
getType() &&
"expandCheck operands have different types?");
181 DEBUG(
dbgs() <<
"Analyzing ICmpInst condition:\n");
184 auto RangeCheck = parseLoopICmp(ICI);
186 DEBUG(
dbgs() <<
"Failed to parse the loop latch condition!\n");
192 const SCEV *RHSS = RangeCheck->Limit;
194 auto CanExpand = [
this](
const SCEV *S) {
197 if (!CanExpand(RHSS))
203 bool IsIncreasing =
false;
204 if (!SE->isMonotonicPredicate(IndexAR, Pred, IsIncreasing))
211 const SCEV *NewLHSS = IsIncreasing
214 if (NewLHSS == IndexAR) {
215 DEBUG(
dbgs() <<
"Can't compute NewLHSS!\n");
222 if (!CanExpand(NewLHSS))
225 DEBUG(
dbgs() <<
"NewLHSS is loop invariant and safe to expand. Expand!\n");
227 Instruction *InsertAt = Preheader->getTerminator();
228 return expandCheck(Expander, Builder, Pred, NewLHSS, RHSS, InsertAt);
231 bool LoopPredication::widenGuardConditions(
IntrinsicInst *Guard,
236 IRBuilder<> Builder(cast<Instruction>(Preheader->getTerminator()));
248 unsigned NumWidened = 0;
250 Value *Condition = Worklist.pop_back_val();
251 if (!Visited.
insert(Condition).second)
255 using namespace llvm::PatternMatch;
257 Worklist.push_back(LHS);
258 Worklist.push_back(RHS);
262 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(Condition)) {
263 if (
auto NewRangeCheck = widenICmpRangeCheck(ICI, Expander, Builder)) {
264 Checks.
push_back(NewRangeCheck.getValue());
272 }
while (Worklist.size() != 0);
279 Value *LastCheck =
nullptr;
280 for (
auto *
Check : Checks)
287 DEBUG(
dbgs() <<
"Widened checks = " << NumWidened <<
"\n");
291 bool LoopPredication::runOnLoop(
Loop *
Loop) {
302 if (!GuardDecl || GuardDecl->use_empty())
305 DL = &M->getDataLayout();
314 for (
const auto BB :
L->
blocks())
316 if (
auto *II = dyn_cast<IntrinsicInst>(&
I))
317 if (II->getIntrinsicID() == Intrinsic::experimental_guard)
325 bool Changed =
false;
326 for (
auto *Guard : Guards)
327 Changed |= widenGuardConditions(Guard, Expander);
Pass interface - Implemented by all 'passes'.
static bool Check(DecodeStatus &Out, DecodeStatus In)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
A Module instance is used to store all the information related to an LLVM module. ...
The main scalar evolution driver.
INITIALIZE_PASS_BEGIN(LoopPredicationLegacyPass,"loop-predication","Loop predication", false, false) INITIALIZE_PASS_END(LoopPredicationLegacyPass
LoopT * getParentLoop() const
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
BlockT * getHeader() const
const SCEV * getStart() const
bool match(Val *V, const Pattern &P)
#define INITIALIZE_PASS_DEPENDENCY(depName)
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_NODISCARD bool empty() const
This node represents a polynomial recurrence on the trip count of the specified loop.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
iterator_range< block_iterator > blocks() const
A set of analyses that are preserved following a run of a transformation pass.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Type * getType() const
Return the LLVM type of this SCEV expression.
Represent the analysis usage information of a pass.
Pass * createLoopPredicationPass()
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
Value * getOperand(unsigned i) const
Predicate getPredicate() const
Return the predicate for this instruction.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void dump() const
Support for debugging, callable in GDB: V->dump()
void dump() const
This method is used for debugging.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
This class uses information about analyze scalars to rewrite expressions in canonical form...
iterator insert(iterator I, T &&Elt)
This class represents an analyzed expression in the program.
Represents a single loop in the control flow graph.
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const Loop * getLoop() const
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void initializeLoopPredicationLegacyPassPass(PassRegistry &)
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
LLVM Value Representation.
A container for analyses that lazily runs them and caches their results.
bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE)
Return true if the given expression is safe to expand in the sense that all materialized values are s...
A wrapper class for inspecting calls to intrinsic functions.