blob: 2468f0b86f8817760ea5909838d778a191261c4c [file] [log] [blame]
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00001//===- HexagonRemoveExtendArgs.cpp - Remove unnecessary argument sign extends //
Tony Linthicumb4b54152011-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
Benjamin Kramerf3fd7ee2012-02-06 10:19:29 +000015#include "HexagonTargetMachine.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000016#include "llvm/Function.h"
17#include "llvm/Instructions.h"
Benjamin Kramerf3fd7ee2012-02-06 10:19:29 +000018#include "llvm/Pass.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000019#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Benjamin Kramerf3fd7ee2012-02-06 10:19:29 +000020#include "llvm/Transforms/Scalar.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000021
22using namespace llvm;
23namespace {
24 struct HexagonRemoveExtendArgs : public FunctionPass {
25 public:
26 static char ID;
27 HexagonRemoveExtendArgs() : FunctionPass(ID) {}
28 virtual bool runOnFunction(Function &F);
29
30 const char *getPassName() const {
31 return "Remove sign extends";
32 }
33
34 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
35 AU.addRequired<MachineFunctionAnalysis>();
36 AU.addPreserved<MachineFunctionAnalysis>();
37 FunctionPass::getAnalysisUsage(AU);
38 }
39 };
40}
41
42char HexagonRemoveExtendArgs::ID = 0;
43RegisterPass<HexagonRemoveExtendArgs> X("reargs",
44 "Remove Sign and Zero Extends for Args"
45 );
46
47
48
49bool HexagonRemoveExtendArgs::runOnFunction(Function &F) {
50 unsigned Idx = 1;
51 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
52 ++AI, ++Idx) {
53 if (F.paramHasAttr(Idx, Attribute::SExt)) {
54 Argument* Arg = AI;
55 if (!isa<PointerType>(Arg->getType())) {
56 for (Instruction::use_iterator UI = Arg->use_begin();
57 UI != Arg->use_end();) {
58 if (isa<SExtInst>(*UI)) {
59 Instruction* Use = cast<Instruction>(*UI);
60 SExtInst* SI = new SExtInst(Arg, Use->getType());
61 assert (EVT::getEVT(SI->getType()) ==
62 (EVT::getEVT(Use->getType())));
63 ++UI;
64 Use->replaceAllUsesWith(SI);
65 Instruction* First = F.getEntryBlock().begin();
66 SI->insertBefore(First);
67 Use->eraseFromParent();
68 } else {
69 ++UI;
70 }
71 }
72 }
73 }
74 }
75 return true;
76}
77
78
79
80FunctionPass *llvm::createHexagonRemoveExtendOps(HexagonTargetMachine &TM) {
81 return new HexagonRemoveExtendArgs();
82}