/// Note: This function does not erase the operation on a successful fold.
LogicalResult OpBuilder::tryFold(Operation *op,
SmallVectorImpl<Value> &results) {
- results.reserve(op->getNumResults());
+ ResultRange opResults = op->getResults();
+
+ results.reserve(opResults.size());
auto cleanupFailure = [&] {
- results.assign(op->result_begin(), op->result_end());
+ results.assign(opResults.begin(), opResults.end());
return failure();
};
// Check to see if any operands to the operation is constant and whether
// the operation knows how to constant fold itself.
SmallVector<Attribute, 4> constOperands(op->getNumOperands());
- for (unsigned i = 0, e = op->getNumOperands(); i != e; ++i)
+ for (unsigned i = 0, e = constOperands.size(); i != e; ++i)
matchPattern(op->getOperand(i), m_Constant(&constOperands[i]));
// Try to fold the operation.
// Populate the results with the folded results.
Dialect *dialect = op->getDialect();
- for (auto &it : llvm::enumerate(foldResults)) {
+ for (auto it : llvm::zip(foldResults, opResults.getTypes())) {
+ Type expectedType = std::get<1>(it);
+
// Normal values get pushed back directly.
- if (auto value = it.value().dyn_cast<Value>()) {
+ if (auto value = std::get<0>(it).dyn_cast<Value>()) {
+ if (value.getType() != expectedType)
+ return cleanupFailure();
+
results.push_back(value);
continue;
}
return cleanupFailure();
// Ask the dialect to materialize a constant operation for this value.
- Attribute attr = it.value().get<Attribute>();
- auto *constOp = dialect->materializeConstant(
- cstBuilder, attr, op->getResult(it.index()).getType(), op->getLoc());
+ Attribute attr = std::get<0>(it).get<Attribute>();
+ auto *constOp = dialect->materializeConstant(cstBuilder, attr, expectedType,
+ op->getLoc());
if (!constOp) {
// Erase any generated constants.
for (Operation *cst : generatedConstants)