hask-opt/RgnCSEPass.cpp
/// Attempt to eliminate a redundant operation.
Attempt to eliminate a redundant operation.
Attempt to eliminate a redundant operation .
LogicalResult RgnCSE::simplifyOperation(std::set<RgnValOp >&seen,
  ScopedMapTy &knownValues,
                                        DominanceInfo &domInfo, Operation *op) {
  if (op->hasTrait<OpTrait::IsTerminator>())
    return failure();
  if (isOpTriviallyDead(op)) {
    opsToErase.push_back(op);
    return success();
  }
  if ((op->getNumRegions() != 0) && !isa<RgnValOp>(op)) {
    return failure();
  }
  if (RgnValOp rgnval = dyn_cast<RgnValOp>(op)) {
    Region &rgn = rgnval.getRegion();
    if (rgn.getBlocks().size() == 0 || rgn.getBlocks().size() > 2) {
      llvm::errs() << "TODO: investigate!\n";
      return failure();
    }
    for(RgnValOp other: seen) {
      if (isRgnValOpEqual(knownValues, rgnval, other)) {
        rgnval->replaceAllUsesWith(other);
        opsToErase.push_back(rgnval);
        return success();
      }
    }
    seen.insert(rgnval);
    return failure();
  }
  if (!MemoryEffectOpInterface::hasNoEffect(op))
    return failure();
  if (auto *existing = knownValues.lookup(op)) {
    op->replaceAllUsesWith(existing);
    opsToErase.push_back(op);
    if (existing->getLoc().isa<UnknownLoc>() &&
        !op->getLoc().isa<UnknownLoc>()) {
      existing->setLoc(op->getLoc());
    }
    return success();
  }
  knownValues.insert(op, op);
  return failure();
}
LogicalResult RgnCSE :: simplifyOperation ( std :: set < RgnValOp > & seen , ScopedMapTy & knownValues , DominanceInfo & domInfo , Operation * op ) { if ( op -> hasTrait < OpTrait :: IsTerminator > ( ) ) return failure ( ) ; if ( isOpTriviallyDead ( op ) ) { opsToErase . push_back ( op ) ; return success ( ) ; } if ( ( op -> getNumRegions ( ) != 0 ) && ! isa < RgnValOp > ( op ) ) { return failure ( ) ; } if ( RgnValOp rgnval = dyn_cast < RgnValOp > ( op ) ) { Region & rgn = rgnval . getRegion ( ) ; if ( rgn . getBlocks ( ) . size ( ) == 0 || rgn . getBlocks ( ) . size ( ) > 2 ) { llvm :: errs ( ) << " \n " ; return failure ( ) ; } for ( RgnValOp other : seen ) { if ( isRgnValOpEqual ( knownValues , rgnval , other ) ) { rgnval -> replaceAllUsesWith ( other ) ; opsToErase . push_back ( rgnval ) ; return success ( ) ; } } seen . insert ( rgnval ) ; return failure ( ) ; } if ( ! MemoryEffectOpInterface :: hasNoEffect ( op ) ) return failure ( ) ; if ( auto * existing = knownValues . lookup ( op ) ) { op -> replaceAllUsesWith ( existing ) ; opsToErase . push_back ( op ) ; if ( existing -> getLoc ( ) . isa < UnknownLoc > ( ) && ! op -> getLoc ( ) . isa < UnknownLoc > ( ) ) { existing -> setLoc ( op -> getLoc ( ) ) ; } return success ( ) ; } knownValues . insert ( op , op ) ; return failure ( ) ; }
Attempt to eliminate a redundant operation.
Attempt to eliminate a redundant operation .
// llvm::errs() << "op:\n===\n";  // llvm::errs() << *op;  // llvm::errs() << "\n\thash: " << SimpleOperationInfo::computeOpHashToplevel(op) << "\n\n"; // Don't simplify terminator operations. // If the operation is already trivially dead just add it to the erase list. // ++numDCE; // Don't simplify operations with nested blocks. We don't currently model // equality comparisons correctly among other things. It is also unclear // whether we would want to CSE such operations. // assert(false && "found illegal rgn val op"); // TODO: We currently only eliminate non side-effecting // operations. // Look for an existing definition for the operation. // If we find one then replace all uses of the current operation with the // existing one and mark it for deletion. // If the existing operation has an unknown location and the current // operation doesn't, then set the existing op's location to that of the // current op. // ++numCSE; // Otherwise, we add this operation to the known values map.