OpenASIP  2.0
LowerIntrinsics.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * @file LowerIntrinsics.cc
27  *
28  * Converts llvm intrinsics to libcalls.
29  *
30  * @author Veli-Pekka Jaaskelainen 2008 (vjaaskel-no.spam-cs.tut.fi)
31  * @author Mikael Lepistö 2009 (mikael.lepisto-no.spam-tut.fi)
32  */
33 
34 #define DEBUG_TYPE "lowerintrinsics"
35 
36 #include "CompilerWarnings.hh"
37 IGNORE_COMPILER_WARNING("-Wunused-parameter")
38 
39 #include "llvm/Transforms/Scalar.h"
40 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
41 #include "tce_config.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/IR/Instructions.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/Intrinsics.h"
46 #include "llvm/IR/DerivedTypes.h"
47 #include "llvm/IR/LLVMContext.h"
48 #include "llvm/Support/Compiler.h"
49 #include "llvm/Pass.h"
50 #include "llvm/CodeGen/IntrinsicLowering.h"
51 #include "tce_config.h"
52 
54 
55 #include "llvm/IR/DataLayout.h"
56 typedef llvm::DataLayout TargetData;
57 
58 using namespace llvm;
59 
60 #include <iostream>
61 #include <set>
62 
63 namespace {
64  class LowerIntrinsics : public FunctionPass {
65  public:
66  static char ID; // Pass ID, replacement for typeid
67  LowerIntrinsics();
68  virtual ~LowerIntrinsics();
69 
70  // from llvm::Pass:
71  bool doInitialization(Module &M);
72  bool doFinalization (Module &M);
73 
74  // to suppress Clang warnings
75  using llvm::FunctionPass::doInitialization;
76  using llvm::FunctionPass::doFinalization;
77 
78  bool runOnBasicBlock(BasicBlock &BB);
79  bool runOnFunction(Function &F) override;
80 
81  private:
82  /// List of intrinsics to replace.
83  std::set<unsigned> replace_;
84  IntrinsicLowering* iLowering_;
85  TargetData* td_;
86  };
87 
88  char LowerIntrinsics::ID = 0;
89  RegisterPass<LowerIntrinsics>
90  X("lowerintrinsics", "Lower llvm intrinsics back to libcalls.");
91 }
92 
93 
94 /**
95  * Constructor
96  */
97 LowerIntrinsics::LowerIntrinsics() :
98  FunctionPass(ID),
99  iLowering_(NULL), td_(NULL) {
100 }
101 
102 /**
103  * Destructor
104  */
105 LowerIntrinsics::~LowerIntrinsics() {
106 }
107 
108 
109 bool
110 LowerIntrinsics::doInitialization(Module &M) {
111 
112  // Initialize list of intrinsics to lower.
113  replace_.insert(Intrinsic::flt_rounds);
114  replace_.insert(Intrinsic::ceil);
115  replace_.insert(Intrinsic::floor);
116  replace_.insert(Intrinsic::round);
117  replace_.insert(Intrinsic::exp2);
118  replace_.insert(Intrinsic::memcpy);
119  replace_.insert(Intrinsic::memset);
120  replace_.insert(Intrinsic::memmove);
121 
122  assert(iLowering_ == NULL && td_ == NULL);
123  td_ = new TargetData(&M);
124  iLowering_ = new IntrinsicLowering(*td_);
125  return true;
126 }
127 
128 bool
129 LowerIntrinsics::doFinalization(Module& /*M*/) {
130  if (iLowering_ != NULL) {
131  delete iLowering_;
132  iLowering_ = NULL;
133  }
134  if (td_ != NULL) {
135  delete td_;
136  td_ = NULL;
137  }
138  return true;
139 }
140 
141 
142 bool
143 LowerIntrinsics::runOnBasicBlock(BasicBlock &BB) {
144 
145  bool changed = true;
146  while (changed) {
147  changed = false;
148  for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
149  CallInst* ci = dyn_cast<CallInst>(&(*I));
150  if (ci != NULL && ci->arg_size() != 0) {
151  Function* callee = ci->getCalledFunction();
152  if (callee != NULL && callee->isIntrinsic() &&
153  replace_.find(callee->getIntrinsicID()) != replace_.end()) {
154  if (callee->getIntrinsicID() == Intrinsic::flt_rounds) {
155  // Replace FLT_ROUNDS intrinsic with the actual
156  // constant value to avoid stupid "if (1 == 0)"
157  // code even with full optimizations.
158  I->replaceAllUsesWith(
159  ConstantInt::get(
160  Type::getInt32Ty(BB.getContext()), 0, true));
161  I->eraseFromParent();
162  changed = true;
163  break;
164  } else {
165  iLowering_->LowerIntrinsicCall(ci);
166  changed = true;
167  break;
168  }
169  }
170  }
171  }
172  }
173  return true;
174 }
175 
176 bool
177 LowerIntrinsics::runOnFunction(Function &F) {
178 
179  for (BasicBlock &BB : F) {
180  runOnBasicBlock(BB);
181  }
182 
183  return true;
184 }
185 
llvm
Definition: InlineAsmParser.hh:49
BlocksTranslator::FU_TYPE::ID
@ ID
assert
#define assert(condition)
Definition: Application.hh:86
TargetData
POP_COMPILER_DIAGS typedef llvm::DataLayout TargetData
Definition: LowerIntrinsics.cc:56
X
static llvm::RegisterPass< InnerLoopFinder > X("find-innerloops-test", "Finds inner loops test.", false, true)
IGNORE_COMPILER_WARNING
#define IGNORE_COMPILER_WARNING(X)
Definition: CompilerWarnings.hh:51
POP_COMPILER_DIAGS
#define POP_COMPILER_DIAGS
Definition: CompilerWarnings.hh:68
TargetData
llvm::DataLayout TargetData
Definition: LLVMBackend.cc:131
CompilerWarnings.hh