xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1fcaf7f86SDimitry Andric //===----- RISCVCodeGenPrepare.cpp ----------------------------------------===//
2fcaf7f86SDimitry Andric //
3fcaf7f86SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fcaf7f86SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fcaf7f86SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fcaf7f86SDimitry Andric //
7fcaf7f86SDimitry Andric //===----------------------------------------------------------------------===//
8fcaf7f86SDimitry Andric //
9fcaf7f86SDimitry Andric // This is a RISCV specific version of CodeGenPrepare.
10fcaf7f86SDimitry Andric // It munges the code in the input function to better prepare it for
11fcaf7f86SDimitry Andric // SelectionDAG-based code generation. This works around limitations in it's
12fcaf7f86SDimitry Andric // basic-block-at-a-time approach.
13fcaf7f86SDimitry Andric //
14fcaf7f86SDimitry Andric //===----------------------------------------------------------------------===//
15fcaf7f86SDimitry Andric 
16fcaf7f86SDimitry Andric #include "RISCV.h"
17fcaf7f86SDimitry Andric #include "RISCVTargetMachine.h"
18fcaf7f86SDimitry Andric #include "llvm/ADT/Statistic.h"
19fcaf7f86SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
20fcaf7f86SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
21*bdd1243dSDimitry Andric #include "llvm/IR/InstVisitor.h"
22972a253aSDimitry Andric #include "llvm/IR/PatternMatch.h"
23fcaf7f86SDimitry Andric #include "llvm/InitializePasses.h"
24fcaf7f86SDimitry Andric #include "llvm/Pass.h"
25fcaf7f86SDimitry Andric 
26fcaf7f86SDimitry Andric using namespace llvm;
27fcaf7f86SDimitry Andric 
28fcaf7f86SDimitry Andric #define DEBUG_TYPE "riscv-codegenprepare"
29fcaf7f86SDimitry Andric #define PASS_NAME "RISCV CodeGenPrepare"
30fcaf7f86SDimitry Andric 
31fcaf7f86SDimitry Andric STATISTIC(NumZExtToSExt, "Number of SExt instructions converted to ZExt");
32fcaf7f86SDimitry Andric 
33fcaf7f86SDimitry Andric namespace {
34fcaf7f86SDimitry Andric 
35*bdd1243dSDimitry Andric class RISCVCodeGenPrepare : public FunctionPass,
36*bdd1243dSDimitry Andric                             public InstVisitor<RISCVCodeGenPrepare, bool> {
37fcaf7f86SDimitry Andric   const DataLayout *DL;
38fcaf7f86SDimitry Andric   const RISCVSubtarget *ST;
39fcaf7f86SDimitry Andric 
40fcaf7f86SDimitry Andric public:
41fcaf7f86SDimitry Andric   static char ID;
42fcaf7f86SDimitry Andric 
43fcaf7f86SDimitry Andric   RISCVCodeGenPrepare() : FunctionPass(ID) {}
44fcaf7f86SDimitry Andric 
45fcaf7f86SDimitry Andric   bool runOnFunction(Function &F) override;
46fcaf7f86SDimitry Andric 
47fcaf7f86SDimitry Andric   StringRef getPassName() const override { return PASS_NAME; }
48fcaf7f86SDimitry Andric 
49fcaf7f86SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
50fcaf7f86SDimitry Andric     AU.setPreservesCFG();
51fcaf7f86SDimitry Andric     AU.addRequired<TargetPassConfig>();
52fcaf7f86SDimitry Andric   }
53fcaf7f86SDimitry Andric 
54*bdd1243dSDimitry Andric   bool visitInstruction(Instruction &I) { return false; }
55*bdd1243dSDimitry Andric   bool visitZExtInst(ZExtInst &I);
56*bdd1243dSDimitry Andric   bool visitAnd(BinaryOperator &BO);
57fcaf7f86SDimitry Andric };
58fcaf7f86SDimitry Andric 
59fcaf7f86SDimitry Andric } // end anonymous namespace
60fcaf7f86SDimitry Andric 
61*bdd1243dSDimitry Andric bool RISCVCodeGenPrepare::visitZExtInst(ZExtInst &ZExt) {
62fcaf7f86SDimitry Andric   if (!ST->is64Bit())
63fcaf7f86SDimitry Andric     return false;
64fcaf7f86SDimitry Andric 
65*bdd1243dSDimitry Andric   Value *Src = ZExt.getOperand(0);
66fcaf7f86SDimitry Andric 
67fcaf7f86SDimitry Andric   // We only care about ZExt from i32 to i64.
68*bdd1243dSDimitry Andric   if (!ZExt.getType()->isIntegerTy(64) || !Src->getType()->isIntegerTy(32))
69fcaf7f86SDimitry Andric     return false;
70fcaf7f86SDimitry Andric 
71fcaf7f86SDimitry Andric   // Look for an opportunity to replace (i64 (zext (i32 X))) with a sext if we
72fcaf7f86SDimitry Andric   // can determine that the sign bit of X is zero via a dominating condition.
73fcaf7f86SDimitry Andric   // This often occurs with widened induction variables.
74fcaf7f86SDimitry Andric   if (isImpliedByDomCondition(ICmpInst::ICMP_SGE, Src,
75*bdd1243dSDimitry Andric                               Constant::getNullValue(Src->getType()), &ZExt,
76a4a491e2SDimitry Andric                               *DL).value_or(false)) {
77*bdd1243dSDimitry Andric     auto *SExt = new SExtInst(Src, ZExt.getType(), "", &ZExt);
78*bdd1243dSDimitry Andric     SExt->takeName(&ZExt);
79*bdd1243dSDimitry Andric     SExt->setDebugLoc(ZExt.getDebugLoc());
80fcaf7f86SDimitry Andric 
81*bdd1243dSDimitry Andric     ZExt.replaceAllUsesWith(SExt);
82*bdd1243dSDimitry Andric     ZExt.eraseFromParent();
83fcaf7f86SDimitry Andric     ++NumZExtToSExt;
84fcaf7f86SDimitry Andric     return true;
85fcaf7f86SDimitry Andric   }
86fcaf7f86SDimitry Andric 
87972a253aSDimitry Andric   // Convert (zext (abs(i32 X, i1 1))) -> (sext (abs(i32 X, i1 1))). If abs of
88972a253aSDimitry Andric   // INT_MIN is poison, the sign bit is zero.
89972a253aSDimitry Andric   using namespace PatternMatch;
90972a253aSDimitry Andric   if (match(Src, m_Intrinsic<Intrinsic::abs>(m_Value(), m_One()))) {
91*bdd1243dSDimitry Andric     auto *SExt = new SExtInst(Src, ZExt.getType(), "", &ZExt);
92*bdd1243dSDimitry Andric     SExt->takeName(&ZExt);
93*bdd1243dSDimitry Andric     SExt->setDebugLoc(ZExt.getDebugLoc());
94972a253aSDimitry Andric 
95*bdd1243dSDimitry Andric     ZExt.replaceAllUsesWith(SExt);
96*bdd1243dSDimitry Andric     ZExt.eraseFromParent();
97972a253aSDimitry Andric     ++NumZExtToSExt;
98972a253aSDimitry Andric     return true;
99972a253aSDimitry Andric   }
100972a253aSDimitry Andric 
101fcaf7f86SDimitry Andric   return false;
102fcaf7f86SDimitry Andric }
103fcaf7f86SDimitry Andric 
104fcaf7f86SDimitry Andric // Try to optimize (i64 (and (zext/sext (i32 X), C1))) if C1 has bit 31 set,
105fcaf7f86SDimitry Andric // but bits 63:32 are zero. If we can prove that bit 31 of X is 0, we can fill
106fcaf7f86SDimitry Andric // the upper 32 bits with ones. A separate transform will turn (zext X) into
107fcaf7f86SDimitry Andric // (sext X) for the same condition.
108*bdd1243dSDimitry Andric bool RISCVCodeGenPrepare::visitAnd(BinaryOperator &BO) {
109fcaf7f86SDimitry Andric   if (!ST->is64Bit())
110fcaf7f86SDimitry Andric     return false;
111fcaf7f86SDimitry Andric 
112*bdd1243dSDimitry Andric   if (!BO.getType()->isIntegerTy(64))
113fcaf7f86SDimitry Andric     return false;
114fcaf7f86SDimitry Andric 
115fcaf7f86SDimitry Andric   // Left hand side should be sext or zext.
116*bdd1243dSDimitry Andric   Instruction *LHS = dyn_cast<Instruction>(BO.getOperand(0));
117fcaf7f86SDimitry Andric   if (!LHS || (!isa<SExtInst>(LHS) && !isa<ZExtInst>(LHS)))
118fcaf7f86SDimitry Andric     return false;
119fcaf7f86SDimitry Andric 
120fcaf7f86SDimitry Andric   Value *LHSSrc = LHS->getOperand(0);
121fcaf7f86SDimitry Andric   if (!LHSSrc->getType()->isIntegerTy(32))
122fcaf7f86SDimitry Andric     return false;
123fcaf7f86SDimitry Andric 
124fcaf7f86SDimitry Andric   // Right hand side should be a constant.
125*bdd1243dSDimitry Andric   Value *RHS = BO.getOperand(1);
126fcaf7f86SDimitry Andric 
127fcaf7f86SDimitry Andric   auto *CI = dyn_cast<ConstantInt>(RHS);
128fcaf7f86SDimitry Andric   if (!CI)
129fcaf7f86SDimitry Andric     return false;
130fcaf7f86SDimitry Andric   uint64_t C = CI->getZExtValue();
131fcaf7f86SDimitry Andric 
132fcaf7f86SDimitry Andric   // Look for constants that fit in 32 bits but not simm12, and can be made
133fcaf7f86SDimitry Andric   // into simm12 by sign extending bit 31. This will allow use of ANDI.
134fcaf7f86SDimitry Andric   // TODO: Is worth making simm32?
135fcaf7f86SDimitry Andric   if (!isUInt<32>(C) || isInt<12>(C) || !isInt<12>(SignExtend64<32>(C)))
136fcaf7f86SDimitry Andric     return false;
137fcaf7f86SDimitry Andric 
138fcaf7f86SDimitry Andric   // If we can determine the sign bit of the input is 0, we can replace the
139fcaf7f86SDimitry Andric   // And mask constant.
140fcaf7f86SDimitry Andric   if (!isImpliedByDomCondition(ICmpInst::ICMP_SGE, LHSSrc,
141fcaf7f86SDimitry Andric                                Constant::getNullValue(LHSSrc->getType()),
142a4a491e2SDimitry Andric                                LHS, *DL).value_or(false))
143fcaf7f86SDimitry Andric     return false;
144fcaf7f86SDimitry Andric 
145fcaf7f86SDimitry Andric   // Sign extend the constant and replace the And operand.
146fcaf7f86SDimitry Andric   C = SignExtend64<32>(C);
147*bdd1243dSDimitry Andric   BO.setOperand(1, ConstantInt::get(LHS->getType(), C));
148fcaf7f86SDimitry Andric 
149fcaf7f86SDimitry Andric   return true;
150fcaf7f86SDimitry Andric }
151fcaf7f86SDimitry Andric 
152fcaf7f86SDimitry Andric bool RISCVCodeGenPrepare::runOnFunction(Function &F) {
153fcaf7f86SDimitry Andric   if (skipFunction(F))
154fcaf7f86SDimitry Andric     return false;
155fcaf7f86SDimitry Andric 
156fcaf7f86SDimitry Andric   auto &TPC = getAnalysis<TargetPassConfig>();
157fcaf7f86SDimitry Andric   auto &TM = TPC.getTM<RISCVTargetMachine>();
158fcaf7f86SDimitry Andric   ST = &TM.getSubtarget<RISCVSubtarget>(F);
159fcaf7f86SDimitry Andric 
160fcaf7f86SDimitry Andric   DL = &F.getParent()->getDataLayout();
161fcaf7f86SDimitry Andric 
162fcaf7f86SDimitry Andric   bool MadeChange = false;
163*bdd1243dSDimitry Andric   for (auto &BB : F)
164*bdd1243dSDimitry Andric     for (Instruction &I : llvm::make_early_inc_range(BB))
165*bdd1243dSDimitry Andric       MadeChange |= visit(I);
166fcaf7f86SDimitry Andric 
167fcaf7f86SDimitry Andric   return MadeChange;
168fcaf7f86SDimitry Andric }
169fcaf7f86SDimitry Andric 
170fcaf7f86SDimitry Andric INITIALIZE_PASS_BEGIN(RISCVCodeGenPrepare, DEBUG_TYPE, PASS_NAME, false, false)
171fcaf7f86SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
172fcaf7f86SDimitry Andric INITIALIZE_PASS_END(RISCVCodeGenPrepare, DEBUG_TYPE, PASS_NAME, false, false)
173fcaf7f86SDimitry Andric 
174fcaf7f86SDimitry Andric char RISCVCodeGenPrepare::ID = 0;
175fcaf7f86SDimitry Andric 
176fcaf7f86SDimitry Andric FunctionPass *llvm::createRISCVCodeGenPreparePass() {
177fcaf7f86SDimitry Andric   return new RISCVCodeGenPrepare();
178fcaf7f86SDimitry Andric }
179