xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonOptimizeSZextends.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===- HexagonOptimizeSZextends.cpp - Remove unnecessary argument extends -===//
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 //
90b57cec5SDimitry Andric // Pass that removes sign extends for function parameters. These parameters
100b57cec5SDimitry Andric // are already sign extended by the caller per Hexagon's ABI
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
14e8d8bef9SDimitry Andric #include "Hexagon.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/StackProtector.h"
16e8d8bef9SDimitry Andric #include "llvm/CodeGen/ValueTypes.h"
170b57cec5SDimitry Andric #include "llvm/IR/Function.h"
180b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
190b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
20480093f4SDimitry Andric #include "llvm/IR/IntrinsicsHexagon.h"
210b57cec5SDimitry Andric #include "llvm/Pass.h"
220b57cec5SDimitry Andric #include "llvm/Transforms/Scalar.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric namespace llvm {
270b57cec5SDimitry Andric   FunctionPass *createHexagonOptimizeSZextends();
280b57cec5SDimitry Andric   void initializeHexagonOptimizeSZextendsPass(PassRegistry&);
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric namespace {
320b57cec5SDimitry Andric   struct HexagonOptimizeSZextends : public FunctionPass {
330b57cec5SDimitry Andric   public:
340b57cec5SDimitry Andric     static char ID;
HexagonOptimizeSZextends__anon16852a220111::HexagonOptimizeSZextends350b57cec5SDimitry Andric     HexagonOptimizeSZextends() : FunctionPass(ID) {
360b57cec5SDimitry Andric       initializeHexagonOptimizeSZextendsPass(*PassRegistry::getPassRegistry());
370b57cec5SDimitry Andric     }
380b57cec5SDimitry Andric     bool runOnFunction(Function &F) override;
390b57cec5SDimitry Andric 
getPassName__anon16852a220111::HexagonOptimizeSZextends400b57cec5SDimitry Andric     StringRef getPassName() const override { return "Remove sign extends"; }
410b57cec5SDimitry Andric 
getAnalysisUsage__anon16852a220111::HexagonOptimizeSZextends420b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
430b57cec5SDimitry Andric       AU.addPreserved<StackProtector>();
440b57cec5SDimitry Andric       FunctionPass::getAnalysisUsage(AU);
450b57cec5SDimitry Andric     }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric     bool intrinsicAlreadySextended(Intrinsic::ID IntID);
480b57cec5SDimitry Andric   };
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric char HexagonOptimizeSZextends::ID = 0;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric INITIALIZE_PASS(HexagonOptimizeSZextends, "reargs",
540b57cec5SDimitry Andric                 "Remove Sign and Zero Extends for Args", false, false)
550b57cec5SDimitry Andric 
intrinsicAlreadySextended(Intrinsic::ID IntID)560b57cec5SDimitry Andric bool HexagonOptimizeSZextends::intrinsicAlreadySextended(Intrinsic::ID IntID) {
570b57cec5SDimitry Andric   switch(IntID) {
580b57cec5SDimitry Andric     case llvm::Intrinsic::hexagon_A2_addh_l16_sat_ll:
590b57cec5SDimitry Andric       return true;
600b57cec5SDimitry Andric     default:
610b57cec5SDimitry Andric       break;
620b57cec5SDimitry Andric   }
630b57cec5SDimitry Andric   return false;
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
runOnFunction(Function & F)660b57cec5SDimitry Andric bool HexagonOptimizeSZextends::runOnFunction(Function &F) {
670b57cec5SDimitry Andric   if (skipFunction(F))
680b57cec5SDimitry Andric     return false;
690b57cec5SDimitry Andric 
70*349cc55cSDimitry Andric   unsigned Idx = 0;
710b57cec5SDimitry Andric   // Try to optimize sign extends in formal parameters. It's relying on
720b57cec5SDimitry Andric   // callee already sign extending the values. I'm not sure if our ABI
730b57cec5SDimitry Andric   // requires callee to sign extend though.
740b57cec5SDimitry Andric   for (auto &Arg : F.args()) {
75*349cc55cSDimitry Andric     if (F.getAttributes().hasParamAttr(Idx, Attribute::SExt)) {
760b57cec5SDimitry Andric       if (!isa<PointerType>(Arg.getType())) {
77*349cc55cSDimitry Andric         for (Use &U : llvm::make_early_inc_range(Arg.uses())) {
78*349cc55cSDimitry Andric           if (isa<SExtInst>(U)) {
79*349cc55cSDimitry Andric             Instruction* Use = cast<Instruction>(U);
800b57cec5SDimitry Andric             SExtInst* SI = new SExtInst(&Arg, Use->getType());
810b57cec5SDimitry Andric             assert (EVT::getEVT(SI->getType()) ==
820b57cec5SDimitry Andric                     (EVT::getEVT(Use->getType())));
830b57cec5SDimitry Andric             Use->replaceAllUsesWith(SI);
840b57cec5SDimitry Andric             Instruction* First = &F.getEntryBlock().front();
850b57cec5SDimitry Andric             SI->insertBefore(First);
860b57cec5SDimitry Andric             Use->eraseFromParent();
870b57cec5SDimitry Andric           }
880b57cec5SDimitry Andric         }
890b57cec5SDimitry Andric       }
900b57cec5SDimitry Andric     }
910b57cec5SDimitry Andric     ++Idx;
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   // Try to remove redundant sext operations on Hexagon. The hardware
950b57cec5SDimitry Andric   // already sign extends many 16 bit intrinsic operations to 32 bits.
960b57cec5SDimitry Andric   // For example:
970b57cec5SDimitry Andric   // %34 = tail call i32 @llvm.hexagon.A2.addh.l16.sat.ll(i32 %x, i32 %y)
980b57cec5SDimitry Andric   // %sext233 = shl i32 %34, 16
990b57cec5SDimitry Andric   // %conv52 = ashr exact i32 %sext233, 16
1000b57cec5SDimitry Andric   for (auto &B : F) {
1010b57cec5SDimitry Andric     for (auto &I : B) {
1020b57cec5SDimitry Andric       // Look for arithmetic shift right by 16.
1030b57cec5SDimitry Andric       BinaryOperator *Ashr = dyn_cast<BinaryOperator>(&I);
1040b57cec5SDimitry Andric       if (!(Ashr && Ashr->getOpcode() == Instruction::AShr))
1050b57cec5SDimitry Andric         continue;
1060b57cec5SDimitry Andric       Value *AshrOp1 = Ashr->getOperand(1);
1070b57cec5SDimitry Andric       ConstantInt *C = dyn_cast<ConstantInt>(AshrOp1);
1080b57cec5SDimitry Andric       // Right shifted by 16.
1090b57cec5SDimitry Andric       if (!(C && C->getSExtValue() == 16))
1100b57cec5SDimitry Andric         continue;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric       // The first operand of Ashr comes from logical shift left.
1130b57cec5SDimitry Andric       Instruction *Shl = dyn_cast<Instruction>(Ashr->getOperand(0));
1140b57cec5SDimitry Andric       if (!(Shl && Shl->getOpcode() == Instruction::Shl))
1150b57cec5SDimitry Andric         continue;
1160b57cec5SDimitry Andric       Value *Intr = Shl->getOperand(0);
1170b57cec5SDimitry Andric       Value *ShlOp1 = Shl->getOperand(1);
1180b57cec5SDimitry Andric       C = dyn_cast<ConstantInt>(ShlOp1);
1190b57cec5SDimitry Andric       // Left shifted by 16.
1200b57cec5SDimitry Andric       if (!(C && C->getSExtValue() == 16))
1210b57cec5SDimitry Andric         continue;
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric       // The first operand of Shl comes from an intrinsic.
1240b57cec5SDimitry Andric       if (IntrinsicInst *I = dyn_cast<IntrinsicInst>(Intr)) {
1250b57cec5SDimitry Andric         if (!intrinsicAlreadySextended(I->getIntrinsicID()))
1260b57cec5SDimitry Andric           continue;
1270b57cec5SDimitry Andric         // All is well. Replace all uses of AShr with I.
1280b57cec5SDimitry Andric         for (auto UI = Ashr->user_begin(), UE = Ashr->user_end();
1290b57cec5SDimitry Andric              UI != UE; ++UI) {
1300b57cec5SDimitry Andric           const Use &TheUse = UI.getUse();
1310b57cec5SDimitry Andric           if (Instruction *J = dyn_cast<Instruction>(TheUse.getUser())) {
1320b57cec5SDimitry Andric             J->replaceUsesOfWith(Ashr, I);
1330b57cec5SDimitry Andric           }
1340b57cec5SDimitry Andric         }
1350b57cec5SDimitry Andric       }
1360b57cec5SDimitry Andric     }
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   return true;
1400b57cec5SDimitry Andric }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric 
createHexagonOptimizeSZextends()1430b57cec5SDimitry Andric FunctionPass *llvm::createHexagonOptimizeSZextends() {
1440b57cec5SDimitry Andric   return new HexagonOptimizeSZextends();
1450b57cec5SDimitry Andric }
146