LLVM API Documentation

HexagonRemoveSZExtArgs.cpp
Go to the documentation of this file.
00001 //===- HexagonRemoveExtendArgs.cpp - Remove unnecessary argument sign extends //
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Pass that removes sign extends for function parameters. These parameters
00011 // are already sign extended by the caller per Hexagon's ABI
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "Hexagon.h"
00016 #include "HexagonTargetMachine.h"
00017 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
00018 #include "llvm/IR/Function.h"
00019 #include "llvm/IR/Instructions.h"
00020 #include "llvm/Pass.h"
00021 #include "llvm/Transforms/Scalar.h"
00022 
00023 using namespace llvm;
00024 
00025 namespace llvm {
00026   void initializeHexagonRemoveExtendArgsPass(PassRegistry&);
00027 }
00028 
00029 namespace {
00030   struct HexagonRemoveExtendArgs : public FunctionPass {
00031   public:
00032     static char ID;
00033     HexagonRemoveExtendArgs() : FunctionPass(ID) {
00034       initializeHexagonRemoveExtendArgsPass(*PassRegistry::getPassRegistry());
00035     }
00036     bool runOnFunction(Function &F) override;
00037 
00038     const char *getPassName() const override {
00039       return "Remove sign extends";
00040     }
00041 
00042     void getAnalysisUsage(AnalysisUsage &AU) const override {
00043       AU.addRequired<MachineFunctionAnalysis>();
00044       AU.addPreserved<MachineFunctionAnalysis>();
00045       AU.addPreserved("stack-protector");
00046       FunctionPass::getAnalysisUsage(AU);
00047     }
00048   };
00049 }
00050 
00051 char HexagonRemoveExtendArgs::ID = 0;
00052 
00053 INITIALIZE_PASS(HexagonRemoveExtendArgs, "reargs",
00054                 "Remove Sign and Zero Extends for Args", false, false)
00055 
00056 bool HexagonRemoveExtendArgs::runOnFunction(Function &F) {
00057   unsigned Idx = 1;
00058   for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
00059        ++AI, ++Idx) {
00060     if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) {
00061       Argument* Arg = AI;
00062       if (!isa<PointerType>(Arg->getType())) {
00063         for (auto UI = Arg->user_begin(); UI != Arg->user_end();) {
00064           if (isa<SExtInst>(*UI)) {
00065             Instruction* I = cast<Instruction>(*UI);
00066             SExtInst* SI = new SExtInst(Arg, I->getType());
00067             assert (EVT::getEVT(SI->getType()) ==
00068                     (EVT::getEVT(I->getType())));
00069             ++UI;
00070             I->replaceAllUsesWith(SI);
00071             Instruction* First = F.getEntryBlock().begin();
00072             SI->insertBefore(First);
00073             I->eraseFromParent();
00074           } else {
00075             ++UI;
00076           }
00077         }
00078       }
00079     }
00080   }
00081   return true;
00082 }
00083 
00084 
00085 
00086 FunctionPass*
00087 llvm::createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM) {
00088   return new HexagonRemoveExtendArgs();
00089 }