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