diff options
author | Jan Voung <jvoung@chromium.org> | 2013-06-19 16:12:25 -0700 |
---|---|---|
committer | Jan Voung <jvoung@chromium.org> | 2013-06-19 16:12:25 -0700 |
commit | 73af8c9c57122e5ae61f80d916653db0dbb1e91f (patch) | |
tree | 49fdcd5d299096f2cf86f9eb8360394ef5d14054 /lib/Transforms | |
parent | bce9cade729651fdddd9852d0467e0cd7476a54f (diff) |
Rewrite llvm.flt.rounds to "1" for now, and disallow llvm.flt.rounds.
Until there is an intrinsic to *set* the rounding mode,
this intrinsic to *get* the rounding mode isn't so useful.
Separately we will add a test that for each platform, the
initial rounding mode is "1" (round to nearest). That is
the case right now for x86, ARM, and MIPS.
(see https://codereview.chromium.org/16785003/)
BUG=https://code.google.com/p/nativeclient/issues/detail?id=3491
R=mseaborn@chromium.org
Review URL: https://codereview.chromium.org/17229007
Diffstat (limited to 'lib/Transforms')
-rw-r--r-- | lib/Transforms/NaCl/PNaClABISimplify.cpp | 2 | ||||
-rw-r--r-- | lib/Transforms/NaCl/RewriteLLVMIntrinsics.cpp | 71 |
2 files changed, 73 insertions, 0 deletions
diff --git a/lib/Transforms/NaCl/PNaClABISimplify.cpp b/lib/Transforms/NaCl/PNaClABISimplify.cpp index 0fd7176ed9..a85829eb3b 100644 --- a/lib/Transforms/NaCl/PNaClABISimplify.cpp +++ b/lib/Transforms/NaCl/PNaClABISimplify.cpp @@ -25,6 +25,8 @@ void llvm::PNaClABISimplifyAddPreOptPasses(PassManager &PM) { // LowerExpect converts Intrinsic::expect into branch weights, // which can then be removed after BlockPlacement. PM.add(createLowerExpectIntrinsicPass()); + // Rewrite unsupported intrinsics to simpler constructs. + PM.add(createRewriteLLVMIntrinsicsPass()); // LowerInvoke prevents use of C++ exception handling, which is not // yet supported in the PNaCl ABI. PM.add(createLowerInvokePass()); diff --git a/lib/Transforms/NaCl/RewriteLLVMIntrinsics.cpp b/lib/Transforms/NaCl/RewriteLLVMIntrinsics.cpp new file mode 100644 index 0000000000..17cd2347cc --- /dev/null +++ b/lib/Transforms/NaCl/RewriteLLVMIntrinsics.cpp @@ -0,0 +1,71 @@ +//===- RewriteLLVMIntrinsics.cpp - Rewrite LLVM intrinsics to other values ===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This pass replaces calls to LLVM intrinsics that are *not* part of the +// PNaCl stable bitcode ABI into simpler values. +// +//===----------------------------------------------------------------------===// + +#include "llvm/IR/Instructions.h" +#include "llvm/IR/Intrinsics.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" +#include "llvm/Transforms/NaCl.h" + +using namespace llvm; + +namespace { + class RewriteLLVMIntrinsics : public ModulePass { + public: + static char ID; + RewriteLLVMIntrinsics() : ModulePass(ID) { + // This is a module pass because this makes it easier to access uses + // of global intrinsic functions. + initializeRewriteLLVMIntrinsicsPass(*PassRegistry::getPassRegistry()); + } + + virtual bool runOnModule(Module &M); + }; +} + +char RewriteLLVMIntrinsics::ID = 0; +INITIALIZE_PASS(RewriteLLVMIntrinsics, "rewrite-llvm-intrinsic-calls", + "Rewrite LLVM intrinsic calls to simpler expressions", + false, false) + +bool RewriteLLVMIntrinsics::runOnModule(Module &M) { + bool Changed = false; + + // Iterate over all uses of the llvm.flt.rounds, and replace it with + // the constant "1" (round-to-nearest). Until we add a second intrinsic + // like llvm.set.flt.round it is impossible to have a rounding mode + // that is not the initial rounding mode (round-to-nearest). + // We can remove this rewrite after adding a set() intrinsic. + Function *FltRounds = Intrinsic::getDeclaration(&M, Intrinsic::flt_rounds); + Type *RetType = FltRounds->getFunctionType()->getReturnType(); + for (Value::use_iterator UI = FltRounds->use_begin(), + UE = FltRounds->use_end(); UI != UE;) { + Value *Use = *UI++; + if (CallInst *Call = dyn_cast<CallInst>(Use)) { + Constant *C = ConstantInt::get(RetType, 1); + Call->replaceAllUsesWith(C); + Call->eraseFromParent(); + Changed = true; + } else { + report_fatal_error("Taking the address of llvm.flt.rounds is invalid"); + } + } + FltRounds->eraseFromParent(); + + return Changed; +} + +ModulePass *llvm::createRewriteLLVMIntrinsicsPass() { + return new RewriteLLVMIntrinsics(); +} |