Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombineInternal.h
35268 views
//===- AggressiveInstCombineInternal.h --------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the instruction pattern combiner classes.9// Currently, it handles pattern expressions for:10// * Truncate instruction11//12//===----------------------------------------------------------------------===//1314#ifndef LLVM_LIB_TRANSFORMS_AGGRESSIVEINSTCOMBINE_COMBINEINTERNAL_H15#define LLVM_LIB_TRANSFORMS_AGGRESSIVEINSTCOMBINE_COMBINEINTERNAL_H1617#include "llvm/ADT/MapVector.h"18#include "llvm/ADT/SmallVector.h"19#include "llvm/Analysis/ValueTracking.h"20#include "llvm/Support/KnownBits.h"2122using namespace llvm;2324//===----------------------------------------------------------------------===//25// TruncInstCombine - looks for expression graphs dominated by trunc26// instructions and for each eligible graph, it will create a reduced bit-width27// expression and replace the old expression with this new one and remove the28// old one. Eligible expression graph is such that:29// 1. Contains only supported instructions.30// 2. Supported leaves: ZExtInst, SExtInst, TruncInst and Constant value.31// 3. Can be evaluated into type with reduced legal bit-width (or Trunc type).32// 4. All instructions in the graph must not have users outside the graph.33// Only exception is for {ZExt, SExt}Inst with operand type equal to the34// new reduced type chosen in (3).35//36// The motivation for this optimization is that evaluating and expression using37// smaller bit-width is preferable, especially for vectorization where we can38// fit more values in one vectorized instruction. In addition, this optimization39// may decrease the number of cast instructions, but will not increase it.40//===----------------------------------------------------------------------===//4142namespace llvm {43class AssumptionCache;44class DataLayout;45class DominatorTree;46class Function;47class Instruction;48class TargetLibraryInfo;49class TruncInst;50class Type;51class Value;5253class TruncInstCombine {54AssumptionCache &AC;55TargetLibraryInfo &TLI;56const DataLayout &DL;57const DominatorTree &DT;5859/// List of all TruncInst instructions to be processed.60SmallVector<TruncInst *, 4> Worklist;6162/// Current processed TruncInst instruction.63TruncInst *CurrentTruncInst = nullptr;6465/// Information per each instruction in the expression graph.66struct Info {67/// Number of LSBs that are needed to generate a valid expression.68unsigned ValidBitWidth = 0;69/// Minimum number of LSBs needed to generate the ValidBitWidth.70unsigned MinBitWidth = 0;71/// The reduced value generated to replace the old instruction.72Value *NewValue = nullptr;73};74/// An ordered map representing expression graph post-dominated by current75/// processed TruncInst. It maps each instruction in the graph to its Info76/// structure. The map is ordered such that each instruction appears before77/// all other instructions in the graph that uses it.78MapVector<Instruction *, Info> InstInfoMap;7980public:81TruncInstCombine(AssumptionCache &AC, TargetLibraryInfo &TLI,82const DataLayout &DL, const DominatorTree &DT)83: AC(AC), TLI(TLI), DL(DL), DT(DT) {}8485/// Perform TruncInst pattern optimization on given function.86bool run(Function &F);8788private:89/// Build expression graph dominated by the /p CurrentTruncInst and append it90/// to the InstInfoMap container.91///92/// \return true only if succeed to generate an eligible sub expression graph.93bool buildTruncExpressionGraph();9495/// Calculate the minimal allowed bit-width of the chain ending with the96/// currently visited truncate's operand.97///98/// \return minimum number of bits to which the chain ending with the99/// truncate's operand can be shrunk to.100unsigned getMinBitWidth();101102/// Build an expression graph dominated by the current processed TruncInst and103/// Check if it is eligible to be reduced to a smaller type.104///105/// \return the scalar version of the new type to be used for the reduced106/// expression graph, or nullptr if the expression graph is not107/// eligible to be reduced.108Type *getBestTruncatedType();109110KnownBits computeKnownBits(const Value *V) const {111return llvm::computeKnownBits(V, DL, /*Depth=*/0, &AC,112/*CtxI=*/cast<Instruction>(CurrentTruncInst),113&DT);114}115116unsigned ComputeNumSignBits(const Value *V) const {117return llvm::ComputeNumSignBits(118V, DL, /*Depth=*/0, &AC, /*CtxI=*/cast<Instruction>(CurrentTruncInst),119&DT);120}121122/// Given a \p V value and a \p SclTy scalar type return the generated reduced123/// value of \p V based on the type \p SclTy.124///125/// \param V value to be reduced.126/// \param SclTy scalar version of new type to reduce to.127/// \return the new reduced value.128Value *getReducedOperand(Value *V, Type *SclTy);129130/// Create a new expression graph using the reduced /p SclTy type and replace131/// the old expression graph with it. Also erase all instructions in the old132/// graph, except those that are still needed outside the graph.133///134/// \param SclTy scalar version of new type to reduce expression graph into.135void ReduceExpressionGraph(Type *SclTy);136};137} // end namespace llvm.138139#endif140141142