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