1 //===- DataFlowAnalysis.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Analysis/DataFlowAnalysis.h"
10 #include "mlir/IR/Operation.h"
11 #include "mlir/Interfaces/CallInterfaces.h"
12 #include "mlir/Interfaces/ControlFlowInterfaces.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 
16 #include <queue>
17 
18 using namespace mlir;
19 using namespace mlir::detail;
20 
21 namespace {
22 /// This class contains various state used when computing the lattice elements
23 /// of a callable operation.
24 class CallableLatticeState {
25 public:
26   /// Build a lattice state with a given callable region, and a specified number
27   /// of results to be initialized to the default lattice element.
28   CallableLatticeState(ForwardDataFlowAnalysisBase &analysis,
29                        Region *callableRegion, unsigned numResults)
30       : callableArguments(callableRegion->getArguments()),
31         resultLatticeElements(numResults) {
32     for (AbstractLatticeElement *&it : resultLatticeElements)
33       it = analysis.createLatticeElement();
34   }
35 
36   /// Returns the arguments to the callable region.
37   Block::BlockArgListType getCallableArguments() const {
38     return callableArguments;
39   }
40 
41   /// Returns the lattice element for the results of the callable region.
42   auto getResultLatticeElements() {
43     return llvm::make_pointee_range(resultLatticeElements);
44   }
45 
46   /// Add a call to this callable. This is only used if the callable defines a
47   /// symbol.
48   void addSymbolCall(Operation *op) { symbolCalls.push_back(op); }
49 
50   /// Return the calls that reference this callable. This is only used
51   /// if the callable defines a symbol.
52   ArrayRef<Operation *> getSymbolCalls() const { return symbolCalls; }
53 
54 private:
55   /// The arguments of the callable region.
56   Block::BlockArgListType callableArguments;
57 
58   /// The lattice state for each of the results of this region. The return
59   /// values of the callable aren't SSA values, so we need to track them
60   /// separately.
61   SmallVector<AbstractLatticeElement *, 4> resultLatticeElements;
62 
63   /// The calls referencing this callable if this callable defines a symbol.
64   /// This removes the need to recompute symbol references during propagation.
65   /// Value based references are trivial to resolve, so they can be done
66   /// in-place.
67   SmallVector<Operation *, 4> symbolCalls;
68 };
69 
70 /// This class represents the solver for a forward dataflow analysis. This class
71 /// acts as the propagation engine for computing which lattice elements.
72 class ForwardDataFlowSolver {
73 public:
74   /// Initialize the solver with the given top-level operation.
75   ForwardDataFlowSolver(ForwardDataFlowAnalysisBase &analysis, Operation *op);
76 
77   /// Run the solver until it converges.
78   void solve();
79 
80 private:
81   /// Initialize the set of symbol defining callables that can have their
82   /// arguments and results tracked. 'op' is the top-level operation that the
83   /// solver is operating on.
84   void initializeSymbolCallables(Operation *op);
85 
86   /// Visit the users of the given IR that reside within executable blocks.
87   template <typename T>
88   void visitUsers(T &value) {
89     for (Operation *user : value.getUsers())
90       if (isBlockExecutable(user->getBlock()))
91         visitOperation(user);
92   }
93 
94   /// Visit the given operation and compute any necessary lattice state.
95   void visitOperation(Operation *op);
96 
97   /// Visit the given call operation and compute any necessary lattice state.
98   void visitCallOperation(CallOpInterface op);
99 
100   /// Visit the given callable operation and compute any necessary lattice
101   /// state.
102   void visitCallableOperation(Operation *op);
103 
104   /// Visit the given region branch operation, which defines regions, and
105   /// compute any necessary lattice state. This also resolves the lattice state
106   /// of both the operation results and any nested regions.
107   void visitRegionBranchOperation(
108       RegionBranchOpInterface branch,
109       ArrayRef<AbstractLatticeElement *> operandLattices);
110 
111   /// Visit the given set of region successors, computing any necessary lattice
112   /// state. The provided function returns the input operands to the region at
113   /// the given index. If the index is 'None', the input operands correspond to
114   /// the parent operation results.
115   void visitRegionSuccessors(
116       Operation *parentOp, ArrayRef<RegionSuccessor> regionSuccessors,
117       ArrayRef<AbstractLatticeElement *> operandLattices,
118       function_ref<OperandRange(Optional<unsigned>)> getInputsForRegion);
119 
120   /// Visit the given terminator operation and compute any necessary lattice
121   /// state.
122   void
123   visitTerminatorOperation(Operation *op,
124                            ArrayRef<AbstractLatticeElement *> operandLattices);
125 
126   /// Visit the given terminator operation that exits a callable region. These
127   /// are terminators with no CFG successors.
128   void visitCallableTerminatorOperation(
129       Operation *callable, Operation *terminator,
130       ArrayRef<AbstractLatticeElement *> operandLattices);
131 
132   /// Visit the given block and compute any necessary lattice state.
133   void visitBlock(Block *block);
134 
135   /// Visit argument #'i' of the given block and compute any necessary lattice
136   /// state.
137   void visitBlockArgument(Block *block, int i);
138 
139   /// Mark the entry block of the given region as executable. Returns NoChange
140   /// if the block was already marked executable. If `markPessimisticFixpoint`
141   /// is true, the arguments of the entry block are also marked as having
142   /// reached the pessimistic fixpoint.
143   ChangeResult markEntryBlockExecutable(Region *region,
144                                         bool markPessimisticFixpoint);
145 
146   /// Mark the given block as executable. Returns NoChange if the block was
147   /// already marked executable.
148   ChangeResult markBlockExecutable(Block *block);
149 
150   /// Returns true if the given block is executable.
151   bool isBlockExecutable(Block *block) const;
152 
153   /// Mark the edge between 'from' and 'to' as executable.
154   void markEdgeExecutable(Block *from, Block *to);
155 
156   /// Return true if the edge between 'from' and 'to' is executable.
157   bool isEdgeExecutable(Block *from, Block *to) const;
158 
159   /// Mark the given value as having reached the pessimistic fixpoint. This
160   /// means that we cannot further refine the state of this value.
161   void markPessimisticFixpoint(Value value);
162 
163   /// Mark all of the given values as having reaching the pessimistic fixpoint.
164   template <typename ValuesT>
165   void markAllPessimisticFixpoint(ValuesT values) {
166     for (auto value : values)
167       markPessimisticFixpoint(value);
168   }
169   template <typename ValuesT>
170   void markAllPessimisticFixpoint(Operation *op, ValuesT values) {
171     markAllPessimisticFixpoint(values);
172     opWorklist.push(op);
173   }
174   template <typename ValuesT>
175   void markAllPessimisticFixpointAndVisitUsers(ValuesT values) {
176     for (auto value : values) {
177       AbstractLatticeElement &lattice = analysis.getLatticeElement(value);
178       if (lattice.markPessimisticFixpoint() == ChangeResult::Change)
179         visitUsers(value);
180     }
181   }
182 
183   /// Returns true if the given value was marked as having reached the
184   /// pessimistic fixpoint.
185   bool isAtFixpoint(Value value) const;
186 
187   /// Merge in the given lattice 'from' into the lattice 'to'. 'owner'
188   /// corresponds to the parent operation of the lattice for 'to'.
189   void join(Operation *owner, AbstractLatticeElement &to,
190             const AbstractLatticeElement &from);
191 
192   /// A reference to the dataflow analysis being computed.
193   ForwardDataFlowAnalysisBase &analysis;
194 
195   /// The set of blocks that are known to execute, or are intrinsically live.
196   SmallPtrSet<Block *, 16> executableBlocks;
197 
198   /// The set of control flow edges that are known to execute.
199   DenseSet<std::pair<Block *, Block *>> executableEdges;
200 
201   /// A worklist containing blocks that need to be processed.
202   std::queue<Block *> blockWorklist;
203 
204   /// A worklist of operations that need to be processed.
205   std::queue<Operation *> opWorklist;
206 
207   /// The callable operations that have their argument/result state tracked.
208   DenseMap<Operation *, CallableLatticeState> callableLatticeState;
209 
210   /// A map between a call operation and the resolved symbol callable. This
211   /// avoids re-resolving symbol references during propagation. Value based
212   /// callables are trivial to resolve, so they can be done in-place.
213   DenseMap<Operation *, Operation *> callToSymbolCallable;
214 
215   /// A symbol table used for O(1) symbol lookups during simplification.
216   SymbolTableCollection symbolTable;
217 };
218 } // namespace
219 
220 ForwardDataFlowSolver::ForwardDataFlowSolver(
221     ForwardDataFlowAnalysisBase &analysis, Operation *op)
222     : analysis(analysis) {
223   /// Initialize the solver with the regions within this operation.
224   for (Region &region : op->getRegions()) {
225     // Mark the entry block as executable. The values passed to these regions
226     // are also invisible, so mark any arguments as reaching the pessimistic
227     // fixpoint.
228     markEntryBlockExecutable(&region, /*markPessimisticFixpoint=*/true);
229   }
230   initializeSymbolCallables(op);
231 }
232 
233 void ForwardDataFlowSolver::solve() {
234   while (!blockWorklist.empty() || !opWorklist.empty()) {
235     // Process any operations in the op worklist.
236     while (!opWorklist.empty()) {
237       Operation *nextOp = opWorklist.front();
238       opWorklist.pop();
239       visitUsers(*nextOp);
240     }
241 
242     // Process any blocks in the block worklist.
243     while (!blockWorklist.empty()) {
244       Block *nextBlock = blockWorklist.front();
245       blockWorklist.pop();
246       visitBlock(nextBlock);
247     }
248   }
249 }
250 
251 void ForwardDataFlowSolver::initializeSymbolCallables(Operation *op) {
252   // Initialize the set of symbol callables that can have their state tracked.
253   // This tracks which symbol callable operations we can propagate within and
254   // out of.
255   auto walkFn = [&](Operation *symTable, bool allUsesVisible) {
256     Region &symbolTableRegion = symTable->getRegion(0);
257     Block *symbolTableBlock = &symbolTableRegion.front();
258     for (auto callable : symbolTableBlock->getOps<CallableOpInterface>()) {
259       // We won't be able to track external callables.
260       Region *callableRegion = callable.getCallableRegion();
261       if (!callableRegion)
262         continue;
263       // We only care about symbol defining callables here.
264       auto symbol = dyn_cast<SymbolOpInterface>(callable.getOperation());
265       if (!symbol)
266         continue;
267       callableLatticeState.try_emplace(callable, analysis, callableRegion,
268                                        callable.getCallableResults().size());
269 
270       // If not all of the uses of this symbol are visible, we can't track the
271       // state of the arguments.
272       if (symbol.isPublic() || (!allUsesVisible && symbol.isNested())) {
273         for (Region &region : callable->getRegions())
274           markEntryBlockExecutable(&region, /*markPessimisticFixpoint=*/true);
275       }
276     }
277     if (callableLatticeState.empty())
278       return;
279 
280     // After computing the valid callables, walk any symbol uses to check
281     // for non-call references. We won't be able to track the lattice state
282     // for arguments to these callables, as we can't guarantee that we can see
283     // all of its calls.
284     Optional<SymbolTable::UseRange> uses =
285         SymbolTable::getSymbolUses(&symbolTableRegion);
286     if (!uses) {
287       // If we couldn't gather the symbol uses, conservatively assume that
288       // we can't track information for any nested symbols.
289       op->walk([&](CallableOpInterface op) { callableLatticeState.erase(op); });
290       return;
291     }
292 
293     for (const SymbolTable::SymbolUse &use : *uses) {
294       // If the use is a call, track it to avoid the need to recompute the
295       // reference later.
296       if (auto callOp = dyn_cast<CallOpInterface>(use.getUser())) {
297         Operation *symCallable = callOp.resolveCallable(&symbolTable);
298         auto callableLatticeIt = callableLatticeState.find(symCallable);
299         if (callableLatticeIt != callableLatticeState.end()) {
300           callToSymbolCallable.try_emplace(callOp, symCallable);
301 
302           // We only need to record the call in the lattice if it produces any
303           // values.
304           if (callOp->getNumResults())
305             callableLatticeIt->second.addSymbolCall(callOp);
306         }
307         continue;
308       }
309       // This use isn't a call, so don't we know all of the callers.
310       auto *symbol = symbolTable.lookupSymbolIn(op, use.getSymbolRef());
311       auto it = callableLatticeState.find(symbol);
312       if (it != callableLatticeState.end()) {
313         for (Region &region : it->first->getRegions())
314           markEntryBlockExecutable(&region, /*markPessimisticFixpoint=*/true);
315       }
316     }
317   };
318   SymbolTable::walkSymbolTables(op, /*allSymUsesVisible=*/!op->getBlock(),
319                                 walkFn);
320 }
321 
322 void ForwardDataFlowSolver::visitOperation(Operation *op) {
323   // Collect all of the lattice elements feeding into this operation. If any are
324   // not yet resolved, bail out and wait for them to resolve.
325   SmallVector<AbstractLatticeElement *, 8> operandLattices;
326   operandLattices.reserve(op->getNumOperands());
327   for (Value operand : op->getOperands()) {
328     AbstractLatticeElement *operandLattice =
329         analysis.lookupLatticeElement(operand);
330     if (!operandLattice || operandLattice->isUninitialized())
331       return;
332     operandLattices.push_back(operandLattice);
333   }
334 
335   // If this is a terminator operation, process any control flow lattice state.
336   if (op->hasTrait<OpTrait::IsTerminator>())
337     visitTerminatorOperation(op, operandLattices);
338 
339   // Process call operations. The call visitor processes result values, so we
340   // can exit afterwards.
341   if (CallOpInterface call = dyn_cast<CallOpInterface>(op))
342     return visitCallOperation(call);
343 
344   // Process callable operations. These are specially handled region operations
345   // that track dataflow via calls.
346   if (isa<CallableOpInterface>(op)) {
347     // If this callable has a tracked lattice state, it will be visited by calls
348     // that reference it instead. This way, we don't assume that it is
349     // executable unless there is a proper reference to it.
350     if (callableLatticeState.count(op))
351       return;
352     return visitCallableOperation(op);
353   }
354 
355   // Process region holding operations.
356   if (op->getNumRegions()) {
357     // Check to see if we can reason about the internal control flow of this
358     // region operation.
359     if (auto branch = dyn_cast<RegionBranchOpInterface>(op))
360       return visitRegionBranchOperation(branch, operandLattices);
361 
362     for (Region &region : op->getRegions()) {
363       analysis.visitNonControlFlowArguments(op, RegionSuccessor(&region),
364                                             operandLattices);
365       // `visitNonControlFlowArguments` is required to define all of the region
366       // argument lattices.
367       assert(llvm::none_of(
368                  region.getArguments(),
369                  [&](Value value) {
370                    return analysis.getLatticeElement(value).isUninitialized();
371                  }) &&
372              "expected `visitNonControlFlowArguments` to define all argument "
373              "lattices");
374       markEntryBlockExecutable(&region, /*markPessimisticFixpoint=*/false);
375     }
376   }
377 
378   // If this op produces no results, it can't produce any constants.
379   if (op->getNumResults() == 0)
380     return;
381 
382   // If all of the results of this operation are already resolved, bail out
383   // early.
384   auto isAtFixpointFn = [&](Value value) { return isAtFixpoint(value); };
385   if (llvm::all_of(op->getResults(), isAtFixpointFn))
386     return;
387 
388   // Visit the current operation.
389   if (analysis.visitOperation(op, operandLattices) == ChangeResult::Change)
390     opWorklist.push(op);
391 
392   // `visitOperation` is required to define all of the result lattices.
393   assert(llvm::none_of(
394              op->getResults(),
395              [&](Value value) {
396                return analysis.getLatticeElement(value).isUninitialized();
397              }) &&
398          "expected `visitOperation` to define all result lattices");
399 }
400 
401 void ForwardDataFlowSolver::visitCallableOperation(Operation *op) {
402   // Mark the regions as executable. If we aren't tracking lattice state for
403   // this callable, mark all of the region arguments as having reached a
404   // fixpoint.
405   bool isTrackingLatticeState = callableLatticeState.count(op);
406   for (Region &region : op->getRegions())
407     markEntryBlockExecutable(&region, !isTrackingLatticeState);
408 
409   // TODO: Add support for non-symbol callables when necessary. If the callable
410   // has non-call uses we would mark as having reached pessimistic fixpoint,
411   // otherwise allow for propagating the return values out.
412   markAllPessimisticFixpoint(op, op->getResults());
413 }
414 
415 void ForwardDataFlowSolver::visitCallOperation(CallOpInterface op) {
416   ResultRange callResults = op->getResults();
417 
418   // Resolve the callable operation for this call.
419   Operation *callableOp = nullptr;
420   if (Value callableValue = op.getCallableForCallee().dyn_cast<Value>())
421     callableOp = callableValue.getDefiningOp();
422   else
423     callableOp = callToSymbolCallable.lookup(op);
424 
425   // The callable of this call can't be resolved, mark any results overdefined.
426   if (!callableOp)
427     return markAllPessimisticFixpoint(op, callResults);
428 
429   // If this callable is tracking state, merge the argument operands with the
430   // arguments of the callable.
431   auto callableLatticeIt = callableLatticeState.find(callableOp);
432   if (callableLatticeIt == callableLatticeState.end())
433     return markAllPessimisticFixpoint(op, callResults);
434 
435   OperandRange callOperands = op.getArgOperands();
436   auto callableArgs = callableLatticeIt->second.getCallableArguments();
437   for (auto it : llvm::zip(callOperands, callableArgs)) {
438     BlockArgument callableArg = std::get<1>(it);
439     AbstractLatticeElement &argValue = analysis.getLatticeElement(callableArg);
440     AbstractLatticeElement &operandValue =
441         analysis.getLatticeElement(std::get<0>(it));
442     if (argValue.join(operandValue) == ChangeResult::Change)
443       visitUsers(callableArg);
444   }
445 
446   // Visit the callable.
447   visitCallableOperation(callableOp);
448 
449   // Merge in the lattice state for the callable results as well.
450   auto callableResults = callableLatticeIt->second.getResultLatticeElements();
451   for (auto it : llvm::zip(callResults, callableResults))
452     join(/*owner=*/op,
453          /*to=*/analysis.getLatticeElement(std::get<0>(it)),
454          /*from=*/std::get<1>(it));
455 }
456 
457 void ForwardDataFlowSolver::visitRegionBranchOperation(
458     RegionBranchOpInterface branch,
459     ArrayRef<AbstractLatticeElement *> operandLattices) {
460   // Check to see which regions are executable.
461   SmallVector<RegionSuccessor, 1> successors;
462   analysis.getSuccessorsForOperands(branch, /*sourceIndex=*/llvm::None,
463                                     operandLattices, successors);
464 
465   // If the interface identified that no region will be executed. Mark
466   // any results of this operation as overdefined, as we can't reason about
467   // them.
468   // TODO: If we had an interface to detect pass through operands, we could
469   // resolve some results based on the lattice state of the operands. We could
470   // also allow for the parent operation to have itself as a region successor.
471   if (successors.empty())
472     return markAllPessimisticFixpoint(branch, branch->getResults());
473   return visitRegionSuccessors(branch, successors, operandLattices,
474                                [&](Optional<unsigned> index) {
475                                  return branch.getSuccessorEntryOperands(index);
476                                });
477 }
478 
479 void ForwardDataFlowSolver::visitRegionSuccessors(
480     Operation *parentOp, ArrayRef<RegionSuccessor> regionSuccessors,
481     ArrayRef<AbstractLatticeElement *> operandLattices,
482     function_ref<OperandRange(Optional<unsigned>)> getInputsForRegion) {
483   for (const RegionSuccessor &it : regionSuccessors) {
484     Region *region = it.getSuccessor();
485     ValueRange succArgs = it.getSuccessorInputs();
486 
487     // Check to see if this is the parent operation.
488     if (!region) {
489       ResultRange results = parentOp->getResults();
490       if (llvm::all_of(results, [&](Value res) { return isAtFixpoint(res); }))
491         continue;
492 
493       // Mark the results outside of the input range as having reached the
494       // pessimistic fixpoint.
495       // TODO: This isn't exactly ideal. There may be situations in which a
496       // region operation can provide information for certain results that
497       // aren't part of the control flow.
498       if (succArgs.size() != results.size()) {
499         opWorklist.push(parentOp);
500         if (succArgs.empty()) {
501           markAllPessimisticFixpoint(results);
502           continue;
503         }
504 
505         unsigned firstResIdx = succArgs[0].cast<OpResult>().getResultNumber();
506         markAllPessimisticFixpoint(results.take_front(firstResIdx));
507         markAllPessimisticFixpoint(
508             results.drop_front(firstResIdx + succArgs.size()));
509       }
510 
511       // Update the lattice for any operation results.
512       OperandRange operands = getInputsForRegion(/*index=*/llvm::None);
513       for (auto it : llvm::zip(succArgs, operands))
514         join(parentOp, analysis.getLatticeElement(std::get<0>(it)),
515              analysis.getLatticeElement(std::get<1>(it)));
516       continue;
517     }
518     assert(!region->empty() && "expected region to be non-empty");
519     Block *entryBlock = &region->front();
520     markBlockExecutable(entryBlock);
521 
522     // If all of the arguments have already reached a fixpoint, the arguments
523     // have already been fully resolved.
524     Block::BlockArgListType arguments = entryBlock->getArguments();
525     if (llvm::all_of(arguments, [&](Value arg) { return isAtFixpoint(arg); }))
526       continue;
527 
528     if (succArgs.size() != arguments.size()) {
529       if (analysis.visitNonControlFlowArguments(
530               parentOp, it, operandLattices) == ChangeResult::Change) {
531         unsigned firstArgIdx =
532             succArgs.empty() ? 0
533                              : succArgs[0].cast<BlockArgument>().getArgNumber();
534         for (Value v : arguments.take_front(firstArgIdx)) {
535           assert(!analysis.getLatticeElement(v).isUninitialized() &&
536                  "Non-control flow block arg has no lattice value after "
537                  "analysis callback");
538           visitUsers(v);
539         }
540         for (Value v : arguments.drop_front(firstArgIdx + succArgs.size())) {
541           assert(!analysis.getLatticeElement(v).isUninitialized() &&
542                  "Non-control flow block arg has no lattice value after "
543                  "analysis callback");
544           visitUsers(v);
545         }
546       }
547     }
548 
549     // Update the lattice of arguments that have inputs from the predecessor.
550     OperandRange succOperands = getInputsForRegion(region->getRegionNumber());
551     for (auto it : llvm::zip(succArgs, succOperands)) {
552       AbstractLatticeElement &argValue =
553           analysis.getLatticeElement(std::get<0>(it));
554       AbstractLatticeElement &operandValue =
555           analysis.getLatticeElement(std::get<1>(it));
556       if (argValue.join(operandValue) == ChangeResult::Change)
557         visitUsers(std::get<0>(it));
558     }
559   }
560 }
561 
562 void ForwardDataFlowSolver::visitTerminatorOperation(
563     Operation *op, ArrayRef<AbstractLatticeElement *> operandLattices) {
564   // If this operation has no successors, we treat it as an exiting terminator.
565   if (op->getNumSuccessors() == 0) {
566     Region *parentRegion = op->getParentRegion();
567     Operation *parentOp = parentRegion->getParentOp();
568 
569     // Check to see if this is a terminator for a callable region.
570     if (isa<CallableOpInterface>(parentOp))
571       return visitCallableTerminatorOperation(parentOp, op, operandLattices);
572 
573     // Otherwise, check to see if the parent tracks region control flow.
574     auto regionInterface = dyn_cast<RegionBranchOpInterface>(parentOp);
575     if (!regionInterface || !isBlockExecutable(parentOp->getBlock()))
576       return;
577 
578     // Query the set of successors of the current region using the current
579     // optimistic lattice state.
580     SmallVector<RegionSuccessor, 1> regionSuccessors;
581     analysis.getSuccessorsForOperands(regionInterface,
582                                       parentRegion->getRegionNumber(),
583                                       operandLattices, regionSuccessors);
584     if (regionSuccessors.empty())
585       return;
586 
587     // Try to get "region-like" successor operands if possible in order to
588     // propagate the operand states to the successors.
589     if (isRegionReturnLike(op)) {
590       auto getOperands = [&](Optional<unsigned> regionIndex) {
591         // Determine the individual region successor operands for the given
592         // region index (if any).
593         return *getRegionBranchSuccessorOperands(op, regionIndex);
594       };
595       return visitRegionSuccessors(parentOp, regionSuccessors, operandLattices,
596                                    getOperands);
597     }
598 
599     // If this terminator is not "region-like", conservatively mark all of the
600     // successor values as having reached the pessimistic fixpoint.
601     for (auto &it : regionSuccessors) {
602       // If the successor is a region, mark the entry block as executable so
603       // that we visit operations defined within. If the successor is the
604       // parent operation, we simply mark the control flow results as having
605       // reached the pessimistic state.
606       if (Region *region = it.getSuccessor())
607         markEntryBlockExecutable(region, /*markPessimisticFixpoint=*/true);
608       else
609         markAllPessimisticFixpointAndVisitUsers(it.getSuccessorInputs());
610     }
611   }
612 
613   // Try to resolve to a specific set of successors with the current optimistic
614   // lattice state.
615   Block *block = op->getBlock();
616   if (auto branch = dyn_cast<BranchOpInterface>(op)) {
617     SmallVector<Block *> successors;
618     if (succeeded(analysis.getSuccessorsForOperands(branch, operandLattices,
619                                                     successors))) {
620       for (Block *succ : successors)
621         markEdgeExecutable(block, succ);
622       return;
623     }
624   }
625 
626   // Otherwise, conservatively treat all edges as executable.
627   for (Block *succ : op->getSuccessors())
628     markEdgeExecutable(block, succ);
629 }
630 
631 void ForwardDataFlowSolver::visitCallableTerminatorOperation(
632     Operation *callable, Operation *terminator,
633     ArrayRef<AbstractLatticeElement *> operandLattices) {
634   // If there are no exiting values, we have nothing to track.
635   if (terminator->getNumOperands() == 0)
636     return;
637 
638   // If this callable isn't tracking any lattice state there is nothing to do.
639   auto latticeIt = callableLatticeState.find(callable);
640   if (latticeIt == callableLatticeState.end())
641     return;
642   assert(callable->getNumResults() == 0 && "expected symbol callable");
643 
644   // If this terminator is not "return-like", conservatively mark all of the
645   // call-site results as having reached the pessimistic fixpoint.
646   auto callableResultLattices = latticeIt->second.getResultLatticeElements();
647   if (!terminator->hasTrait<OpTrait::ReturnLike>()) {
648     for (auto &it : callableResultLattices)
649       it.markPessimisticFixpoint();
650     for (Operation *call : latticeIt->second.getSymbolCalls())
651       markAllPessimisticFixpoint(call, call->getResults());
652     return;
653   }
654 
655   // Merge the lattice state for terminator operands into the results.
656   ChangeResult result = ChangeResult::NoChange;
657   for (auto it : llvm::zip(operandLattices, callableResultLattices))
658     result |= std::get<1>(it).join(*std::get<0>(it));
659   if (result == ChangeResult::NoChange)
660     return;
661 
662   // If any of the result lattices changed, update the callers.
663   for (Operation *call : latticeIt->second.getSymbolCalls())
664     for (auto it : llvm::zip(call->getResults(), callableResultLattices))
665       join(call, analysis.getLatticeElement(std::get<0>(it)), std::get<1>(it));
666 }
667 
668 void ForwardDataFlowSolver::visitBlock(Block *block) {
669   // If the block is not the entry block we need to compute the lattice state
670   // for the block arguments. Entry block argument lattices are computed
671   // elsewhere, such as when visiting the parent operation.
672   if (!block->isEntryBlock()) {
673     for (int i : llvm::seq<int>(0, block->getNumArguments()))
674       visitBlockArgument(block, i);
675   }
676 
677   // Visit all of the operations within the block.
678   for (Operation &op : *block)
679     visitOperation(&op);
680 }
681 
682 void ForwardDataFlowSolver::visitBlockArgument(Block *block, int i) {
683   BlockArgument arg = block->getArgument(i);
684   AbstractLatticeElement &argLattice = analysis.getLatticeElement(arg);
685   if (argLattice.isAtFixpoint())
686     return;
687 
688   ChangeResult updatedLattice = ChangeResult::NoChange;
689   for (auto it = block->pred_begin(), e = block->pred_end(); it != e; ++it) {
690     Block *pred = *it;
691 
692     // We only care about this predecessor if it is going to execute.
693     if (!isEdgeExecutable(pred, block))
694       continue;
695 
696     // Try to get the operand forwarded by the predecessor. If we can't reason
697     // about the terminator of the predecessor, mark as having reached a
698     // fixpoint.
699     auto branch = dyn_cast<BranchOpInterface>(pred->getTerminator());
700     if (!branch) {
701       updatedLattice |= argLattice.markPessimisticFixpoint();
702       break;
703     }
704     Value operand = branch.getSuccessorOperands(it.getSuccessorIndex())[i];
705     if (!operand) {
706       updatedLattice |= argLattice.markPessimisticFixpoint();
707       break;
708     }
709 
710     // If the operand hasn't been resolved, it is uninitialized and can merge
711     // with anything.
712     AbstractLatticeElement *operandLattice =
713         analysis.lookupLatticeElement(operand);
714     if (!operandLattice)
715       continue;
716 
717     // Otherwise, join the operand lattice into the argument lattice.
718     updatedLattice |= argLattice.join(*operandLattice);
719     if (argLattice.isAtFixpoint())
720       break;
721   }
722 
723   // If the lattice changed, visit users of the argument.
724   if (updatedLattice == ChangeResult::Change)
725     visitUsers(arg);
726 }
727 
728 ChangeResult
729 ForwardDataFlowSolver::markEntryBlockExecutable(Region *region,
730                                                 bool markPessimisticFixpoint) {
731   if (!region->empty()) {
732     if (markPessimisticFixpoint)
733       markAllPessimisticFixpoint(region->front().getArguments());
734     return markBlockExecutable(&region->front());
735   }
736   return ChangeResult::NoChange;
737 }
738 
739 ChangeResult ForwardDataFlowSolver::markBlockExecutable(Block *block) {
740   bool marked = executableBlocks.insert(block).second;
741   if (marked)
742     blockWorklist.push(block);
743   return marked ? ChangeResult::Change : ChangeResult::NoChange;
744 }
745 
746 bool ForwardDataFlowSolver::isBlockExecutable(Block *block) const {
747   return executableBlocks.count(block);
748 }
749 
750 void ForwardDataFlowSolver::markEdgeExecutable(Block *from, Block *to) {
751   executableEdges.insert(std::make_pair(from, to));
752 
753   // Mark the destination as executable, and reprocess its arguments if it was
754   // already executable.
755   if (markBlockExecutable(to) == ChangeResult::NoChange) {
756     for (int i : llvm::seq<int>(0, to->getNumArguments()))
757       visitBlockArgument(to, i);
758   }
759 }
760 
761 bool ForwardDataFlowSolver::isEdgeExecutable(Block *from, Block *to) const {
762   return executableEdges.count(std::make_pair(from, to));
763 }
764 
765 void ForwardDataFlowSolver::markPessimisticFixpoint(Value value) {
766   analysis.getLatticeElement(value).markPessimisticFixpoint();
767 }
768 
769 bool ForwardDataFlowSolver::isAtFixpoint(Value value) const {
770   if (auto *lattice = analysis.lookupLatticeElement(value))
771     return lattice->isAtFixpoint();
772   return false;
773 }
774 
775 void ForwardDataFlowSolver::join(Operation *owner, AbstractLatticeElement &to,
776                                  const AbstractLatticeElement &from) {
777   if (to.join(from) == ChangeResult::Change)
778     opWorklist.push(owner);
779 }
780 
781 //===----------------------------------------------------------------------===//
782 // AbstractLatticeElement
783 //===----------------------------------------------------------------------===//
784 
785 AbstractLatticeElement::~AbstractLatticeElement() = default;
786 
787 //===----------------------------------------------------------------------===//
788 // ForwardDataFlowAnalysisBase
789 //===----------------------------------------------------------------------===//
790 
791 ForwardDataFlowAnalysisBase::~ForwardDataFlowAnalysisBase() = default;
792 
793 AbstractLatticeElement &
794 ForwardDataFlowAnalysisBase::getLatticeElement(Value value) {
795   AbstractLatticeElement *&latticeValue = latticeValues[value];
796   if (!latticeValue)
797     latticeValue = createLatticeElement(value);
798   return *latticeValue;
799 }
800 
801 AbstractLatticeElement *
802 ForwardDataFlowAnalysisBase::lookupLatticeElement(Value value) {
803   return latticeValues.lookup(value);
804 }
805 
806 void ForwardDataFlowAnalysisBase::run(Operation *topLevelOp) {
807   // Run the main dataflow solver.
808   ForwardDataFlowSolver solver(*this, topLevelOp);
809   solver.solve();
810 
811   // Any values that are still uninitialized now go to a pessimistic fixpoint,
812   // otherwise we assume an optimistic fixpoint has been reached.
813   for (auto &it : latticeValues)
814     if (it.second->isUninitialized())
815       it.second->markPessimisticFixpoint();
816     else
817       it.second->markOptimisticFixpoint();
818 }
819