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