//===- TruncInstCombine.cpp -----------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // TruncInstCombine - looks for expression graphs post-dominated by TruncInst // and for each eligible graph, it will create a reduced bit-width expression, // replace the old expression with this new one and remove the old expression. // Eligible expression graph is such that: // 1. Contains only supported instructions. // 2. Supported leaves: ZExtInst, SExtInst, TruncInst and Constant value. // 3. Can be evaluated into type with reduced legal bit-width. // 4. All instructions in the graph must not have users outside the graph. // The only exception is for {ZExt, SExt}Inst with operand type equal to // the new reduced type evaluated in (3). // // The motivation for this optimization is that evaluating and expression using // smaller bit-width is preferable, especially for vectorization where we can // fit more values in one vectorized instruction. In addition, this optimization // may decrease the number of cast instructions, but will not increase it. // //===----------------------------------------------------------------------===// #include "AggressiveInstCombineInternal.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Instruction.h" #include "llvm/Support/KnownBits.h" usingnamespacellvm; #define DEBUG_TYPE … STATISTIC(NumExprsReduced, "Number of truncations eliminated by reducing bit " "width of expression graph"); STATISTIC(NumInstrsReduced, "Number of instructions whose bit width was reduced"); /// Given an instruction and a container, it fills all the relevant operands of /// that instruction, with respect to the Trunc expression graph optimizaton. static void getRelevantOperands(Instruction *I, SmallVectorImpl<Value *> &Ops) { … } bool TruncInstCombine::buildTruncExpressionGraph() { … } unsigned TruncInstCombine::getMinBitWidth() { … } Type *TruncInstCombine::getBestTruncatedType() { … } /// Given a reduced scalar type \p Ty and a \p V value, return a reduced type /// for \p V, according to its type, if it vector type, return the vector /// version of \p Ty, otherwise return \p Ty. static Type *getReducedType(Value *V, Type *Ty) { … } Value *TruncInstCombine::getReducedOperand(Value *V, Type *SclTy) { … } void TruncInstCombine::ReduceExpressionGraph(Type *SclTy) { … } bool TruncInstCombine::run(Function &F) { … }