blob: 2b459a4336b359c8ffc9bcf5e9343a9c0cd09a90 [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 Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Function.h"
19#include "llvm/IR/Instructions.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000020#include "llvm/Pass.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000021#include "llvm/Transforms/Scalar.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000022
23using namespace llvm;
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000024
25namespace llvm {
26 void initializeHexagonRemoveExtendArgsPass(PassRegistry&);
27}
28
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029namespace {
30 struct HexagonRemoveExtendArgs : public FunctionPass {
31 public:
32 static char ID;
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000033 HexagonRemoveExtendArgs() : FunctionPass(ID) {
34 initializeHexagonRemoveExtendArgsPass(*PassRegistry::getPassRegistry());
35 }
Craig Topper906c2cd2014-04-29 07:58:16 +000036 bool runOnFunction(Function &F) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000037
Craig Topper906c2cd2014-04-29 07:58:16 +000038 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000039 return "Remove sign extends";
40 }
41
Craig Topper906c2cd2014-04-29 07:58:16 +000042 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000043 AU.addRequired<MachineFunctionAnalysis>();
44 AU.addPreserved<MachineFunctionAnalysis>();
Josh Magee22b8ba22013-12-19 03:17:11 +000045 AU.addPreserved("stack-protector");
Tony Linthicum1213a7a2011-12-12 21:14:40 +000046 FunctionPass::getAnalysisUsage(AU);
47 }
48 };
49}
50
51char HexagonRemoveExtendArgs::ID = 0;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000052
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000053INITIALIZE_PASS(HexagonRemoveExtendArgs, "reargs",
54 "Remove Sign and Zero Extends for Args", false, false)
Tony Linthicum1213a7a2011-12-12 21:14:40 +000055
56bool HexagonRemoveExtendArgs::runOnFunction(Function &F) {
57 unsigned Idx = 1;
58 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
59 ++AI, ++Idx) {
Bill Wendling94dcaf82012-12-30 12:45:13 +000060 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000061 Argument* Arg = AI;
62 if (!isa<PointerType>(Arg->getType())) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000063 for (auto UI = Arg->user_begin(); UI != Arg->user_end();) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000064 if (isa<SExtInst>(*UI)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +000065 Instruction* I = cast<Instruction>(*UI);
66 SExtInst* SI = new SExtInst(Arg, I->getType());
Tony Linthicum1213a7a2011-12-12 21:14:40 +000067 assert (EVT::getEVT(SI->getType()) ==
Chandler Carruthcdf47882014-03-09 03:16:01 +000068 (EVT::getEVT(I->getType())));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000069 ++UI;
Chandler Carruthcdf47882014-03-09 03:16:01 +000070 I->replaceAllUsesWith(SI);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000071 Instruction* First = F.getEntryBlock().begin();
72 SI->insertBefore(First);
Chandler Carruthcdf47882014-03-09 03:16:01 +000073 I->eraseFromParent();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000074 } else {
75 ++UI;
76 }
77 }
78 }
79 }
80 }
81 return true;
82}
83
84
85
Krzysztof Parzyszekd5007472013-05-06 18:38:37 +000086FunctionPass*
87llvm::createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088 return new HexagonRemoveExtendArgs();
89}