1 //===- ReduceInstructionFlags.cpp - Specialized Delta Pass ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Try to remove optimization flags on instructions
11 //===----------------------------------------------------------------------===//
13 #include "ReduceInstructionFlags.h"
15 #include "llvm/IR/InstIterator.h"
16 #include "llvm/IR/Instruction.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Operator.h"
22 static void reduceFlagsInModule(Oracle
&O
, ReducerWorkItem
&WorkItem
) {
23 for (Function
&F
: WorkItem
.getModule()) {
24 for (Instruction
&I
: instructions(F
)) {
25 if (auto *OBO
= dyn_cast
<OverflowingBinaryOperator
>(&I
)) {
26 if (OBO
->hasNoSignedWrap() && !O
.shouldKeep())
27 I
.setHasNoSignedWrap(false);
28 if (OBO
->hasNoUnsignedWrap() && !O
.shouldKeep())
29 I
.setHasNoUnsignedWrap(false);
30 } else if (auto *PE
= dyn_cast
<PossiblyExactOperator
>(&I
)) {
31 if (PE
->isExact() && !O
.shouldKeep())
33 } else if (auto *GEP
= dyn_cast
<GetElementPtrInst
>(&I
)) {
34 if (GEP
->isInBounds() && !O
.shouldKeep())
35 GEP
->setIsInBounds(false);
36 } else if (auto *FPOp
= dyn_cast
<FPMathOperator
>(&I
)) {
37 FastMathFlags Flags
= FPOp
->getFastMathFlags();
39 if (Flags
.allowReassoc() && !O
.shouldKeep())
40 Flags
.setAllowReassoc(false);
42 if (Flags
.noNaNs() && !O
.shouldKeep())
43 Flags
.setNoNaNs(false);
45 if (Flags
.noInfs() && !O
.shouldKeep())
46 Flags
.setNoInfs(false);
48 if (Flags
.noSignedZeros() && !O
.shouldKeep())
49 Flags
.setNoSignedZeros(false);
51 if (Flags
.allowReciprocal() && !O
.shouldKeep())
52 Flags
.setAllowReciprocal(false);
54 if (Flags
.allowContract() && !O
.shouldKeep())
55 Flags
.setAllowContract(false);
57 if (Flags
.approxFunc() && !O
.shouldKeep())
58 Flags
.setApproxFunc(false);
60 I
.copyFastMathFlags(Flags
);
66 void llvm::reduceInstructionFlagsDeltaPass(TestRunner
&Test
) {
67 runDeltaPass(Test
, reduceFlagsInModule
, "Reducing Instruction Flags");