66a00e12dd09a45c77005dbeca0694940214156c
[oota-llvm.git] / lib / Target / Hexagon / HexagonRemoveSZExtArgs.cpp
1 //===- 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 #include "HexagonTargetMachine.h"
16 #include "llvm/Function.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Pass.h"
19 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
20 #include "llvm/Transforms/Scalar.h"
21
22 using namespace llvm;
23 namespace {
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
42 char HexagonRemoveExtendArgs::ID = 0;
43 RegisterPass<HexagonRemoveExtendArgs> X("reargs",
44                                         "Remove Sign and Zero Extends for Args"
45                                         );
46
47
48
49 bool 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
80 FunctionPass *llvm::createHexagonRemoveExtendOps(HexagonTargetMachine &TM) {
81   return new HexagonRemoveExtendArgs();
82 }