blob: 04de6db6e4dcd14c7cf928a9017a87a4b24a24f1 [file] [log] [blame]
Tatiana Shpeismande8829f2018-08-24 23:38:14 -07001//===- SSAValue.cpp - MLIR SSAValue Classes ------------===//
Chris Lattnerd4964212018-08-01 10:43:18 -07002//
3// Copyright 2019 The MLIR Authors.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// =============================================================================
17
18#include "mlir/IR/SSAValue.h"
19#include "mlir/IR/Instructions.h"
Tatiana Shpeismande8829f2018-08-24 23:38:14 -070020#include "mlir/IR/StandardOps.h"
Chris Lattnerd4964212018-08-01 10:43:18 -070021#include "mlir/IR/Statements.h"
22using namespace mlir;
23
24/// If this value is the result of an OperationInst, return the instruction
25/// that defines it.
26OperationInst *SSAValue::getDefiningInst() {
27 if (auto *result = dyn_cast<InstResult>(this))
28 return result->getOwner();
29 return nullptr;
30}
31
32/// If this value is the result of an OperationStmt, return the statement
33/// that defines it.
34OperationStmt *SSAValue::getDefiningStmt() {
35 if (auto *result = dyn_cast<StmtResult>(this))
36 return result->getOwner();
37 return nullptr;
38}
39
40Operation *SSAValue::getDefiningOperation() {
41 if (auto *inst = getDefiningInst())
42 return inst;
43 if (auto *stmt = getDefiningStmt())
44 return stmt;
45 return nullptr;
46}
Tatiana Shpeismande8829f2018-08-24 23:38:14 -070047
48//===----------------------------------------------------------------------===//
49// MLValue implementation.
50//===----------------------------------------------------------------------===//
51
52// MLValue can be used a a dimension id if it is valid as a symbol, or
53// it is an induction variable, or it is a result of affine apply operation
54// with dimension id arguments.
55bool MLValue::isValidDim() const {
56 if (auto *stmt = getDefiningStmt()) {
57 // Top level statement or constant operation is ok.
58 if (stmt->getParentStmt() == nullptr || stmt->is<ConstantOp>())
59 return true;
60 // Affine apply operation is ok if all of its operands are ok.
61 if (auto op = stmt->getAs<AffineApplyOp>())
62 return op->isValidDim();
63 return false;
64 }
65 // This value is either a function argument or an induction variable. Both are
66 // ok.
67 return true;
68}
69
70// MLValue can be used as a symbol if it is a constant, or it is defined at
71// the top level, or it is a result of affine apply operation with symbol
72// arguments.
73bool MLValue::isValidSymbol() const {
74 if (auto *stmt = getDefiningStmt()) {
75 // Top level statement or constant operation is ok.
76 if (stmt->getParentStmt() == nullptr || stmt->is<ConstantOp>())
77 return true;
78 // Affine apply operation is ok if all of its operands are ok.
79 if (auto op = stmt->getAs<AffineApplyOp>())
80 return op->isValidSymbol();
81 return false;
82 }
83 // This value is either a function argument or an induction variable.
84 // Function argument is ok, induction variable is not.
85 return isa<MLFuncArgument>(this);
86}