blob: 7937a7908b06e592a9e2131ed8f4a7340f2c0184 [file] [log] [blame]
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +00001//===- 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
25using namespace llvm;
26
27namespace llvm {
28 FunctionPass *createHexagonOptimizeSZextends();
29 void initializeHexagonOptimizeSZextendsPass(PassRegistry&);
30}
31
32namespace {
33 struct HexagonOptimizeSZextends : public FunctionPass {
34 public:
35 static char ID;
36 HexagonOptimizeSZextends() : FunctionPass(ID) {
37 initializeHexagonOptimizeSZextendsPass(*PassRegistry::getPassRegistry());
38 }
39 bool runOnFunction(Function &F) override;
40
41 const char *getPassName() const override {
42 return "Remove sign extends";
43 }
44
45 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
56char HexagonOptimizeSZextends::ID = 0;
57
58INITIALIZE_PASS(HexagonOptimizeSZextends, "reargs",
59 "Remove Sign and Zero Extends for Args", false, false)
60
61bool 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
71bool HexagonOptimizeSZextends::runOnFunction(Function &F) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +000072 if (skipFunction(F))
73 return false;
74
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +000075 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);
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +000090 Instruction* First = &F.getEntryBlock().front();
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +000091 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
151FunctionPass *llvm::createHexagonOptimizeSZextends() {
152 return new HexagonOptimizeSZextends();
153}