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