blob: 101de3d8fbee70cda3ac51e6853f593275a21569 [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
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +000015#include "llvm/CodeGen/StackProtector.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/Instructions.h"
18#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/Pass.h"
20#include "llvm/Transforms/Scalar.h"
21
22#include "Hexagon.h"
23
24using namespace llvm;
25
26namespace llvm {
27 FunctionPass *createHexagonOptimizeSZextends();
28 void initializeHexagonOptimizeSZextendsPass(PassRegistry&);
29}
30
31namespace {
32 struct HexagonOptimizeSZextends : public FunctionPass {
33 public:
34 static char ID;
35 HexagonOptimizeSZextends() : FunctionPass(ID) {
36 initializeHexagonOptimizeSZextendsPass(*PassRegistry::getPassRegistry());
37 }
38 bool runOnFunction(Function &F) override;
39
Mehdi Amini117296c2016-10-01 02:56:57 +000040 StringRef getPassName() const override { return "Remove sign extends"; }
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +000041
42 void getAnalysisUsage(AnalysisUsage &AU) const override {
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +000043 AU.addPreserved<StackProtector>();
44 FunctionPass::getAnalysisUsage(AU);
45 }
46
47 bool intrinsicAlreadySextended(Intrinsic::ID IntID);
48 };
49}
50
51char HexagonOptimizeSZextends::ID = 0;
52
53INITIALIZE_PASS(HexagonOptimizeSZextends, "reargs",
54 "Remove Sign and Zero Extends for Args", false, false)
55
56bool HexagonOptimizeSZextends::intrinsicAlreadySextended(Intrinsic::ID IntID) {
57 switch(IntID) {
58 case llvm::Intrinsic::hexagon_A2_addh_l16_sat_ll:
59 return true;
60 default:
61 break;
62 }
63 return false;
64}
65
66bool HexagonOptimizeSZextends::runOnFunction(Function &F) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +000067 if (skipFunction(F))
68 return false;
69
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +000070 unsigned Idx = 1;
71 // Try to optimize sign extends in formal parameters. It's relying on
72 // callee already sign extending the values. I'm not sure if our ABI
73 // requires callee to sign extend though.
74 for (auto &Arg : F.args()) {
75 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) {
76 if (!isa<PointerType>(Arg.getType())) {
77 for (auto UI = Arg.use_begin(); UI != Arg.use_end();) {
78 if (isa<SExtInst>(*UI)) {
79 Instruction* Use = cast<Instruction>(*UI);
80 SExtInst* SI = new SExtInst(&Arg, Use->getType());
81 assert (EVT::getEVT(SI->getType()) ==
82 (EVT::getEVT(Use->getType())));
83 ++UI;
84 Use->replaceAllUsesWith(SI);
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +000085 Instruction* First = &F.getEntryBlock().front();
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +000086 SI->insertBefore(First);
87 Use->eraseFromParent();
88 } else {
89 ++UI;
90 }
91 }
92 }
93 }
94 ++Idx;
95 }
96
97 // Try to remove redundant sext operations on Hexagon. The hardware
98 // already sign extends many 16 bit intrinsic operations to 32 bits.
99 // For example:
100 // %34 = tail call i32 @llvm.hexagon.A2.addh.l16.sat.ll(i32 %x, i32 %y)
101 // %sext233 = shl i32 %34, 16
102 // %conv52 = ashr exact i32 %sext233, 16
103 for (auto &B : F) {
104 for (auto &I : B) {
105 // Look for arithmetic shift right by 16.
106 BinaryOperator *Ashr = dyn_cast<BinaryOperator>(&I);
107 if (!(Ashr && Ashr->getOpcode() == Instruction::AShr))
108 continue;
109 Value *AshrOp1 = Ashr->getOperand(1);
110 ConstantInt *C = dyn_cast<ConstantInt>(AshrOp1);
111 // Right shifted by 16.
112 if (!(C && C->getSExtValue() == 16))
113 continue;
114
115 // The first operand of Ashr comes from logical shift left.
116 Instruction *Shl = dyn_cast<Instruction>(Ashr->getOperand(0));
117 if (!(Shl && Shl->getOpcode() == Instruction::Shl))
118 continue;
119 Value *Intr = Shl->getOperand(0);
120 Value *ShlOp1 = Shl->getOperand(1);
121 C = dyn_cast<ConstantInt>(ShlOp1);
122 // Left shifted by 16.
123 if (!(C && C->getSExtValue() == 16))
124 continue;
125
126 // The first operand of Shl comes from an intrinsic.
127 if (IntrinsicInst *I = dyn_cast<IntrinsicInst>(Intr)) {
128 if (!intrinsicAlreadySextended(I->getIntrinsicID()))
129 continue;
130 // All is well. Replace all uses of AShr with I.
131 for (auto UI = Ashr->user_begin(), UE = Ashr->user_end();
132 UI != UE; ++UI) {
133 const Use &TheUse = UI.getUse();
134 if (Instruction *J = dyn_cast<Instruction>(TheUse.getUser())) {
135 J->replaceUsesOfWith(Ashr, I);
136 }
137 }
138 }
139 }
140 }
141
142 return true;
143}
144
145
146FunctionPass *llvm::createHexagonOptimizeSZextends() {
147 return new HexagonOptimizeSZextends();
148}