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