blob: 0c2407508869b8cd42b40450ed6e8da3ce1eb51a [file] [log] [blame]
Benjamin Kramerbde91762012-06-02 10:20:22 +00001//===- HexagonRemoveExtendArgs.cpp - Remove unnecessary argument sign extends //
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
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
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "Hexagon.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000016#include "HexagonTargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Chandler Carruthb81dfa62015-01-28 04:57:56 +000018#include "llvm/CodeGen/StackProtector.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Function.h"
20#include "llvm/IR/Instructions.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000021#include "llvm/Pass.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000022#include "llvm/Transforms/Scalar.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023
24using namespace llvm;
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000025
26namespace llvm {
27 void initializeHexagonRemoveExtendArgsPass(PassRegistry&);
28}
29
Tony Linthicum1213a7a2011-12-12 21:14:40 +000030namespace {
31 struct HexagonRemoveExtendArgs : public FunctionPass {
32 public:
33 static char ID;
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000034 HexagonRemoveExtendArgs() : FunctionPass(ID) {
35 initializeHexagonRemoveExtendArgsPass(*PassRegistry::getPassRegistry());
36 }
Craig Topper906c2cd2014-04-29 07:58:16 +000037 bool runOnFunction(Function &F) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000038
Craig Topper906c2cd2014-04-29 07:58:16 +000039 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000040 return "Remove sign extends";
41 }
42
Craig Topper906c2cd2014-04-29 07:58:16 +000043 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000044 AU.addRequired<MachineFunctionAnalysis>();
45 AU.addPreserved<MachineFunctionAnalysis>();
Chandler Carruthb81dfa62015-01-28 04:57:56 +000046 AU.addPreserved<StackProtector>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000047 FunctionPass::getAnalysisUsage(AU);
48 }
49 };
50}
51
52char HexagonRemoveExtendArgs::ID = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000054INITIALIZE_PASS(HexagonRemoveExtendArgs, "reargs",
55 "Remove Sign and Zero Extends for Args", false, false)
Tony Linthicum1213a7a2011-12-12 21:14:40 +000056
57bool HexagonRemoveExtendArgs::runOnFunction(Function &F) {
58 unsigned Idx = 1;
59 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
60 ++AI, ++Idx) {
Bill Wendling94dcaf82012-12-30 12:45:13 +000061 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000062 Argument* Arg = AI;
63 if (!isa<PointerType>(Arg->getType())) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000064 for (auto UI = Arg->user_begin(); UI != Arg->user_end();) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000065 if (isa<SExtInst>(*UI)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000066 Instruction* I = cast<Instruction>(*UI);
67 SExtInst* SI = new SExtInst(Arg, I->getType());
Tony Linthicum1213a7a2011-12-12 21:14:40 +000068 assert (EVT::getEVT(SI->getType()) ==
Chandler Carruthcdf47882014-03-09 03:16:01 +000069 (EVT::getEVT(I->getType())));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000070 ++UI;
Chandler Carruthcdf47882014-03-09 03:16:01 +000071 I->replaceAllUsesWith(SI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000072 Instruction* First = F.getEntryBlock().begin();
73 SI->insertBefore(First);
Chandler Carruthcdf47882014-03-09 03:16:01 +000074 I->eraseFromParent();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000075 } else {
76 ++UI;
77 }
78 }
79 }
80 }
81 }
82 return true;
83}
84
85
86
Krzysztof Parzyszekd5007472013-05-06 18:38:37 +000087FunctionPass*
88llvm::createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000089 return new HexagonRemoveExtendArgs();
90}