LLVM 22.0.0git
HexagonOptimizeSZextends.cpp
Go to the documentation of this file.
1//===- HexagonOptimizeSZextends.cpp - Remove unnecessary argument extends -===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Pass that removes sign extends for function parameters. These parameters
10// are already sign extended by the caller per Hexagon's ABI
11//
12//===----------------------------------------------------------------------===//
13
14#include "Hexagon.h"
17#include "llvm/IR/Function.h"
20#include "llvm/IR/IntrinsicsHexagon.h"
21#include "llvm/Pass.h"
23
24using namespace llvm;
25
26namespace {
27 struct HexagonOptimizeSZextends : public FunctionPass {
28 public:
29 static char ID;
30 HexagonOptimizeSZextends() : FunctionPass(ID) {}
31 bool runOnFunction(Function &F) override;
32
33 StringRef getPassName() const override { return "Remove sign extends"; }
34
35 void getAnalysisUsage(AnalysisUsage &AU) const override {
38 }
39
40 bool intrinsicAlreadySextended(Intrinsic::ID IntID);
41 };
42}
43
44char HexagonOptimizeSZextends::ID = 0;
45
46INITIALIZE_PASS(HexagonOptimizeSZextends, "reargs",
47 "Remove Sign and Zero Extends for Args", false, false)
48
49bool HexagonOptimizeSZextends::intrinsicAlreadySextended(Intrinsic::ID IntID) {
50 switch(IntID) {
51 case llvm::Intrinsic::hexagon_A2_addh_l16_sat_ll:
52 return true;
53 default:
54 break;
55 }
56 return false;
57}
58
59bool HexagonOptimizeSZextends::runOnFunction(Function &F) {
60 if (skipFunction(F))
61 return false;
62
63 unsigned Idx = 0;
64 // Try to optimize sign extends in formal parameters. It's relying on
65 // callee already sign extending the values. I'm not sure if our ABI
66 // requires callee to sign extend though.
67 for (auto &Arg : F.args()) {
68 if (F.getAttributes().hasParamAttr(Idx, Attribute::SExt)) {
69 if (!isa<PointerType>(Arg.getType())) {
70 for (Use &U : llvm::make_early_inc_range(Arg.uses())) {
71 if (isa<SExtInst>(U)) {
72 Instruction* Use = cast<Instruction>(U);
73 SExtInst* SI = new SExtInst(&Arg, Use->getType());
74 assert (EVT::getEVT(SI->getType()) ==
75 (EVT::getEVT(Use->getType())));
77 BasicBlock::iterator First = F.getEntryBlock().begin();
78 SI->insertBefore(First);
79 Use->eraseFromParent();
80 }
81 }
82 }
83 }
84 ++Idx;
85 }
86
87 // Try to remove redundant sext operations on Hexagon. The hardware
88 // already sign extends many 16 bit intrinsic operations to 32 bits.
89 // For example:
90 // %34 = tail call i32 @llvm.hexagon.A2.addh.l16.sat.ll(i32 %x, i32 %y)
91 // %sext233 = shl i32 %34, 16
92 // %conv52 = ashr exact i32 %sext233, 16
93 for (auto &B : F) {
94 for (auto &I : B) {
95 // Look for arithmetic shift right by 16.
96 BinaryOperator *Ashr = dyn_cast<BinaryOperator>(&I);
97 if (!(Ashr && Ashr->getOpcode() == Instruction::AShr))
98 continue;
99 Value *AshrOp1 = Ashr->getOperand(1);
100 ConstantInt *C = dyn_cast<ConstantInt>(AshrOp1);
101 // Right shifted by 16.
102 if (!(C && C->getSExtValue() == 16))
103 continue;
104
105 // The first operand of Ashr comes from logical shift left.
106 Instruction *Shl = dyn_cast<Instruction>(Ashr->getOperand(0));
107 if (!(Shl && Shl->getOpcode() == Instruction::Shl))
108 continue;
109 Value *Intr = Shl->getOperand(0);
110 Value *ShlOp1 = Shl->getOperand(1);
111 C = dyn_cast<ConstantInt>(ShlOp1);
112 // Left shifted by 16.
113 if (!(C && C->getSExtValue() == 16))
114 continue;
115
116 // The first operand of Shl comes from an intrinsic.
117 if (IntrinsicInst *I = dyn_cast<IntrinsicInst>(Intr)) {
118 if (!intrinsicAlreadySextended(I->getIntrinsicID()))
119 continue;
120 // All is well. Replace all uses of AShr with I.
121 for (auto UI = Ashr->user_begin(), UE = Ashr->user_end();
122 UI != UE; ++UI) {
123 const Use &TheUse = UI.getUse();
124 if (Instruction *J = dyn_cast<Instruction>(TheUse.getUser())) {
125 J->replaceUsesOfWith(Ashr, I);
126 }
127 }
128 }
129 }
130 }
131
132 return true;
133}
134
135
137 return new HexagonOptimizeSZextends();
138}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Intr
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
Represent the analysis usage information of a pass.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
BinaryOps getOpcode() const
Definition: InstrTypes.h:374
This is the shared class of boolean and integer constants.
Definition: Constants.h:87
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:312
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:112
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
This class represents a sign extension of integer types.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:61
Value * getOperand(unsigned i) const
Definition: User.h:232
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
user_iterator user_begin()
Definition: Value.h:402
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:546
user_iterator user_end()
Definition: Value.h:410
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:663
FunctionPass * createHexagonOptimizeSZextends()
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:299