• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- RegionUtils.h - Region-related transformation utilities --*- C++ -*-===//
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 #ifndef MLIR_TRANSFORMS_REGIONUTILS_H_
10 #define MLIR_TRANSFORMS_REGIONUTILS_H_
11 
12 #include "mlir/IR/Region.h"
13 #include "mlir/IR/Value.h"
14 
15 #include "llvm/ADT/SetVector.h"
16 
17 namespace mlir {
18 
19 /// Check if all values in the provided range are defined above the `limit`
20 /// region.  That is, if they are defined in a region that is a proper ancestor
21 /// of `limit`.
22 template <typename Range>
areValuesDefinedAbove(Range values,Region & limit)23 bool areValuesDefinedAbove(Range values, Region &limit) {
24   for (Value v : values)
25     if (!v.getParentRegion()->isProperAncestor(&limit))
26       return false;
27   return true;
28 }
29 
30 /// Replace all uses of `orig` within the given region with `replacement`.
31 void replaceAllUsesInRegionWith(Value orig, Value replacement, Region &region);
32 
33 /// Calls `callback` for each use of a value within `region` or its descendants
34 /// that was defined at the ancestors of the `limit`.
35 void visitUsedValuesDefinedAbove(Region &region, Region &limit,
36                                  function_ref<void(OpOperand *)> callback);
37 
38 /// Calls `callback` for each use of a value within any of the regions provided
39 /// that was defined in one of the ancestors.
40 void visitUsedValuesDefinedAbove(MutableArrayRef<Region> regions,
41                                  function_ref<void(OpOperand *)> callback);
42 
43 /// Fill `values` with a list of values defined at the ancestors of the `limit`
44 /// region and used within `region` or its descendants.
45 void getUsedValuesDefinedAbove(Region &region, Region &limit,
46                                llvm::SetVector<Value> &values);
47 
48 /// Fill `values` with a list of values used within any of the regions provided
49 /// but defined in one of the ancestors.
50 void getUsedValuesDefinedAbove(MutableArrayRef<Region> regions,
51                                llvm::SetVector<Value> &values);
52 
53 /// Run a set of structural simplifications over the given regions. This
54 /// includes transformations like unreachable block elimination, dead argument
55 /// elimination, as well as some other DCE. This function returns success if any
56 /// of the regions were simplified, failure otherwise.
57 LogicalResult simplifyRegions(MutableArrayRef<Region> regions);
58 
59 } // namespace mlir
60 
61 #endif // MLIR_TRANSFORMS_REGIONUTILS_H_
62