1 //===- HexagonRemoveExtendArgs.cpp - Remove unnecessary argument sign extends //
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Pass that removes sign extends for function parameters. These parameters
11 // are already sign extended by the caller per Hexagon's ABI
13 //===----------------------------------------------------------------------===//
16 #include "HexagonTargetMachine.h"
17 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
18 #include "llvm/CodeGen/StackProtector.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Transforms/Scalar.h"
27 FunctionPass *createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM);
28 void initializeHexagonRemoveExtendArgsPass(PassRegistry&);
32 struct HexagonRemoveExtendArgs : public FunctionPass {
35 HexagonRemoveExtendArgs() : FunctionPass(ID) {
36 initializeHexagonRemoveExtendArgsPass(*PassRegistry::getPassRegistry());
38 bool runOnFunction(Function &F) override;
40 const char *getPassName() const override {
41 return "Remove sign extends";
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.addRequired<MachineFunctionAnalysis>();
46 AU.addPreserved<MachineFunctionAnalysis>();
47 AU.addPreserved<StackProtector>();
48 FunctionPass::getAnalysisUsage(AU);
53 char HexagonRemoveExtendArgs::ID = 0;
55 INITIALIZE_PASS(HexagonRemoveExtendArgs, "reargs",
56 "Remove Sign and Zero Extends for Args", false, false)
58 bool HexagonRemoveExtendArgs::runOnFunction(Function &F) {
60 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
62 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) {
64 if (!isa<PointerType>(Arg->getType())) {
65 for (auto UI = Arg->user_begin(); UI != Arg->user_end();) {
66 if (isa<SExtInst>(*UI)) {
67 Instruction* I = cast<Instruction>(*UI);
68 SExtInst* SI = new SExtInst(Arg, I->getType());
69 assert (EVT::getEVT(SI->getType()) ==
70 (EVT::getEVT(I->getType())));
72 I->replaceAllUsesWith(SI);
73 Instruction* First = F.getEntryBlock().begin();
74 SI->insertBefore(First);
89 llvm::createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM) {
90 return new HexagonRemoveExtendArgs();