10b57cec5SDimitry Andric //===- TruncInstCombine.cpp -----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
981ad6265SDimitry Andric // TruncInstCombine - looks for expression graphs post-dominated by TruncInst
1081ad6265SDimitry Andric // and for each eligible graph, it will create a reduced bit-width expression,
1181ad6265SDimitry Andric // replace the old expression with this new one and remove the old expression.
1281ad6265SDimitry Andric // Eligible expression graph is such that:
130b57cec5SDimitry Andric // 1. Contains only supported instructions.
140b57cec5SDimitry Andric // 2. Supported leaves: ZExtInst, SExtInst, TruncInst and Constant value.
150b57cec5SDimitry Andric // 3. Can be evaluated into type with reduced legal bit-width.
1681ad6265SDimitry Andric // 4. All instructions in the graph must not have users outside the graph.
170b57cec5SDimitry Andric // The only exception is for {ZExt, SExt}Inst with operand type equal to
180b57cec5SDimitry Andric // the new reduced type evaluated in (3).
190b57cec5SDimitry Andric //
200b57cec5SDimitry Andric // The motivation for this optimization is that evaluating and expression using
210b57cec5SDimitry Andric // smaller bit-width is preferable, especially for vectorization where we can
220b57cec5SDimitry Andric // fit more values in one vectorized instruction. In addition, this optimization
230b57cec5SDimitry Andric // may decrease the number of cast instructions, but will not increase it.
240b57cec5SDimitry Andric //
250b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric #include "AggressiveInstCombineInternal.h"
280b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
295ffd83dbSDimitry Andric #include "llvm/ADT/Statistic.h"
300b57cec5SDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
310b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
320b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
330b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
34e8d8bef9SDimitry Andric #include "llvm/IR/Instruction.h"
35349cc55cSDimitry Andric #include "llvm/Support/KnownBits.h"
365ffd83dbSDimitry Andric
370b57cec5SDimitry Andric using namespace llvm;
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric #define DEBUG_TYPE "aggressive-instcombine"
400b57cec5SDimitry Andric
4181ad6265SDimitry Andric STATISTIC(NumExprsReduced, "Number of truncations eliminated by reducing bit "
4281ad6265SDimitry Andric "width of expression graph");
435ffd83dbSDimitry Andric STATISTIC(NumInstrsReduced,
445ffd83dbSDimitry Andric "Number of instructions whose bit width was reduced");
455ffd83dbSDimitry Andric
460b57cec5SDimitry Andric /// Given an instruction and a container, it fills all the relevant operands of
4781ad6265SDimitry Andric /// that instruction, with respect to the Trunc expression graph optimizaton.
getRelevantOperands(Instruction * I,SmallVectorImpl<Value * > & Ops)480b57cec5SDimitry Andric static void getRelevantOperands(Instruction *I, SmallVectorImpl<Value *> &Ops) {
490b57cec5SDimitry Andric unsigned Opc = I->getOpcode();
500b57cec5SDimitry Andric switch (Opc) {
510b57cec5SDimitry Andric case Instruction::Trunc:
520b57cec5SDimitry Andric case Instruction::ZExt:
530b57cec5SDimitry Andric case Instruction::SExt:
540b57cec5SDimitry Andric // These CastInst are considered leaves of the evaluated expression, thus,
550b57cec5SDimitry Andric // their operands are not relevent.
560b57cec5SDimitry Andric break;
570b57cec5SDimitry Andric case Instruction::Add:
580b57cec5SDimitry Andric case Instruction::Sub:
590b57cec5SDimitry Andric case Instruction::Mul:
600b57cec5SDimitry Andric case Instruction::And:
610b57cec5SDimitry Andric case Instruction::Or:
620b57cec5SDimitry Andric case Instruction::Xor:
63349cc55cSDimitry Andric case Instruction::Shl:
64349cc55cSDimitry Andric case Instruction::LShr:
65349cc55cSDimitry Andric case Instruction::AShr:
66349cc55cSDimitry Andric case Instruction::UDiv:
67349cc55cSDimitry Andric case Instruction::URem:
68349cc55cSDimitry Andric case Instruction::InsertElement:
690b57cec5SDimitry Andric Ops.push_back(I->getOperand(0));
700b57cec5SDimitry Andric Ops.push_back(I->getOperand(1));
710b57cec5SDimitry Andric break;
72349cc55cSDimitry Andric case Instruction::ExtractElement:
73349cc55cSDimitry Andric Ops.push_back(I->getOperand(0));
74349cc55cSDimitry Andric break;
755ffd83dbSDimitry Andric case Instruction::Select:
765ffd83dbSDimitry Andric Ops.push_back(I->getOperand(1));
775ffd83dbSDimitry Andric Ops.push_back(I->getOperand(2));
785ffd83dbSDimitry Andric break;
7981ad6265SDimitry Andric case Instruction::PHI:
8081ad6265SDimitry Andric for (Value *V : cast<PHINode>(I)->incoming_values())
8181ad6265SDimitry Andric Ops.push_back(V);
8281ad6265SDimitry Andric break;
830b57cec5SDimitry Andric default:
840b57cec5SDimitry Andric llvm_unreachable("Unreachable!");
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric
buildTruncExpressionGraph()8881ad6265SDimitry Andric bool TruncInstCombine::buildTruncExpressionGraph() {
890b57cec5SDimitry Andric SmallVector<Value *, 8> Worklist;
900b57cec5SDimitry Andric SmallVector<Instruction *, 8> Stack;
9181ad6265SDimitry Andric // Clear old instructions info.
920b57cec5SDimitry Andric InstInfoMap.clear();
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric Worklist.push_back(CurrentTruncInst->getOperand(0));
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric while (!Worklist.empty()) {
970b57cec5SDimitry Andric Value *Curr = Worklist.back();
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric if (isa<Constant>(Curr)) {
1000b57cec5SDimitry Andric Worklist.pop_back();
1010b57cec5SDimitry Andric continue;
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric auto *I = dyn_cast<Instruction>(Curr);
1050b57cec5SDimitry Andric if (!I)
1060b57cec5SDimitry Andric return false;
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric if (!Stack.empty() && Stack.back() == I) {
1090b57cec5SDimitry Andric // Already handled all instruction operands, can remove it from both the
1100b57cec5SDimitry Andric // Worklist and the Stack, and add it to the instruction info map.
1110b57cec5SDimitry Andric Worklist.pop_back();
1120b57cec5SDimitry Andric Stack.pop_back();
1130b57cec5SDimitry Andric // Insert I to the Info map.
1140b57cec5SDimitry Andric InstInfoMap.insert(std::make_pair(I, Info()));
1150b57cec5SDimitry Andric continue;
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andric if (InstInfoMap.count(I)) {
1190b57cec5SDimitry Andric Worklist.pop_back();
1200b57cec5SDimitry Andric continue;
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric // Add the instruction to the stack before start handling its operands.
1240b57cec5SDimitry Andric Stack.push_back(I);
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric unsigned Opc = I->getOpcode();
1270b57cec5SDimitry Andric switch (Opc) {
1280b57cec5SDimitry Andric case Instruction::Trunc:
1290b57cec5SDimitry Andric case Instruction::ZExt:
1300b57cec5SDimitry Andric case Instruction::SExt:
1310b57cec5SDimitry Andric // trunc(trunc(x)) -> trunc(x)
1320b57cec5SDimitry Andric // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest
1330b57cec5SDimitry Andric // trunc(ext(x)) -> trunc(x) if the source type is larger than the new
1340b57cec5SDimitry Andric // dest
1350b57cec5SDimitry Andric break;
1360b57cec5SDimitry Andric case Instruction::Add:
1370b57cec5SDimitry Andric case Instruction::Sub:
1380b57cec5SDimitry Andric case Instruction::Mul:
1390b57cec5SDimitry Andric case Instruction::And:
1400b57cec5SDimitry Andric case Instruction::Or:
1415ffd83dbSDimitry Andric case Instruction::Xor:
142349cc55cSDimitry Andric case Instruction::Shl:
143349cc55cSDimitry Andric case Instruction::LShr:
144349cc55cSDimitry Andric case Instruction::AShr:
145349cc55cSDimitry Andric case Instruction::UDiv:
146349cc55cSDimitry Andric case Instruction::URem:
147349cc55cSDimitry Andric case Instruction::InsertElement:
148349cc55cSDimitry Andric case Instruction::ExtractElement:
1495ffd83dbSDimitry Andric case Instruction::Select: {
1500b57cec5SDimitry Andric SmallVector<Value *, 2> Operands;
1510b57cec5SDimitry Andric getRelevantOperands(I, Operands);
152e8d8bef9SDimitry Andric append_range(Worklist, Operands);
1530b57cec5SDimitry Andric break;
1540b57cec5SDimitry Andric }
15581ad6265SDimitry Andric case Instruction::PHI: {
15681ad6265SDimitry Andric SmallVector<Value *, 2> Operands;
15781ad6265SDimitry Andric getRelevantOperands(I, Operands);
15881ad6265SDimitry Andric // Add only operands not in Stack to prevent cycle
15981ad6265SDimitry Andric for (auto *Op : Operands)
160bdd1243dSDimitry Andric if (!llvm::is_contained(Stack, Op))
16181ad6265SDimitry Andric Worklist.push_back(Op);
16281ad6265SDimitry Andric break;
16381ad6265SDimitry Andric }
1640b57cec5SDimitry Andric default:
1650b57cec5SDimitry Andric // TODO: Can handle more cases here:
166349cc55cSDimitry Andric // 1. shufflevector
167349cc55cSDimitry Andric // 2. sdiv, srem
1680b57cec5SDimitry Andric // ...
1690b57cec5SDimitry Andric return false;
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric return true;
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric
getMinBitWidth()1750b57cec5SDimitry Andric unsigned TruncInstCombine::getMinBitWidth() {
1760b57cec5SDimitry Andric SmallVector<Value *, 8> Worklist;
1770b57cec5SDimitry Andric SmallVector<Instruction *, 8> Stack;
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric Value *Src = CurrentTruncInst->getOperand(0);
1800b57cec5SDimitry Andric Type *DstTy = CurrentTruncInst->getType();
1810b57cec5SDimitry Andric unsigned TruncBitWidth = DstTy->getScalarSizeInBits();
1820b57cec5SDimitry Andric unsigned OrigBitWidth =
1830b57cec5SDimitry Andric CurrentTruncInst->getOperand(0)->getType()->getScalarSizeInBits();
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andric if (isa<Constant>(Src))
1860b57cec5SDimitry Andric return TruncBitWidth;
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric Worklist.push_back(Src);
1890b57cec5SDimitry Andric InstInfoMap[cast<Instruction>(Src)].ValidBitWidth = TruncBitWidth;
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric while (!Worklist.empty()) {
1920b57cec5SDimitry Andric Value *Curr = Worklist.back();
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric if (isa<Constant>(Curr)) {
1950b57cec5SDimitry Andric Worklist.pop_back();
1960b57cec5SDimitry Andric continue;
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric // Otherwise, it must be an instruction.
2000b57cec5SDimitry Andric auto *I = cast<Instruction>(Curr);
2010b57cec5SDimitry Andric
2020b57cec5SDimitry Andric auto &Info = InstInfoMap[I];
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric SmallVector<Value *, 2> Operands;
2050b57cec5SDimitry Andric getRelevantOperands(I, Operands);
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andric if (!Stack.empty() && Stack.back() == I) {
2080b57cec5SDimitry Andric // Already handled all instruction operands, can remove it from both, the
2090b57cec5SDimitry Andric // Worklist and the Stack, and update MinBitWidth.
2100b57cec5SDimitry Andric Worklist.pop_back();
2110b57cec5SDimitry Andric Stack.pop_back();
2120b57cec5SDimitry Andric for (auto *Operand : Operands)
2130b57cec5SDimitry Andric if (auto *IOp = dyn_cast<Instruction>(Operand))
2140b57cec5SDimitry Andric Info.MinBitWidth =
2150b57cec5SDimitry Andric std::max(Info.MinBitWidth, InstInfoMap[IOp].MinBitWidth);
2160b57cec5SDimitry Andric continue;
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andric // Add the instruction to the stack before start handling its operands.
2200b57cec5SDimitry Andric Stack.push_back(I);
2210b57cec5SDimitry Andric unsigned ValidBitWidth = Info.ValidBitWidth;
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andric // Update minimum bit-width before handling its operands. This is required
2240b57cec5SDimitry Andric // when the instruction is part of a loop.
2250b57cec5SDimitry Andric Info.MinBitWidth = std::max(Info.MinBitWidth, Info.ValidBitWidth);
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric for (auto *Operand : Operands)
2280b57cec5SDimitry Andric if (auto *IOp = dyn_cast<Instruction>(Operand)) {
2290b57cec5SDimitry Andric // If we already calculated the minimum bit-width for this valid
2300b57cec5SDimitry Andric // bit-width, or for a smaller valid bit-width, then just keep the
2310b57cec5SDimitry Andric // answer we already calculated.
2320b57cec5SDimitry Andric unsigned IOpBitwidth = InstInfoMap.lookup(IOp).ValidBitWidth;
2330b57cec5SDimitry Andric if (IOpBitwidth >= ValidBitWidth)
2340b57cec5SDimitry Andric continue;
2355ffd83dbSDimitry Andric InstInfoMap[IOp].ValidBitWidth = ValidBitWidth;
2360b57cec5SDimitry Andric Worklist.push_back(IOp);
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric unsigned MinBitWidth = InstInfoMap.lookup(cast<Instruction>(Src)).MinBitWidth;
2400b57cec5SDimitry Andric assert(MinBitWidth >= TruncBitWidth);
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andric if (MinBitWidth > TruncBitWidth) {
2430b57cec5SDimitry Andric // In this case reducing expression with vector type might generate a new
2440b57cec5SDimitry Andric // vector type, which is not preferable as it might result in generating
2450b57cec5SDimitry Andric // sub-optimal code.
2460b57cec5SDimitry Andric if (DstTy->isVectorTy())
2470b57cec5SDimitry Andric return OrigBitWidth;
2480b57cec5SDimitry Andric // Use the smallest integer type in the range [MinBitWidth, OrigBitWidth).
2490b57cec5SDimitry Andric Type *Ty = DL.getSmallestLegalIntType(DstTy->getContext(), MinBitWidth);
2500b57cec5SDimitry Andric // Update minimum bit-width with the new destination type bit-width if
2510b57cec5SDimitry Andric // succeeded to find such, otherwise, with original bit-width.
2520b57cec5SDimitry Andric MinBitWidth = Ty ? Ty->getScalarSizeInBits() : OrigBitWidth;
2530b57cec5SDimitry Andric } else { // MinBitWidth == TruncBitWidth
2540b57cec5SDimitry Andric // In this case the expression can be evaluated with the trunc instruction
2550b57cec5SDimitry Andric // destination type, and trunc instruction can be omitted. However, we
2560b57cec5SDimitry Andric // should not perform the evaluation if the original type is a legal scalar
2570b57cec5SDimitry Andric // type and the target type is illegal.
2580b57cec5SDimitry Andric bool FromLegal = MinBitWidth == 1 || DL.isLegalInteger(OrigBitWidth);
2590b57cec5SDimitry Andric bool ToLegal = MinBitWidth == 1 || DL.isLegalInteger(MinBitWidth);
2600b57cec5SDimitry Andric if (!DstTy->isVectorTy() && FromLegal && !ToLegal)
2610b57cec5SDimitry Andric return OrigBitWidth;
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric return MinBitWidth;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
getBestTruncatedType()2660b57cec5SDimitry Andric Type *TruncInstCombine::getBestTruncatedType() {
26781ad6265SDimitry Andric if (!buildTruncExpressionGraph())
2680b57cec5SDimitry Andric return nullptr;
2690b57cec5SDimitry Andric
2700b57cec5SDimitry Andric // We don't want to duplicate instructions, which isn't profitable. Thus, we
2710b57cec5SDimitry Andric // can't shrink something that has multiple users, unless all users are
2720b57cec5SDimitry Andric // post-dominated by the trunc instruction, i.e., were visited during the
2730b57cec5SDimitry Andric // expression evaluation.
2740b57cec5SDimitry Andric unsigned DesiredBitWidth = 0;
2750b57cec5SDimitry Andric for (auto Itr : InstInfoMap) {
2760b57cec5SDimitry Andric Instruction *I = Itr.first;
2770b57cec5SDimitry Andric if (I->hasOneUse())
2780b57cec5SDimitry Andric continue;
2790b57cec5SDimitry Andric bool IsExtInst = (isa<ZExtInst>(I) || isa<SExtInst>(I));
2800b57cec5SDimitry Andric for (auto *U : I->users())
2810b57cec5SDimitry Andric if (auto *UI = dyn_cast<Instruction>(U))
2820b57cec5SDimitry Andric if (UI != CurrentTruncInst && !InstInfoMap.count(UI)) {
2830b57cec5SDimitry Andric if (!IsExtInst)
2840b57cec5SDimitry Andric return nullptr;
2850b57cec5SDimitry Andric // If this is an extension from the dest type, we can eliminate it,
2860b57cec5SDimitry Andric // even if it has multiple users. Thus, update the DesiredBitWidth and
2870b57cec5SDimitry Andric // validate all extension instructions agrees on same DesiredBitWidth.
2880b57cec5SDimitry Andric unsigned ExtInstBitWidth =
2890b57cec5SDimitry Andric I->getOperand(0)->getType()->getScalarSizeInBits();
2900b57cec5SDimitry Andric if (DesiredBitWidth && DesiredBitWidth != ExtInstBitWidth)
2910b57cec5SDimitry Andric return nullptr;
2920b57cec5SDimitry Andric DesiredBitWidth = ExtInstBitWidth;
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric unsigned OrigBitWidth =
2970b57cec5SDimitry Andric CurrentTruncInst->getOperand(0)->getType()->getScalarSizeInBits();
2980b57cec5SDimitry Andric
299349cc55cSDimitry Andric // Initialize MinBitWidth for shift instructions with the minimum number
300349cc55cSDimitry Andric // that is greater than shift amount (i.e. shift amount + 1).
301349cc55cSDimitry Andric // For `lshr` adjust MinBitWidth so that all potentially truncated
302349cc55cSDimitry Andric // bits of the value-to-be-shifted are zeros.
303349cc55cSDimitry Andric // For `ashr` adjust MinBitWidth so that all potentially truncated
304349cc55cSDimitry Andric // bits of the value-to-be-shifted are sign bits (all zeros or ones)
305349cc55cSDimitry Andric // and even one (first) untruncated bit is sign bit.
306349cc55cSDimitry Andric // Exit early if MinBitWidth is not less than original bitwidth.
307349cc55cSDimitry Andric for (auto &Itr : InstInfoMap) {
308349cc55cSDimitry Andric Instruction *I = Itr.first;
309349cc55cSDimitry Andric if (I->isShift()) {
310349cc55cSDimitry Andric KnownBits KnownRHS = computeKnownBits(I->getOperand(1));
311349cc55cSDimitry Andric unsigned MinBitWidth = KnownRHS.getMaxValue()
312349cc55cSDimitry Andric .uadd_sat(APInt(OrigBitWidth, 1))
313349cc55cSDimitry Andric .getLimitedValue(OrigBitWidth);
314349cc55cSDimitry Andric if (MinBitWidth == OrigBitWidth)
315349cc55cSDimitry Andric return nullptr;
316349cc55cSDimitry Andric if (I->getOpcode() == Instruction::LShr) {
317349cc55cSDimitry Andric KnownBits KnownLHS = computeKnownBits(I->getOperand(0));
318349cc55cSDimitry Andric MinBitWidth =
319349cc55cSDimitry Andric std::max(MinBitWidth, KnownLHS.getMaxValue().getActiveBits());
320349cc55cSDimitry Andric }
321349cc55cSDimitry Andric if (I->getOpcode() == Instruction::AShr) {
322349cc55cSDimitry Andric unsigned NumSignBits = ComputeNumSignBits(I->getOperand(0));
323349cc55cSDimitry Andric MinBitWidth = std::max(MinBitWidth, OrigBitWidth - NumSignBits + 1);
324349cc55cSDimitry Andric }
325349cc55cSDimitry Andric if (MinBitWidth >= OrigBitWidth)
326349cc55cSDimitry Andric return nullptr;
327349cc55cSDimitry Andric Itr.second.MinBitWidth = MinBitWidth;
328349cc55cSDimitry Andric }
329349cc55cSDimitry Andric if (I->getOpcode() == Instruction::UDiv ||
330349cc55cSDimitry Andric I->getOpcode() == Instruction::URem) {
331349cc55cSDimitry Andric unsigned MinBitWidth = 0;
332349cc55cSDimitry Andric for (const auto &Op : I->operands()) {
333349cc55cSDimitry Andric KnownBits Known = computeKnownBits(Op);
334349cc55cSDimitry Andric MinBitWidth =
335349cc55cSDimitry Andric std::max(Known.getMaxValue().getActiveBits(), MinBitWidth);
336349cc55cSDimitry Andric if (MinBitWidth >= OrigBitWidth)
337349cc55cSDimitry Andric return nullptr;
338349cc55cSDimitry Andric }
339349cc55cSDimitry Andric Itr.second.MinBitWidth = MinBitWidth;
340349cc55cSDimitry Andric }
341349cc55cSDimitry Andric }
342349cc55cSDimitry Andric
3430b57cec5SDimitry Andric // Calculate minimum allowed bit-width allowed for shrinking the currently
3440b57cec5SDimitry Andric // visited truncate's operand.
3450b57cec5SDimitry Andric unsigned MinBitWidth = getMinBitWidth();
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andric // Check that we can shrink to smaller bit-width than original one and that
3480b57cec5SDimitry Andric // it is similar to the DesiredBitWidth is such exists.
3490b57cec5SDimitry Andric if (MinBitWidth >= OrigBitWidth ||
3500b57cec5SDimitry Andric (DesiredBitWidth && DesiredBitWidth != MinBitWidth))
3510b57cec5SDimitry Andric return nullptr;
3520b57cec5SDimitry Andric
3530b57cec5SDimitry Andric return IntegerType::get(CurrentTruncInst->getContext(), MinBitWidth);
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andric /// Given a reduced scalar type \p Ty and a \p V value, return a reduced type
3570b57cec5SDimitry Andric /// for \p V, according to its type, if it vector type, return the vector
3580b57cec5SDimitry Andric /// version of \p Ty, otherwise return \p Ty.
getReducedType(Value * V,Type * Ty)3590b57cec5SDimitry Andric static Type *getReducedType(Value *V, Type *Ty) {
3600b57cec5SDimitry Andric assert(Ty && !Ty->isVectorTy() && "Expect Scalar Type");
361e8d8bef9SDimitry Andric if (auto *VTy = dyn_cast<VectorType>(V->getType()))
362e8d8bef9SDimitry Andric return VectorType::get(Ty, VTy->getElementCount());
3630b57cec5SDimitry Andric return Ty;
3640b57cec5SDimitry Andric }
3650b57cec5SDimitry Andric
getReducedOperand(Value * V,Type * SclTy)3660b57cec5SDimitry Andric Value *TruncInstCombine::getReducedOperand(Value *V, Type *SclTy) {
3670b57cec5SDimitry Andric Type *Ty = getReducedType(V, SclTy);
3680b57cec5SDimitry Andric if (auto *C = dyn_cast<Constant>(V)) {
369*5f757f3fSDimitry Andric C = ConstantExpr::getTrunc(C, Ty);
3700b57cec5SDimitry Andric // If we got a constantexpr back, try to simplify it with DL info.
3715ffd83dbSDimitry Andric return ConstantFoldConstant(C, DL, &TLI);
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric
3740b57cec5SDimitry Andric auto *I = cast<Instruction>(V);
3750b57cec5SDimitry Andric Info Entry = InstInfoMap.lookup(I);
3760b57cec5SDimitry Andric assert(Entry.NewValue);
3770b57cec5SDimitry Andric return Entry.NewValue;
3780b57cec5SDimitry Andric }
3790b57cec5SDimitry Andric
ReduceExpressionGraph(Type * SclTy)38081ad6265SDimitry Andric void TruncInstCombine::ReduceExpressionGraph(Type *SclTy) {
3815ffd83dbSDimitry Andric NumInstrsReduced += InstInfoMap.size();
38281ad6265SDimitry Andric // Pairs of old and new phi-nodes
38381ad6265SDimitry Andric SmallVector<std::pair<PHINode *, PHINode *>, 2> OldNewPHINodes;
3840b57cec5SDimitry Andric for (auto &Itr : InstInfoMap) { // Forward
3850b57cec5SDimitry Andric Instruction *I = Itr.first;
3860b57cec5SDimitry Andric TruncInstCombine::Info &NodeInfo = Itr.second;
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andric assert(!NodeInfo.NewValue && "Instruction has been evaluated");
3890b57cec5SDimitry Andric
3900b57cec5SDimitry Andric IRBuilder<> Builder(I);
3910b57cec5SDimitry Andric Value *Res = nullptr;
3920b57cec5SDimitry Andric unsigned Opc = I->getOpcode();
3930b57cec5SDimitry Andric switch (Opc) {
3940b57cec5SDimitry Andric case Instruction::Trunc:
3950b57cec5SDimitry Andric case Instruction::ZExt:
3960b57cec5SDimitry Andric case Instruction::SExt: {
3970b57cec5SDimitry Andric Type *Ty = getReducedType(I, SclTy);
3980b57cec5SDimitry Andric // If the source type of the cast is the type we're trying for then we can
3990b57cec5SDimitry Andric // just return the source. There's no need to insert it because it is not
4000b57cec5SDimitry Andric // new.
4010b57cec5SDimitry Andric if (I->getOperand(0)->getType() == Ty) {
4020b57cec5SDimitry Andric assert(!isa<TruncInst>(I) && "Cannot reach here with TruncInst");
4030b57cec5SDimitry Andric NodeInfo.NewValue = I->getOperand(0);
4040b57cec5SDimitry Andric continue;
4050b57cec5SDimitry Andric }
4060b57cec5SDimitry Andric // Otherwise, must be the same type of cast, so just reinsert a new one.
4070b57cec5SDimitry Andric // This also handles the case of zext(trunc(x)) -> zext(x).
4080b57cec5SDimitry Andric Res = Builder.CreateIntCast(I->getOperand(0), Ty,
4090b57cec5SDimitry Andric Opc == Instruction::SExt);
4100b57cec5SDimitry Andric
4110b57cec5SDimitry Andric // Update Worklist entries with new value if needed.
4120b57cec5SDimitry Andric // There are three possible changes to the Worklist:
4130b57cec5SDimitry Andric // 1. Update Old-TruncInst -> New-TruncInst.
4140b57cec5SDimitry Andric // 2. Remove Old-TruncInst (if New node is not TruncInst).
4150b57cec5SDimitry Andric // 3. Add New-TruncInst (if Old node was not TruncInst).
416e8d8bef9SDimitry Andric auto *Entry = find(Worklist, I);
4170b57cec5SDimitry Andric if (Entry != Worklist.end()) {
4180b57cec5SDimitry Andric if (auto *NewCI = dyn_cast<TruncInst>(Res))
4190b57cec5SDimitry Andric *Entry = NewCI;
4200b57cec5SDimitry Andric else
4210b57cec5SDimitry Andric Worklist.erase(Entry);
4220b57cec5SDimitry Andric } else if (auto *NewCI = dyn_cast<TruncInst>(Res))
4230b57cec5SDimitry Andric Worklist.push_back(NewCI);
4240b57cec5SDimitry Andric break;
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric case Instruction::Add:
4270b57cec5SDimitry Andric case Instruction::Sub:
4280b57cec5SDimitry Andric case Instruction::Mul:
4290b57cec5SDimitry Andric case Instruction::And:
4300b57cec5SDimitry Andric case Instruction::Or:
431349cc55cSDimitry Andric case Instruction::Xor:
432349cc55cSDimitry Andric case Instruction::Shl:
433349cc55cSDimitry Andric case Instruction::LShr:
434349cc55cSDimitry Andric case Instruction::AShr:
435349cc55cSDimitry Andric case Instruction::UDiv:
436349cc55cSDimitry Andric case Instruction::URem: {
4370b57cec5SDimitry Andric Value *LHS = getReducedOperand(I->getOperand(0), SclTy);
4380b57cec5SDimitry Andric Value *RHS = getReducedOperand(I->getOperand(1), SclTy);
4390b57cec5SDimitry Andric Res = Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);
440349cc55cSDimitry Andric // Preserve `exact` flag since truncation doesn't change exactness
441349cc55cSDimitry Andric if (auto *PEO = dyn_cast<PossiblyExactOperator>(I))
442349cc55cSDimitry Andric if (auto *ResI = dyn_cast<Instruction>(Res))
443349cc55cSDimitry Andric ResI->setIsExact(PEO->isExact());
444349cc55cSDimitry Andric break;
445349cc55cSDimitry Andric }
446349cc55cSDimitry Andric case Instruction::ExtractElement: {
447349cc55cSDimitry Andric Value *Vec = getReducedOperand(I->getOperand(0), SclTy);
448349cc55cSDimitry Andric Value *Idx = I->getOperand(1);
449349cc55cSDimitry Andric Res = Builder.CreateExtractElement(Vec, Idx);
450349cc55cSDimitry Andric break;
451349cc55cSDimitry Andric }
452349cc55cSDimitry Andric case Instruction::InsertElement: {
453349cc55cSDimitry Andric Value *Vec = getReducedOperand(I->getOperand(0), SclTy);
454349cc55cSDimitry Andric Value *NewElt = getReducedOperand(I->getOperand(1), SclTy);
455349cc55cSDimitry Andric Value *Idx = I->getOperand(2);
456349cc55cSDimitry Andric Res = Builder.CreateInsertElement(Vec, NewElt, Idx);
4570b57cec5SDimitry Andric break;
4580b57cec5SDimitry Andric }
4595ffd83dbSDimitry Andric case Instruction::Select: {
4605ffd83dbSDimitry Andric Value *Op0 = I->getOperand(0);
4615ffd83dbSDimitry Andric Value *LHS = getReducedOperand(I->getOperand(1), SclTy);
4625ffd83dbSDimitry Andric Value *RHS = getReducedOperand(I->getOperand(2), SclTy);
4635ffd83dbSDimitry Andric Res = Builder.CreateSelect(Op0, LHS, RHS);
4645ffd83dbSDimitry Andric break;
4655ffd83dbSDimitry Andric }
46681ad6265SDimitry Andric case Instruction::PHI: {
46781ad6265SDimitry Andric Res = Builder.CreatePHI(getReducedType(I, SclTy), I->getNumOperands());
46881ad6265SDimitry Andric OldNewPHINodes.push_back(
46981ad6265SDimitry Andric std::make_pair(cast<PHINode>(I), cast<PHINode>(Res)));
47081ad6265SDimitry Andric break;
47181ad6265SDimitry Andric }
4720b57cec5SDimitry Andric default:
4730b57cec5SDimitry Andric llvm_unreachable("Unhandled instruction");
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric
4760b57cec5SDimitry Andric NodeInfo.NewValue = Res;
4770b57cec5SDimitry Andric if (auto *ResI = dyn_cast<Instruction>(Res))
4780b57cec5SDimitry Andric ResI->takeName(I);
4790b57cec5SDimitry Andric }
4800b57cec5SDimitry Andric
48181ad6265SDimitry Andric for (auto &Node : OldNewPHINodes) {
48281ad6265SDimitry Andric PHINode *OldPN = Node.first;
48381ad6265SDimitry Andric PHINode *NewPN = Node.second;
48481ad6265SDimitry Andric for (auto Incoming : zip(OldPN->incoming_values(), OldPN->blocks()))
48581ad6265SDimitry Andric NewPN->addIncoming(getReducedOperand(std::get<0>(Incoming), SclTy),
48681ad6265SDimitry Andric std::get<1>(Incoming));
48781ad6265SDimitry Andric }
48881ad6265SDimitry Andric
4890b57cec5SDimitry Andric Value *Res = getReducedOperand(CurrentTruncInst->getOperand(0), SclTy);
4900b57cec5SDimitry Andric Type *DstTy = CurrentTruncInst->getType();
4910b57cec5SDimitry Andric if (Res->getType() != DstTy) {
4920b57cec5SDimitry Andric IRBuilder<> Builder(CurrentTruncInst);
4930b57cec5SDimitry Andric Res = Builder.CreateIntCast(Res, DstTy, false);
4940b57cec5SDimitry Andric if (auto *ResI = dyn_cast<Instruction>(Res))
4950b57cec5SDimitry Andric ResI->takeName(CurrentTruncInst);
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric CurrentTruncInst->replaceAllUsesWith(Res);
4980b57cec5SDimitry Andric
49981ad6265SDimitry Andric // Erase old expression graph, which was replaced by the reduced expression
50081ad6265SDimitry Andric // graph.
5010b57cec5SDimitry Andric CurrentTruncInst->eraseFromParent();
50281ad6265SDimitry Andric // First, erase old phi-nodes and its uses
50381ad6265SDimitry Andric for (auto &Node : OldNewPHINodes) {
50481ad6265SDimitry Andric PHINode *OldPN = Node.first;
50581ad6265SDimitry Andric OldPN->replaceAllUsesWith(PoisonValue::get(OldPN->getType()));
50681ad6265SDimitry Andric InstInfoMap.erase(OldPN);
50781ad6265SDimitry Andric OldPN->eraseFromParent();
50881ad6265SDimitry Andric }
50981ad6265SDimitry Andric // Now we have expression graph turned into dag.
51081ad6265SDimitry Andric // We iterate backward, which means we visit the instruction before we
51181ad6265SDimitry Andric // visit any of its operands, this way, when we get to the operand, we already
51281ad6265SDimitry Andric // removed the instructions (from the expression dag) that uses it.
5130eae32dcSDimitry Andric for (auto &I : llvm::reverse(InstInfoMap)) {
5140b57cec5SDimitry Andric // We still need to check that the instruction has no users before we erase
5150b57cec5SDimitry Andric // it, because {SExt, ZExt}Inst Instruction might have other users that was
5160b57cec5SDimitry Andric // not reduced, in such case, we need to keep that instruction.
5170eae32dcSDimitry Andric if (I.first->use_empty())
5180eae32dcSDimitry Andric I.first->eraseFromParent();
51981ad6265SDimitry Andric else
52081ad6265SDimitry Andric assert((isa<SExtInst>(I.first) || isa<ZExtInst>(I.first)) &&
52181ad6265SDimitry Andric "Only {SExt, ZExt}Inst might have unreduced users");
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric }
5240b57cec5SDimitry Andric
run(Function & F)5250b57cec5SDimitry Andric bool TruncInstCombine::run(Function &F) {
5260b57cec5SDimitry Andric bool MadeIRChange = false;
5270b57cec5SDimitry Andric
5280b57cec5SDimitry Andric // Collect all TruncInst in the function into the Worklist for evaluating.
5290b57cec5SDimitry Andric for (auto &BB : F) {
5300b57cec5SDimitry Andric // Ignore unreachable basic block.
5310b57cec5SDimitry Andric if (!DT.isReachableFromEntry(&BB))
5320b57cec5SDimitry Andric continue;
5330b57cec5SDimitry Andric for (auto &I : BB)
5340b57cec5SDimitry Andric if (auto *CI = dyn_cast<TruncInst>(&I))
5350b57cec5SDimitry Andric Worklist.push_back(CI);
5360b57cec5SDimitry Andric }
5370b57cec5SDimitry Andric
5380b57cec5SDimitry Andric // Process all TruncInst in the Worklist, for each instruction:
53981ad6265SDimitry Andric // 1. Check if it dominates an eligible expression graph to be reduced.
54081ad6265SDimitry Andric // 2. Create a reduced expression graph and replace the old one with it.
5410b57cec5SDimitry Andric while (!Worklist.empty()) {
5420b57cec5SDimitry Andric CurrentTruncInst = Worklist.pop_back_val();
5430b57cec5SDimitry Andric
5440b57cec5SDimitry Andric if (Type *NewDstSclTy = getBestTruncatedType()) {
5450b57cec5SDimitry Andric LLVM_DEBUG(
54681ad6265SDimitry Andric dbgs() << "ICE: TruncInstCombine reducing type of expression graph "
5470b57cec5SDimitry Andric "dominated by: "
5480b57cec5SDimitry Andric << CurrentTruncInst << '\n');
54981ad6265SDimitry Andric ReduceExpressionGraph(NewDstSclTy);
55081ad6265SDimitry Andric ++NumExprsReduced;
5510b57cec5SDimitry Andric MadeIRChange = true;
5520b57cec5SDimitry Andric }
5530b57cec5SDimitry Andric }
5540b57cec5SDimitry Andric
5550b57cec5SDimitry Andric return MadeIRChange;
5560b57cec5SDimitry Andric }
557