blob: 34bf4eacfdc0cf410851109a3325644aac7436af [file] [log] [blame]
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00001//===- HexagonRemoveExtendArgs.cpp - Remove unnecessary argument sign extends //
Tony Linthicumb4b54152011-12-12 21:14:40 +00002//
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 Carruthd04a8d42012-12-03 16:50:05 +000015#include "Hexagon.h"
Benjamin Kramerf3fd7ee2012-02-06 10:19:29 +000016#include "HexagonTargetMachine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Function.h"
19#include "llvm/IR/Instructions.h"
Benjamin Kramerf3fd7ee2012-02-06 10:19:29 +000020#include "llvm/Pass.h"
Benjamin Kramerf3fd7ee2012-02-06 10:19:29 +000021#include "llvm/Transforms/Scalar.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000022
23using namespace llvm;
24namespace {
25 struct HexagonRemoveExtendArgs : public FunctionPass {
26 public:
27 static char ID;
28 HexagonRemoveExtendArgs() : FunctionPass(ID) {}
29 virtual bool runOnFunction(Function &F);
30
31 const char *getPassName() const {
32 return "Remove sign extends";
33 }
34
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addRequired<MachineFunctionAnalysis>();
37 AU.addPreserved<MachineFunctionAnalysis>();
38 FunctionPass::getAnalysisUsage(AU);
39 }
40 };
41}
42
43char HexagonRemoveExtendArgs::ID = 0;
44RegisterPass<HexagonRemoveExtendArgs> X("reargs",
45 "Remove Sign and Zero Extends for Args"
46 );
47
48
49
50bool HexagonRemoveExtendArgs::runOnFunction(Function &F) {
51 unsigned Idx = 1;
52 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
53 ++AI, ++Idx) {
Bill Wendling39cd0c82012-12-30 12:45:13 +000054 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt)) {
Tony Linthicumb4b54152011-12-12 21:14:40 +000055 Argument* Arg = AI;
56 if (!isa<PointerType>(Arg->getType())) {
57 for (Instruction::use_iterator UI = Arg->use_begin();
58 UI != Arg->use_end();) {
59 if (isa<SExtInst>(*UI)) {
60 Instruction* Use = cast<Instruction>(*UI);
61 SExtInst* SI = new SExtInst(Arg, Use->getType());
62 assert (EVT::getEVT(SI->getType()) ==
63 (EVT::getEVT(Use->getType())));
64 ++UI;
65 Use->replaceAllUsesWith(SI);
66 Instruction* First = F.getEntryBlock().begin();
67 SI->insertBefore(First);
68 Use->eraseFromParent();
69 } else {
70 ++UI;
71 }
72 }
73 }
74 }
75 }
76 return true;
77}
78
79
80
81FunctionPass *llvm::createHexagonRemoveExtendOps(HexagonTargetMachine &TM) {
82 return new HexagonRemoveExtendArgs();
83}