• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- HexagonOptimizeSZextends.cpp - Remove unnecessary argument extends -===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Pass that removes sign extends for function parameters. These parameters
11 // are already sign extended by the caller per Hexagon's ABI
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
16 #include "llvm/CodeGen/StackProtector.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Transforms/Scalar.h"
22 
23 #include "Hexagon.h"
24 
25 using namespace llvm;
26 
27 namespace llvm {
28   FunctionPass *createHexagonOptimizeSZextends();
29   void initializeHexagonOptimizeSZextendsPass(PassRegistry&);
30 }
31 
32 namespace {
33   struct HexagonOptimizeSZextends : public FunctionPass {
34   public:
35     static char ID;
HexagonOptimizeSZextends__anone32ffb230111::HexagonOptimizeSZextends36     HexagonOptimizeSZextends() : FunctionPass(ID) {
37       initializeHexagonOptimizeSZextendsPass(*PassRegistry::getPassRegistry());
38     }
39     bool runOnFunction(Function &F) override;
40 
getPassName__anone32ffb230111::HexagonOptimizeSZextends41     const char *getPassName() const override {
42       return "Remove sign extends";
43     }
44 
getAnalysisUsage__anone32ffb230111::HexagonOptimizeSZextends45     void getAnalysisUsage(AnalysisUsage &AU) const override {
46       AU.addRequired<MachineFunctionAnalysis>();
47       AU.addPreserved<MachineFunctionAnalysis>();
48       AU.addPreserved<StackProtector>();
49       FunctionPass::getAnalysisUsage(AU);
50     }
51 
52     bool intrinsicAlreadySextended(Intrinsic::ID IntID);
53   };
54 }
55 
56 char HexagonOptimizeSZextends::ID = 0;
57 
58 INITIALIZE_PASS(HexagonOptimizeSZextends, "reargs",
59                 "Remove Sign and Zero Extends for Args", false, false)
60 
intrinsicAlreadySextended(Intrinsic::ID IntID)61 bool HexagonOptimizeSZextends::intrinsicAlreadySextended(Intrinsic::ID IntID) {
62   switch(IntID) {
63     case llvm::Intrinsic::hexagon_A2_addh_l16_sat_ll:
64       return true;
65     default:
66       break;
67   }
68   return false;
69 }
70 
runOnFunction(Function & F)71 bool HexagonOptimizeSZextends::runOnFunction(Function &F) {
72   if (skipFunction(F))
73     return false;
74 
75   unsigned Idx = 1;
76   // Try to optimize sign extends in formal parameters. It's relying on
77   // callee already sign extending the values. I'm not sure if our ABI
78   // requires callee to sign extend though.
79   for (auto &Arg : F.args()) {
80     if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) {
81       if (!isa<PointerType>(Arg.getType())) {
82         for (auto UI = Arg.use_begin(); UI != Arg.use_end();) {
83           if (isa<SExtInst>(*UI)) {
84             Instruction* Use = cast<Instruction>(*UI);
85             SExtInst* SI = new SExtInst(&Arg, Use->getType());
86             assert (EVT::getEVT(SI->getType()) ==
87                     (EVT::getEVT(Use->getType())));
88             ++UI;
89             Use->replaceAllUsesWith(SI);
90             Instruction* First = &F.getEntryBlock().front();
91             SI->insertBefore(First);
92             Use->eraseFromParent();
93           } else {
94             ++UI;
95           }
96         }
97       }
98     }
99     ++Idx;
100   }
101 
102   // Try to remove redundant sext operations on Hexagon. The hardware
103   // already sign extends many 16 bit intrinsic operations to 32 bits.
104   // For example:
105   // %34 = tail call i32 @llvm.hexagon.A2.addh.l16.sat.ll(i32 %x, i32 %y)
106   // %sext233 = shl i32 %34, 16
107   // %conv52 = ashr exact i32 %sext233, 16
108   for (auto &B : F) {
109     for (auto &I : B) {
110       // Look for arithmetic shift right by 16.
111       BinaryOperator *Ashr = dyn_cast<BinaryOperator>(&I);
112       if (!(Ashr && Ashr->getOpcode() == Instruction::AShr))
113         continue;
114       Value *AshrOp1 = Ashr->getOperand(1);
115       ConstantInt *C = dyn_cast<ConstantInt>(AshrOp1);
116       // Right shifted by 16.
117       if (!(C && C->getSExtValue() == 16))
118         continue;
119 
120       // The first operand of Ashr comes from logical shift left.
121       Instruction *Shl = dyn_cast<Instruction>(Ashr->getOperand(0));
122       if (!(Shl && Shl->getOpcode() == Instruction::Shl))
123         continue;
124       Value *Intr = Shl->getOperand(0);
125       Value *ShlOp1 = Shl->getOperand(1);
126       C = dyn_cast<ConstantInt>(ShlOp1);
127       // Left shifted by 16.
128       if (!(C && C->getSExtValue() == 16))
129         continue;
130 
131       // The first operand of Shl comes from an intrinsic.
132       if (IntrinsicInst *I = dyn_cast<IntrinsicInst>(Intr)) {
133         if (!intrinsicAlreadySextended(I->getIntrinsicID()))
134           continue;
135         // All is well. Replace all uses of AShr with I.
136         for (auto UI = Ashr->user_begin(), UE = Ashr->user_end();
137              UI != UE; ++UI) {
138           const Use &TheUse = UI.getUse();
139           if (Instruction *J = dyn_cast<Instruction>(TheUse.getUser())) {
140             J->replaceUsesOfWith(Ashr, I);
141           }
142         }
143       }
144     }
145   }
146 
147   return true;
148 }
149 
150 
createHexagonOptimizeSZextends()151 FunctionPass *llvm::createHexagonOptimizeSZextends() {
152   return new HexagonOptimizeSZextends();
153 }
154