diff options
-rw-r--r-- | include/llvm/InitializePasses.h | 1 | ||||
-rw-r--r-- | include/llvm/Transforms/NaCl.h | 1 | ||||
-rw-r--r-- | lib/Analysis/NaCl/PNaClABIVerifyModule.cpp | 7 | ||||
-rw-r--r-- | lib/Transforms/NaCl/PNaClABISimplify.cpp | 2 | ||||
-rw-r--r-- | lib/Transforms/NaCl/RewriteLLVMIntrinsics.cpp | 71 | ||||
-rw-r--r-- | test/Transforms/NaCl/rewrite-flt-rounds.ll | 38 | ||||
-rw-r--r-- | tools/opt/opt.cpp | 1 |
7 files changed, 118 insertions, 3 deletions
diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h index c339e45302..4e643c4d23 100644 --- a/include/llvm/InitializePasses.h +++ b/include/llvm/InitializePasses.h @@ -298,6 +298,7 @@ void initializePromoteIntegersPass(PassRegistry&); void initializeReplacePtrsWithIntsPass(PassRegistry&); void initializeResolveAliasesPass(PassRegistry&); void initializeResolvePNaClIntrinsicsPass(PassRegistry&); +void initializeRewriteLLVMIntrinsicsPass(PassRegistry&); void initializeRewritePNaClLibraryCallsPass(PassRegistry&); void initializeStripAttributesPass(PassRegistry&); void initializeStripMetadataPass(PassRegistry&); diff --git a/include/llvm/Transforms/NaCl.h b/include/llvm/Transforms/NaCl.h index bc39302489..6924320cfe 100644 --- a/include/llvm/Transforms/NaCl.h +++ b/include/llvm/Transforms/NaCl.h @@ -39,6 +39,7 @@ FunctionPass *createPromoteIntegersPass(); ModulePass *createReplacePtrsWithIntsPass(); ModulePass *createResolveAliasesPass(); FunctionPass *createResolvePNaClIntrinsicsPass(); +ModulePass *createRewriteLLVMIntrinsicsPass(); ModulePass *createRewritePNaClLibraryCallsPass(); ModulePass *createStripAttributesPass(); ModulePass *createStripMetadataPass(); diff --git a/lib/Analysis/NaCl/PNaClABIVerifyModule.cpp b/lib/Analysis/NaCl/PNaClABIVerifyModule.cpp index 3a422288d8..288d5da2ad 100644 --- a/lib/Analysis/NaCl/PNaClABIVerifyModule.cpp +++ b/lib/Analysis/NaCl/PNaClABIVerifyModule.cpp @@ -249,6 +249,10 @@ bool PNaClABIVerifyModule::isWhitelistedIntrinsic(const Function *F, // We run -lower-expect to convert Intrinsic::expect into branch weights // and consume in the middle-end. The backend just ignores llvm.expect. case Intrinsic::expect: + // For FLT_ROUNDS macro from float.h. It works for ARM and X86 + // (but not MIPS). Also, wait until we add a set_flt_rounds intrinsic + // before we bless this. + case Intrinsic::flt_rounds: return false; // (3) Dev intrinsics. @@ -258,9 +262,6 @@ bool PNaClABIVerifyModule::isWhitelistedIntrinsic(const Function *F, case Intrinsic::ctlz: // Support via compiler_rt if arch doesn't have it? case Intrinsic::ctpop: // Support via compiler_rt if arch doesn't have it? case Intrinsic::cttz: // Support via compiler_rt if arch doesn't have it? - case Intrinsic::flt_rounds: // For FLT_ROUNDS macro from float.h. - // We do not have fesetround() in newlib, can we return a - // consistent rounding mode though? case Intrinsic::nacl_target_arch: // Used by translator self-build. case Intrinsic::pow: // Rounding is supposed to be the same as libm. case Intrinsic::powi: // Rounding not defined: support with fast-math? 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(); +} diff --git a/test/Transforms/NaCl/rewrite-flt-rounds.ll b/test/Transforms/NaCl/rewrite-flt-rounds.ll new file mode 100644 index 0000000000..3c368b8bc3 --- /dev/null +++ b/test/Transforms/NaCl/rewrite-flt-rounds.ll @@ -0,0 +1,38 @@ +; RUN: opt < %s -rewrite-llvm-intrinsic-calls -S | FileCheck %s +; RUN: opt < %s -rewrite-llvm-intrinsic-calls -S | FileCheck %s -check-prefix=CLEANED +; Test the RewriteLLVMIntrinsics pass + +declare i32 @llvm.flt.rounds() + +; No declaration or definition of llvm.flt.rounds() should remain. +; CLEANED-NOT: @llvm.flt.rounds + +define i32 @call_flt_rounds() { +; CHECK: call_flt_rounds +; CHECK-NEXT: ret i32 1 + %val = call i32 @llvm.flt.rounds() + ret i32 %val +} + +; A more complex example with a number of calls in several BBs. +define i32 @multiple_calls(i64* %arg, i32 %num) { +; CHECK: multiple_calls +entryblock: +; CHECK: entryblock + %v1 = call i32 @llvm.flt.rounds() + br label %block1 +block1: +; CHECK: block1: +; CHECK-NEXT: %v3 = add i32 1, 1 + %v2 = call i32 @llvm.flt.rounds() + %v3 = add i32 %v2, %v1 + br label %exitblock +exitblock: +; CHECK: exitblock: +; CHECK-NEXT: %v4 = add i32 1, %v3 +; CHECK-NEXT: %v6 = add i32 1, %v4 + %v4 = add i32 %v2, %v3 + %v5 = call i32 @llvm.flt.rounds() + %v6 = add i32 %v5, %v4 + ret i32 %v6 +} diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp index 866597ec72..804382fca7 100644 --- a/tools/opt/opt.cpp +++ b/tools/opt/opt.cpp @@ -624,6 +624,7 @@ int main(int argc, char **argv) { initializeReplacePtrsWithIntsPass(Registry); initializeResolveAliasesPass(Registry); initializeResolvePNaClIntrinsicsPass(Registry); + initializeRewriteLLVMIntrinsicsPass(Registry); initializeRewritePNaClLibraryCallsPass(Registry); initializeStripAttributesPass(Registry); initializeStripMetadataPass(Registry); |