1e3b55780SDimitry Andric //===--- ExpandLargeDivRem.cpp - Expand large div/rem ---------------------===//
2e3b55780SDimitry Andric //
3e3b55780SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e3b55780SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e3b55780SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e3b55780SDimitry Andric //
7e3b55780SDimitry Andric //===----------------------------------------------------------------------===//
8e3b55780SDimitry Andric //
9e3b55780SDimitry Andric // This pass expands div/rem instructions with a bitwidth above a threshold
10e3b55780SDimitry Andric // into a call to auto-generated functions.
11e3b55780SDimitry Andric // This is useful for targets like x86_64 that cannot lower divisions
12e3b55780SDimitry Andric // with more than 128 bits or targets like x86_32 that cannot lower divisions
13e3b55780SDimitry Andric // with more than 64 bits.
14e3b55780SDimitry Andric //
15e3b55780SDimitry Andric //===----------------------------------------------------------------------===//
16e3b55780SDimitry Andric
17b1c73532SDimitry Andric #include "llvm/CodeGen/ExpandLargeDivRem.h"
18e3b55780SDimitry Andric #include "llvm/ADT/SmallVector.h"
19e3b55780SDimitry Andric #include "llvm/ADT/StringExtras.h"
20e3b55780SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
21e3b55780SDimitry Andric #include "llvm/CodeGen/Passes.h"
22e3b55780SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
23e3b55780SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
24e3b55780SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
25e3b55780SDimitry Andric #include "llvm/IR/IRBuilder.h"
26e3b55780SDimitry Andric #include "llvm/IR/InstIterator.h"
27e3b55780SDimitry Andric #include "llvm/IR/PassManager.h"
28e3b55780SDimitry Andric #include "llvm/InitializePasses.h"
29e3b55780SDimitry Andric #include "llvm/Pass.h"
30e3b55780SDimitry Andric #include "llvm/Support/CommandLine.h"
31e3b55780SDimitry Andric #include "llvm/Target/TargetMachine.h"
32e3b55780SDimitry Andric #include "llvm/Transforms/Utils/IntegerDivision.h"
33e3b55780SDimitry Andric
34e3b55780SDimitry Andric using namespace llvm;
35e3b55780SDimitry Andric
36e3b55780SDimitry Andric static cl::opt<unsigned>
37e3b55780SDimitry Andric ExpandDivRemBits("expand-div-rem-bits", cl::Hidden,
38e3b55780SDimitry Andric cl::init(llvm::IntegerType::MAX_INT_BITS),
39e3b55780SDimitry Andric cl::desc("div and rem instructions on integers with "
40e3b55780SDimitry Andric "more than <N> bits are expanded."));
41e3b55780SDimitry Andric
isConstantPowerOfTwo(llvm::Value * V,bool SignedOp)42e3b55780SDimitry Andric static bool isConstantPowerOfTwo(llvm::Value *V, bool SignedOp) {
43e3b55780SDimitry Andric auto *C = dyn_cast<ConstantInt>(V);
44e3b55780SDimitry Andric if (!C)
45e3b55780SDimitry Andric return false;
46e3b55780SDimitry Andric
47e3b55780SDimitry Andric APInt Val = C->getValue();
48e3b55780SDimitry Andric if (SignedOp && Val.isNegative())
49e3b55780SDimitry Andric Val = -Val;
50e3b55780SDimitry Andric return Val.isPowerOf2();
51e3b55780SDimitry Andric }
52e3b55780SDimitry Andric
isSigned(unsigned int Opcode)53e3b55780SDimitry Andric static bool isSigned(unsigned int Opcode) {
54e3b55780SDimitry Andric return Opcode == Instruction::SDiv || Opcode == Instruction::SRem;
55e3b55780SDimitry Andric }
56e3b55780SDimitry Andric
scalarize(BinaryOperator * BO,SmallVectorImpl<BinaryOperator * > & Replace)57ac9a064cSDimitry Andric static void scalarize(BinaryOperator *BO,
58ac9a064cSDimitry Andric SmallVectorImpl<BinaryOperator *> &Replace) {
59ac9a064cSDimitry Andric VectorType *VTy = cast<FixedVectorType>(BO->getType());
60ac9a064cSDimitry Andric
61ac9a064cSDimitry Andric IRBuilder<> Builder(BO);
62ac9a064cSDimitry Andric
63ac9a064cSDimitry Andric unsigned NumElements = VTy->getElementCount().getFixedValue();
64ac9a064cSDimitry Andric Value *Result = PoisonValue::get(VTy);
65ac9a064cSDimitry Andric for (unsigned Idx = 0; Idx < NumElements; ++Idx) {
66ac9a064cSDimitry Andric Value *LHS = Builder.CreateExtractElement(BO->getOperand(0), Idx);
67ac9a064cSDimitry Andric Value *RHS = Builder.CreateExtractElement(BO->getOperand(1), Idx);
68ac9a064cSDimitry Andric Value *Op = Builder.CreateBinOp(BO->getOpcode(), LHS, RHS);
69ac9a064cSDimitry Andric Result = Builder.CreateInsertElement(Result, Op, Idx);
70ac9a064cSDimitry Andric if (auto *NewBO = dyn_cast<BinaryOperator>(Op)) {
71ac9a064cSDimitry Andric NewBO->copyIRFlags(Op, true);
72ac9a064cSDimitry Andric Replace.push_back(NewBO);
73ac9a064cSDimitry Andric }
74ac9a064cSDimitry Andric }
75ac9a064cSDimitry Andric BO->replaceAllUsesWith(Result);
76ac9a064cSDimitry Andric BO->dropAllReferences();
77ac9a064cSDimitry Andric BO->eraseFromParent();
78ac9a064cSDimitry Andric }
79ac9a064cSDimitry Andric
runImpl(Function & F,const TargetLowering & TLI)80e3b55780SDimitry Andric static bool runImpl(Function &F, const TargetLowering &TLI) {
81e3b55780SDimitry Andric SmallVector<BinaryOperator *, 4> Replace;
82ac9a064cSDimitry Andric SmallVector<BinaryOperator *, 4> ReplaceVector;
83e3b55780SDimitry Andric bool Modified = false;
84e3b55780SDimitry Andric
85e3b55780SDimitry Andric unsigned MaxLegalDivRemBitWidth = TLI.getMaxDivRemBitWidthSupported();
86e3b55780SDimitry Andric if (ExpandDivRemBits != llvm::IntegerType::MAX_INT_BITS)
87e3b55780SDimitry Andric MaxLegalDivRemBitWidth = ExpandDivRemBits;
88e3b55780SDimitry Andric
89e3b55780SDimitry Andric if (MaxLegalDivRemBitWidth >= llvm::IntegerType::MAX_INT_BITS)
90e3b55780SDimitry Andric return false;
91e3b55780SDimitry Andric
92e3b55780SDimitry Andric for (auto &I : instructions(F)) {
93e3b55780SDimitry Andric switch (I.getOpcode()) {
94e3b55780SDimitry Andric case Instruction::UDiv:
95e3b55780SDimitry Andric case Instruction::SDiv:
96e3b55780SDimitry Andric case Instruction::URem:
97e3b55780SDimitry Andric case Instruction::SRem: {
98ac9a064cSDimitry Andric // TODO: This pass doesn't handle scalable vectors.
99ac9a064cSDimitry Andric if (I.getOperand(0)->getType()->isScalableTy())
100ac9a064cSDimitry Andric continue;
101ac9a064cSDimitry Andric
102ac9a064cSDimitry Andric auto *IntTy = dyn_cast<IntegerType>(I.getType()->getScalarType());
103e3b55780SDimitry Andric if (!IntTy || IntTy->getIntegerBitWidth() <= MaxLegalDivRemBitWidth)
104e3b55780SDimitry Andric continue;
105e3b55780SDimitry Andric
106e3b55780SDimitry Andric // The backend has peephole optimizations for powers of two.
107ac9a064cSDimitry Andric // TODO: We don't consider vectors here.
108e3b55780SDimitry Andric if (isConstantPowerOfTwo(I.getOperand(1), isSigned(I.getOpcode())))
109e3b55780SDimitry Andric continue;
110e3b55780SDimitry Andric
111ac9a064cSDimitry Andric if (I.getOperand(0)->getType()->isVectorTy())
112ac9a064cSDimitry Andric ReplaceVector.push_back(&cast<BinaryOperator>(I));
113ac9a064cSDimitry Andric else
114e3b55780SDimitry Andric Replace.push_back(&cast<BinaryOperator>(I));
115e3b55780SDimitry Andric Modified = true;
116e3b55780SDimitry Andric break;
117e3b55780SDimitry Andric }
118e3b55780SDimitry Andric default:
119e3b55780SDimitry Andric break;
120e3b55780SDimitry Andric }
121e3b55780SDimitry Andric }
122e3b55780SDimitry Andric
123ac9a064cSDimitry Andric while (!ReplaceVector.empty()) {
124ac9a064cSDimitry Andric BinaryOperator *BO = ReplaceVector.pop_back_val();
125ac9a064cSDimitry Andric scalarize(BO, Replace);
126ac9a064cSDimitry Andric }
127ac9a064cSDimitry Andric
128e3b55780SDimitry Andric if (Replace.empty())
129e3b55780SDimitry Andric return false;
130e3b55780SDimitry Andric
131e3b55780SDimitry Andric while (!Replace.empty()) {
132e3b55780SDimitry Andric BinaryOperator *I = Replace.pop_back_val();
133e3b55780SDimitry Andric
134e3b55780SDimitry Andric if (I->getOpcode() == Instruction::UDiv ||
135e3b55780SDimitry Andric I->getOpcode() == Instruction::SDiv) {
136e3b55780SDimitry Andric expandDivision(I);
137e3b55780SDimitry Andric } else {
138e3b55780SDimitry Andric expandRemainder(I);
139e3b55780SDimitry Andric }
140e3b55780SDimitry Andric }
141e3b55780SDimitry Andric
142e3b55780SDimitry Andric return Modified;
143e3b55780SDimitry Andric }
144e3b55780SDimitry Andric
145e3b55780SDimitry Andric namespace {
146e3b55780SDimitry Andric class ExpandLargeDivRemLegacyPass : public FunctionPass {
147e3b55780SDimitry Andric public:
148e3b55780SDimitry Andric static char ID;
149e3b55780SDimitry Andric
ExpandLargeDivRemLegacyPass()150e3b55780SDimitry Andric ExpandLargeDivRemLegacyPass() : FunctionPass(ID) {
151e3b55780SDimitry Andric initializeExpandLargeDivRemLegacyPassPass(*PassRegistry::getPassRegistry());
152e3b55780SDimitry Andric }
153e3b55780SDimitry Andric
runOnFunction(Function & F)154e3b55780SDimitry Andric bool runOnFunction(Function &F) override {
155e3b55780SDimitry Andric auto *TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
156e3b55780SDimitry Andric auto *TLI = TM->getSubtargetImpl(F)->getTargetLowering();
157e3b55780SDimitry Andric return runImpl(F, *TLI);
158e3b55780SDimitry Andric }
159e3b55780SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const160e3b55780SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
161e3b55780SDimitry Andric AU.addRequired<TargetPassConfig>();
162e3b55780SDimitry Andric AU.addPreserved<AAResultsWrapperPass>();
163e3b55780SDimitry Andric AU.addPreserved<GlobalsAAWrapperPass>();
164e3b55780SDimitry Andric }
165e3b55780SDimitry Andric };
166e3b55780SDimitry Andric } // namespace
167e3b55780SDimitry Andric
run(Function & F,FunctionAnalysisManager & FAM)168b1c73532SDimitry Andric PreservedAnalyses ExpandLargeDivRemPass::run(Function &F,
169b1c73532SDimitry Andric FunctionAnalysisManager &FAM) {
170b1c73532SDimitry Andric const TargetSubtargetInfo *STI = TM->getSubtargetImpl(F);
171b1c73532SDimitry Andric return runImpl(F, *STI->getTargetLowering()) ? PreservedAnalyses::none()
172b1c73532SDimitry Andric : PreservedAnalyses::all();
173b1c73532SDimitry Andric }
174b1c73532SDimitry Andric
175e3b55780SDimitry Andric char ExpandLargeDivRemLegacyPass::ID = 0;
176e3b55780SDimitry Andric INITIALIZE_PASS_BEGIN(ExpandLargeDivRemLegacyPass, "expand-large-div-rem",
177e3b55780SDimitry Andric "Expand large div/rem", false, false)
178e3b55780SDimitry Andric INITIALIZE_PASS_END(ExpandLargeDivRemLegacyPass, "expand-large-div-rem",
179e3b55780SDimitry Andric "Expand large div/rem", false, false)
180e3b55780SDimitry Andric
createExpandLargeDivRemPass()181e3b55780SDimitry Andric FunctionPass *llvm::createExpandLargeDivRemPass() {
182e3b55780SDimitry Andric return new ExpandLargeDivRemLegacyPass();
183e3b55780SDimitry Andric }
184