Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 1 | //===- HexagonRemoveExtendArgs.cpp - Remove unnecessary argument sign extends // |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "Hexagon.h" |
Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 16 | #include "HexagonTargetMachine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/StackProtector.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/Instructions.h" |
Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Benjamin Kramer | ae87d7b | 2012-02-06 10:19:29 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Scalar.h" |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
Colin LeMahieu | 56efafc | 2015-06-15 19:05:35 +0000 | [diff] [blame] | 27 | FunctionPass *createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM); |
Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 28 | void initializeHexagonRemoveExtendArgsPass(PassRegistry&); |
| 29 | } |
| 30 | |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | struct HexagonRemoveExtendArgs : public FunctionPass { |
| 33 | public: |
| 34 | static char ID; |
Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 35 | HexagonRemoveExtendArgs() : FunctionPass(ID) { |
| 36 | initializeHexagonRemoveExtendArgsPass(*PassRegistry::getPassRegistry()); |
| 37 | } |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 38 | bool runOnFunction(Function &F) override; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 39 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 40 | const char *getPassName() const override { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 41 | return "Remove sign extends"; |
| 42 | } |
| 43 | |
Craig Topper | 906c2cd | 2014-04-29 07:58:16 +0000 | [diff] [blame] | 44 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 45 | AU.addRequired<MachineFunctionAnalysis>(); |
| 46 | AU.addPreserved<MachineFunctionAnalysis>(); |
Chandler Carruth | b81dfa6 | 2015-01-28 04:57:56 +0000 | [diff] [blame] | 47 | AU.addPreserved<StackProtector>(); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 48 | FunctionPass::getAnalysisUsage(AU); |
| 49 | } |
| 50 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 51 | } |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 52 | |
| 53 | char HexagonRemoveExtendArgs::ID = 0; |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 54 | |
Krzysztof Parzyszek | 18ee119 | 2013-05-06 21:58:00 +0000 | [diff] [blame] | 55 | INITIALIZE_PASS(HexagonRemoveExtendArgs, "reargs", |
| 56 | "Remove Sign and Zero Extends for Args", false, false) |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 57 | |
| 58 | bool HexagonRemoveExtendArgs::runOnFunction(Function &F) { |
| 59 | unsigned Idx = 1; |
| 60 | for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE; |
| 61 | ++AI, ++Idx) { |
Bill Wendling | 94dcaf8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 62 | if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 63 | Argument* Arg = AI; |
| 64 | if (!isa<PointerType>(Arg->getType())) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 65 | for (auto UI = Arg->user_begin(); UI != Arg->user_end();) { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 66 | if (isa<SExtInst>(*UI)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 67 | Instruction* I = cast<Instruction>(*UI); |
| 68 | SExtInst* SI = new SExtInst(Arg, I->getType()); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 69 | assert (EVT::getEVT(SI->getType()) == |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 70 | (EVT::getEVT(I->getType()))); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 71 | ++UI; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 72 | I->replaceAllUsesWith(SI); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 73 | Instruction* First = F.getEntryBlock().begin(); |
| 74 | SI->insertBefore(First); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 75 | I->eraseFromParent(); |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 76 | } else { |
| 77 | ++UI; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
Krzysztof Parzyszek | d500747 | 2013-05-06 18:38:37 +0000 | [diff] [blame] | 88 | FunctionPass* |
| 89 | llvm::createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM) { |
Tony Linthicum | 1213a7a | 2011-12-12 21:14:40 +0000 | [diff] [blame] | 90 | return new HexagonRemoveExtendArgs(); |
| 91 | } |