xref: /aosp_15_r20/external/AFLplusplus/instrumentation/split-compares-pass.so.cc (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1*08b48e0bSAndroid Build Coastguard Worker /*
2*08b48e0bSAndroid Build Coastguard Worker  * Copyright 2016 laf-intel
3*08b48e0bSAndroid Build Coastguard Worker  * extended for floating point by Heiko Eißfeldt
4*08b48e0bSAndroid Build Coastguard Worker  * adapted to new pass manager by Heiko Eißfeldt
5*08b48e0bSAndroid Build Coastguard Worker  *
6*08b48e0bSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
7*08b48e0bSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
8*08b48e0bSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
9*08b48e0bSAndroid Build Coastguard Worker  *
10*08b48e0bSAndroid Build Coastguard Worker  *     https://www.apache.org/licenses/LICENSE-2.0
11*08b48e0bSAndroid Build Coastguard Worker  *
12*08b48e0bSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
13*08b48e0bSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
14*08b48e0bSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*08b48e0bSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
16*08b48e0bSAndroid Build Coastguard Worker  * limitations under the License.
17*08b48e0bSAndroid Build Coastguard Worker  */
18*08b48e0bSAndroid Build Coastguard Worker 
19*08b48e0bSAndroid Build Coastguard Worker #include <stdio.h>
20*08b48e0bSAndroid Build Coastguard Worker #include <stdlib.h>
21*08b48e0bSAndroid Build Coastguard Worker #include <unistd.h>
22*08b48e0bSAndroid Build Coastguard Worker 
23*08b48e0bSAndroid Build Coastguard Worker #include <list>
24*08b48e0bSAndroid Build Coastguard Worker #include <string>
25*08b48e0bSAndroid Build Coastguard Worker #include <fstream>
26*08b48e0bSAndroid Build Coastguard Worker #include <sys/time.h>
27*08b48e0bSAndroid Build Coastguard Worker 
28*08b48e0bSAndroid Build Coastguard Worker #include "llvm/Config/llvm-config.h"
29*08b48e0bSAndroid Build Coastguard Worker 
30*08b48e0bSAndroid Build Coastguard Worker #include "llvm/Pass.h"
31*08b48e0bSAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
32*08b48e0bSAndroid Build Coastguard Worker 
33*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 11
34*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/Passes/PassPlugin.h"
35*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/Passes/PassBuilder.h"
36*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/IR/PassManager.h"
37*08b48e0bSAndroid Build Coastguard Worker #else
38*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/IR/LegacyPassManager.h"
39*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/Transforms/IPO/PassManagerBuilder.h"
40*08b48e0bSAndroid Build Coastguard Worker #endif
41*08b48e0bSAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/BasicBlockUtils.h"
42*08b48e0bSAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
43*08b48e0bSAndroid Build Coastguard Worker #if LLVM_VERSION_MAJOR >= 14                /* how about stable interfaces? */
44*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/Passes/OptimizationLevel.h"
45*08b48e0bSAndroid Build Coastguard Worker #endif
46*08b48e0bSAndroid Build Coastguard Worker 
47*08b48e0bSAndroid Build Coastguard Worker #include "llvm/IR/IRBuilder.h"
48*08b48e0bSAndroid Build Coastguard Worker #if LLVM_VERSION_MAJOR >= 4 || \
49*08b48e0bSAndroid Build Coastguard Worker     (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 4)
50*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/IR/Verifier.h"
51*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/IR/DebugInfo.h"
52*08b48e0bSAndroid Build Coastguard Worker #else
53*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/Analysis/Verifier.h"
54*08b48e0bSAndroid Build Coastguard Worker   #include "llvm/DebugInfo.h"
55*08b48e0bSAndroid Build Coastguard Worker   #define nullptr 0
56*08b48e0bSAndroid Build Coastguard Worker #endif
57*08b48e0bSAndroid Build Coastguard Worker 
58*08b48e0bSAndroid Build Coastguard Worker using namespace llvm;
59*08b48e0bSAndroid Build Coastguard Worker #include "afl-llvm-common.h"
60*08b48e0bSAndroid Build Coastguard Worker 
61*08b48e0bSAndroid Build Coastguard Worker // uncomment this toggle function verification at each step. horribly slow, but
62*08b48e0bSAndroid Build Coastguard Worker // helps to pinpoint a potential problem in the splitting code.
63*08b48e0bSAndroid Build Coastguard Worker // #define VERIFY_TOO_MUCH 1
64*08b48e0bSAndroid Build Coastguard Worker 
65*08b48e0bSAndroid Build Coastguard Worker namespace {
66*08b48e0bSAndroid Build Coastguard Worker 
67*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 11
68*08b48e0bSAndroid Build Coastguard Worker class SplitComparesTransform : public PassInfoMixin<SplitComparesTransform> {
69*08b48e0bSAndroid Build Coastguard Worker 
70*08b48e0bSAndroid Build Coastguard Worker  public:
71*08b48e0bSAndroid Build Coastguard Worker   //  static char ID;
SplitComparesTransform()72*08b48e0bSAndroid Build Coastguard Worker   SplitComparesTransform() : enableFPSplit(0) {
73*08b48e0bSAndroid Build Coastguard Worker 
74*08b48e0bSAndroid Build Coastguard Worker #else
75*08b48e0bSAndroid Build Coastguard Worker class SplitComparesTransform : public ModulePass {
76*08b48e0bSAndroid Build Coastguard Worker 
77*08b48e0bSAndroid Build Coastguard Worker  public:
78*08b48e0bSAndroid Build Coastguard Worker   static char ID;
79*08b48e0bSAndroid Build Coastguard Worker   SplitComparesTransform() : ModulePass(ID), enableFPSplit(0) {
80*08b48e0bSAndroid Build Coastguard Worker 
81*08b48e0bSAndroid Build Coastguard Worker #endif
82*08b48e0bSAndroid Build Coastguard Worker 
83*08b48e0bSAndroid Build Coastguard Worker     initInstrumentList();
84*08b48e0bSAndroid Build Coastguard Worker 
85*08b48e0bSAndroid Build Coastguard Worker   }
86*08b48e0bSAndroid Build Coastguard Worker 
87*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 11
88*08b48e0bSAndroid Build Coastguard Worker   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
89*08b48e0bSAndroid Build Coastguard Worker #else
90*08b48e0bSAndroid Build Coastguard Worker   bool runOnModule(Module &M) override;
91*08b48e0bSAndroid Build Coastguard Worker #endif
92*08b48e0bSAndroid Build Coastguard Worker 
93*08b48e0bSAndroid Build Coastguard Worker  private:
94*08b48e0bSAndroid Build Coastguard Worker   int enableFPSplit;
95*08b48e0bSAndroid Build Coastguard Worker 
96*08b48e0bSAndroid Build Coastguard Worker   unsigned target_bitwidth = 8;
97*08b48e0bSAndroid Build Coastguard Worker 
98*08b48e0bSAndroid Build Coastguard Worker   size_t count = 0;
99*08b48e0bSAndroid Build Coastguard Worker 
100*08b48e0bSAndroid Build Coastguard Worker   size_t splitFPCompares(Module &M);
101*08b48e0bSAndroid Build Coastguard Worker   bool   simplifyFPCompares(Module &M);
102*08b48e0bSAndroid Build Coastguard Worker   size_t nextPowerOfTwo(size_t in);
103*08b48e0bSAndroid Build Coastguard Worker 
104*08b48e0bSAndroid Build Coastguard Worker   using CmpWorklist = SmallVector<CmpInst *, 8>;
105*08b48e0bSAndroid Build Coastguard Worker 
106*08b48e0bSAndroid Build Coastguard Worker   /// simplify the comparison and then split the comparison until the
107*08b48e0bSAndroid Build Coastguard Worker   /// target_bitwidth is reached.
108*08b48e0bSAndroid Build Coastguard Worker   bool simplifyAndSplit(CmpInst *I, Module &M);
109*08b48e0bSAndroid Build Coastguard Worker   /// simplify a non-strict comparison (e.g., less than or equals)
110*08b48e0bSAndroid Build Coastguard Worker   bool simplifyOrEqualsCompare(CmpInst *IcmpInst, Module &M,
111*08b48e0bSAndroid Build Coastguard Worker                                CmpWorklist &worklist);
112*08b48e0bSAndroid Build Coastguard Worker   /// simplify a signed comparison (signed less or greater than)
113*08b48e0bSAndroid Build Coastguard Worker   bool simplifySignedCompare(CmpInst *IcmpInst, Module &M,
114*08b48e0bSAndroid Build Coastguard Worker                              CmpWorklist &worklist);
115*08b48e0bSAndroid Build Coastguard Worker   /// splits an icmp into nested icmps recursivly until target_bitwidth is
116*08b48e0bSAndroid Build Coastguard Worker   /// reached
117*08b48e0bSAndroid Build Coastguard Worker   bool splitCompare(CmpInst *I, Module &M, CmpWorklist &worklist);
118*08b48e0bSAndroid Build Coastguard Worker 
119*08b48e0bSAndroid Build Coastguard Worker   /// print an error to llvm's errs stream, but only if not ordered to be quiet
120*08b48e0bSAndroid Build Coastguard Worker   void reportError(const StringRef msg, Instruction *I, Module &M) {
121*08b48e0bSAndroid Build Coastguard Worker 
122*08b48e0bSAndroid Build Coastguard Worker     if (!be_quiet) {
123*08b48e0bSAndroid Build Coastguard Worker 
124*08b48e0bSAndroid Build Coastguard Worker       errs() << "[AFL++ SplitComparesTransform] ERROR: " << msg << "\n";
125*08b48e0bSAndroid Build Coastguard Worker       if (debug) {
126*08b48e0bSAndroid Build Coastguard Worker 
127*08b48e0bSAndroid Build Coastguard Worker         if (I) {
128*08b48e0bSAndroid Build Coastguard Worker 
129*08b48e0bSAndroid Build Coastguard Worker           errs() << "Instruction = " << *I << "\n";
130*08b48e0bSAndroid Build Coastguard Worker           if (auto BB = I->getParent()) {
131*08b48e0bSAndroid Build Coastguard Worker 
132*08b48e0bSAndroid Build Coastguard Worker             if (auto F = BB->getParent()) {
133*08b48e0bSAndroid Build Coastguard Worker 
134*08b48e0bSAndroid Build Coastguard Worker               if (F->hasName()) {
135*08b48e0bSAndroid Build Coastguard Worker 
136*08b48e0bSAndroid Build Coastguard Worker                 errs() << "|-> in function " << F->getName() << " ";
137*08b48e0bSAndroid Build Coastguard Worker 
138*08b48e0bSAndroid Build Coastguard Worker               }
139*08b48e0bSAndroid Build Coastguard Worker 
140*08b48e0bSAndroid Build Coastguard Worker             }
141*08b48e0bSAndroid Build Coastguard Worker 
142*08b48e0bSAndroid Build Coastguard Worker           }
143*08b48e0bSAndroid Build Coastguard Worker 
144*08b48e0bSAndroid Build Coastguard Worker         }
145*08b48e0bSAndroid Build Coastguard Worker 
146*08b48e0bSAndroid Build Coastguard Worker         auto n = M.getName();
147*08b48e0bSAndroid Build Coastguard Worker         if (n.size() > 0) { errs() << "in module " << n << "\n"; }
148*08b48e0bSAndroid Build Coastguard Worker 
149*08b48e0bSAndroid Build Coastguard Worker       }
150*08b48e0bSAndroid Build Coastguard Worker 
151*08b48e0bSAndroid Build Coastguard Worker     }
152*08b48e0bSAndroid Build Coastguard Worker 
153*08b48e0bSAndroid Build Coastguard Worker   }
154*08b48e0bSAndroid Build Coastguard Worker 
155*08b48e0bSAndroid Build Coastguard Worker   bool isSupportedBitWidth(unsigned bitw) {
156*08b48e0bSAndroid Build Coastguard Worker 
157*08b48e0bSAndroid Build Coastguard Worker     // IDK whether the icmp code works on other bitwidths. I guess not? So we
158*08b48e0bSAndroid Build Coastguard Worker     // try to avoid dealing with other weird icmp's that llvm might use (looking
159*08b48e0bSAndroid Build Coastguard Worker     // at you `icmp i0`).
160*08b48e0bSAndroid Build Coastguard Worker     switch (bitw) {
161*08b48e0bSAndroid Build Coastguard Worker 
162*08b48e0bSAndroid Build Coastguard Worker       case 8:
163*08b48e0bSAndroid Build Coastguard Worker       case 16:
164*08b48e0bSAndroid Build Coastguard Worker       case 32:
165*08b48e0bSAndroid Build Coastguard Worker       case 64:
166*08b48e0bSAndroid Build Coastguard Worker       case 128:
167*08b48e0bSAndroid Build Coastguard Worker       case 256:
168*08b48e0bSAndroid Build Coastguard Worker         return true;
169*08b48e0bSAndroid Build Coastguard Worker       default:
170*08b48e0bSAndroid Build Coastguard Worker         return false;
171*08b48e0bSAndroid Build Coastguard Worker 
172*08b48e0bSAndroid Build Coastguard Worker     }
173*08b48e0bSAndroid Build Coastguard Worker 
174*08b48e0bSAndroid Build Coastguard Worker   }
175*08b48e0bSAndroid Build Coastguard Worker 
176*08b48e0bSAndroid Build Coastguard Worker };
177*08b48e0bSAndroid Build Coastguard Worker 
178*08b48e0bSAndroid Build Coastguard Worker }  // namespace
179*08b48e0bSAndroid Build Coastguard Worker 
180*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 11
181*08b48e0bSAndroid Build Coastguard Worker extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo()182*08b48e0bSAndroid Build Coastguard Worker llvmGetPassPluginInfo() {
183*08b48e0bSAndroid Build Coastguard Worker 
184*08b48e0bSAndroid Build Coastguard Worker   return {LLVM_PLUGIN_API_VERSION, "splitcompares", "v0.1",
185*08b48e0bSAndroid Build Coastguard Worker           /* lambda to insert our pass into the pass pipeline. */
186*08b48e0bSAndroid Build Coastguard Worker           [](PassBuilder &PB) {
187*08b48e0bSAndroid Build Coastguard Worker 
188*08b48e0bSAndroid Build Coastguard Worker   #if 1
189*08b48e0bSAndroid Build Coastguard Worker     #if LLVM_VERSION_MAJOR <= 13
190*08b48e0bSAndroid Build Coastguard Worker             using OptimizationLevel = typename PassBuilder::OptimizationLevel;
191*08b48e0bSAndroid Build Coastguard Worker     #endif
192*08b48e0bSAndroid Build Coastguard Worker             PB.registerOptimizerLastEPCallback(
193*08b48e0bSAndroid Build Coastguard Worker                 [](ModulePassManager &MPM, OptimizationLevel OL) {
194*08b48e0bSAndroid Build Coastguard Worker 
195*08b48e0bSAndroid Build Coastguard Worker                   MPM.addPass(SplitComparesTransform());
196*08b48e0bSAndroid Build Coastguard Worker 
197*08b48e0bSAndroid Build Coastguard Worker                 });
198*08b48e0bSAndroid Build Coastguard Worker 
199*08b48e0bSAndroid Build Coastguard Worker   /* TODO LTO registration */
200*08b48e0bSAndroid Build Coastguard Worker   #else
201*08b48e0bSAndroid Build Coastguard Worker             using PipelineElement = typename PassBuilder::PipelineElement;
202*08b48e0bSAndroid Build Coastguard Worker             PB.registerPipelineParsingCallback([](StringRef          Name,
203*08b48e0bSAndroid Build Coastguard Worker                                                   ModulePassManager &MPM,
204*08b48e0bSAndroid Build Coastguard Worker                                                   ArrayRef<PipelineElement>) {
205*08b48e0bSAndroid Build Coastguard Worker 
206*08b48e0bSAndroid Build Coastguard Worker               if (Name == "splitcompares") {
207*08b48e0bSAndroid Build Coastguard Worker 
208*08b48e0bSAndroid Build Coastguard Worker                 MPM.addPass(SplitComparesTransform());
209*08b48e0bSAndroid Build Coastguard Worker                 return true;
210*08b48e0bSAndroid Build Coastguard Worker 
211*08b48e0bSAndroid Build Coastguard Worker               } else {
212*08b48e0bSAndroid Build Coastguard Worker 
213*08b48e0bSAndroid Build Coastguard Worker                 return false;
214*08b48e0bSAndroid Build Coastguard Worker 
215*08b48e0bSAndroid Build Coastguard Worker               }
216*08b48e0bSAndroid Build Coastguard Worker 
217*08b48e0bSAndroid Build Coastguard Worker             });
218*08b48e0bSAndroid Build Coastguard Worker 
219*08b48e0bSAndroid Build Coastguard Worker   #endif
220*08b48e0bSAndroid Build Coastguard Worker 
221*08b48e0bSAndroid Build Coastguard Worker           }};
222*08b48e0bSAndroid Build Coastguard Worker 
223*08b48e0bSAndroid Build Coastguard Worker }
224*08b48e0bSAndroid Build Coastguard Worker 
225*08b48e0bSAndroid Build Coastguard Worker #else
226*08b48e0bSAndroid Build Coastguard Worker char SplitComparesTransform::ID = 0;
227*08b48e0bSAndroid Build Coastguard Worker #endif
228*08b48e0bSAndroid Build Coastguard Worker 
229*08b48e0bSAndroid Build Coastguard Worker /// This function splits FCMP instructions with xGE or xLE predicates into two
230*08b48e0bSAndroid Build Coastguard Worker /// FCMP instructions with predicate xGT or xLT and EQ
simplifyFPCompares(Module & M)231*08b48e0bSAndroid Build Coastguard Worker bool SplitComparesTransform::simplifyFPCompares(Module &M) {
232*08b48e0bSAndroid Build Coastguard Worker 
233*08b48e0bSAndroid Build Coastguard Worker   LLVMContext               &C = M.getContext();
234*08b48e0bSAndroid Build Coastguard Worker   std::vector<Instruction *> fcomps;
235*08b48e0bSAndroid Build Coastguard Worker   IntegerType               *Int1Ty = IntegerType::getInt1Ty(C);
236*08b48e0bSAndroid Build Coastguard Worker 
237*08b48e0bSAndroid Build Coastguard Worker   /* iterate over all functions, bbs and instruction and add
238*08b48e0bSAndroid Build Coastguard Worker    * all integer comparisons with >= and <= predicates to the icomps vector */
239*08b48e0bSAndroid Build Coastguard Worker   for (auto &F : M) {
240*08b48e0bSAndroid Build Coastguard Worker 
241*08b48e0bSAndroid Build Coastguard Worker     if (!isInInstrumentList(&F, MNAME)) continue;
242*08b48e0bSAndroid Build Coastguard Worker 
243*08b48e0bSAndroid Build Coastguard Worker     for (auto &BB : F) {
244*08b48e0bSAndroid Build Coastguard Worker 
245*08b48e0bSAndroid Build Coastguard Worker       for (auto &IN : BB) {
246*08b48e0bSAndroid Build Coastguard Worker 
247*08b48e0bSAndroid Build Coastguard Worker         CmpInst *selectcmpInst = nullptr;
248*08b48e0bSAndroid Build Coastguard Worker 
249*08b48e0bSAndroid Build Coastguard Worker         if ((selectcmpInst = dyn_cast<CmpInst>(&IN))) {
250*08b48e0bSAndroid Build Coastguard Worker 
251*08b48e0bSAndroid Build Coastguard Worker           if (enableFPSplit &&
252*08b48e0bSAndroid Build Coastguard Worker               (selectcmpInst->getPredicate() == CmpInst::FCMP_OGE ||
253*08b48e0bSAndroid Build Coastguard Worker                selectcmpInst->getPredicate() == CmpInst::FCMP_UGE ||
254*08b48e0bSAndroid Build Coastguard Worker                selectcmpInst->getPredicate() == CmpInst::FCMP_OLE ||
255*08b48e0bSAndroid Build Coastguard Worker                selectcmpInst->getPredicate() == CmpInst::FCMP_ULE)) {
256*08b48e0bSAndroid Build Coastguard Worker 
257*08b48e0bSAndroid Build Coastguard Worker             auto op0 = selectcmpInst->getOperand(0);
258*08b48e0bSAndroid Build Coastguard Worker             auto op1 = selectcmpInst->getOperand(1);
259*08b48e0bSAndroid Build Coastguard Worker 
260*08b48e0bSAndroid Build Coastguard Worker             Type *TyOp0 = op0->getType();
261*08b48e0bSAndroid Build Coastguard Worker             Type *TyOp1 = op1->getType();
262*08b48e0bSAndroid Build Coastguard Worker 
263*08b48e0bSAndroid Build Coastguard Worker             /* this is probably not needed but we do it anyway */
264*08b48e0bSAndroid Build Coastguard Worker             if (TyOp0 != TyOp1) { continue; }
265*08b48e0bSAndroid Build Coastguard Worker 
266*08b48e0bSAndroid Build Coastguard Worker             if (TyOp0->isArrayTy() || TyOp0->isVectorTy()) { continue; }
267*08b48e0bSAndroid Build Coastguard Worker 
268*08b48e0bSAndroid Build Coastguard Worker             fcomps.push_back(selectcmpInst);
269*08b48e0bSAndroid Build Coastguard Worker 
270*08b48e0bSAndroid Build Coastguard Worker           }
271*08b48e0bSAndroid Build Coastguard Worker 
272*08b48e0bSAndroid Build Coastguard Worker         }
273*08b48e0bSAndroid Build Coastguard Worker 
274*08b48e0bSAndroid Build Coastguard Worker       }
275*08b48e0bSAndroid Build Coastguard Worker 
276*08b48e0bSAndroid Build Coastguard Worker     }
277*08b48e0bSAndroid Build Coastguard Worker 
278*08b48e0bSAndroid Build Coastguard Worker   }
279*08b48e0bSAndroid Build Coastguard Worker 
280*08b48e0bSAndroid Build Coastguard Worker   if (!fcomps.size()) { return false; }
281*08b48e0bSAndroid Build Coastguard Worker 
282*08b48e0bSAndroid Build Coastguard Worker   /* transform for floating point */
283*08b48e0bSAndroid Build Coastguard Worker   for (auto &FcmpInst : fcomps) {
284*08b48e0bSAndroid Build Coastguard Worker 
285*08b48e0bSAndroid Build Coastguard Worker     BasicBlock *bb = FcmpInst->getParent();
286*08b48e0bSAndroid Build Coastguard Worker 
287*08b48e0bSAndroid Build Coastguard Worker     auto op0 = FcmpInst->getOperand(0);
288*08b48e0bSAndroid Build Coastguard Worker     auto op1 = FcmpInst->getOperand(1);
289*08b48e0bSAndroid Build Coastguard Worker 
290*08b48e0bSAndroid Build Coastguard Worker     /* find out what the new predicate is going to be */
291*08b48e0bSAndroid Build Coastguard Worker     auto cmp_inst = dyn_cast<CmpInst>(FcmpInst);
292*08b48e0bSAndroid Build Coastguard Worker     if (!cmp_inst) { continue; }
293*08b48e0bSAndroid Build Coastguard Worker     auto               pred = cmp_inst->getPredicate();
294*08b48e0bSAndroid Build Coastguard Worker     CmpInst::Predicate new_pred;
295*08b48e0bSAndroid Build Coastguard Worker 
296*08b48e0bSAndroid Build Coastguard Worker     switch (pred) {
297*08b48e0bSAndroid Build Coastguard Worker 
298*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UGE:
299*08b48e0bSAndroid Build Coastguard Worker         new_pred = CmpInst::FCMP_UGT;
300*08b48e0bSAndroid Build Coastguard Worker         break;
301*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OGE:
302*08b48e0bSAndroid Build Coastguard Worker         new_pred = CmpInst::FCMP_OGT;
303*08b48e0bSAndroid Build Coastguard Worker         break;
304*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_ULE:
305*08b48e0bSAndroid Build Coastguard Worker         new_pred = CmpInst::FCMP_ULT;
306*08b48e0bSAndroid Build Coastguard Worker         break;
307*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OLE:
308*08b48e0bSAndroid Build Coastguard Worker         new_pred = CmpInst::FCMP_OLT;
309*08b48e0bSAndroid Build Coastguard Worker         break;
310*08b48e0bSAndroid Build Coastguard Worker       default:  // keep the compiler happy
311*08b48e0bSAndroid Build Coastguard Worker         continue;
312*08b48e0bSAndroid Build Coastguard Worker 
313*08b48e0bSAndroid Build Coastguard Worker     }
314*08b48e0bSAndroid Build Coastguard Worker 
315*08b48e0bSAndroid Build Coastguard Worker     /* split before the fcmp instruction */
316*08b48e0bSAndroid Build Coastguard Worker     BasicBlock *end_bb = bb->splitBasicBlock(BasicBlock::iterator(FcmpInst));
317*08b48e0bSAndroid Build Coastguard Worker 
318*08b48e0bSAndroid Build Coastguard Worker     /* the old bb now contains a unconditional jump to the new one (end_bb)
319*08b48e0bSAndroid Build Coastguard Worker      * we need to delete it later */
320*08b48e0bSAndroid Build Coastguard Worker 
321*08b48e0bSAndroid Build Coastguard Worker     /* create the FCMP instruction with new_pred and add it to the old basic
322*08b48e0bSAndroid Build Coastguard Worker      * block bb it is now at the position where the old FcmpInst was */
323*08b48e0bSAndroid Build Coastguard Worker     Instruction *fcmp_np;
324*08b48e0bSAndroid Build Coastguard Worker     fcmp_np = CmpInst::Create(Instruction::FCmp, new_pred, op0, op1);
325*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
326*08b48e0bSAndroid Build Coastguard Worker     fcmp_np->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
327*08b48e0bSAndroid Build Coastguard Worker #else
328*08b48e0bSAndroid Build Coastguard Worker     bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()),
329*08b48e0bSAndroid Build Coastguard Worker                              fcmp_np);
330*08b48e0bSAndroid Build Coastguard Worker #endif
331*08b48e0bSAndroid Build Coastguard Worker 
332*08b48e0bSAndroid Build Coastguard Worker     /* create a new basic block which holds the new EQ fcmp */
333*08b48e0bSAndroid Build Coastguard Worker     Instruction *fcmp_eq;
334*08b48e0bSAndroid Build Coastguard Worker     /* insert middle_bb before end_bb */
335*08b48e0bSAndroid Build Coastguard Worker     BasicBlock *middle_bb =
336*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::Create(C, "injected", end_bb->getParent(), end_bb);
337*08b48e0bSAndroid Build Coastguard Worker     fcmp_eq = CmpInst::Create(Instruction::FCmp, CmpInst::FCMP_OEQ, op0, op1);
338*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
339*08b48e0bSAndroid Build Coastguard Worker     fcmp_eq->insertInto(middle_bb, middle_bb->end());
340*08b48e0bSAndroid Build Coastguard Worker #else
341*08b48e0bSAndroid Build Coastguard Worker     middle_bb->getInstList().push_back(fcmp_eq);
342*08b48e0bSAndroid Build Coastguard Worker #endif
343*08b48e0bSAndroid Build Coastguard Worker     /* add an unconditional branch to the end of middle_bb with destination
344*08b48e0bSAndroid Build Coastguard Worker      * end_bb */
345*08b48e0bSAndroid Build Coastguard Worker     BranchInst::Create(end_bb, middle_bb);
346*08b48e0bSAndroid Build Coastguard Worker 
347*08b48e0bSAndroid Build Coastguard Worker     /* replace the uncond branch with a conditional one, which depends on the
348*08b48e0bSAndroid Build Coastguard Worker      * new_pred fcmp. True goes to end, false to the middle (injected) bb */
349*08b48e0bSAndroid Build Coastguard Worker     auto term = bb->getTerminator();
350*08b48e0bSAndroid Build Coastguard Worker     BranchInst::Create(end_bb, middle_bb, fcmp_np, bb);
351*08b48e0bSAndroid Build Coastguard Worker     term->eraseFromParent();
352*08b48e0bSAndroid Build Coastguard Worker 
353*08b48e0bSAndroid Build Coastguard Worker     /* replace the old FcmpInst (which is the first inst in end_bb) with a PHI
354*08b48e0bSAndroid Build Coastguard Worker      * inst to wire up the loose ends */
355*08b48e0bSAndroid Build Coastguard Worker     PHINode *PN = PHINode::Create(Int1Ty, 2, "");
356*08b48e0bSAndroid Build Coastguard Worker     /* the first result depends on the outcome of fcmp_eq */
357*08b48e0bSAndroid Build Coastguard Worker     PN->addIncoming(fcmp_eq, middle_bb);
358*08b48e0bSAndroid Build Coastguard Worker     /* if the source was the original bb we know that the fcmp_np yielded true
359*08b48e0bSAndroid Build Coastguard Worker      * hence we can hardcode this value */
360*08b48e0bSAndroid Build Coastguard Worker     PN->addIncoming(ConstantInt::get(Int1Ty, 1), bb);
361*08b48e0bSAndroid Build Coastguard Worker     /* replace the old FcmpInst with our new and shiny PHI inst */
362*08b48e0bSAndroid Build Coastguard Worker     BasicBlock::iterator ii(FcmpInst);
363*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
364*08b48e0bSAndroid Build Coastguard Worker     ReplaceInstWithInst(FcmpInst->getParent(), ii, PN);
365*08b48e0bSAndroid Build Coastguard Worker #else
366*08b48e0bSAndroid Build Coastguard Worker     ReplaceInstWithInst(FcmpInst->getParent()->getInstList(), ii, PN);
367*08b48e0bSAndroid Build Coastguard Worker #endif
368*08b48e0bSAndroid Build Coastguard Worker 
369*08b48e0bSAndroid Build Coastguard Worker   }
370*08b48e0bSAndroid Build Coastguard Worker 
371*08b48e0bSAndroid Build Coastguard Worker   return true;
372*08b48e0bSAndroid Build Coastguard Worker 
373*08b48e0bSAndroid Build Coastguard Worker }
374*08b48e0bSAndroid Build Coastguard Worker 
375*08b48e0bSAndroid Build Coastguard Worker /// This function splits ICMP instructions with xGE or xLE predicates into two
376*08b48e0bSAndroid Build Coastguard Worker /// ICMP instructions with predicate xGT or xLT and EQ
simplifyOrEqualsCompare(CmpInst * IcmpInst,Module & M,CmpWorklist & worklist)377*08b48e0bSAndroid Build Coastguard Worker bool SplitComparesTransform::simplifyOrEqualsCompare(CmpInst     *IcmpInst,
378*08b48e0bSAndroid Build Coastguard Worker                                                      Module      &M,
379*08b48e0bSAndroid Build Coastguard Worker                                                      CmpWorklist &worklist) {
380*08b48e0bSAndroid Build Coastguard Worker 
381*08b48e0bSAndroid Build Coastguard Worker   LLVMContext &C = M.getContext();
382*08b48e0bSAndroid Build Coastguard Worker   IntegerType *Int1Ty = IntegerType::getInt1Ty(C);
383*08b48e0bSAndroid Build Coastguard Worker 
384*08b48e0bSAndroid Build Coastguard Worker   /* find out what the new predicate is going to be */
385*08b48e0bSAndroid Build Coastguard Worker   auto cmp_inst = dyn_cast<CmpInst>(IcmpInst);
386*08b48e0bSAndroid Build Coastguard Worker   if (!cmp_inst) { return false; }
387*08b48e0bSAndroid Build Coastguard Worker 
388*08b48e0bSAndroid Build Coastguard Worker   BasicBlock *bb = IcmpInst->getParent();
389*08b48e0bSAndroid Build Coastguard Worker 
390*08b48e0bSAndroid Build Coastguard Worker   auto op0 = IcmpInst->getOperand(0);
391*08b48e0bSAndroid Build Coastguard Worker   auto op1 = IcmpInst->getOperand(1);
392*08b48e0bSAndroid Build Coastguard Worker 
393*08b48e0bSAndroid Build Coastguard Worker   CmpInst::Predicate pred = cmp_inst->getPredicate();
394*08b48e0bSAndroid Build Coastguard Worker   CmpInst::Predicate new_pred;
395*08b48e0bSAndroid Build Coastguard Worker 
396*08b48e0bSAndroid Build Coastguard Worker   switch (pred) {
397*08b48e0bSAndroid Build Coastguard Worker 
398*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_UGE:
399*08b48e0bSAndroid Build Coastguard Worker       new_pred = CmpInst::ICMP_UGT;
400*08b48e0bSAndroid Build Coastguard Worker       break;
401*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SGE:
402*08b48e0bSAndroid Build Coastguard Worker       new_pred = CmpInst::ICMP_SGT;
403*08b48e0bSAndroid Build Coastguard Worker       break;
404*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_ULE:
405*08b48e0bSAndroid Build Coastguard Worker       new_pred = CmpInst::ICMP_ULT;
406*08b48e0bSAndroid Build Coastguard Worker       break;
407*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SLE:
408*08b48e0bSAndroid Build Coastguard Worker       new_pred = CmpInst::ICMP_SLT;
409*08b48e0bSAndroid Build Coastguard Worker       break;
410*08b48e0bSAndroid Build Coastguard Worker     default:  // keep the compiler happy
411*08b48e0bSAndroid Build Coastguard Worker       return false;
412*08b48e0bSAndroid Build Coastguard Worker 
413*08b48e0bSAndroid Build Coastguard Worker   }
414*08b48e0bSAndroid Build Coastguard Worker 
415*08b48e0bSAndroid Build Coastguard Worker   /* split before the icmp instruction */
416*08b48e0bSAndroid Build Coastguard Worker   BasicBlock *end_bb = bb->splitBasicBlock(BasicBlock::iterator(IcmpInst));
417*08b48e0bSAndroid Build Coastguard Worker 
418*08b48e0bSAndroid Build Coastguard Worker   /* the old bb now contains a unconditional jump to the new one (end_bb)
419*08b48e0bSAndroid Build Coastguard Worker    * we need to delete it later */
420*08b48e0bSAndroid Build Coastguard Worker 
421*08b48e0bSAndroid Build Coastguard Worker   /* create the ICMP instruction with new_pred and add it to the old basic
422*08b48e0bSAndroid Build Coastguard Worker    * block bb it is now at the position where the old IcmpInst was */
423*08b48e0bSAndroid Build Coastguard Worker   CmpInst *icmp_np = CmpInst::Create(Instruction::ICmp, new_pred, op0, op1);
424*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
425*08b48e0bSAndroid Build Coastguard Worker   icmp_np->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
426*08b48e0bSAndroid Build Coastguard Worker #else
427*08b48e0bSAndroid Build Coastguard Worker   bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()), icmp_np);
428*08b48e0bSAndroid Build Coastguard Worker #endif
429*08b48e0bSAndroid Build Coastguard Worker 
430*08b48e0bSAndroid Build Coastguard Worker   /* create a new basic block which holds the new EQ icmp */
431*08b48e0bSAndroid Build Coastguard Worker   CmpInst *icmp_eq;
432*08b48e0bSAndroid Build Coastguard Worker   /* insert middle_bb before end_bb */
433*08b48e0bSAndroid Build Coastguard Worker   BasicBlock *middle_bb =
434*08b48e0bSAndroid Build Coastguard Worker       BasicBlock::Create(C, "injected", end_bb->getParent(), end_bb);
435*08b48e0bSAndroid Build Coastguard Worker   icmp_eq = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, op0, op1);
436*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
437*08b48e0bSAndroid Build Coastguard Worker   icmp_eq->insertInto(middle_bb, middle_bb->end());
438*08b48e0bSAndroid Build Coastguard Worker #else
439*08b48e0bSAndroid Build Coastguard Worker   middle_bb->getInstList().push_back(icmp_eq);
440*08b48e0bSAndroid Build Coastguard Worker #endif
441*08b48e0bSAndroid Build Coastguard Worker   /* add an unconditional branch to the end of middle_bb with destination
442*08b48e0bSAndroid Build Coastguard Worker    * end_bb */
443*08b48e0bSAndroid Build Coastguard Worker   BranchInst::Create(end_bb, middle_bb);
444*08b48e0bSAndroid Build Coastguard Worker 
445*08b48e0bSAndroid Build Coastguard Worker   /* replace the uncond branch with a conditional one, which depends on the
446*08b48e0bSAndroid Build Coastguard Worker    * new_pred icmp. True goes to end, false to the middle (injected) bb */
447*08b48e0bSAndroid Build Coastguard Worker   auto term = bb->getTerminator();
448*08b48e0bSAndroid Build Coastguard Worker   BranchInst::Create(end_bb, middle_bb, icmp_np, bb);
449*08b48e0bSAndroid Build Coastguard Worker   term->eraseFromParent();
450*08b48e0bSAndroid Build Coastguard Worker 
451*08b48e0bSAndroid Build Coastguard Worker   /* replace the old IcmpInst (which is the first inst in end_bb) with a PHI
452*08b48e0bSAndroid Build Coastguard Worker    * inst to wire up the loose ends */
453*08b48e0bSAndroid Build Coastguard Worker   PHINode *PN = PHINode::Create(Int1Ty, 2, "");
454*08b48e0bSAndroid Build Coastguard Worker   /* the first result depends on the outcome of icmp_eq */
455*08b48e0bSAndroid Build Coastguard Worker   PN->addIncoming(icmp_eq, middle_bb);
456*08b48e0bSAndroid Build Coastguard Worker   /* if the source was the original bb we know that the icmp_np yielded true
457*08b48e0bSAndroid Build Coastguard Worker    * hence we can hardcode this value */
458*08b48e0bSAndroid Build Coastguard Worker   PN->addIncoming(ConstantInt::get(Int1Ty, 1), bb);
459*08b48e0bSAndroid Build Coastguard Worker   /* replace the old IcmpInst with our new and shiny PHI inst */
460*08b48e0bSAndroid Build Coastguard Worker   BasicBlock::iterator ii(IcmpInst);
461*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
462*08b48e0bSAndroid Build Coastguard Worker   ReplaceInstWithInst(IcmpInst->getParent(), ii, PN);
463*08b48e0bSAndroid Build Coastguard Worker #else
464*08b48e0bSAndroid Build Coastguard Worker   ReplaceInstWithInst(IcmpInst->getParent()->getInstList(), ii, PN);
465*08b48e0bSAndroid Build Coastguard Worker #endif
466*08b48e0bSAndroid Build Coastguard Worker   if (new_pred == CmpInst::ICMP_SGT || new_pred == CmpInst::ICMP_SLT) {
467*08b48e0bSAndroid Build Coastguard Worker 
468*08b48e0bSAndroid Build Coastguard Worker     simplifySignedCompare(icmp_np, M, worklist);
469*08b48e0bSAndroid Build Coastguard Worker 
470*08b48e0bSAndroid Build Coastguard Worker   }
471*08b48e0bSAndroid Build Coastguard Worker 
472*08b48e0bSAndroid Build Coastguard Worker   worklist.push_back(icmp_eq);
473*08b48e0bSAndroid Build Coastguard Worker 
474*08b48e0bSAndroid Build Coastguard Worker   return true;
475*08b48e0bSAndroid Build Coastguard Worker 
476*08b48e0bSAndroid Build Coastguard Worker }
477*08b48e0bSAndroid Build Coastguard Worker 
478*08b48e0bSAndroid Build Coastguard Worker /// Simplify a signed comparison operator by splitting it into a unsigned and
479*08b48e0bSAndroid Build Coastguard Worker /// bit comparison. add all resulting comparisons to
480*08b48e0bSAndroid Build Coastguard Worker /// the worklist passed as a reference.
simplifySignedCompare(CmpInst * IcmpInst,Module & M,CmpWorklist & worklist)481*08b48e0bSAndroid Build Coastguard Worker bool SplitComparesTransform::simplifySignedCompare(CmpInst *IcmpInst, Module &M,
482*08b48e0bSAndroid Build Coastguard Worker                                                    CmpWorklist &worklist) {
483*08b48e0bSAndroid Build Coastguard Worker 
484*08b48e0bSAndroid Build Coastguard Worker   LLVMContext &C = M.getContext();
485*08b48e0bSAndroid Build Coastguard Worker   IntegerType *Int1Ty = IntegerType::getInt1Ty(C);
486*08b48e0bSAndroid Build Coastguard Worker 
487*08b48e0bSAndroid Build Coastguard Worker   BasicBlock *bb = IcmpInst->getParent();
488*08b48e0bSAndroid Build Coastguard Worker 
489*08b48e0bSAndroid Build Coastguard Worker   auto op0 = IcmpInst->getOperand(0);
490*08b48e0bSAndroid Build Coastguard Worker   auto op1 = IcmpInst->getOperand(1);
491*08b48e0bSAndroid Build Coastguard Worker 
492*08b48e0bSAndroid Build Coastguard Worker   IntegerType *intTyOp0 = dyn_cast<IntegerType>(op0->getType());
493*08b48e0bSAndroid Build Coastguard Worker   if (!intTyOp0) { return false; }
494*08b48e0bSAndroid Build Coastguard Worker   unsigned     bitw = intTyOp0->getBitWidth();
495*08b48e0bSAndroid Build Coastguard Worker   IntegerType *IntType = IntegerType::get(C, bitw);
496*08b48e0bSAndroid Build Coastguard Worker 
497*08b48e0bSAndroid Build Coastguard Worker   /* get the new predicate */
498*08b48e0bSAndroid Build Coastguard Worker   auto cmp_inst = dyn_cast<CmpInst>(IcmpInst);
499*08b48e0bSAndroid Build Coastguard Worker   if (!cmp_inst) { return false; }
500*08b48e0bSAndroid Build Coastguard Worker   auto               pred = cmp_inst->getPredicate();
501*08b48e0bSAndroid Build Coastguard Worker   CmpInst::Predicate new_pred;
502*08b48e0bSAndroid Build Coastguard Worker 
503*08b48e0bSAndroid Build Coastguard Worker   if (pred == CmpInst::ICMP_SGT) {
504*08b48e0bSAndroid Build Coastguard Worker 
505*08b48e0bSAndroid Build Coastguard Worker     new_pred = CmpInst::ICMP_UGT;
506*08b48e0bSAndroid Build Coastguard Worker 
507*08b48e0bSAndroid Build Coastguard Worker   } else {
508*08b48e0bSAndroid Build Coastguard Worker 
509*08b48e0bSAndroid Build Coastguard Worker     new_pred = CmpInst::ICMP_ULT;
510*08b48e0bSAndroid Build Coastguard Worker 
511*08b48e0bSAndroid Build Coastguard Worker   }
512*08b48e0bSAndroid Build Coastguard Worker 
513*08b48e0bSAndroid Build Coastguard Worker   BasicBlock *end_bb = bb->splitBasicBlock(BasicBlock::iterator(IcmpInst));
514*08b48e0bSAndroid Build Coastguard Worker 
515*08b48e0bSAndroid Build Coastguard Worker   /* create a 1 bit compare for the sign bit. to do this shift and trunc
516*08b48e0bSAndroid Build Coastguard Worker    * the original operands so only the first bit remains.*/
517*08b48e0bSAndroid Build Coastguard Worker   Value *s_op0, *t_op0, *s_op1, *t_op1, *icmp_sign_bit;
518*08b48e0bSAndroid Build Coastguard Worker 
519*08b48e0bSAndroid Build Coastguard Worker   IRBuilder<> IRB(bb->getTerminator());
520*08b48e0bSAndroid Build Coastguard Worker   s_op0 = IRB.CreateLShr(op0, ConstantInt::get(IntType, bitw - 1));
521*08b48e0bSAndroid Build Coastguard Worker   t_op0 = IRB.CreateTruncOrBitCast(s_op0, Int1Ty);
522*08b48e0bSAndroid Build Coastguard Worker   s_op1 = IRB.CreateLShr(op1, ConstantInt::get(IntType, bitw - 1));
523*08b48e0bSAndroid Build Coastguard Worker   t_op1 = IRB.CreateTruncOrBitCast(s_op1, Int1Ty);
524*08b48e0bSAndroid Build Coastguard Worker   /* compare of the sign bits */
525*08b48e0bSAndroid Build Coastguard Worker   icmp_sign_bit = IRB.CreateICmp(CmpInst::ICMP_EQ, t_op0, t_op1);
526*08b48e0bSAndroid Build Coastguard Worker 
527*08b48e0bSAndroid Build Coastguard Worker   /* create a new basic block which is executed if the signedness bit is
528*08b48e0bSAndroid Build Coastguard Worker    * different */
529*08b48e0bSAndroid Build Coastguard Worker   CmpInst    *icmp_inv_sig_cmp;
530*08b48e0bSAndroid Build Coastguard Worker   BasicBlock *sign_bb =
531*08b48e0bSAndroid Build Coastguard Worker       BasicBlock::Create(C, "sign", end_bb->getParent(), end_bb);
532*08b48e0bSAndroid Build Coastguard Worker   if (pred == CmpInst::ICMP_SGT) {
533*08b48e0bSAndroid Build Coastguard Worker 
534*08b48e0bSAndroid Build Coastguard Worker     /* if we check for > and the op0 positive and op1 negative then the final
535*08b48e0bSAndroid Build Coastguard Worker      * result is true. if op0 negative and op1 pos, the cmp must result
536*08b48e0bSAndroid Build Coastguard Worker      * in false
537*08b48e0bSAndroid Build Coastguard Worker      */
538*08b48e0bSAndroid Build Coastguard Worker     icmp_inv_sig_cmp =
539*08b48e0bSAndroid Build Coastguard Worker         CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT, t_op0, t_op1);
540*08b48e0bSAndroid Build Coastguard Worker 
541*08b48e0bSAndroid Build Coastguard Worker   } else {
542*08b48e0bSAndroid Build Coastguard Worker 
543*08b48e0bSAndroid Build Coastguard Worker     /* just the inverse of the above statement */
544*08b48e0bSAndroid Build Coastguard Worker     icmp_inv_sig_cmp =
545*08b48e0bSAndroid Build Coastguard Worker         CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_UGT, t_op0, t_op1);
546*08b48e0bSAndroid Build Coastguard Worker 
547*08b48e0bSAndroid Build Coastguard Worker   }
548*08b48e0bSAndroid Build Coastguard Worker 
549*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
550*08b48e0bSAndroid Build Coastguard Worker   icmp_inv_sig_cmp->insertInto(sign_bb, sign_bb->end());
551*08b48e0bSAndroid Build Coastguard Worker #else
552*08b48e0bSAndroid Build Coastguard Worker   sign_bb->getInstList().push_back(icmp_inv_sig_cmp);
553*08b48e0bSAndroid Build Coastguard Worker #endif
554*08b48e0bSAndroid Build Coastguard Worker   BranchInst::Create(end_bb, sign_bb);
555*08b48e0bSAndroid Build Coastguard Worker 
556*08b48e0bSAndroid Build Coastguard Worker   /* create a new bb which is executed if signedness is equal */
557*08b48e0bSAndroid Build Coastguard Worker   CmpInst    *icmp_usign_cmp;
558*08b48e0bSAndroid Build Coastguard Worker   BasicBlock *middle_bb =
559*08b48e0bSAndroid Build Coastguard Worker       BasicBlock::Create(C, "injected", end_bb->getParent(), end_bb);
560*08b48e0bSAndroid Build Coastguard Worker   /* we can do a normal unsigned compare now */
561*08b48e0bSAndroid Build Coastguard Worker   icmp_usign_cmp = CmpInst::Create(Instruction::ICmp, new_pred, op0, op1);
562*08b48e0bSAndroid Build Coastguard Worker 
563*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
564*08b48e0bSAndroid Build Coastguard Worker   icmp_usign_cmp->insertInto(middle_bb, middle_bb->end());
565*08b48e0bSAndroid Build Coastguard Worker #else
566*08b48e0bSAndroid Build Coastguard Worker   middle_bb->getInstList().push_back(icmp_usign_cmp);
567*08b48e0bSAndroid Build Coastguard Worker #endif
568*08b48e0bSAndroid Build Coastguard Worker   BranchInst::Create(end_bb, middle_bb);
569*08b48e0bSAndroid Build Coastguard Worker 
570*08b48e0bSAndroid Build Coastguard Worker   auto term = bb->getTerminator();
571*08b48e0bSAndroid Build Coastguard Worker   /* if the sign is eq do a normal unsigned cmp, else we have to check the
572*08b48e0bSAndroid Build Coastguard Worker    * signedness bit */
573*08b48e0bSAndroid Build Coastguard Worker   BranchInst::Create(middle_bb, sign_bb, icmp_sign_bit, bb);
574*08b48e0bSAndroid Build Coastguard Worker   term->eraseFromParent();
575*08b48e0bSAndroid Build Coastguard Worker 
576*08b48e0bSAndroid Build Coastguard Worker   PHINode *PN = PHINode::Create(Int1Ty, 2, "");
577*08b48e0bSAndroid Build Coastguard Worker 
578*08b48e0bSAndroid Build Coastguard Worker   PN->addIncoming(icmp_usign_cmp, middle_bb);
579*08b48e0bSAndroid Build Coastguard Worker   PN->addIncoming(icmp_inv_sig_cmp, sign_bb);
580*08b48e0bSAndroid Build Coastguard Worker 
581*08b48e0bSAndroid Build Coastguard Worker   BasicBlock::iterator ii(IcmpInst);
582*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
583*08b48e0bSAndroid Build Coastguard Worker   ReplaceInstWithInst(IcmpInst->getParent(), ii, PN);
584*08b48e0bSAndroid Build Coastguard Worker #else
585*08b48e0bSAndroid Build Coastguard Worker   ReplaceInstWithInst(IcmpInst->getParent()->getInstList(), ii, PN);
586*08b48e0bSAndroid Build Coastguard Worker #endif
587*08b48e0bSAndroid Build Coastguard Worker 
588*08b48e0bSAndroid Build Coastguard Worker   // save for later
589*08b48e0bSAndroid Build Coastguard Worker   worklist.push_back(icmp_usign_cmp);
590*08b48e0bSAndroid Build Coastguard Worker 
591*08b48e0bSAndroid Build Coastguard Worker   // signed comparisons are not supported by the splitting code, so we must not
592*08b48e0bSAndroid Build Coastguard Worker   // add it to the worklist.
593*08b48e0bSAndroid Build Coastguard Worker   // worklist.push_back(icmp_inv_sig_cmp);
594*08b48e0bSAndroid Build Coastguard Worker 
595*08b48e0bSAndroid Build Coastguard Worker   return true;
596*08b48e0bSAndroid Build Coastguard Worker 
597*08b48e0bSAndroid Build Coastguard Worker }
598*08b48e0bSAndroid Build Coastguard Worker 
splitCompare(CmpInst * cmp_inst,Module & M,CmpWorklist & worklist)599*08b48e0bSAndroid Build Coastguard Worker bool SplitComparesTransform::splitCompare(CmpInst *cmp_inst, Module &M,
600*08b48e0bSAndroid Build Coastguard Worker                                           CmpWorklist &worklist) {
601*08b48e0bSAndroid Build Coastguard Worker 
602*08b48e0bSAndroid Build Coastguard Worker   auto pred = cmp_inst->getPredicate();
603*08b48e0bSAndroid Build Coastguard Worker   switch (pred) {
604*08b48e0bSAndroid Build Coastguard Worker 
605*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_EQ:
606*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_NE:
607*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_UGT:
608*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_ULT:
609*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_UGE:
610*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_ULE:
611*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SGT:
612*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SLT:
613*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SGE:
614*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SLE:
615*08b48e0bSAndroid Build Coastguard Worker       break;
616*08b48e0bSAndroid Build Coastguard Worker     default:
617*08b48e0bSAndroid Build Coastguard Worker       if (!be_quiet)
618*08b48e0bSAndroid Build Coastguard Worker         fprintf(stderr, "Error: split-compare: Unsupported predicate (%u)\n",
619*08b48e0bSAndroid Build Coastguard Worker                 pred);
620*08b48e0bSAndroid Build Coastguard Worker       // unsupported predicate!
621*08b48e0bSAndroid Build Coastguard Worker       return false;
622*08b48e0bSAndroid Build Coastguard Worker 
623*08b48e0bSAndroid Build Coastguard Worker   }
624*08b48e0bSAndroid Build Coastguard Worker 
625*08b48e0bSAndroid Build Coastguard Worker   auto op0 = cmp_inst->getOperand(0);
626*08b48e0bSAndroid Build Coastguard Worker   auto op1 = cmp_inst->getOperand(1);
627*08b48e0bSAndroid Build Coastguard Worker 
628*08b48e0bSAndroid Build Coastguard Worker   // get bitwidth by checking the bitwidth of the first operator
629*08b48e0bSAndroid Build Coastguard Worker   IntegerType *intTyOp0 = dyn_cast<IntegerType>(op0->getType());
630*08b48e0bSAndroid Build Coastguard Worker   if (!intTyOp0) {
631*08b48e0bSAndroid Build Coastguard Worker 
632*08b48e0bSAndroid Build Coastguard Worker     // not an integer type
633*08b48e0bSAndroid Build Coastguard Worker     if (!be_quiet)
634*08b48e0bSAndroid Build Coastguard Worker       fprintf(stderr, "Error: split-compare: not an integer type\n");
635*08b48e0bSAndroid Build Coastguard Worker     return false;
636*08b48e0bSAndroid Build Coastguard Worker 
637*08b48e0bSAndroid Build Coastguard Worker   }
638*08b48e0bSAndroid Build Coastguard Worker 
639*08b48e0bSAndroid Build Coastguard Worker   unsigned bitw = intTyOp0->getBitWidth();
640*08b48e0bSAndroid Build Coastguard Worker   if (bitw == target_bitwidth) {
641*08b48e0bSAndroid Build Coastguard Worker 
642*08b48e0bSAndroid Build Coastguard Worker     // already the target bitwidth so we have to do nothing here.
643*08b48e0bSAndroid Build Coastguard Worker     return true;
644*08b48e0bSAndroid Build Coastguard Worker 
645*08b48e0bSAndroid Build Coastguard Worker   }
646*08b48e0bSAndroid Build Coastguard Worker 
647*08b48e0bSAndroid Build Coastguard Worker   LLVMContext &C = M.getContext();
648*08b48e0bSAndroid Build Coastguard Worker   IntegerType *Int1Ty = IntegerType::getInt1Ty(C);
649*08b48e0bSAndroid Build Coastguard Worker   BasicBlock  *bb = cmp_inst->getParent();
650*08b48e0bSAndroid Build Coastguard Worker   IntegerType *OldIntType = IntegerType::get(C, bitw);
651*08b48e0bSAndroid Build Coastguard Worker   IntegerType *NewIntType = IntegerType::get(C, bitw / 2);
652*08b48e0bSAndroid Build Coastguard Worker   BasicBlock  *end_bb = bb->splitBasicBlock(BasicBlock::iterator(cmp_inst));
653*08b48e0bSAndroid Build Coastguard Worker   CmpInst     *icmp_high, *icmp_low;
654*08b48e0bSAndroid Build Coastguard Worker 
655*08b48e0bSAndroid Build Coastguard Worker   /* create the comparison of the top halves of the original operands */
656*08b48e0bSAndroid Build Coastguard Worker   Value *s_op0, *op0_high, *s_op1, *op1_high;
657*08b48e0bSAndroid Build Coastguard Worker 
658*08b48e0bSAndroid Build Coastguard Worker   IRBuilder<> IRB(bb->getTerminator());
659*08b48e0bSAndroid Build Coastguard Worker 
660*08b48e0bSAndroid Build Coastguard Worker   s_op0 = IRB.CreateBinOp(Instruction::LShr, op0,
661*08b48e0bSAndroid Build Coastguard Worker                           ConstantInt::get(OldIntType, bitw / 2));
662*08b48e0bSAndroid Build Coastguard Worker   op0_high = IRB.CreateTruncOrBitCast(s_op0, NewIntType);
663*08b48e0bSAndroid Build Coastguard Worker 
664*08b48e0bSAndroid Build Coastguard Worker   s_op1 = IRB.CreateBinOp(Instruction::LShr, op1,
665*08b48e0bSAndroid Build Coastguard Worker                           ConstantInt::get(OldIntType, bitw / 2));
666*08b48e0bSAndroid Build Coastguard Worker   op1_high = IRB.CreateTruncOrBitCast(s_op1, NewIntType);
667*08b48e0bSAndroid Build Coastguard Worker   icmp_high = cast<CmpInst>(IRB.CreateICmp(pred, op0_high, op1_high));
668*08b48e0bSAndroid Build Coastguard Worker 
669*08b48e0bSAndroid Build Coastguard Worker   PHINode *PN = nullptr;
670*08b48e0bSAndroid Build Coastguard Worker 
671*08b48e0bSAndroid Build Coastguard Worker   /* now we have to destinguish between == != and > < */
672*08b48e0bSAndroid Build Coastguard Worker   switch (pred) {
673*08b48e0bSAndroid Build Coastguard Worker 
674*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_EQ:
675*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_NE: {
676*08b48e0bSAndroid Build Coastguard Worker 
677*08b48e0bSAndroid Build Coastguard Worker       /* transformation for == and != icmps */
678*08b48e0bSAndroid Build Coastguard Worker 
679*08b48e0bSAndroid Build Coastguard Worker       /* create a compare for the lower half of the original operands */
680*08b48e0bSAndroid Build Coastguard Worker       BasicBlock *cmp_low_bb =
681*08b48e0bSAndroid Build Coastguard Worker           BasicBlock::Create(C, "" /*"injected"*/, end_bb->getParent(), end_bb);
682*08b48e0bSAndroid Build Coastguard Worker 
683*08b48e0bSAndroid Build Coastguard Worker       Value      *op0_low, *op1_low;
684*08b48e0bSAndroid Build Coastguard Worker       IRBuilder<> Builder(cmp_low_bb);
685*08b48e0bSAndroid Build Coastguard Worker 
686*08b48e0bSAndroid Build Coastguard Worker       op0_low = Builder.CreateTrunc(op0, NewIntType);
687*08b48e0bSAndroid Build Coastguard Worker       op1_low = Builder.CreateTrunc(op1, NewIntType);
688*08b48e0bSAndroid Build Coastguard Worker       icmp_low = cast<CmpInst>(Builder.CreateICmp(pred, op0_low, op1_low));
689*08b48e0bSAndroid Build Coastguard Worker 
690*08b48e0bSAndroid Build Coastguard Worker       BranchInst::Create(end_bb, cmp_low_bb);
691*08b48e0bSAndroid Build Coastguard Worker 
692*08b48e0bSAndroid Build Coastguard Worker       /* dependent on the cmp of the high parts go to the end or go on with
693*08b48e0bSAndroid Build Coastguard Worker        * the comparison */
694*08b48e0bSAndroid Build Coastguard Worker       auto term = bb->getTerminator();
695*08b48e0bSAndroid Build Coastguard Worker 
696*08b48e0bSAndroid Build Coastguard Worker       if (pred == CmpInst::ICMP_EQ) {
697*08b48e0bSAndroid Build Coastguard Worker 
698*08b48e0bSAndroid Build Coastguard Worker         BranchInst::Create(cmp_low_bb, end_bb, icmp_high, bb);
699*08b48e0bSAndroid Build Coastguard Worker 
700*08b48e0bSAndroid Build Coastguard Worker       } else {
701*08b48e0bSAndroid Build Coastguard Worker 
702*08b48e0bSAndroid Build Coastguard Worker         // CmpInst::ICMP_NE
703*08b48e0bSAndroid Build Coastguard Worker         BranchInst::Create(end_bb, cmp_low_bb, icmp_high, bb);
704*08b48e0bSAndroid Build Coastguard Worker 
705*08b48e0bSAndroid Build Coastguard Worker       }
706*08b48e0bSAndroid Build Coastguard Worker 
707*08b48e0bSAndroid Build Coastguard Worker       term->eraseFromParent();
708*08b48e0bSAndroid Build Coastguard Worker 
709*08b48e0bSAndroid Build Coastguard Worker       /* create the PHI and connect the edges accordingly */
710*08b48e0bSAndroid Build Coastguard Worker       PN = PHINode::Create(Int1Ty, 2, "");
711*08b48e0bSAndroid Build Coastguard Worker       PN->addIncoming(icmp_low, cmp_low_bb);
712*08b48e0bSAndroid Build Coastguard Worker       Value *val = nullptr;
713*08b48e0bSAndroid Build Coastguard Worker       if (pred == CmpInst::ICMP_EQ) {
714*08b48e0bSAndroid Build Coastguard Worker 
715*08b48e0bSAndroid Build Coastguard Worker         val = ConstantInt::get(Int1Ty, 0);
716*08b48e0bSAndroid Build Coastguard Worker 
717*08b48e0bSAndroid Build Coastguard Worker       } else {
718*08b48e0bSAndroid Build Coastguard Worker 
719*08b48e0bSAndroid Build Coastguard Worker         /* CmpInst::ICMP_NE */
720*08b48e0bSAndroid Build Coastguard Worker         val = ConstantInt::get(Int1Ty, 1);
721*08b48e0bSAndroid Build Coastguard Worker 
722*08b48e0bSAndroid Build Coastguard Worker       }
723*08b48e0bSAndroid Build Coastguard Worker 
724*08b48e0bSAndroid Build Coastguard Worker       PN->addIncoming(val, icmp_high->getParent());
725*08b48e0bSAndroid Build Coastguard Worker       break;
726*08b48e0bSAndroid Build Coastguard Worker 
727*08b48e0bSAndroid Build Coastguard Worker     }
728*08b48e0bSAndroid Build Coastguard Worker 
729*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SGE:
730*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SLE:
731*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SGT:
732*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_SLT:
733*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_UGE:
734*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_ULE:
735*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_UGT:
736*08b48e0bSAndroid Build Coastguard Worker     case CmpInst::ICMP_ULT: {
737*08b48e0bSAndroid Build Coastguard Worker 
738*08b48e0bSAndroid Build Coastguard Worker       /* transformations for < and > */
739*08b48e0bSAndroid Build Coastguard Worker 
740*08b48e0bSAndroid Build Coastguard Worker       /* create a basic block which checks for the inverse predicate.
741*08b48e0bSAndroid Build Coastguard Worker        * if this is true we can go to the end if not we have to go to the
742*08b48e0bSAndroid Build Coastguard Worker        * bb which checks the lower half of the operands */
743*08b48e0bSAndroid Build Coastguard Worker       Instruction *op0_low, *op1_low;
744*08b48e0bSAndroid Build Coastguard Worker       CmpInst     *icmp_inv_cmp = nullptr;
745*08b48e0bSAndroid Build Coastguard Worker       BasicBlock  *inv_cmp_bb =
746*08b48e0bSAndroid Build Coastguard Worker           BasicBlock::Create(C, "inv_cmp", end_bb->getParent(), end_bb);
747*08b48e0bSAndroid Build Coastguard Worker       if (pred == CmpInst::ICMP_UGT) {
748*08b48e0bSAndroid Build Coastguard Worker 
749*08b48e0bSAndroid Build Coastguard Worker         icmp_inv_cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT,
750*08b48e0bSAndroid Build Coastguard Worker                                        op0_high, op1_high);
751*08b48e0bSAndroid Build Coastguard Worker 
752*08b48e0bSAndroid Build Coastguard Worker       } else if (pred == CmpInst::ICMP_ULT) {
753*08b48e0bSAndroid Build Coastguard Worker 
754*08b48e0bSAndroid Build Coastguard Worker         icmp_inv_cmp = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_UGT,
755*08b48e0bSAndroid Build Coastguard Worker                                        op0_high, op1_high);
756*08b48e0bSAndroid Build Coastguard Worker 
757*08b48e0bSAndroid Build Coastguard Worker       } else {
758*08b48e0bSAndroid Build Coastguard Worker 
759*08b48e0bSAndroid Build Coastguard Worker         // Never gonna appen
760*08b48e0bSAndroid Build Coastguard Worker         if (!be_quiet)
761*08b48e0bSAndroid Build Coastguard Worker           fprintf(stderr,
762*08b48e0bSAndroid Build Coastguard Worker                   "Error: split-compare: Equals or signed not removed: %d\n",
763*08b48e0bSAndroid Build Coastguard Worker                   pred);
764*08b48e0bSAndroid Build Coastguard Worker 
765*08b48e0bSAndroid Build Coastguard Worker       }
766*08b48e0bSAndroid Build Coastguard Worker 
767*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
768*08b48e0bSAndroid Build Coastguard Worker       icmp_inv_cmp->insertInto(inv_cmp_bb, inv_cmp_bb->end());
769*08b48e0bSAndroid Build Coastguard Worker #else
770*08b48e0bSAndroid Build Coastguard Worker       inv_cmp_bb->getInstList().push_back(icmp_inv_cmp);
771*08b48e0bSAndroid Build Coastguard Worker #endif
772*08b48e0bSAndroid Build Coastguard Worker       worklist.push_back(icmp_inv_cmp);
773*08b48e0bSAndroid Build Coastguard Worker 
774*08b48e0bSAndroid Build Coastguard Worker       auto term = bb->getTerminator();
775*08b48e0bSAndroid Build Coastguard Worker       term->eraseFromParent();
776*08b48e0bSAndroid Build Coastguard Worker       BranchInst::Create(end_bb, inv_cmp_bb, icmp_high, bb);
777*08b48e0bSAndroid Build Coastguard Worker 
778*08b48e0bSAndroid Build Coastguard Worker       /* create a bb which handles the cmp of the lower halves */
779*08b48e0bSAndroid Build Coastguard Worker       BasicBlock *cmp_low_bb =
780*08b48e0bSAndroid Build Coastguard Worker           BasicBlock::Create(C, "" /*"injected"*/, end_bb->getParent(), end_bb);
781*08b48e0bSAndroid Build Coastguard Worker       op0_low = new TruncInst(op0, NewIntType);
782*08b48e0bSAndroid Build Coastguard Worker       op1_low = new TruncInst(op1, NewIntType);
783*08b48e0bSAndroid Build Coastguard Worker       icmp_low = CmpInst::Create(Instruction::ICmp, pred, op0_low, op1_low);
784*08b48e0bSAndroid Build Coastguard Worker 
785*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
786*08b48e0bSAndroid Build Coastguard Worker       op0_low->insertInto(cmp_low_bb, cmp_low_bb->end());
787*08b48e0bSAndroid Build Coastguard Worker       op1_low->insertInto(cmp_low_bb, cmp_low_bb->end());
788*08b48e0bSAndroid Build Coastguard Worker       icmp_low->insertInto(cmp_low_bb, cmp_low_bb->end());
789*08b48e0bSAndroid Build Coastguard Worker #else
790*08b48e0bSAndroid Build Coastguard Worker       cmp_low_bb->getInstList().push_back(op0_low);
791*08b48e0bSAndroid Build Coastguard Worker       cmp_low_bb->getInstList().push_back(op1_low);
792*08b48e0bSAndroid Build Coastguard Worker       cmp_low_bb->getInstList().push_back(icmp_low);
793*08b48e0bSAndroid Build Coastguard Worker #endif
794*08b48e0bSAndroid Build Coastguard Worker       BranchInst::Create(end_bb, cmp_low_bb);
795*08b48e0bSAndroid Build Coastguard Worker 
796*08b48e0bSAndroid Build Coastguard Worker       BranchInst::Create(end_bb, cmp_low_bb, icmp_inv_cmp, inv_cmp_bb);
797*08b48e0bSAndroid Build Coastguard Worker 
798*08b48e0bSAndroid Build Coastguard Worker       PN = PHINode::Create(Int1Ty, 3);
799*08b48e0bSAndroid Build Coastguard Worker       PN->addIncoming(icmp_low, cmp_low_bb);
800*08b48e0bSAndroid Build Coastguard Worker       PN->addIncoming(ConstantInt::get(Int1Ty, 1), bb);
801*08b48e0bSAndroid Build Coastguard Worker       PN->addIncoming(ConstantInt::get(Int1Ty, 0), inv_cmp_bb);
802*08b48e0bSAndroid Build Coastguard Worker       break;
803*08b48e0bSAndroid Build Coastguard Worker 
804*08b48e0bSAndroid Build Coastguard Worker     }
805*08b48e0bSAndroid Build Coastguard Worker 
806*08b48e0bSAndroid Build Coastguard Worker     default:
807*08b48e0bSAndroid Build Coastguard Worker       if (!be_quiet)
808*08b48e0bSAndroid Build Coastguard Worker         fprintf(stderr, "Error: split-compare: should not happen\n");
809*08b48e0bSAndroid Build Coastguard Worker       return false;
810*08b48e0bSAndroid Build Coastguard Worker 
811*08b48e0bSAndroid Build Coastguard Worker   }
812*08b48e0bSAndroid Build Coastguard Worker 
813*08b48e0bSAndroid Build Coastguard Worker   BasicBlock::iterator ii(cmp_inst);
814*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
815*08b48e0bSAndroid Build Coastguard Worker   ReplaceInstWithInst(cmp_inst->getParent(), ii, PN);
816*08b48e0bSAndroid Build Coastguard Worker #else
817*08b48e0bSAndroid Build Coastguard Worker   ReplaceInstWithInst(cmp_inst->getParent()->getInstList(), ii, PN);
818*08b48e0bSAndroid Build Coastguard Worker #endif
819*08b48e0bSAndroid Build Coastguard Worker 
820*08b48e0bSAndroid Build Coastguard Worker   // We split the comparison into low and high. If this isn't our target
821*08b48e0bSAndroid Build Coastguard Worker   // bitwidth we recursively split the low and high parts again until we have
822*08b48e0bSAndroid Build Coastguard Worker   // target bitwidth.
823*08b48e0bSAndroid Build Coastguard Worker   if ((bitw / 2) > target_bitwidth) {
824*08b48e0bSAndroid Build Coastguard Worker 
825*08b48e0bSAndroid Build Coastguard Worker     worklist.push_back(icmp_high);
826*08b48e0bSAndroid Build Coastguard Worker     worklist.push_back(icmp_low);
827*08b48e0bSAndroid Build Coastguard Worker 
828*08b48e0bSAndroid Build Coastguard Worker   }
829*08b48e0bSAndroid Build Coastguard Worker 
830*08b48e0bSAndroid Build Coastguard Worker   return true;
831*08b48e0bSAndroid Build Coastguard Worker 
832*08b48e0bSAndroid Build Coastguard Worker }
833*08b48e0bSAndroid Build Coastguard Worker 
simplifyAndSplit(CmpInst * I,Module & M)834*08b48e0bSAndroid Build Coastguard Worker bool SplitComparesTransform::simplifyAndSplit(CmpInst *I, Module &M) {
835*08b48e0bSAndroid Build Coastguard Worker 
836*08b48e0bSAndroid Build Coastguard Worker   CmpWorklist worklist;
837*08b48e0bSAndroid Build Coastguard Worker 
838*08b48e0bSAndroid Build Coastguard Worker   auto op0 = I->getOperand(0);
839*08b48e0bSAndroid Build Coastguard Worker   auto op1 = I->getOperand(1);
840*08b48e0bSAndroid Build Coastguard Worker   if (!op0 || !op1) { return false; }
841*08b48e0bSAndroid Build Coastguard Worker   auto op0Ty = dyn_cast<IntegerType>(op0->getType());
842*08b48e0bSAndroid Build Coastguard Worker   if (!op0Ty || !isa<IntegerType>(op1->getType())) { return true; }
843*08b48e0bSAndroid Build Coastguard Worker 
844*08b48e0bSAndroid Build Coastguard Worker   unsigned bitw = op0Ty->getBitWidth();
845*08b48e0bSAndroid Build Coastguard Worker 
846*08b48e0bSAndroid Build Coastguard Worker #ifdef VERIFY_TOO_MUCH
847*08b48e0bSAndroid Build Coastguard Worker   auto F = I->getParent()->getParent();
848*08b48e0bSAndroid Build Coastguard Worker #endif
849*08b48e0bSAndroid Build Coastguard Worker 
850*08b48e0bSAndroid Build Coastguard Worker   // we run the comparison simplification on all compares regardless of their
851*08b48e0bSAndroid Build Coastguard Worker   // bitwidth.
852*08b48e0bSAndroid Build Coastguard Worker   if (I->getPredicate() == CmpInst::ICMP_UGE ||
853*08b48e0bSAndroid Build Coastguard Worker       I->getPredicate() == CmpInst::ICMP_SGE ||
854*08b48e0bSAndroid Build Coastguard Worker       I->getPredicate() == CmpInst::ICMP_ULE ||
855*08b48e0bSAndroid Build Coastguard Worker       I->getPredicate() == CmpInst::ICMP_SLE) {
856*08b48e0bSAndroid Build Coastguard Worker 
857*08b48e0bSAndroid Build Coastguard Worker     if (!simplifyOrEqualsCompare(I, M, worklist)) {
858*08b48e0bSAndroid Build Coastguard Worker 
859*08b48e0bSAndroid Build Coastguard Worker       reportError(
860*08b48e0bSAndroid Build Coastguard Worker           "Failed to simplify inequality or equals comparison "
861*08b48e0bSAndroid Build Coastguard Worker           "(UGE,SGE,ULE,SLE)",
862*08b48e0bSAndroid Build Coastguard Worker           I, M);
863*08b48e0bSAndroid Build Coastguard Worker 
864*08b48e0bSAndroid Build Coastguard Worker     }
865*08b48e0bSAndroid Build Coastguard Worker 
866*08b48e0bSAndroid Build Coastguard Worker   } else if (I->getPredicate() == CmpInst::ICMP_SGT ||
867*08b48e0bSAndroid Build Coastguard Worker 
868*08b48e0bSAndroid Build Coastguard Worker              I->getPredicate() == CmpInst::ICMP_SLT) {
869*08b48e0bSAndroid Build Coastguard Worker 
870*08b48e0bSAndroid Build Coastguard Worker     if (!simplifySignedCompare(I, M, worklist)) {
871*08b48e0bSAndroid Build Coastguard Worker 
872*08b48e0bSAndroid Build Coastguard Worker       reportError("Failed to simplify signed comparison (SGT,SLT)", I, M);
873*08b48e0bSAndroid Build Coastguard Worker 
874*08b48e0bSAndroid Build Coastguard Worker     }
875*08b48e0bSAndroid Build Coastguard Worker 
876*08b48e0bSAndroid Build Coastguard Worker   }
877*08b48e0bSAndroid Build Coastguard Worker 
878*08b48e0bSAndroid Build Coastguard Worker #ifdef VERIFY_TOO_MUCH
879*08b48e0bSAndroid Build Coastguard Worker   if (verifyFunction(*F, &errs())) {
880*08b48e0bSAndroid Build Coastguard Worker 
881*08b48e0bSAndroid Build Coastguard Worker     reportError("simpliyfing compare lead to broken function", nullptr, M);
882*08b48e0bSAndroid Build Coastguard Worker 
883*08b48e0bSAndroid Build Coastguard Worker   }
884*08b48e0bSAndroid Build Coastguard Worker 
885*08b48e0bSAndroid Build Coastguard Worker #endif
886*08b48e0bSAndroid Build Coastguard Worker 
887*08b48e0bSAndroid Build Coastguard Worker   // the simplification methods replace the original CmpInst and push the
888*08b48e0bSAndroid Build Coastguard Worker   // resulting new CmpInst into the worklist. If the worklist is empty then
889*08b48e0bSAndroid Build Coastguard Worker   // we only have to split the original CmpInst.
890*08b48e0bSAndroid Build Coastguard Worker   if (worklist.size() == 0) { worklist.push_back(I); }
891*08b48e0bSAndroid Build Coastguard Worker 
892*08b48e0bSAndroid Build Coastguard Worker   while (!worklist.empty()) {
893*08b48e0bSAndroid Build Coastguard Worker 
894*08b48e0bSAndroid Build Coastguard Worker     CmpInst *cmp = worklist.pop_back_val();
895*08b48e0bSAndroid Build Coastguard Worker     // we split the simplified compares into comparisons with smaller bitwidths
896*08b48e0bSAndroid Build Coastguard Worker     // if they are larger than our target_bitwidth.
897*08b48e0bSAndroid Build Coastguard Worker     if (bitw > target_bitwidth) {
898*08b48e0bSAndroid Build Coastguard Worker 
899*08b48e0bSAndroid Build Coastguard Worker       if (!splitCompare(cmp, M, worklist)) {
900*08b48e0bSAndroid Build Coastguard Worker 
901*08b48e0bSAndroid Build Coastguard Worker         reportError("Failed to split comparison", cmp, M);
902*08b48e0bSAndroid Build Coastguard Worker 
903*08b48e0bSAndroid Build Coastguard Worker       }
904*08b48e0bSAndroid Build Coastguard Worker 
905*08b48e0bSAndroid Build Coastguard Worker #ifdef VERIFY_TOO_MUCH
906*08b48e0bSAndroid Build Coastguard Worker       if (verifyFunction(*F, &errs())) {
907*08b48e0bSAndroid Build Coastguard Worker 
908*08b48e0bSAndroid Build Coastguard Worker         reportError("splitting compare lead to broken function", nullptr, M);
909*08b48e0bSAndroid Build Coastguard Worker 
910*08b48e0bSAndroid Build Coastguard Worker       }
911*08b48e0bSAndroid Build Coastguard Worker 
912*08b48e0bSAndroid Build Coastguard Worker #endif
913*08b48e0bSAndroid Build Coastguard Worker 
914*08b48e0bSAndroid Build Coastguard Worker     }
915*08b48e0bSAndroid Build Coastguard Worker 
916*08b48e0bSAndroid Build Coastguard Worker   }
917*08b48e0bSAndroid Build Coastguard Worker 
918*08b48e0bSAndroid Build Coastguard Worker   count++;
919*08b48e0bSAndroid Build Coastguard Worker   return true;
920*08b48e0bSAndroid Build Coastguard Worker 
921*08b48e0bSAndroid Build Coastguard Worker }
922*08b48e0bSAndroid Build Coastguard Worker 
nextPowerOfTwo(size_t in)923*08b48e0bSAndroid Build Coastguard Worker size_t SplitComparesTransform::nextPowerOfTwo(size_t in) {
924*08b48e0bSAndroid Build Coastguard Worker 
925*08b48e0bSAndroid Build Coastguard Worker   --in;
926*08b48e0bSAndroid Build Coastguard Worker   in |= in >> 1;
927*08b48e0bSAndroid Build Coastguard Worker   in |= in >> 2;
928*08b48e0bSAndroid Build Coastguard Worker   in |= in >> 4;
929*08b48e0bSAndroid Build Coastguard Worker   //  in |= in >> 8;
930*08b48e0bSAndroid Build Coastguard Worker   //  in |= in >> 16;
931*08b48e0bSAndroid Build Coastguard Worker   return in + 1;
932*08b48e0bSAndroid Build Coastguard Worker 
933*08b48e0bSAndroid Build Coastguard Worker }
934*08b48e0bSAndroid Build Coastguard Worker 
935*08b48e0bSAndroid Build Coastguard Worker /* splits fcmps into two nested fcmps with sign compare and the rest */
splitFPCompares(Module & M)936*08b48e0bSAndroid Build Coastguard Worker size_t SplitComparesTransform::splitFPCompares(Module &M) {
937*08b48e0bSAndroid Build Coastguard Worker 
938*08b48e0bSAndroid Build Coastguard Worker   size_t count = 0;
939*08b48e0bSAndroid Build Coastguard Worker 
940*08b48e0bSAndroid Build Coastguard Worker   LLVMContext &C = M.getContext();
941*08b48e0bSAndroid Build Coastguard Worker 
942*08b48e0bSAndroid Build Coastguard Worker #if LLVM_VERSION_MAJOR >= 4 || \
943*08b48e0bSAndroid Build Coastguard Worker     (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 7)
944*08b48e0bSAndroid Build Coastguard Worker   const DataLayout &dl = M.getDataLayout();
945*08b48e0bSAndroid Build Coastguard Worker 
946*08b48e0bSAndroid Build Coastguard Worker   /* define unions with floating point and (sign, exponent, mantissa)  triples
947*08b48e0bSAndroid Build Coastguard Worker    */
948*08b48e0bSAndroid Build Coastguard Worker   if (dl.isLittleEndian()) {
949*08b48e0bSAndroid Build Coastguard Worker 
950*08b48e0bSAndroid Build Coastguard Worker   } else if (dl.isBigEndian()) {
951*08b48e0bSAndroid Build Coastguard Worker 
952*08b48e0bSAndroid Build Coastguard Worker   } else {
953*08b48e0bSAndroid Build Coastguard Worker 
954*08b48e0bSAndroid Build Coastguard Worker     return count;
955*08b48e0bSAndroid Build Coastguard Worker 
956*08b48e0bSAndroid Build Coastguard Worker   }
957*08b48e0bSAndroid Build Coastguard Worker 
958*08b48e0bSAndroid Build Coastguard Worker #endif
959*08b48e0bSAndroid Build Coastguard Worker 
960*08b48e0bSAndroid Build Coastguard Worker   std::vector<CmpInst *> fcomps;
961*08b48e0bSAndroid Build Coastguard Worker 
962*08b48e0bSAndroid Build Coastguard Worker   /* get all EQ, NE, GT, and LT fcmps. if the other two
963*08b48e0bSAndroid Build Coastguard Worker    * functions were executed only these four predicates should exist */
964*08b48e0bSAndroid Build Coastguard Worker   for (auto &F : M) {
965*08b48e0bSAndroid Build Coastguard Worker 
966*08b48e0bSAndroid Build Coastguard Worker     if (!isInInstrumentList(&F, MNAME)) continue;
967*08b48e0bSAndroid Build Coastguard Worker 
968*08b48e0bSAndroid Build Coastguard Worker     for (auto &BB : F) {
969*08b48e0bSAndroid Build Coastguard Worker 
970*08b48e0bSAndroid Build Coastguard Worker       for (auto &IN : BB) {
971*08b48e0bSAndroid Build Coastguard Worker 
972*08b48e0bSAndroid Build Coastguard Worker         CmpInst *selectcmpInst = nullptr;
973*08b48e0bSAndroid Build Coastguard Worker 
974*08b48e0bSAndroid Build Coastguard Worker         if ((selectcmpInst = dyn_cast<CmpInst>(&IN))) {
975*08b48e0bSAndroid Build Coastguard Worker 
976*08b48e0bSAndroid Build Coastguard Worker           if (selectcmpInst->getPredicate() == CmpInst::FCMP_OEQ ||
977*08b48e0bSAndroid Build Coastguard Worker               selectcmpInst->getPredicate() == CmpInst::FCMP_UEQ ||
978*08b48e0bSAndroid Build Coastguard Worker               selectcmpInst->getPredicate() == CmpInst::FCMP_ONE ||
979*08b48e0bSAndroid Build Coastguard Worker               selectcmpInst->getPredicate() == CmpInst::FCMP_UNE ||
980*08b48e0bSAndroid Build Coastguard Worker               selectcmpInst->getPredicate() == CmpInst::FCMP_UGT ||
981*08b48e0bSAndroid Build Coastguard Worker               selectcmpInst->getPredicate() == CmpInst::FCMP_OGT ||
982*08b48e0bSAndroid Build Coastguard Worker               selectcmpInst->getPredicate() == CmpInst::FCMP_ULT ||
983*08b48e0bSAndroid Build Coastguard Worker               selectcmpInst->getPredicate() == CmpInst::FCMP_OLT) {
984*08b48e0bSAndroid Build Coastguard Worker 
985*08b48e0bSAndroid Build Coastguard Worker             auto op0 = selectcmpInst->getOperand(0);
986*08b48e0bSAndroid Build Coastguard Worker             auto op1 = selectcmpInst->getOperand(1);
987*08b48e0bSAndroid Build Coastguard Worker 
988*08b48e0bSAndroid Build Coastguard Worker             Type *TyOp0 = op0->getType();
989*08b48e0bSAndroid Build Coastguard Worker             Type *TyOp1 = op1->getType();
990*08b48e0bSAndroid Build Coastguard Worker 
991*08b48e0bSAndroid Build Coastguard Worker             if (TyOp0 != TyOp1) { continue; }
992*08b48e0bSAndroid Build Coastguard Worker 
993*08b48e0bSAndroid Build Coastguard Worker             if (TyOp0->isArrayTy() || TyOp0->isVectorTy()) { continue; }
994*08b48e0bSAndroid Build Coastguard Worker 
995*08b48e0bSAndroid Build Coastguard Worker             fcomps.push_back(selectcmpInst);
996*08b48e0bSAndroid Build Coastguard Worker 
997*08b48e0bSAndroid Build Coastguard Worker           }
998*08b48e0bSAndroid Build Coastguard Worker 
999*08b48e0bSAndroid Build Coastguard Worker         }
1000*08b48e0bSAndroid Build Coastguard Worker 
1001*08b48e0bSAndroid Build Coastguard Worker       }
1002*08b48e0bSAndroid Build Coastguard Worker 
1003*08b48e0bSAndroid Build Coastguard Worker     }
1004*08b48e0bSAndroid Build Coastguard Worker 
1005*08b48e0bSAndroid Build Coastguard Worker   }
1006*08b48e0bSAndroid Build Coastguard Worker 
1007*08b48e0bSAndroid Build Coastguard Worker   if (!fcomps.size()) { return count; }
1008*08b48e0bSAndroid Build Coastguard Worker 
1009*08b48e0bSAndroid Build Coastguard Worker   IntegerType *Int1Ty = IntegerType::getInt1Ty(C);
1010*08b48e0bSAndroid Build Coastguard Worker 
1011*08b48e0bSAndroid Build Coastguard Worker   for (auto &FcmpInst : fcomps) {
1012*08b48e0bSAndroid Build Coastguard Worker 
1013*08b48e0bSAndroid Build Coastguard Worker     BasicBlock *bb = FcmpInst->getParent();
1014*08b48e0bSAndroid Build Coastguard Worker 
1015*08b48e0bSAndroid Build Coastguard Worker     auto op0 = FcmpInst->getOperand(0);
1016*08b48e0bSAndroid Build Coastguard Worker     auto op1 = FcmpInst->getOperand(1);
1017*08b48e0bSAndroid Build Coastguard Worker 
1018*08b48e0bSAndroid Build Coastguard Worker     unsigned op_size;
1019*08b48e0bSAndroid Build Coastguard Worker     op_size = op0->getType()->getPrimitiveSizeInBits();
1020*08b48e0bSAndroid Build Coastguard Worker 
1021*08b48e0bSAndroid Build Coastguard Worker     if (op_size != op1->getType()->getPrimitiveSizeInBits()) { continue; }
1022*08b48e0bSAndroid Build Coastguard Worker 
1023*08b48e0bSAndroid Build Coastguard Worker     const unsigned int sizeInBits = op0->getType()->getPrimitiveSizeInBits();
1024*08b48e0bSAndroid Build Coastguard Worker 
1025*08b48e0bSAndroid Build Coastguard Worker     // BUG FIXME TODO: u64 does not work for > 64 bit ... e.g. 80 and 128 bit
1026*08b48e0bSAndroid Build Coastguard Worker     if (sizeInBits > 64) { continue; }
1027*08b48e0bSAndroid Build Coastguard Worker 
1028*08b48e0bSAndroid Build Coastguard Worker     IntegerType       *intType = IntegerType::get(C, op_size);
1029*08b48e0bSAndroid Build Coastguard Worker     const unsigned int precision = sizeInBits == 32    ? 24
1030*08b48e0bSAndroid Build Coastguard Worker                                    : sizeInBits == 64  ? 53
1031*08b48e0bSAndroid Build Coastguard Worker                                    : sizeInBits == 128 ? 113
1032*08b48e0bSAndroid Build Coastguard Worker                                    : sizeInBits == 16  ? 11
1033*08b48e0bSAndroid Build Coastguard Worker                                    : sizeInBits == 80  ? 65
1034*08b48e0bSAndroid Build Coastguard Worker                                                        : sizeInBits - 8;
1035*08b48e0bSAndroid Build Coastguard Worker 
1036*08b48e0bSAndroid Build Coastguard Worker     const unsigned           shiftR_exponent = precision - 1;
1037*08b48e0bSAndroid Build Coastguard Worker     const unsigned long long mask_fraction =
1038*08b48e0bSAndroid Build Coastguard Worker         (1ULL << (shiftR_exponent - 1)) | ((1ULL << (shiftR_exponent - 1)) - 1);
1039*08b48e0bSAndroid Build Coastguard Worker     const unsigned long long mask_exponent =
1040*08b48e0bSAndroid Build Coastguard Worker         (1ULL << (sizeInBits - precision)) - 1;
1041*08b48e0bSAndroid Build Coastguard Worker 
1042*08b48e0bSAndroid Build Coastguard Worker     // round up sizes to the next power of two
1043*08b48e0bSAndroid Build Coastguard Worker     // this should help with integer compare splitting
1044*08b48e0bSAndroid Build Coastguard Worker     size_t exTySizeBytes = ((sizeInBits - precision + 7) >> 3);
1045*08b48e0bSAndroid Build Coastguard Worker     size_t frTySizeBytes = ((precision - 1ULL + 7) >> 3);
1046*08b48e0bSAndroid Build Coastguard Worker 
1047*08b48e0bSAndroid Build Coastguard Worker     IntegerType *IntExponentTy =
1048*08b48e0bSAndroid Build Coastguard Worker         IntegerType::get(C, nextPowerOfTwo(exTySizeBytes) << 3);
1049*08b48e0bSAndroid Build Coastguard Worker     IntegerType *IntFractionTy =
1050*08b48e0bSAndroid Build Coastguard Worker         IntegerType::get(C, nextPowerOfTwo(frTySizeBytes) << 3);
1051*08b48e0bSAndroid Build Coastguard Worker 
1052*08b48e0bSAndroid Build Coastguard Worker     //    errs() << "Fractions: IntFractionTy size " <<
1053*08b48e0bSAndroid Build Coastguard Worker     //     IntFractionTy->getPrimitiveSizeInBits() << ", op_size " << op_size <<
1054*08b48e0bSAndroid Build Coastguard Worker     //     ", mask " << mask_fraction <<
1055*08b48e0bSAndroid Build Coastguard Worker     //     ", precision " << precision << "\n";
1056*08b48e0bSAndroid Build Coastguard Worker 
1057*08b48e0bSAndroid Build Coastguard Worker     BasicBlock *end_bb = bb->splitBasicBlock(BasicBlock::iterator(FcmpInst));
1058*08b48e0bSAndroid Build Coastguard Worker 
1059*08b48e0bSAndroid Build Coastguard Worker     /* create the integers from floats directly */
1060*08b48e0bSAndroid Build Coastguard Worker     Instruction *bpre_op0, *bpre_op1;
1061*08b48e0bSAndroid Build Coastguard Worker     bpre_op0 = CastInst::Create(Instruction::BitCast, op0,
1062*08b48e0bSAndroid Build Coastguard Worker                                 IntegerType::get(C, op_size));
1063*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1064*08b48e0bSAndroid Build Coastguard Worker     bpre_op0->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
1065*08b48e0bSAndroid Build Coastguard Worker #else
1066*08b48e0bSAndroid Build Coastguard Worker     bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()),
1067*08b48e0bSAndroid Build Coastguard Worker                              bpre_op0);
1068*08b48e0bSAndroid Build Coastguard Worker #endif
1069*08b48e0bSAndroid Build Coastguard Worker 
1070*08b48e0bSAndroid Build Coastguard Worker     bpre_op1 = CastInst::Create(Instruction::BitCast, op1,
1071*08b48e0bSAndroid Build Coastguard Worker                                 IntegerType::get(C, op_size));
1072*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1073*08b48e0bSAndroid Build Coastguard Worker     bpre_op1->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
1074*08b48e0bSAndroid Build Coastguard Worker #else
1075*08b48e0bSAndroid Build Coastguard Worker     bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()),
1076*08b48e0bSAndroid Build Coastguard Worker                              bpre_op1);
1077*08b48e0bSAndroid Build Coastguard Worker #endif
1078*08b48e0bSAndroid Build Coastguard Worker 
1079*08b48e0bSAndroid Build Coastguard Worker     /* Check if any operand is NaN.
1080*08b48e0bSAndroid Build Coastguard Worker      * If so, all comparisons except unequal (which yields true) yield false */
1081*08b48e0bSAndroid Build Coastguard Worker 
1082*08b48e0bSAndroid Build Coastguard Worker     /* build mask for NaN */
1083*08b48e0bSAndroid Build Coastguard Worker     const unsigned long long NaN_lowend = mask_exponent << precision;
1084*08b48e0bSAndroid Build Coastguard Worker     //    errs() << "Fractions: IntFractionTy size " <<
1085*08b48e0bSAndroid Build Coastguard Worker     //     IntFractionTy->getPrimitiveSizeInBits() << ", op_size " << op_size <<
1086*08b48e0bSAndroid Build Coastguard Worker     //     ", mask_fraction 0x";
1087*08b48e0bSAndroid Build Coastguard Worker     //    errs().write_hex(mask_fraction);
1088*08b48e0bSAndroid Build Coastguard Worker     //    errs() << ", precision " << precision <<
1089*08b48e0bSAndroid Build Coastguard Worker     //     ", NaN_lowend 0x";
1090*08b48e0bSAndroid Build Coastguard Worker     //    errs().write_hex(NaN_lowend); errs() << "\n";
1091*08b48e0bSAndroid Build Coastguard Worker 
1092*08b48e0bSAndroid Build Coastguard Worker     /* Check op0 for NaN */
1093*08b48e0bSAndroid Build Coastguard Worker     /* Shift left 1 Bit, ignore sign bit */
1094*08b48e0bSAndroid Build Coastguard Worker     Instruction *nan_op0, *nan_op1;
1095*08b48e0bSAndroid Build Coastguard Worker     nan_op0 = BinaryOperator::Create(Instruction::Shl, bpre_op0,
1096*08b48e0bSAndroid Build Coastguard Worker                                      ConstantInt::get(bpre_op0->getType(), 1));
1097*08b48e0bSAndroid Build Coastguard Worker     /* Check op1 for NaN */
1098*08b48e0bSAndroid Build Coastguard Worker     /* Shift right 1 Bit, ignore sign bit */
1099*08b48e0bSAndroid Build Coastguard Worker     nan_op1 = BinaryOperator::Create(Instruction::Shl, bpre_op1,
1100*08b48e0bSAndroid Build Coastguard Worker                                      ConstantInt::get(bpre_op1->getType(), 1));
1101*08b48e0bSAndroid Build Coastguard Worker     /* compare to NaN interval */
1102*08b48e0bSAndroid Build Coastguard Worker     Instruction *is_op0_nan =
1103*08b48e0bSAndroid Build Coastguard Worker         CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_UGT, nan_op0,
1104*08b48e0bSAndroid Build Coastguard Worker                         ConstantInt::get(intType, NaN_lowend));
1105*08b48e0bSAndroid Build Coastguard Worker     /* compare to NaN interval */
1106*08b48e0bSAndroid Build Coastguard Worker     Instruction *is_op1_nan =
1107*08b48e0bSAndroid Build Coastguard Worker         CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_UGT, nan_op1,
1108*08b48e0bSAndroid Build Coastguard Worker                         ConstantInt::get(intType, NaN_lowend));
1109*08b48e0bSAndroid Build Coastguard Worker     /* combine checks */
1110*08b48e0bSAndroid Build Coastguard Worker     Instruction *is_nan =
1111*08b48e0bSAndroid Build Coastguard Worker         BinaryOperator::Create(Instruction::Or, is_op0_nan, is_op1_nan);
1112*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1113*08b48e0bSAndroid Build Coastguard Worker     nan_op0->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
1114*08b48e0bSAndroid Build Coastguard Worker     is_op0_nan->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
1115*08b48e0bSAndroid Build Coastguard Worker     nan_op1->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
1116*08b48e0bSAndroid Build Coastguard Worker     is_op1_nan->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
1117*08b48e0bSAndroid Build Coastguard Worker     is_nan->insertInto(bb, BasicBlock::iterator(bb->getTerminator()));
1118*08b48e0bSAndroid Build Coastguard Worker #else
1119*08b48e0bSAndroid Build Coastguard Worker     bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()),
1120*08b48e0bSAndroid Build Coastguard Worker                              nan_op0);
1121*08b48e0bSAndroid Build Coastguard Worker 
1122*08b48e0bSAndroid Build Coastguard Worker     bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()),
1123*08b48e0bSAndroid Build Coastguard Worker                              is_op0_nan);
1124*08b48e0bSAndroid Build Coastguard Worker 
1125*08b48e0bSAndroid Build Coastguard Worker     bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()),
1126*08b48e0bSAndroid Build Coastguard Worker                              nan_op1);
1127*08b48e0bSAndroid Build Coastguard Worker 
1128*08b48e0bSAndroid Build Coastguard Worker     bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()),
1129*08b48e0bSAndroid Build Coastguard Worker                              is_op1_nan);
1130*08b48e0bSAndroid Build Coastguard Worker 
1131*08b48e0bSAndroid Build Coastguard Worker     bb->getInstList().insert(BasicBlock::iterator(bb->getTerminator()), is_nan);
1132*08b48e0bSAndroid Build Coastguard Worker #endif
1133*08b48e0bSAndroid Build Coastguard Worker 
1134*08b48e0bSAndroid Build Coastguard Worker     /* the result of the comparison, when at least one op is NaN
1135*08b48e0bSAndroid Build Coastguard Worker        is true only for the "NOT EQUAL" predicates. */
1136*08b48e0bSAndroid Build Coastguard Worker     bool NaNcmp_result = FcmpInst->getPredicate() == CmpInst::FCMP_ONE ||
1137*08b48e0bSAndroid Build Coastguard Worker                          FcmpInst->getPredicate() == CmpInst::FCMP_UNE;
1138*08b48e0bSAndroid Build Coastguard Worker 
1139*08b48e0bSAndroid Build Coastguard Worker     BasicBlock *nonan_bb =
1140*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::Create(C, "noNaN", end_bb->getParent(), end_bb);
1141*08b48e0bSAndroid Build Coastguard Worker 
1142*08b48e0bSAndroid Build Coastguard Worker     BranchInst::Create(end_bb, nonan_bb);
1143*08b48e0bSAndroid Build Coastguard Worker 
1144*08b48e0bSAndroid Build Coastguard Worker     auto term = bb->getTerminator();
1145*08b48e0bSAndroid Build Coastguard Worker     /* if no operand is NaN goto nonan_bb else to handleNaN_bb */
1146*08b48e0bSAndroid Build Coastguard Worker     BranchInst::Create(end_bb, nonan_bb, is_nan, bb);
1147*08b48e0bSAndroid Build Coastguard Worker     term->eraseFromParent();
1148*08b48e0bSAndroid Build Coastguard Worker 
1149*08b48e0bSAndroid Build Coastguard Worker     /*** now working in nonan_bb ***/
1150*08b48e0bSAndroid Build Coastguard Worker 
1151*08b48e0bSAndroid Build Coastguard Worker     /* Treat -0.0 as equal to +0.0, that is for -0.0 make it +0.0 */
1152*08b48e0bSAndroid Build Coastguard Worker     Instruction             *b_op0, *b_op1;
1153*08b48e0bSAndroid Build Coastguard Worker     Instruction             *isMzero_op0, *isMzero_op1;
1154*08b48e0bSAndroid Build Coastguard Worker     const unsigned long long MinusZero = 1UL << (sizeInBits - 1U);
1155*08b48e0bSAndroid Build Coastguard Worker     const unsigned long long PlusZero = 0;
1156*08b48e0bSAndroid Build Coastguard Worker 
1157*08b48e0bSAndroid Build Coastguard Worker     isMzero_op0 = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, bpre_op0,
1158*08b48e0bSAndroid Build Coastguard Worker                                   ConstantInt::get(intType, MinusZero));
1159*08b48e0bSAndroid Build Coastguard Worker     isMzero_op1 = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, bpre_op1,
1160*08b48e0bSAndroid Build Coastguard Worker                                   ConstantInt::get(intType, MinusZero));
1161*08b48e0bSAndroid Build Coastguard Worker     b_op0 = SelectInst::Create(isMzero_op0, ConstantInt::get(intType, PlusZero),
1162*08b48e0bSAndroid Build Coastguard Worker                                bpre_op0);
1163*08b48e0bSAndroid Build Coastguard Worker     b_op1 = SelectInst::Create(isMzero_op1, ConstantInt::get(intType, PlusZero),
1164*08b48e0bSAndroid Build Coastguard Worker                                bpre_op1);
1165*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1166*08b48e0bSAndroid Build Coastguard Worker     isMzero_op0->insertInto(nonan_bb,
1167*08b48e0bSAndroid Build Coastguard Worker                             BasicBlock::iterator(nonan_bb->getTerminator()));
1168*08b48e0bSAndroid Build Coastguard Worker     isMzero_op1->insertInto(nonan_bb,
1169*08b48e0bSAndroid Build Coastguard Worker                             BasicBlock::iterator(nonan_bb->getTerminator()));
1170*08b48e0bSAndroid Build Coastguard Worker     b_op0->insertInto(nonan_bb,
1171*08b48e0bSAndroid Build Coastguard Worker                       BasicBlock::iterator(nonan_bb->getTerminator()));
1172*08b48e0bSAndroid Build Coastguard Worker     b_op1->insertInto(nonan_bb,
1173*08b48e0bSAndroid Build Coastguard Worker                       BasicBlock::iterator(nonan_bb->getTerminator()));
1174*08b48e0bSAndroid Build Coastguard Worker #else
1175*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1176*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), isMzero_op0);
1177*08b48e0bSAndroid Build Coastguard Worker 
1178*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1179*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), isMzero_op1);
1180*08b48e0bSAndroid Build Coastguard Worker 
1181*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1182*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), b_op0);
1183*08b48e0bSAndroid Build Coastguard Worker 
1184*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1185*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), b_op1);
1186*08b48e0bSAndroid Build Coastguard Worker #endif
1187*08b48e0bSAndroid Build Coastguard Worker 
1188*08b48e0bSAndroid Build Coastguard Worker     /* isolate signs of value of floating point type */
1189*08b48e0bSAndroid Build Coastguard Worker 
1190*08b48e0bSAndroid Build Coastguard Worker     /* create a 1 bit compare for the sign bit. to do this shift and trunc
1191*08b48e0bSAndroid Build Coastguard Worker      * the original operands so only the first bit remains.*/
1192*08b48e0bSAndroid Build Coastguard Worker     Instruction *s_s0, *t_s0, *s_s1, *t_s1, *icmp_sign_bit;
1193*08b48e0bSAndroid Build Coastguard Worker 
1194*08b48e0bSAndroid Build Coastguard Worker     s_s0 =
1195*08b48e0bSAndroid Build Coastguard Worker         BinaryOperator::Create(Instruction::LShr, b_op0,
1196*08b48e0bSAndroid Build Coastguard Worker                                ConstantInt::get(b_op0->getType(), op_size - 1));
1197*08b48e0bSAndroid Build Coastguard Worker     s_s1 =
1198*08b48e0bSAndroid Build Coastguard Worker         BinaryOperator::Create(Instruction::LShr, b_op1,
1199*08b48e0bSAndroid Build Coastguard Worker                                ConstantInt::get(b_op1->getType(), op_size - 1));
1200*08b48e0bSAndroid Build Coastguard Worker     t_s0 = new TruncInst(s_s0, Int1Ty);
1201*08b48e0bSAndroid Build Coastguard Worker     t_s1 = new TruncInst(s_s1, Int1Ty);
1202*08b48e0bSAndroid Build Coastguard Worker     /* compare of the sign bits */
1203*08b48e0bSAndroid Build Coastguard Worker     icmp_sign_bit =
1204*08b48e0bSAndroid Build Coastguard Worker         CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, t_s0, t_s1);
1205*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1206*08b48e0bSAndroid Build Coastguard Worker     s_s0->insertInto(nonan_bb, BasicBlock::iterator(nonan_bb->getTerminator()));
1207*08b48e0bSAndroid Build Coastguard Worker     t_s0->insertInto(nonan_bb, BasicBlock::iterator(nonan_bb->getTerminator()));
1208*08b48e0bSAndroid Build Coastguard Worker     s_s1->insertInto(nonan_bb, BasicBlock::iterator(nonan_bb->getTerminator()));
1209*08b48e0bSAndroid Build Coastguard Worker     t_s1->insertInto(nonan_bb, BasicBlock::iterator(nonan_bb->getTerminator()));
1210*08b48e0bSAndroid Build Coastguard Worker     icmp_sign_bit->insertInto(nonan_bb,
1211*08b48e0bSAndroid Build Coastguard Worker                               BasicBlock::iterator(nonan_bb->getTerminator()));
1212*08b48e0bSAndroid Build Coastguard Worker #else
1213*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1214*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), s_s0);
1215*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1216*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), t_s0);
1217*08b48e0bSAndroid Build Coastguard Worker 
1218*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1219*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), s_s1);
1220*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1221*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), t_s1);
1222*08b48e0bSAndroid Build Coastguard Worker 
1223*08b48e0bSAndroid Build Coastguard Worker     nonan_bb->getInstList().insert(
1224*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(nonan_bb->getTerminator()), icmp_sign_bit);
1225*08b48e0bSAndroid Build Coastguard Worker #endif
1226*08b48e0bSAndroid Build Coastguard Worker 
1227*08b48e0bSAndroid Build Coastguard Worker     /* create a new basic block which is executed if the signedness bits are
1228*08b48e0bSAndroid Build Coastguard Worker      * equal */
1229*08b48e0bSAndroid Build Coastguard Worker     BasicBlock *signequal_bb =
1230*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::Create(C, "signequal", end_bb->getParent(), end_bb);
1231*08b48e0bSAndroid Build Coastguard Worker 
1232*08b48e0bSAndroid Build Coastguard Worker     BranchInst::Create(end_bb, signequal_bb);
1233*08b48e0bSAndroid Build Coastguard Worker 
1234*08b48e0bSAndroid Build Coastguard Worker     /* create a new bb which is executed if exponents are satisfying the compare
1235*08b48e0bSAndroid Build Coastguard Worker      */
1236*08b48e0bSAndroid Build Coastguard Worker     BasicBlock *middle_bb =
1237*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::Create(C, "injected", end_bb->getParent(), end_bb);
1238*08b48e0bSAndroid Build Coastguard Worker 
1239*08b48e0bSAndroid Build Coastguard Worker     BranchInst::Create(end_bb, middle_bb);
1240*08b48e0bSAndroid Build Coastguard Worker 
1241*08b48e0bSAndroid Build Coastguard Worker     term = nonan_bb->getTerminator();
1242*08b48e0bSAndroid Build Coastguard Worker     /* if the signs are different goto end_bb else to signequal_bb */
1243*08b48e0bSAndroid Build Coastguard Worker     BranchInst::Create(signequal_bb, end_bb, icmp_sign_bit, nonan_bb);
1244*08b48e0bSAndroid Build Coastguard Worker     term->eraseFromParent();
1245*08b48e0bSAndroid Build Coastguard Worker 
1246*08b48e0bSAndroid Build Coastguard Worker     /* insert code for equal signs */
1247*08b48e0bSAndroid Build Coastguard Worker 
1248*08b48e0bSAndroid Build Coastguard Worker     /* isolate the exponents */
1249*08b48e0bSAndroid Build Coastguard Worker     Instruction *s_e0, *m_e0, *t_e0, *s_e1, *m_e1, *t_e1;
1250*08b48e0bSAndroid Build Coastguard Worker 
1251*08b48e0bSAndroid Build Coastguard Worker     s_e0 = BinaryOperator::Create(
1252*08b48e0bSAndroid Build Coastguard Worker         Instruction::LShr, b_op0,
1253*08b48e0bSAndroid Build Coastguard Worker         ConstantInt::get(b_op0->getType(), shiftR_exponent));
1254*08b48e0bSAndroid Build Coastguard Worker     s_e1 = BinaryOperator::Create(
1255*08b48e0bSAndroid Build Coastguard Worker         Instruction::LShr, b_op1,
1256*08b48e0bSAndroid Build Coastguard Worker         ConstantInt::get(b_op1->getType(), shiftR_exponent));
1257*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1258*08b48e0bSAndroid Build Coastguard Worker     s_e0->insertInto(signequal_bb,
1259*08b48e0bSAndroid Build Coastguard Worker                      BasicBlock::iterator(signequal_bb->getTerminator()));
1260*08b48e0bSAndroid Build Coastguard Worker     s_e1->insertInto(signequal_bb,
1261*08b48e0bSAndroid Build Coastguard Worker                      BasicBlock::iterator(signequal_bb->getTerminator()));
1262*08b48e0bSAndroid Build Coastguard Worker #else
1263*08b48e0bSAndroid Build Coastguard Worker     signequal_bb->getInstList().insert(
1264*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(signequal_bb->getTerminator()), s_e0);
1265*08b48e0bSAndroid Build Coastguard Worker     signequal_bb->getInstList().insert(
1266*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(signequal_bb->getTerminator()), s_e1);
1267*08b48e0bSAndroid Build Coastguard Worker #endif
1268*08b48e0bSAndroid Build Coastguard Worker 
1269*08b48e0bSAndroid Build Coastguard Worker     t_e0 = new TruncInst(s_e0, IntExponentTy);
1270*08b48e0bSAndroid Build Coastguard Worker     t_e1 = new TruncInst(s_e1, IntExponentTy);
1271*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1272*08b48e0bSAndroid Build Coastguard Worker     t_e0->insertInto(signequal_bb,
1273*08b48e0bSAndroid Build Coastguard Worker                      BasicBlock::iterator(signequal_bb->getTerminator()));
1274*08b48e0bSAndroid Build Coastguard Worker     t_e1->insertInto(signequal_bb,
1275*08b48e0bSAndroid Build Coastguard Worker                      BasicBlock::iterator(signequal_bb->getTerminator()));
1276*08b48e0bSAndroid Build Coastguard Worker #else
1277*08b48e0bSAndroid Build Coastguard Worker     signequal_bb->getInstList().insert(
1278*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(signequal_bb->getTerminator()), t_e0);
1279*08b48e0bSAndroid Build Coastguard Worker     signequal_bb->getInstList().insert(
1280*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(signequal_bb->getTerminator()), t_e1);
1281*08b48e0bSAndroid Build Coastguard Worker #endif
1282*08b48e0bSAndroid Build Coastguard Worker 
1283*08b48e0bSAndroid Build Coastguard Worker     if (sizeInBits - precision < exTySizeBytes * 8) {
1284*08b48e0bSAndroid Build Coastguard Worker 
1285*08b48e0bSAndroid Build Coastguard Worker       m_e0 = BinaryOperator::Create(
1286*08b48e0bSAndroid Build Coastguard Worker           Instruction::And, t_e0,
1287*08b48e0bSAndroid Build Coastguard Worker           ConstantInt::get(t_e0->getType(), mask_exponent));
1288*08b48e0bSAndroid Build Coastguard Worker       m_e1 = BinaryOperator::Create(
1289*08b48e0bSAndroid Build Coastguard Worker           Instruction::And, t_e1,
1290*08b48e0bSAndroid Build Coastguard Worker           ConstantInt::get(t_e1->getType(), mask_exponent));
1291*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1292*08b48e0bSAndroid Build Coastguard Worker       m_e0->insertInto(signequal_bb,
1293*08b48e0bSAndroid Build Coastguard Worker                        BasicBlock::iterator(signequal_bb->getTerminator()));
1294*08b48e0bSAndroid Build Coastguard Worker       m_e1->insertInto(signequal_bb,
1295*08b48e0bSAndroid Build Coastguard Worker                        BasicBlock::iterator(signequal_bb->getTerminator()));
1296*08b48e0bSAndroid Build Coastguard Worker #else
1297*08b48e0bSAndroid Build Coastguard Worker       signequal_bb->getInstList().insert(
1298*08b48e0bSAndroid Build Coastguard Worker           BasicBlock::iterator(signequal_bb->getTerminator()), m_e0);
1299*08b48e0bSAndroid Build Coastguard Worker       signequal_bb->getInstList().insert(
1300*08b48e0bSAndroid Build Coastguard Worker           BasicBlock::iterator(signequal_bb->getTerminator()), m_e1);
1301*08b48e0bSAndroid Build Coastguard Worker #endif
1302*08b48e0bSAndroid Build Coastguard Worker 
1303*08b48e0bSAndroid Build Coastguard Worker     } else {
1304*08b48e0bSAndroid Build Coastguard Worker 
1305*08b48e0bSAndroid Build Coastguard Worker       m_e0 = t_e0;
1306*08b48e0bSAndroid Build Coastguard Worker       m_e1 = t_e1;
1307*08b48e0bSAndroid Build Coastguard Worker 
1308*08b48e0bSAndroid Build Coastguard Worker     }
1309*08b48e0bSAndroid Build Coastguard Worker 
1310*08b48e0bSAndroid Build Coastguard Worker     /* compare the exponents of the operands */
1311*08b48e0bSAndroid Build Coastguard Worker     Instruction *icmp_exponents_equal;
1312*08b48e0bSAndroid Build Coastguard Worker     Instruction *icmp_exponent_result;
1313*08b48e0bSAndroid Build Coastguard Worker     BasicBlock  *signequal2_bb = signequal_bb;
1314*08b48e0bSAndroid Build Coastguard Worker     switch (FcmpInst->getPredicate()) {
1315*08b48e0bSAndroid Build Coastguard Worker 
1316*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UEQ:
1317*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OEQ:
1318*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent_result =
1319*08b48e0bSAndroid Build Coastguard Worker             CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, m_e0, m_e1);
1320*08b48e0bSAndroid Build Coastguard Worker         break;
1321*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_ONE:
1322*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UNE:
1323*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent_result =
1324*08b48e0bSAndroid Build Coastguard Worker             CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_NE, m_e0, m_e1);
1325*08b48e0bSAndroid Build Coastguard Worker         break;
1326*08b48e0bSAndroid Build Coastguard Worker       /* compare the exponents of the operands (signs are equal)
1327*08b48e0bSAndroid Build Coastguard Worker        * if exponents are equal -> proceed to mantissa comparison
1328*08b48e0bSAndroid Build Coastguard Worker        * else get result depending on sign
1329*08b48e0bSAndroid Build Coastguard Worker        */
1330*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OGT:
1331*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UGT:
1332*08b48e0bSAndroid Build Coastguard Worker         Instruction *icmp_exponent;
1333*08b48e0bSAndroid Build Coastguard Worker         icmp_exponents_equal =
1334*08b48e0bSAndroid Build Coastguard Worker             CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, m_e0, m_e1);
1335*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1336*08b48e0bSAndroid Build Coastguard Worker         icmp_exponents_equal->insertInto(
1337*08b48e0bSAndroid Build Coastguard Worker             signequal_bb, BasicBlock::iterator(signequal_bb->getTerminator()));
1338*08b48e0bSAndroid Build Coastguard Worker #else
1339*08b48e0bSAndroid Build Coastguard Worker         signequal_bb->getInstList().insert(
1340*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(signequal_bb->getTerminator()),
1341*08b48e0bSAndroid Build Coastguard Worker             icmp_exponents_equal);
1342*08b48e0bSAndroid Build Coastguard Worker #endif
1343*08b48e0bSAndroid Build Coastguard Worker 
1344*08b48e0bSAndroid Build Coastguard Worker         // shortcut for unequal exponents
1345*08b48e0bSAndroid Build Coastguard Worker         signequal2_bb = signequal_bb->splitBasicBlock(
1346*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(signequal_bb->getTerminator()));
1347*08b48e0bSAndroid Build Coastguard Worker 
1348*08b48e0bSAndroid Build Coastguard Worker         /* if the exponents are equal goto middle_bb else to signequal2_bb */
1349*08b48e0bSAndroid Build Coastguard Worker         term = signequal_bb->getTerminator();
1350*08b48e0bSAndroid Build Coastguard Worker         BranchInst::Create(middle_bb, signequal2_bb, icmp_exponents_equal,
1351*08b48e0bSAndroid Build Coastguard Worker                            signequal_bb);
1352*08b48e0bSAndroid Build Coastguard Worker         term->eraseFromParent();
1353*08b48e0bSAndroid Build Coastguard Worker 
1354*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent =
1355*08b48e0bSAndroid Build Coastguard Worker             CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_UGT, m_e0, m_e1);
1356*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1357*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent->insertInto(
1358*08b48e0bSAndroid Build Coastguard Worker             signequal2_bb,
1359*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(signequal2_bb->getTerminator()));
1360*08b48e0bSAndroid Build Coastguard Worker #else
1361*08b48e0bSAndroid Build Coastguard Worker         signequal2_bb->getInstList().insert(
1362*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(signequal2_bb->getTerminator()),
1363*08b48e0bSAndroid Build Coastguard Worker             icmp_exponent);
1364*08b48e0bSAndroid Build Coastguard Worker #endif
1365*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent_result =
1366*08b48e0bSAndroid Build Coastguard Worker             BinaryOperator::Create(Instruction::Xor, icmp_exponent, t_s0);
1367*08b48e0bSAndroid Build Coastguard Worker         break;
1368*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OLT:
1369*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_ULT:
1370*08b48e0bSAndroid Build Coastguard Worker         icmp_exponents_equal =
1371*08b48e0bSAndroid Build Coastguard Worker             CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, m_e0, m_e1);
1372*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1373*08b48e0bSAndroid Build Coastguard Worker         icmp_exponents_equal->insertInto(
1374*08b48e0bSAndroid Build Coastguard Worker             signequal_bb, BasicBlock::iterator(signequal_bb->getTerminator()));
1375*08b48e0bSAndroid Build Coastguard Worker #else
1376*08b48e0bSAndroid Build Coastguard Worker         signequal_bb->getInstList().insert(
1377*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(signequal_bb->getTerminator()),
1378*08b48e0bSAndroid Build Coastguard Worker             icmp_exponents_equal);
1379*08b48e0bSAndroid Build Coastguard Worker #endif
1380*08b48e0bSAndroid Build Coastguard Worker 
1381*08b48e0bSAndroid Build Coastguard Worker         // shortcut for unequal exponents
1382*08b48e0bSAndroid Build Coastguard Worker         signequal2_bb = signequal_bb->splitBasicBlock(
1383*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(signequal_bb->getTerminator()));
1384*08b48e0bSAndroid Build Coastguard Worker 
1385*08b48e0bSAndroid Build Coastguard Worker         /* if the exponents are equal goto middle_bb else to signequal2_bb */
1386*08b48e0bSAndroid Build Coastguard Worker         term = signequal_bb->getTerminator();
1387*08b48e0bSAndroid Build Coastguard Worker         BranchInst::Create(middle_bb, signequal2_bb, icmp_exponents_equal,
1388*08b48e0bSAndroid Build Coastguard Worker                            signequal_bb);
1389*08b48e0bSAndroid Build Coastguard Worker         term->eraseFromParent();
1390*08b48e0bSAndroid Build Coastguard Worker 
1391*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent =
1392*08b48e0bSAndroid Build Coastguard Worker             CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT, m_e0, m_e1);
1393*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1394*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent->insertInto(
1395*08b48e0bSAndroid Build Coastguard Worker             signequal2_bb,
1396*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(signequal2_bb->getTerminator()));
1397*08b48e0bSAndroid Build Coastguard Worker #else
1398*08b48e0bSAndroid Build Coastguard Worker         signequal2_bb->getInstList().insert(
1399*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(signequal2_bb->getTerminator()),
1400*08b48e0bSAndroid Build Coastguard Worker             icmp_exponent);
1401*08b48e0bSAndroid Build Coastguard Worker #endif
1402*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent_result =
1403*08b48e0bSAndroid Build Coastguard Worker             BinaryOperator::Create(Instruction::Xor, icmp_exponent, t_s0);
1404*08b48e0bSAndroid Build Coastguard Worker         break;
1405*08b48e0bSAndroid Build Coastguard Worker       default:
1406*08b48e0bSAndroid Build Coastguard Worker         continue;
1407*08b48e0bSAndroid Build Coastguard Worker 
1408*08b48e0bSAndroid Build Coastguard Worker     }
1409*08b48e0bSAndroid Build Coastguard Worker 
1410*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1411*08b48e0bSAndroid Build Coastguard Worker     icmp_exponent_result->insertInto(
1412*08b48e0bSAndroid Build Coastguard Worker         signequal2_bb, BasicBlock::iterator(signequal2_bb->getTerminator()));
1413*08b48e0bSAndroid Build Coastguard Worker #else
1414*08b48e0bSAndroid Build Coastguard Worker     signequal2_bb->getInstList().insert(
1415*08b48e0bSAndroid Build Coastguard Worker         BasicBlock::iterator(signequal2_bb->getTerminator()),
1416*08b48e0bSAndroid Build Coastguard Worker         icmp_exponent_result);
1417*08b48e0bSAndroid Build Coastguard Worker #endif
1418*08b48e0bSAndroid Build Coastguard Worker 
1419*08b48e0bSAndroid Build Coastguard Worker     {
1420*08b48e0bSAndroid Build Coastguard Worker 
1421*08b48e0bSAndroid Build Coastguard Worker       term = signequal2_bb->getTerminator();
1422*08b48e0bSAndroid Build Coastguard Worker 
1423*08b48e0bSAndroid Build Coastguard Worker       switch (FcmpInst->getPredicate()) {
1424*08b48e0bSAndroid Build Coastguard Worker 
1425*08b48e0bSAndroid Build Coastguard Worker         case CmpInst::FCMP_UEQ:
1426*08b48e0bSAndroid Build Coastguard Worker         case CmpInst::FCMP_OEQ:
1427*08b48e0bSAndroid Build Coastguard Worker           /* if the exponents are satifying the compare do a fraction cmp in
1428*08b48e0bSAndroid Build Coastguard Worker            * middle_bb */
1429*08b48e0bSAndroid Build Coastguard Worker           BranchInst::Create(middle_bb, end_bb, icmp_exponent_result,
1430*08b48e0bSAndroid Build Coastguard Worker                              signequal2_bb);
1431*08b48e0bSAndroid Build Coastguard Worker           break;
1432*08b48e0bSAndroid Build Coastguard Worker         case CmpInst::FCMP_ONE:
1433*08b48e0bSAndroid Build Coastguard Worker         case CmpInst::FCMP_UNE:
1434*08b48e0bSAndroid Build Coastguard Worker           /* if the exponents are satifying the compare do a fraction cmp in
1435*08b48e0bSAndroid Build Coastguard Worker            * middle_bb */
1436*08b48e0bSAndroid Build Coastguard Worker           BranchInst::Create(end_bb, middle_bb, icmp_exponent_result,
1437*08b48e0bSAndroid Build Coastguard Worker                              signequal2_bb);
1438*08b48e0bSAndroid Build Coastguard Worker           break;
1439*08b48e0bSAndroid Build Coastguard Worker         case CmpInst::FCMP_OGT:
1440*08b48e0bSAndroid Build Coastguard Worker         case CmpInst::FCMP_UGT:
1441*08b48e0bSAndroid Build Coastguard Worker         case CmpInst::FCMP_OLT:
1442*08b48e0bSAndroid Build Coastguard Worker         case CmpInst::FCMP_ULT:
1443*08b48e0bSAndroid Build Coastguard Worker           BranchInst::Create(end_bb, signequal2_bb);
1444*08b48e0bSAndroid Build Coastguard Worker           break;
1445*08b48e0bSAndroid Build Coastguard Worker         default:
1446*08b48e0bSAndroid Build Coastguard Worker           continue;
1447*08b48e0bSAndroid Build Coastguard Worker 
1448*08b48e0bSAndroid Build Coastguard Worker       }
1449*08b48e0bSAndroid Build Coastguard Worker 
1450*08b48e0bSAndroid Build Coastguard Worker       term->eraseFromParent();
1451*08b48e0bSAndroid Build Coastguard Worker 
1452*08b48e0bSAndroid Build Coastguard Worker     }
1453*08b48e0bSAndroid Build Coastguard Worker 
1454*08b48e0bSAndroid Build Coastguard Worker     /* isolate the mantissa aka fraction */
1455*08b48e0bSAndroid Build Coastguard Worker     Instruction *t_f0, *t_f1;
1456*08b48e0bSAndroid Build Coastguard Worker     bool         needTrunc = IntFractionTy->getPrimitiveSizeInBits() < op_size;
1457*08b48e0bSAndroid Build Coastguard Worker 
1458*08b48e0bSAndroid Build Coastguard Worker     if (precision - 1 < frTySizeBytes * 8) {
1459*08b48e0bSAndroid Build Coastguard Worker 
1460*08b48e0bSAndroid Build Coastguard Worker       Instruction *m_f0, *m_f1;
1461*08b48e0bSAndroid Build Coastguard Worker       m_f0 = BinaryOperator::Create(
1462*08b48e0bSAndroid Build Coastguard Worker           Instruction::And, b_op0,
1463*08b48e0bSAndroid Build Coastguard Worker           ConstantInt::get(b_op0->getType(), mask_fraction));
1464*08b48e0bSAndroid Build Coastguard Worker       m_f1 = BinaryOperator::Create(
1465*08b48e0bSAndroid Build Coastguard Worker           Instruction::And, b_op1,
1466*08b48e0bSAndroid Build Coastguard Worker           ConstantInt::get(b_op1->getType(), mask_fraction));
1467*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1468*08b48e0bSAndroid Build Coastguard Worker       m_f0->insertInto(middle_bb,
1469*08b48e0bSAndroid Build Coastguard Worker                        BasicBlock::iterator(middle_bb->getTerminator()));
1470*08b48e0bSAndroid Build Coastguard Worker       m_f1->insertInto(middle_bb,
1471*08b48e0bSAndroid Build Coastguard Worker                        BasicBlock::iterator(middle_bb->getTerminator()));
1472*08b48e0bSAndroid Build Coastguard Worker #else
1473*08b48e0bSAndroid Build Coastguard Worker       middle_bb->getInstList().insert(
1474*08b48e0bSAndroid Build Coastguard Worker           BasicBlock::iterator(middle_bb->getTerminator()), m_f0);
1475*08b48e0bSAndroid Build Coastguard Worker       middle_bb->getInstList().insert(
1476*08b48e0bSAndroid Build Coastguard Worker           BasicBlock::iterator(middle_bb->getTerminator()), m_f1);
1477*08b48e0bSAndroid Build Coastguard Worker #endif
1478*08b48e0bSAndroid Build Coastguard Worker 
1479*08b48e0bSAndroid Build Coastguard Worker       if (needTrunc) {
1480*08b48e0bSAndroid Build Coastguard Worker 
1481*08b48e0bSAndroid Build Coastguard Worker         t_f0 = new TruncInst(m_f0, IntFractionTy);
1482*08b48e0bSAndroid Build Coastguard Worker         t_f1 = new TruncInst(m_f1, IntFractionTy);
1483*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1484*08b48e0bSAndroid Build Coastguard Worker         t_f0->insertInto(middle_bb,
1485*08b48e0bSAndroid Build Coastguard Worker                          BasicBlock::iterator(middle_bb->getTerminator()));
1486*08b48e0bSAndroid Build Coastguard Worker         t_f1->insertInto(middle_bb,
1487*08b48e0bSAndroid Build Coastguard Worker                          BasicBlock::iterator(middle_bb->getTerminator()));
1488*08b48e0bSAndroid Build Coastguard Worker #else
1489*08b48e0bSAndroid Build Coastguard Worker         middle_bb->getInstList().insert(
1490*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(middle_bb->getTerminator()), t_f0);
1491*08b48e0bSAndroid Build Coastguard Worker         middle_bb->getInstList().insert(
1492*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(middle_bb->getTerminator()), t_f1);
1493*08b48e0bSAndroid Build Coastguard Worker #endif
1494*08b48e0bSAndroid Build Coastguard Worker 
1495*08b48e0bSAndroid Build Coastguard Worker       } else {
1496*08b48e0bSAndroid Build Coastguard Worker 
1497*08b48e0bSAndroid Build Coastguard Worker         t_f0 = m_f0;
1498*08b48e0bSAndroid Build Coastguard Worker         t_f1 = m_f1;
1499*08b48e0bSAndroid Build Coastguard Worker 
1500*08b48e0bSAndroid Build Coastguard Worker       }
1501*08b48e0bSAndroid Build Coastguard Worker 
1502*08b48e0bSAndroid Build Coastguard Worker     } else {
1503*08b48e0bSAndroid Build Coastguard Worker 
1504*08b48e0bSAndroid Build Coastguard Worker       if (needTrunc) {
1505*08b48e0bSAndroid Build Coastguard Worker 
1506*08b48e0bSAndroid Build Coastguard Worker         t_f0 = new TruncInst(b_op0, IntFractionTy);
1507*08b48e0bSAndroid Build Coastguard Worker         t_f1 = new TruncInst(b_op1, IntFractionTy);
1508*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1509*08b48e0bSAndroid Build Coastguard Worker         t_f0->insertInto(middle_bb,
1510*08b48e0bSAndroid Build Coastguard Worker                          BasicBlock::iterator(middle_bb->getTerminator()));
1511*08b48e0bSAndroid Build Coastguard Worker         t_f1->insertInto(middle_bb,
1512*08b48e0bSAndroid Build Coastguard Worker                          BasicBlock::iterator(middle_bb->getTerminator()));
1513*08b48e0bSAndroid Build Coastguard Worker #else
1514*08b48e0bSAndroid Build Coastguard Worker         middle_bb->getInstList().insert(
1515*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(middle_bb->getTerminator()), t_f0);
1516*08b48e0bSAndroid Build Coastguard Worker         middle_bb->getInstList().insert(
1517*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(middle_bb->getTerminator()), t_f1);
1518*08b48e0bSAndroid Build Coastguard Worker #endif
1519*08b48e0bSAndroid Build Coastguard Worker 
1520*08b48e0bSAndroid Build Coastguard Worker       } else {
1521*08b48e0bSAndroid Build Coastguard Worker 
1522*08b48e0bSAndroid Build Coastguard Worker         t_f0 = b_op0;
1523*08b48e0bSAndroid Build Coastguard Worker         t_f1 = b_op1;
1524*08b48e0bSAndroid Build Coastguard Worker 
1525*08b48e0bSAndroid Build Coastguard Worker       }
1526*08b48e0bSAndroid Build Coastguard Worker 
1527*08b48e0bSAndroid Build Coastguard Worker     }
1528*08b48e0bSAndroid Build Coastguard Worker 
1529*08b48e0bSAndroid Build Coastguard Worker     /* compare the fractions of the operands */
1530*08b48e0bSAndroid Build Coastguard Worker     Instruction *icmp_fraction_result;
1531*08b48e0bSAndroid Build Coastguard Worker     BasicBlock  *middle2_bb = middle_bb;
1532*08b48e0bSAndroid Build Coastguard Worker     PHINode     *PN2 = nullptr;
1533*08b48e0bSAndroid Build Coastguard Worker     switch (FcmpInst->getPredicate()) {
1534*08b48e0bSAndroid Build Coastguard Worker 
1535*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UEQ:
1536*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OEQ:
1537*08b48e0bSAndroid Build Coastguard Worker         icmp_fraction_result =
1538*08b48e0bSAndroid Build Coastguard Worker             CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, t_f0, t_f1);
1539*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1540*08b48e0bSAndroid Build Coastguard Worker         icmp_fraction_result->insertInto(
1541*08b48e0bSAndroid Build Coastguard Worker             middle2_bb, BasicBlock::iterator(middle2_bb->getTerminator()));
1542*08b48e0bSAndroid Build Coastguard Worker #else
1543*08b48e0bSAndroid Build Coastguard Worker         middle2_bb->getInstList().insert(
1544*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(middle2_bb->getTerminator()),
1545*08b48e0bSAndroid Build Coastguard Worker             icmp_fraction_result);
1546*08b48e0bSAndroid Build Coastguard Worker #endif
1547*08b48e0bSAndroid Build Coastguard Worker 
1548*08b48e0bSAndroid Build Coastguard Worker         break;
1549*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UNE:
1550*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_ONE:
1551*08b48e0bSAndroid Build Coastguard Worker         icmp_fraction_result =
1552*08b48e0bSAndroid Build Coastguard Worker             CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_NE, t_f0, t_f1);
1553*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1554*08b48e0bSAndroid Build Coastguard Worker         icmp_fraction_result->insertInto(
1555*08b48e0bSAndroid Build Coastguard Worker             middle2_bb, BasicBlock::iterator(middle2_bb->getTerminator()));
1556*08b48e0bSAndroid Build Coastguard Worker #else
1557*08b48e0bSAndroid Build Coastguard Worker         middle2_bb->getInstList().insert(
1558*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(middle2_bb->getTerminator()),
1559*08b48e0bSAndroid Build Coastguard Worker             icmp_fraction_result);
1560*08b48e0bSAndroid Build Coastguard Worker #endif
1561*08b48e0bSAndroid Build Coastguard Worker 
1562*08b48e0bSAndroid Build Coastguard Worker         break;
1563*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OGT:
1564*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UGT:
1565*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OLT:
1566*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_ULT: {
1567*08b48e0bSAndroid Build Coastguard Worker 
1568*08b48e0bSAndroid Build Coastguard Worker         Instruction *icmp_fraction_result2;
1569*08b48e0bSAndroid Build Coastguard Worker 
1570*08b48e0bSAndroid Build Coastguard Worker         middle2_bb = middle_bb->splitBasicBlock(
1571*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(middle_bb->getTerminator()));
1572*08b48e0bSAndroid Build Coastguard Worker 
1573*08b48e0bSAndroid Build Coastguard Worker         BasicBlock *negative_bb = BasicBlock::Create(
1574*08b48e0bSAndroid Build Coastguard Worker             C, "negative_value", middle2_bb->getParent(), middle2_bb);
1575*08b48e0bSAndroid Build Coastguard Worker         BasicBlock *positive_bb = BasicBlock::Create(
1576*08b48e0bSAndroid Build Coastguard Worker             C, "positive_value", negative_bb->getParent(), negative_bb);
1577*08b48e0bSAndroid Build Coastguard Worker 
1578*08b48e0bSAndroid Build Coastguard Worker         if (FcmpInst->getPredicate() == CmpInst::FCMP_OGT ||
1579*08b48e0bSAndroid Build Coastguard Worker             FcmpInst->getPredicate() == CmpInst::FCMP_UGT) {
1580*08b48e0bSAndroid Build Coastguard Worker 
1581*08b48e0bSAndroid Build Coastguard Worker           icmp_fraction_result =
1582*08b48e0bSAndroid Build Coastguard Worker               CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT, t_f0, t_f1);
1583*08b48e0bSAndroid Build Coastguard Worker           icmp_fraction_result2 =
1584*08b48e0bSAndroid Build Coastguard Worker               CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_UGT, t_f0, t_f1);
1585*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1586*08b48e0bSAndroid Build Coastguard Worker           icmp_fraction_result->insertInto(negative_bb, negative_bb->end());
1587*08b48e0bSAndroid Build Coastguard Worker           icmp_fraction_result2->insertInto(positive_bb, positive_bb->end());
1588*08b48e0bSAndroid Build Coastguard Worker #else
1589*08b48e0bSAndroid Build Coastguard Worker           negative_bb->getInstList().push_back(icmp_fraction_result);
1590*08b48e0bSAndroid Build Coastguard Worker           positive_bb->getInstList().push_back(icmp_fraction_result2);
1591*08b48e0bSAndroid Build Coastguard Worker #endif
1592*08b48e0bSAndroid Build Coastguard Worker 
1593*08b48e0bSAndroid Build Coastguard Worker         } else {
1594*08b48e0bSAndroid Build Coastguard Worker 
1595*08b48e0bSAndroid Build Coastguard Worker           icmp_fraction_result =
1596*08b48e0bSAndroid Build Coastguard Worker               CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_UGT, t_f0, t_f1);
1597*08b48e0bSAndroid Build Coastguard Worker           icmp_fraction_result2 =
1598*08b48e0bSAndroid Build Coastguard Worker               CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_ULT, t_f0, t_f1);
1599*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1600*08b48e0bSAndroid Build Coastguard Worker           icmp_fraction_result->insertInto(negative_bb, negative_bb->end());
1601*08b48e0bSAndroid Build Coastguard Worker           icmp_fraction_result2->insertInto(positive_bb, positive_bb->end());
1602*08b48e0bSAndroid Build Coastguard Worker #else
1603*08b48e0bSAndroid Build Coastguard Worker           negative_bb->getInstList().push_back(icmp_fraction_result);
1604*08b48e0bSAndroid Build Coastguard Worker           positive_bb->getInstList().push_back(icmp_fraction_result2);
1605*08b48e0bSAndroid Build Coastguard Worker #endif
1606*08b48e0bSAndroid Build Coastguard Worker 
1607*08b48e0bSAndroid Build Coastguard Worker         }
1608*08b48e0bSAndroid Build Coastguard Worker 
1609*08b48e0bSAndroid Build Coastguard Worker         BranchInst::Create(middle2_bb, negative_bb);
1610*08b48e0bSAndroid Build Coastguard Worker         BranchInst::Create(middle2_bb, positive_bb);
1611*08b48e0bSAndroid Build Coastguard Worker 
1612*08b48e0bSAndroid Build Coastguard Worker         term = middle_bb->getTerminator();
1613*08b48e0bSAndroid Build Coastguard Worker         BranchInst::Create(negative_bb, positive_bb, t_s0, middle_bb);
1614*08b48e0bSAndroid Build Coastguard Worker         term->eraseFromParent();
1615*08b48e0bSAndroid Build Coastguard Worker 
1616*08b48e0bSAndroid Build Coastguard Worker         PN2 = PHINode::Create(Int1Ty, 2, "");
1617*08b48e0bSAndroid Build Coastguard Worker         PN2->addIncoming(icmp_fraction_result, negative_bb);
1618*08b48e0bSAndroid Build Coastguard Worker         PN2->addIncoming(icmp_fraction_result2, positive_bb);
1619*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1620*08b48e0bSAndroid Build Coastguard Worker         PN2->insertInto(middle2_bb,
1621*08b48e0bSAndroid Build Coastguard Worker                         BasicBlock::iterator(middle2_bb->getTerminator()));
1622*08b48e0bSAndroid Build Coastguard Worker #else
1623*08b48e0bSAndroid Build Coastguard Worker         middle2_bb->getInstList().insert(
1624*08b48e0bSAndroid Build Coastguard Worker             BasicBlock::iterator(middle2_bb->getTerminator()), PN2);
1625*08b48e0bSAndroid Build Coastguard Worker #endif
1626*08b48e0bSAndroid Build Coastguard Worker 
1627*08b48e0bSAndroid Build Coastguard Worker       } break;
1628*08b48e0bSAndroid Build Coastguard Worker 
1629*08b48e0bSAndroid Build Coastguard Worker       default:
1630*08b48e0bSAndroid Build Coastguard Worker         continue;
1631*08b48e0bSAndroid Build Coastguard Worker 
1632*08b48e0bSAndroid Build Coastguard Worker     }
1633*08b48e0bSAndroid Build Coastguard Worker 
1634*08b48e0bSAndroid Build Coastguard Worker     PHINode *PN = PHINode::Create(Int1Ty, 4, "");
1635*08b48e0bSAndroid Build Coastguard Worker 
1636*08b48e0bSAndroid Build Coastguard Worker     switch (FcmpInst->getPredicate()) {
1637*08b48e0bSAndroid Build Coastguard Worker 
1638*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UEQ:
1639*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OEQ:
1640*08b48e0bSAndroid Build Coastguard Worker         /* unequal signs cannot be equal values */
1641*08b48e0bSAndroid Build Coastguard Worker         /* goto false branch */
1642*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(ConstantInt::get(Int1Ty, 0), nonan_bb);
1643*08b48e0bSAndroid Build Coastguard Worker         /* unequal exponents cannot be equal values, too */
1644*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(ConstantInt::get(Int1Ty, 0), signequal_bb);
1645*08b48e0bSAndroid Build Coastguard Worker         /* fractions comparison */
1646*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(icmp_fraction_result, middle2_bb);
1647*08b48e0bSAndroid Build Coastguard Worker         /* NaNs */
1648*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(ConstantInt::get(Int1Ty, NaNcmp_result), bb);
1649*08b48e0bSAndroid Build Coastguard Worker         break;
1650*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_ONE:
1651*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UNE:
1652*08b48e0bSAndroid Build Coastguard Worker         /* unequal signs are unequal values */
1653*08b48e0bSAndroid Build Coastguard Worker         /* goto true branch */
1654*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(ConstantInt::get(Int1Ty, 1), nonan_bb);
1655*08b48e0bSAndroid Build Coastguard Worker         /* unequal exponents are unequal values, too */
1656*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(icmp_exponent_result, signequal_bb);
1657*08b48e0bSAndroid Build Coastguard Worker         /* fractions comparison */
1658*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(icmp_fraction_result, middle2_bb);
1659*08b48e0bSAndroid Build Coastguard Worker         /* NaNs */
1660*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(ConstantInt::get(Int1Ty, NaNcmp_result), bb);
1661*08b48e0bSAndroid Build Coastguard Worker         break;
1662*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OGT:
1663*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_UGT:
1664*08b48e0bSAndroid Build Coastguard Worker         /* if op1 is negative goto true branch,
1665*08b48e0bSAndroid Build Coastguard Worker            else go on comparing */
1666*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(t_s1, nonan_bb);
1667*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(icmp_exponent_result, signequal2_bb);
1668*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(PN2, middle2_bb);
1669*08b48e0bSAndroid Build Coastguard Worker         /* NaNs */
1670*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(ConstantInt::get(Int1Ty, NaNcmp_result), bb);
1671*08b48e0bSAndroid Build Coastguard Worker         break;
1672*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_OLT:
1673*08b48e0bSAndroid Build Coastguard Worker       case CmpInst::FCMP_ULT:
1674*08b48e0bSAndroid Build Coastguard Worker         /* if op0 is negative goto true branch,
1675*08b48e0bSAndroid Build Coastguard Worker            else go on comparing */
1676*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(t_s0, nonan_bb);
1677*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(icmp_exponent_result, signequal2_bb);
1678*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(PN2, middle2_bb);
1679*08b48e0bSAndroid Build Coastguard Worker         /* NaNs */
1680*08b48e0bSAndroid Build Coastguard Worker         PN->addIncoming(ConstantInt::get(Int1Ty, NaNcmp_result), bb);
1681*08b48e0bSAndroid Build Coastguard Worker         break;
1682*08b48e0bSAndroid Build Coastguard Worker       default:
1683*08b48e0bSAndroid Build Coastguard Worker         continue;
1684*08b48e0bSAndroid Build Coastguard Worker 
1685*08b48e0bSAndroid Build Coastguard Worker     }
1686*08b48e0bSAndroid Build Coastguard Worker 
1687*08b48e0bSAndroid Build Coastguard Worker     BasicBlock::iterator ii(FcmpInst);
1688*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 16
1689*08b48e0bSAndroid Build Coastguard Worker     ReplaceInstWithInst(FcmpInst->getParent(), ii, PN);
1690*08b48e0bSAndroid Build Coastguard Worker #else
1691*08b48e0bSAndroid Build Coastguard Worker     ReplaceInstWithInst(FcmpInst->getParent()->getInstList(), ii, PN);
1692*08b48e0bSAndroid Build Coastguard Worker #endif
1693*08b48e0bSAndroid Build Coastguard Worker     ++count;
1694*08b48e0bSAndroid Build Coastguard Worker 
1695*08b48e0bSAndroid Build Coastguard Worker   }
1696*08b48e0bSAndroid Build Coastguard Worker 
1697*08b48e0bSAndroid Build Coastguard Worker   return count;
1698*08b48e0bSAndroid Build Coastguard Worker 
1699*08b48e0bSAndroid Build Coastguard Worker }
1700*08b48e0bSAndroid Build Coastguard Worker 
1701*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 11
run(Module & M,ModuleAnalysisManager & MAM)1702*08b48e0bSAndroid Build Coastguard Worker PreservedAnalyses SplitComparesTransform::run(Module                &M,
1703*08b48e0bSAndroid Build Coastguard Worker                                               ModuleAnalysisManager &MAM) {
1704*08b48e0bSAndroid Build Coastguard Worker 
1705*08b48e0bSAndroid Build Coastguard Worker #else
1706*08b48e0bSAndroid Build Coastguard Worker bool SplitComparesTransform::runOnModule(Module &M) {
1707*08b48e0bSAndroid Build Coastguard Worker 
1708*08b48e0bSAndroid Build Coastguard Worker #endif
1709*08b48e0bSAndroid Build Coastguard Worker 
1710*08b48e0bSAndroid Build Coastguard Worker   if ((isatty(2) && getenv("AFL_QUIET") == NULL) ||
1711*08b48e0bSAndroid Build Coastguard Worker       getenv("AFL_DEBUG") != NULL) {
1712*08b48e0bSAndroid Build Coastguard Worker 
1713*08b48e0bSAndroid Build Coastguard Worker     errs() << "Split-compare-newpass by [email protected], extended by "
1714*08b48e0bSAndroid Build Coastguard Worker               "[email protected] (splitting icmp to "
1715*08b48e0bSAndroid Build Coastguard Worker            << target_bitwidth << " bit)\n";
1716*08b48e0bSAndroid Build Coastguard Worker 
1717*08b48e0bSAndroid Build Coastguard Worker     if (getenv("AFL_DEBUG") != NULL && !debug) { debug = 1; }
1718*08b48e0bSAndroid Build Coastguard Worker 
1719*08b48e0bSAndroid Build Coastguard Worker   } else {
1720*08b48e0bSAndroid Build Coastguard Worker 
1721*08b48e0bSAndroid Build Coastguard Worker     be_quiet = 1;
1722*08b48e0bSAndroid Build Coastguard Worker 
1723*08b48e0bSAndroid Build Coastguard Worker   }
1724*08b48e0bSAndroid Build Coastguard Worker 
1725*08b48e0bSAndroid Build Coastguard Worker   char *bitw_env = getenv("AFL_LLVM_LAF_SPLIT_COMPARES_BITW");
1726*08b48e0bSAndroid Build Coastguard Worker   if (!bitw_env) bitw_env = getenv("LAF_SPLIT_COMPARES_BITW");
1727*08b48e0bSAndroid Build Coastguard Worker   if (bitw_env) { target_bitwidth = atoi(bitw_env); }
1728*08b48e0bSAndroid Build Coastguard Worker 
1729*08b48e0bSAndroid Build Coastguard Worker   if (getenv("AFL_LLVM_LAF_SPLIT_FLOATS")) { enableFPSplit = true; }
1730*08b48e0bSAndroid Build Coastguard Worker 
1731*08b48e0bSAndroid Build Coastguard Worker   bool split_comp = false;
1732*08b48e0bSAndroid Build Coastguard Worker 
1733*08b48e0bSAndroid Build Coastguard Worker   if (getenv("AFL_LLVM_LAF_SPLIT_COMPARES")) {
1734*08b48e0bSAndroid Build Coastguard Worker 
1735*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR == 17
1736*08b48e0bSAndroid Build Coastguard Worker     if (!be_quiet)
1737*08b48e0bSAndroid Build Coastguard Worker       fprintf(stderr,
1738*08b48e0bSAndroid Build Coastguard Worker               "WARNING: AFL++ splitting integer comparisons is disabled in "
1739*08b48e0bSAndroid Build Coastguard Worker               "LLVM 17 due bugs, switch to 16 or 18!\n");
1740*08b48e0bSAndroid Build Coastguard Worker #else
1741*08b48e0bSAndroid Build Coastguard Worker     split_comp = true;
1742*08b48e0bSAndroid Build Coastguard Worker #endif
1743*08b48e0bSAndroid Build Coastguard Worker 
1744*08b48e0bSAndroid Build Coastguard Worker   }
1745*08b48e0bSAndroid Build Coastguard Worker 
1746*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 11
1747*08b48e0bSAndroid Build Coastguard Worker   auto PA = PreservedAnalyses::all();
1748*08b48e0bSAndroid Build Coastguard Worker #endif
1749*08b48e0bSAndroid Build Coastguard Worker 
1750*08b48e0bSAndroid Build Coastguard Worker   if (enableFPSplit) {
1751*08b48e0bSAndroid Build Coastguard Worker 
1752*08b48e0bSAndroid Build Coastguard Worker     simplifyFPCompares(M);
1753*08b48e0bSAndroid Build Coastguard Worker     count = splitFPCompares(M);
1754*08b48e0bSAndroid Build Coastguard Worker 
1755*08b48e0bSAndroid Build Coastguard Worker     if (!be_quiet && !debug) {
1756*08b48e0bSAndroid Build Coastguard Worker 
1757*08b48e0bSAndroid Build Coastguard Worker       errs() << "Split-floatingpoint-compare-pass: " << count
1758*08b48e0bSAndroid Build Coastguard Worker              << " FP comparisons split\n";
1759*08b48e0bSAndroid Build Coastguard Worker 
1760*08b48e0bSAndroid Build Coastguard Worker     }
1761*08b48e0bSAndroid Build Coastguard Worker 
1762*08b48e0bSAndroid Build Coastguard Worker   }
1763*08b48e0bSAndroid Build Coastguard Worker 
1764*08b48e0bSAndroid Build Coastguard Worker   if (split_comp) {
1765*08b48e0bSAndroid Build Coastguard Worker 
1766*08b48e0bSAndroid Build Coastguard Worker     std::vector<CmpInst *> worklist;
1767*08b48e0bSAndroid Build Coastguard Worker     /* iterate over all functions, bbs and instruction search for all integer
1768*08b48e0bSAndroid Build Coastguard Worker      * compare instructions. Save them into the worklist for later. */
1769*08b48e0bSAndroid Build Coastguard Worker     for (auto &F : M) {
1770*08b48e0bSAndroid Build Coastguard Worker 
1771*08b48e0bSAndroid Build Coastguard Worker       if (!isInInstrumentList(&F, MNAME)) continue;
1772*08b48e0bSAndroid Build Coastguard Worker 
1773*08b48e0bSAndroid Build Coastguard Worker       for (auto &BB : F) {
1774*08b48e0bSAndroid Build Coastguard Worker 
1775*08b48e0bSAndroid Build Coastguard Worker         for (auto &IN : BB) {
1776*08b48e0bSAndroid Build Coastguard Worker 
1777*08b48e0bSAndroid Build Coastguard Worker           if (auto CI = dyn_cast<CmpInst>(&IN)) {
1778*08b48e0bSAndroid Build Coastguard Worker 
1779*08b48e0bSAndroid Build Coastguard Worker             auto op0 = CI->getOperand(0);
1780*08b48e0bSAndroid Build Coastguard Worker             auto op1 = CI->getOperand(1);
1781*08b48e0bSAndroid Build Coastguard Worker             if (!op0 || !op1) {
1782*08b48e0bSAndroid Build Coastguard Worker 
1783*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 11
1784*08b48e0bSAndroid Build Coastguard Worker               return PA;
1785*08b48e0bSAndroid Build Coastguard Worker #else
1786*08b48e0bSAndroid Build Coastguard Worker               return false;
1787*08b48e0bSAndroid Build Coastguard Worker #endif
1788*08b48e0bSAndroid Build Coastguard Worker 
1789*08b48e0bSAndroid Build Coastguard Worker             }
1790*08b48e0bSAndroid Build Coastguard Worker 
1791*08b48e0bSAndroid Build Coastguard Worker             auto iTy1 = dyn_cast<IntegerType>(op0->getType());
1792*08b48e0bSAndroid Build Coastguard Worker             if (iTy1 && isa<IntegerType>(op1->getType())) {
1793*08b48e0bSAndroid Build Coastguard Worker 
1794*08b48e0bSAndroid Build Coastguard Worker               unsigned bitw = iTy1->getBitWidth();
1795*08b48e0bSAndroid Build Coastguard Worker               if (isSupportedBitWidth(bitw)) { worklist.push_back(CI); }
1796*08b48e0bSAndroid Build Coastguard Worker 
1797*08b48e0bSAndroid Build Coastguard Worker             }
1798*08b48e0bSAndroid Build Coastguard Worker 
1799*08b48e0bSAndroid Build Coastguard Worker           }
1800*08b48e0bSAndroid Build Coastguard Worker 
1801*08b48e0bSAndroid Build Coastguard Worker         }
1802*08b48e0bSAndroid Build Coastguard Worker 
1803*08b48e0bSAndroid Build Coastguard Worker       }
1804*08b48e0bSAndroid Build Coastguard Worker 
1805*08b48e0bSAndroid Build Coastguard Worker     }
1806*08b48e0bSAndroid Build Coastguard Worker 
1807*08b48e0bSAndroid Build Coastguard Worker     // now that we have a list of all integer comparisons we can start replacing
1808*08b48e0bSAndroid Build Coastguard Worker     // them with the splitted alternatives.
1809*08b48e0bSAndroid Build Coastguard Worker     for (auto CI : worklist) {
1810*08b48e0bSAndroid Build Coastguard Worker 
1811*08b48e0bSAndroid Build Coastguard Worker       simplifyAndSplit(CI, M);
1812*08b48e0bSAndroid Build Coastguard Worker 
1813*08b48e0bSAndroid Build Coastguard Worker     }
1814*08b48e0bSAndroid Build Coastguard Worker 
1815*08b48e0bSAndroid Build Coastguard Worker   }
1816*08b48e0bSAndroid Build Coastguard Worker 
1817*08b48e0bSAndroid Build Coastguard Worker   bool brokenDebug = false;
1818*08b48e0bSAndroid Build Coastguard Worker   if (verifyModule(M, &errs()
1819*08b48e0bSAndroid Build Coastguard Worker #if LLVM_VERSION_MAJOR >= 4 || \
1820*08b48e0bSAndroid Build Coastguard Worker     (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 9)
1821*08b48e0bSAndroid Build Coastguard Worker                           ,
1822*08b48e0bSAndroid Build Coastguard Worker                    &brokenDebug  // 9th May 2016
1823*08b48e0bSAndroid Build Coastguard Worker #endif
1824*08b48e0bSAndroid Build Coastguard Worker                    )) {
1825*08b48e0bSAndroid Build Coastguard Worker 
1826*08b48e0bSAndroid Build Coastguard Worker     reportError(
1827*08b48e0bSAndroid Build Coastguard Worker         "Module Verifier failed! Consider reporting a bug with the AFL++ "
1828*08b48e0bSAndroid Build Coastguard Worker         "project.",
1829*08b48e0bSAndroid Build Coastguard Worker         nullptr, M);
1830*08b48e0bSAndroid Build Coastguard Worker 
1831*08b48e0bSAndroid Build Coastguard Worker   }
1832*08b48e0bSAndroid Build Coastguard Worker 
1833*08b48e0bSAndroid Build Coastguard Worker   if (brokenDebug) {
1834*08b48e0bSAndroid Build Coastguard Worker 
1835*08b48e0bSAndroid Build Coastguard Worker     reportError("Module Verifier reported broken Debug Infos - Stripping!",
1836*08b48e0bSAndroid Build Coastguard Worker                 nullptr, M);
1837*08b48e0bSAndroid Build Coastguard Worker     StripDebugInfo(M);
1838*08b48e0bSAndroid Build Coastguard Worker 
1839*08b48e0bSAndroid Build Coastguard Worker   }
1840*08b48e0bSAndroid Build Coastguard Worker 
1841*08b48e0bSAndroid Build Coastguard Worker   if ((isatty(2) && getenv("AFL_QUIET") == NULL) ||
1842*08b48e0bSAndroid Build Coastguard Worker       getenv("AFL_DEBUG") != NULL) {
1843*08b48e0bSAndroid Build Coastguard Worker 
1844*08b48e0bSAndroid Build Coastguard Worker     errs() << count << " comparisons found\n";
1845*08b48e0bSAndroid Build Coastguard Worker 
1846*08b48e0bSAndroid Build Coastguard Worker   }
1847*08b48e0bSAndroid Build Coastguard Worker 
1848*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR >= 11
1849*08b48e0bSAndroid Build Coastguard Worker   /*  if (modified) {
1850*08b48e0bSAndroid Build Coastguard Worker 
1851*08b48e0bSAndroid Build Coastguard Worker       PA.abandon<XX_Manager>();
1852*08b48e0bSAndroid Build Coastguard Worker 
1853*08b48e0bSAndroid Build Coastguard Worker     }*/
1854*08b48e0bSAndroid Build Coastguard Worker 
1855*08b48e0bSAndroid Build Coastguard Worker   return PA;
1856*08b48e0bSAndroid Build Coastguard Worker #else
1857*08b48e0bSAndroid Build Coastguard Worker   return true;
1858*08b48e0bSAndroid Build Coastguard Worker #endif
1859*08b48e0bSAndroid Build Coastguard Worker 
1860*08b48e0bSAndroid Build Coastguard Worker }
1861*08b48e0bSAndroid Build Coastguard Worker 
1862*08b48e0bSAndroid Build Coastguard Worker #if LLVM_MAJOR < 11                                 /* use old pass manager */
1863*08b48e0bSAndroid Build Coastguard Worker 
1864*08b48e0bSAndroid Build Coastguard Worker static void registerSplitComparesPass(const PassManagerBuilder &,
1865*08b48e0bSAndroid Build Coastguard Worker                                       legacy::PassManagerBase &PM) {
1866*08b48e0bSAndroid Build Coastguard Worker 
1867*08b48e0bSAndroid Build Coastguard Worker   PM.add(new SplitComparesTransform());
1868*08b48e0bSAndroid Build Coastguard Worker 
1869*08b48e0bSAndroid Build Coastguard Worker }
1870*08b48e0bSAndroid Build Coastguard Worker 
1871*08b48e0bSAndroid Build Coastguard Worker static RegisterStandardPasses RegisterSplitComparesPass(
1872*08b48e0bSAndroid Build Coastguard Worker     PassManagerBuilder::EP_OptimizerLast, registerSplitComparesPass);
1873*08b48e0bSAndroid Build Coastguard Worker 
1874*08b48e0bSAndroid Build Coastguard Worker static RegisterStandardPasses RegisterSplitComparesTransPass0(
1875*08b48e0bSAndroid Build Coastguard Worker     PassManagerBuilder::EP_EnabledOnOptLevel0, registerSplitComparesPass);
1876*08b48e0bSAndroid Build Coastguard Worker 
1877*08b48e0bSAndroid Build Coastguard Worker   #if LLVM_VERSION_MAJOR >= 11
1878*08b48e0bSAndroid Build Coastguard Worker static RegisterStandardPasses RegisterSplitComparesTransPassLTO(
1879*08b48e0bSAndroid Build Coastguard Worker     PassManagerBuilder::EP_FullLinkTimeOptimizationLast,
1880*08b48e0bSAndroid Build Coastguard Worker     registerSplitComparesPass);
1881*08b48e0bSAndroid Build Coastguard Worker   #endif
1882*08b48e0bSAndroid Build Coastguard Worker 
1883*08b48e0bSAndroid Build Coastguard Worker static RegisterPass<SplitComparesTransform> X("splitcompares",
1884*08b48e0bSAndroid Build Coastguard Worker                                               "AFL++ split compares",
1885*08b48e0bSAndroid Build Coastguard Worker                                               true /* Only looks at CFG */,
1886*08b48e0bSAndroid Build Coastguard Worker                                               true /* Analysis Pass */);
1887*08b48e0bSAndroid Build Coastguard Worker #endif
1888*08b48e0bSAndroid Build Coastguard Worker 
1889