ReduceInstructionFlags.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //===- ReduceInstructionFlags.cpp - Specialized Delta Pass ----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Try to remove optimization flags on instructions
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ReduceInstructionFlags.h"
  13. #include "Delta.h"
  14. #include "llvm/IR/InstIterator.h"
  15. #include "llvm/IR/Instruction.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/IR/Operator.h"
  18. using namespace llvm;
  19. static void reduceFlagsInModule(Oracle &O, ReducerWorkItem &WorkItem) {
  20. for (Function &F : WorkItem.getModule()) {
  21. for (Instruction &I : instructions(F)) {
  22. if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I)) {
  23. if (OBO->hasNoSignedWrap() && !O.shouldKeep())
  24. I.setHasNoSignedWrap(false);
  25. if (OBO->hasNoUnsignedWrap() && !O.shouldKeep())
  26. I.setHasNoUnsignedWrap(false);
  27. } else if (auto *PE = dyn_cast<PossiblyExactOperator>(&I)) {
  28. if (PE->isExact() && !O.shouldKeep())
  29. I.setIsExact(false);
  30. } else if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
  31. if (GEP->isInBounds() && !O.shouldKeep())
  32. GEP->setIsInBounds(false);
  33. } else if (auto *FPOp = dyn_cast<FPMathOperator>(&I)) {
  34. FastMathFlags Flags = FPOp->getFastMathFlags();
  35. if (Flags.allowReassoc() && !O.shouldKeep())
  36. Flags.setAllowReassoc(false);
  37. if (Flags.noNaNs() && !O.shouldKeep())
  38. Flags.setNoNaNs(false);
  39. if (Flags.noInfs() && !O.shouldKeep())
  40. Flags.setNoInfs(false);
  41. if (Flags.noSignedZeros() && !O.shouldKeep())
  42. Flags.setNoSignedZeros(false);
  43. if (Flags.allowReciprocal() && !O.shouldKeep())
  44. Flags.setAllowReciprocal(false);
  45. if (Flags.allowContract() && !O.shouldKeep())
  46. Flags.setAllowContract(false);
  47. if (Flags.approxFunc() && !O.shouldKeep())
  48. Flags.setApproxFunc(false);
  49. I.copyFastMathFlags(Flags);
  50. }
  51. }
  52. }
  53. }
  54. void llvm::reduceInstructionFlagsDeltaPass(TestRunner &Test) {
  55. runDeltaPass(Test, reduceFlagsInModule, "Reducing Instruction Flags");
  56. }