blob: 7069ad36e21ab1728e86b9f299147e76e12418e6 [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 {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000027 FunctionPass *createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM);
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000028 void initializeHexagonRemoveExtendArgsPass(PassRegistry&);
29}
30
Tony Linthicum1213a7a2011-12-12 21:14:40 +000031namespace {
32 struct HexagonRemoveExtendArgs : public FunctionPass {
33 public:
34 static char ID;
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000035 HexagonRemoveExtendArgs() : FunctionPass(ID) {
36 initializeHexagonRemoveExtendArgsPass(*PassRegistry::getPassRegistry());
37 }
Craig Topper906c2cd2014-04-29 07:58:16 +000038 bool runOnFunction(Function &F) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000039
Craig Topper906c2cd2014-04-29 07:58:16 +000040 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000041 return "Remove sign extends";
42 }
43
Craig Topper906c2cd2014-04-29 07:58:16 +000044 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000045 AU.addRequired<MachineFunctionAnalysis>();
46 AU.addPreserved<MachineFunctionAnalysis>();
Chandler Carruthb81dfa62015-01-28 04:57:56 +000047 AU.addPreserved<StackProtector>();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000048 FunctionPass::getAnalysisUsage(AU);
49 }
50 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000051}
Tony Linthicum1213a7a2011-12-12 21:14:40 +000052
53char HexagonRemoveExtendArgs::ID = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000054
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000055INITIALIZE_PASS(HexagonRemoveExtendArgs, "reargs",
56 "Remove Sign and Zero Extends for Args", false, false)
Tony Linthicum1213a7a2011-12-12 21:14:40 +000057
58bool HexagonRemoveExtendArgs::runOnFunction(Function &F) {
59 unsigned Idx = 1;
60 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
61 ++AI, ++Idx) {
Bill Wendling94dcaf82012-12-30 12:45:13 +000062 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000063 Argument* Arg = AI;
64 if (!isa<PointerType>(Arg->getType())) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000065 for (auto UI = Arg->user_begin(); UI != Arg->user_end();) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000066 if (isa<SExtInst>(*UI)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000067 Instruction* I = cast<Instruction>(*UI);
68 SExtInst* SI = new SExtInst(Arg, I->getType());
Tony Linthicum1213a7a2011-12-12 21:14:40 +000069 assert (EVT::getEVT(SI->getType()) ==
Chandler Carruthcdf47882014-03-09 03:16:01 +000070 (EVT::getEVT(I->getType())));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000071 ++UI;
Chandler Carruthcdf47882014-03-09 03:16:01 +000072 I->replaceAllUsesWith(SI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000073 Instruction* First = F.getEntryBlock().begin();
74 SI->insertBefore(First);
Chandler Carruthcdf47882014-03-09 03:16:01 +000075 I->eraseFromParent();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076 } else {
77 ++UI;
78 }
79 }
80 }
81 }
82 }
83 return true;
84}
85
86
87
Krzysztof Parzyszekd5007472013-05-06 18:38:37 +000088FunctionPass*
89llvm::createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090 return new HexagonRemoveExtendArgs();
91}