Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Scalar/Float2Int.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Float2Int.cpp - Demote floating point ops to work on integers ------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the Float2Int pass, which aims to demote floating
10
// point operations to work on integers, where that is losslessly possible.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#define DEBUG_TYPE "float2int"
15
16
#include "llvm/Transforms/Scalar/Float2Int.h"
17
#include "llvm/ADT/APInt.h"
18
#include "llvm/ADT/APSInt.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/Analysis/AliasAnalysis.h"
21
#include "llvm/Analysis/GlobalsModRef.h"
22
#include "llvm/IR/Constants.h"
23
#include "llvm/IR/IRBuilder.h"
24
#include "llvm/IR/InstIterator.h"
25
#include "llvm/IR/Instructions.h"
26
#include "llvm/IR/Module.h"
27
#include "llvm/Pass.h"
28
#include "llvm/Support/Debug.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include "llvm/Transforms/Scalar.h"
31
#include <deque>
32
#include <functional> // For std::function
33
using namespace llvm;
34
35
// The algorithm is simple. Start at instructions that convert from the
36
// float to the int domain: fptoui, fptosi and fcmp. Walk up the def-use
37
// graph, using an equivalence datastructure to unify graphs that interfere.
38
//
39
// Mappable instructions are those with an integer corrollary that, given
40
// integer domain inputs, produce an integer output; fadd, for example.
41
//
42
// If a non-mappable instruction is seen, this entire def-use graph is marked
43
// as non-transformable. If we see an instruction that converts from the
44
// integer domain to FP domain (uitofp,sitofp), we terminate our walk.
45
46
/// The largest integer type worth dealing with.
47
static cl::opt<unsigned>
48
MaxIntegerBW("float2int-max-integer-bw", cl::init(64), cl::Hidden,
49
             cl::desc("Max integer bitwidth to consider in float2int"
50
                      "(default=64)"));
51
52
namespace {
53
  struct Float2IntLegacyPass : public FunctionPass {
54
    static char ID; // Pass identification, replacement for typeid
55
12.9k
    Float2IntLegacyPass() : FunctionPass(ID) {
56
12.9k
      initializeFloat2IntLegacyPassPass(*PassRegistry::getPassRegistry());
57
12.9k
    }
58
59
278k
    bool runOnFunction(Function &F) override {
60
278k
      if (skipFunction(F))
61
45
        return false;
62
278k
63
278k
      return Impl.runImpl(F);
64
278k
    }
65
66
12.9k
    void getAnalysisUsage(AnalysisUsage &AU) const override {
67
12.9k
      AU.setPreservesCFG();
68
12.9k
      AU.addPreserved<GlobalsAAWrapperPass>();
69
12.9k
    }
70
71
  private:
72
    Float2IntPass Impl;
73
  };
74
}
75
76
char Float2IntLegacyPass::ID = 0;
77
INITIALIZE_PASS(Float2IntLegacyPass, "float2int", "Float to int", false, false)
78
79
// Given a FCmp predicate, return a matching ICmp predicate if one
80
// exists, otherwise return BAD_ICMP_PREDICATE.
81
19.8k
static CmpInst::Predicate mapFCmpPred(CmpInst::Predicate P) {
82
19.8k
  switch (P) {
83
19.8k
  case CmpInst::FCMP_OEQ:
84
3.27k
  case CmpInst::FCMP_UEQ:
85
3.27k
    return CmpInst::ICMP_EQ;
86
4.00k
  case CmpInst::FCMP_OGT:
87
4.00k
  case CmpInst::FCMP_UGT:
88
4.00k
    return CmpInst::ICMP_SGT;
89
4.00k
  case CmpInst::FCMP_OGE:
90
253
  case CmpInst::FCMP_UGE:
91
253
    return CmpInst::ICMP_SGE;
92
10.2k
  case CmpInst::FCMP_OLT:
93
10.2k
  case CmpInst::FCMP_ULT:
94
10.2k
    return CmpInst::ICMP_SLT;
95
10.2k
  case CmpInst::FCMP_OLE:
96
161
  case CmpInst::FCMP_ULE:
97
161
    return CmpInst::ICMP_SLE;
98
620
  case CmpInst::FCMP_ONE:
99
620
  case CmpInst::FCMP_UNE:
100
620
    return CmpInst::ICMP_NE;
101
1.23k
  default:
102
1.23k
    return CmpInst::BAD_ICMP_PREDICATE;
103
19.8k
  }
104
19.8k
}
105
106
// Given a floating point binary operator, return the matching
107
// integer version.
108
26
static Instruction::BinaryOps mapBinOpcode(unsigned Opcode) {
109
26
  switch (Opcode) {
110
26
  
default: 0
llvm_unreachable0
("Unhandled opcode!");
111
26
  
case Instruction::FAdd: return Instruction::Add10
;
112
26
  
case Instruction::FSub: return Instruction::Sub5
;
113
26
  
case Instruction::FMul: return Instruction::Mul11
;
114
26
  }
115
26
}
116
117
// Find the roots - instructions that convert from the FP domain to
118
// integer domain.
119
279k
void Float2IntPass::findRoots(Function &F, SmallPtrSet<Instruction*,8> &Roots) {
120
12.2M
  for (auto &I : instructions(F)) {
121
12.2M
    if (isa<VectorType>(I.getType()))
122
44.2k
      continue;
123
12.1M
    switch (I.getOpcode()) {
124
12.1M
    
default: break12.1M
;
125
12.1M
    case Instruction::FPToUI:
126
6.19k
    case Instruction::FPToSI:
127
6.19k
      Roots.insert(&I);
128
6.19k
      break;
129
19.8k
    case Instruction::FCmp:
130
19.8k
      if (mapFCmpPred(cast<CmpInst>(&I)->getPredicate()) !=
131
19.8k
          CmpInst::BAD_ICMP_PREDICATE)
132
18.5k
        Roots.insert(&I);
133
19.8k
      break;
134
12.1M
    }
135
12.1M
  }
136
279k
}
137
138
// Helper - mark I as having been traversed, having range R.
139
127k
void Float2IntPass::seen(Instruction *I, ConstantRange R) {
140
127k
  LLVM_DEBUG(dbgs() << "F2I: " << *I << ":" << R << "\n");
141
127k
  auto IT = SeenInsts.find(I);
142
127k
  if (IT != SeenInsts.end())
143
52.0k
    IT->second = std::move(R);
144
74.9k
  else
145
74.9k
    SeenInsts.insert(std::make_pair(I, std::move(R)));
146
127k
}
147
148
// Helper - get a range representing a poison value.
149
135k
ConstantRange Float2IntPass::badRange() {
150
135k
  return ConstantRange::getFull(MaxIntegerBW + 1);
151
135k
}
152
120k
ConstantRange Float2IntPass::unknownRange() {
153
120k
  return ConstantRange::getEmpty(MaxIntegerBW + 1);
154
120k
}
155
4.17k
ConstantRange Float2IntPass::validateRange(ConstantRange R) {
156
4.17k
  if (R.getBitWidth() > MaxIntegerBW + 1)
157
2
    return badRange();
158
4.16k
  return R;
159
4.16k
}
160
161
// The most obvious way to structure the search is a depth-first, eager
162
// search from each root. However, that require direct recursion and so
163
// can only handle small instruction sequences. Instead, we split the search
164
// up into two phases:
165
//   - walkBackwards:  A breadth-first walk of the use-def graph starting from
166
//                     the roots. Populate "SeenInsts" with interesting
167
//                     instructions and poison values if they're obvious and
168
//                     cheap to compute. Calculate the equivalance set structure
169
//                     while we're here too.
170
//   - walkForwards:  Iterate over SeenInsts in reverse order, so we visit
171
//                     defs before their uses. Calculate the real range info.
172
173
// Breadth-first walk of the use-def graph; determine the set of nodes
174
// we care about and eagerly determine if some of them are poisonous.
175
279k
void Float2IntPass::walkBackwards(const SmallPtrSetImpl<Instruction*> &Roots) {
176
279k
  std::deque<Instruction*> Worklist(Roots.begin(), Roots.end());
177
365k
  while (!Worklist.empty()) {
178
86.3k
    Instruction *I = Worklist.back();
179
86.3k
    Worklist.pop_back();
180
86.3k
181
86.3k
    if (SeenInsts.find(I) != SeenInsts.end())
182
11.3k
      // Seen already.
183
11.3k
      continue;
184
74.9k
185
74.9k
    switch (I->getOpcode()) {
186
74.9k
      // FIXME: Handle select and phi nodes.
187
74.9k
    default:
188
25.6k
      // Path terminated uncleanly.
189
25.6k
      seen(I, badRange());
190
25.6k
      break;
191
74.9k
192
74.9k
    case Instruction::UIToFP:
193
4.17k
    case Instruction::SIToFP: {
194
4.17k
      // Path terminated cleanly - use the type of the integer input to seed
195
4.17k
      // the analysis.
196
4.17k
      unsigned BW = I->getOperand(0)->getType()->getPrimitiveSizeInBits();
197
4.17k
      auto Input = ConstantRange::getFull(BW);
198
4.17k
      auto CastOp = (Instruction::CastOps)I->getOpcode();
199
4.17k
      seen(I, validateRange(Input.castOp(CastOp, MaxIntegerBW+1)));
200
4.17k
      continue;
201
4.17k
    }
202
4.17k
203
45.1k
    case Instruction::FNeg:
204
45.1k
    case Instruction::FAdd:
205
45.1k
    case Instruction::FSub:
206
45.1k
    case Instruction::FMul:
207
45.1k
    case Instruction::FPToUI:
208
45.1k
    case Instruction::FPToSI:
209
45.1k
    case Instruction::FCmp:
210
45.1k
      seen(I, unknownRange());
211
45.1k
      break;
212
70.8k
    }
213
70.8k
214
125k
    
for (Value *O : I->operands())70.8k
{
215
125k
      if (Instruction *OI = dyn_cast<Instruction>(O)) {
216
93.0k
        // Unify def-use chains if they interfere.
217
93.0k
        ECs.unionSets(I, OI);
218
93.0k
        if (SeenInsts.find(I)->second != badRange())
219
61.6k
          Worklist.push_back(OI);
220
93.0k
      } else 
if (32.7k
!isa<ConstantFP>(O)32.7k
) {
221
7.58k
        // Not an instruction or ConstantFP? we can't do anything.
222
7.58k
        seen(I, badRange());
223
7.58k
      }
224
125k
    }
225
70.8k
  }
226
279k
}
227
228
// Walk forwards down the list of seen instructions, so we visit defs before
229
// uses.
230
279k
void Float2IntPass::walkForwards() {
231
279k
  for (auto &It : reverse(SeenInsts)) {
232
74.9k
    if (It.second != unknownRange())
233
30.5k
      continue;
234
44.4k
235
44.4k
    Instruction *I = It.first;
236
44.4k
    std::function<ConstantRange(ArrayRef<ConstantRange>)> Op;
237
44.4k
    switch (I->getOpcode()) {
238
44.4k
      // FIXME: Handle select and phi nodes.
239
44.4k
    default:
240
0
    case Instruction::UIToFP:
241
0
    case Instruction::SIToFP:
242
0
      llvm_unreachable("Should have been handled in walkForwards!");
243
0
244
4
    case Instruction::FNeg:
245
4
      Op = [](ArrayRef<ConstantRange> Ops) {
246
4
        assert(Ops.size() == 1 && "FNeg is a unary operator!");
247
4
        unsigned Size = Ops[0].getBitWidth();
248
4
        auto Zero = ConstantRange(APInt::getNullValue(Size));
249
4
        return Zero.sub(Ops[0]);
250
4
      };
251
4
      break;
252
0
253
20.1k
    case Instruction::FAdd:
254
20.1k
    case Instruction::FSub:
255
20.1k
    case Instruction::FMul:
256
20.1k
      Op = [I](ArrayRef<ConstantRange> Ops) {
257
15.5k
        assert(Ops.size() == 2 && "its a binary operator!");
258
15.5k
        auto BinOp = (Instruction::BinaryOps) I->getOpcode();
259
15.5k
        return Ops[0].binaryOp(BinOp, Ops[1]);
260
15.5k
      };
261
20.1k
      break;
262
20.1k
263
20.1k
    //
264
20.1k
    // Root-only instructions - we'll only see these if they're the
265
20.1k
    //                          first node in a walk.
266
20.1k
    //
267
20.1k
    case Instruction::FPToUI:
268
6.18k
    case Instruction::FPToSI:
269
6.18k
      Op = [I](ArrayRef<ConstantRange> Ops) {
270
6.18k
        assert(Ops.size() == 1 && "FPTo[US]I is a unary operator!");
271
6.18k
        // Note: We're ignoring the casts output size here as that's what the
272
6.18k
        // caller expects.
273
6.18k
        auto CastOp = (Instruction::CastOps)I->getOpcode();
274
6.18k
        return Ops[0].castOp(CastOp, MaxIntegerBW+1);
275
6.18k
      };
276
6.18k
      break;
277
6.18k
278
18.1k
    case Instruction::FCmp:
279
18.1k
      Op = [](ArrayRef<ConstantRange> Ops) {
280
13.1k
        assert(Ops.size() == 2 && "FCmp is a binary operator!");
281
13.1k
        return Ops[0].unionWith(Ops[1]);
282
13.1k
      };
283
18.1k
      break;
284
44.4k
    }
285
44.4k
286
44.4k
    bool Abort = false;
287
44.4k
    SmallVector<ConstantRange,4> OpRanges;
288
82.0k
    for (Value *O : I->operands()) {
289
82.0k
      if (Instruction *OI = dyn_cast<Instruction>(O)) {
290
60.5k
        assert(SeenInsts.find(OI) != SeenInsts.end() &&
291
60.5k
               "def not seen before use!");
292
60.5k
        OpRanges.push_back(SeenInsts.find(OI)->second);
293
60.5k
      } else 
if (ConstantFP *21.4k
CF21.4k
= dyn_cast<ConstantFP>(O)) {
294
21.4k
        // Work out if the floating point number can be losslessly represented
295
21.4k
        // as an integer.
296
21.4k
        // APFloat::convertToInteger(&Exact) purports to do what we want, but
297
21.4k
        // the exactness can be too precise. For example, negative zero can
298
21.4k
        // never be exactly converted to an integer.
299
21.4k
        //
300
21.4k
        // Instead, we ask APFloat to round itself to an integral value - this
301
21.4k
        // preserves sign-of-zero - then compare the result with the original.
302
21.4k
        //
303
21.4k
        const APFloat &F = CF->getValueAPF();
304
21.4k
305
21.4k
        // First, weed out obviously incorrect values. Non-finite numbers
306
21.4k
        // can't be represented and neither can negative zero, unless
307
21.4k
        // we're in fast math mode.
308
21.4k
        if (!F.isFinite() ||
309
21.4k
            
(20.0k
F.isZero()20.0k
&&
F.isNegative()2.30k
&&
isa<FPMathOperator>(I)125
&&
310
20.0k
             
!I->hasNoSignedZeros()125
)) {
311
1.50k
          seen(I, badRange());
312
1.50k
          Abort = true;
313
1.50k
          break;
314
1.50k
        }
315
19.9k
316
19.9k
        APFloat NewF = F;
317
19.9k
        auto Res = NewF.roundToIntegral(APFloat::rmNearestTiesToEven);
318
19.9k
        if (Res != APFloat::opOK || NewF.compare(F) != APFloat::cmpEqual) {
319
8.00k
          seen(I, badRange());
320
8.00k
          Abort = true;
321
8.00k
          break;
322
8.00k
        }
323
11.9k
        // OK, it's representable. Now get it.
324
11.9k
        APSInt Int(MaxIntegerBW+1, false);
325
11.9k
        bool Exact;
326
11.9k
        CF->getValueAPF().convertToInteger(Int,
327
11.9k
                                           APFloat::rmNearestTiesToEven,
328
11.9k
                                           &Exact);
329
11.9k
        OpRanges.push_back(ConstantRange(Int));
330
11.9k
      } else {
331
0
        llvm_unreachable("Should have already marked this as badRange!");
332
0
      }
333
82.0k
    }
334
44.4k
335
44.4k
    // Reduce the operands' ranges to a single range and return.
336
44.4k
    if (!Abort)
337
34.9k
      seen(I, Op(OpRanges));
338
44.4k
  }
339
279k
}
340
341
// If there is a valid transform to be done, do it.
342
279k
bool Float2IntPass::validateAndTransform() {
343
279k
  bool MadeChange = false;
344
279k
345
279k
  // Iterate over every disjoint partition of the def-use graph.
346
372k
  for (auto It = ECs.begin(), E = ECs.end(); It != E; 
++It92.7k
) {
347
92.7k
    ConstantRange R(MaxIntegerBW + 1, false);
348
92.7k
    bool Fail = false;
349
92.7k
    Type *ConvertedToTy = nullptr;
350
92.7k
351
92.7k
    // For every member of the partition, union all the ranges together.
352
92.7k
    for (auto MI = ECs.member_begin(It), ME = ECs.member_end();
353
146k
         MI != ME; 
++MI53.7k
) {
354
57.8k
      Instruction *I = *MI;
355
57.8k
      auto SeenI = SeenInsts.find(I);
356
57.8k
      if (SeenI == SeenInsts.end())
357
7.07k
        continue;
358
50.8k
359
50.8k
      R = R.unionWith(SeenI->second);
360
50.8k
      // We need to ensure I has no users that have not been seen.
361
50.8k
      // If it does, transformation would be illegal.
362
50.8k
      //
363
50.8k
      // Don't count the roots, as they terminate the graphs.
364
50.8k
      if (Roots.count(I) == 0) {
365
28.6k
        // Set the type of the conversion while we're here.
366
28.6k
        if (!ConvertedToTy)
367
10.6k
          ConvertedToTy = I->getType();
368
35.0k
        for (User *U : I->users()) {
369
35.0k
          Instruction *UI = dyn_cast<Instruction>(U);
370
35.0k
          if (!UI || SeenInsts.find(UI) == SeenInsts.end()) {
371
4.09k
            LLVM_DEBUG(dbgs() << "F2I: Failing because of " << *U << "\n");
372
4.09k
            Fail = true;
373
4.09k
            break;
374
4.09k
          }
375
35.0k
        }
376
28.6k
      }
377
50.8k
      if (Fail)
378
4.09k
        break;
379
50.8k
    }
380
92.7k
381
92.7k
    // If the set was empty, or we failed, or the range is poisonous,
382
92.7k
    // bail out.
383
92.7k
    if (ECs.member_begin(It) == ECs.member_end() || 
Fail10.6k
||
384
92.7k
        
R.isFullSet()6.51k
||
R.isSignWrappedSet()37
)
385
92.7k
      continue;
386
37
    assert(ConvertedToTy && "Must have set the convertedtoty by this point!");
387
37
388
37
    // The number of bits required is the maximum of the upper and
389
37
    // lower limits, plus one so it can be signed.
390
37
    unsigned MinBW = std::max(R.getLower().getMinSignedBits(),
391
37
                              R.getUpper().getMinSignedBits()) + 1;
392
37
    LLVM_DEBUG(dbgs() << "F2I: MinBitwidth=" << MinBW << ", R: " << R << "\n");
393
37
394
37
    // If we've run off the realms of the exactly representable integers,
395
37
    // the floating point result will differ from an integer approximation.
396
37
397
37
    // Do we need more bits than are in the mantissa of the type we converted
398
37
    // to? semanticsPrecision returns the number of mantissa bits plus one
399
37
    // for the sign bit.
400
37
    unsigned MaxRepresentableBits
401
37
      = APFloat::semanticsPrecision(ConvertedToTy->getFltSemantics()) - 1;
402
37
    if (MinBW > MaxRepresentableBits) {
403
10
      LLVM_DEBUG(dbgs() << "F2I: Value not guaranteed to be representable!\n");
404
10
      continue;
405
10
    }
406
27
    if (MinBW > 64) {
407
1
      LLVM_DEBUG(
408
1
          dbgs() << "F2I: Value requires more than 64 bits to represent!\n");
409
1
      continue;
410
1
    }
411
26
412
26
    // OK, R is known to be representable. Now pick a type for it.
413
26
    // FIXME: Pick the smallest legal type that will fit.
414
26
    Type *Ty = (MinBW > 32) ? 
Type::getInt64Ty(*Ctx)6
:
Type::getInt32Ty(*Ctx)20
;
415
26
416
26
    for (auto MI = ECs.member_begin(It), ME = ECs.member_end();
417
117
         MI != ME; 
++MI91
)
418
91
      convert(*MI, Ty);
419
26
    MadeChange = true;
420
26
  }
421
279k
422
279k
  return MadeChange;
423
279k
}
424
425
156
Value *Float2IntPass::convert(Instruction *I, Type *ToTy) {
426
156
  if (ConvertedInsts.find(I) != ConvertedInsts.end())
427
65
    // Already converted this instruction.
428
65
    return ConvertedInsts[I];
429
91
430
91
  SmallVector<Value*,4> NewOperands;
431
119
  for (Value *V : I->operands()) {
432
119
    // Don't recurse if we're an instruction that terminates the path.
433
119
    if (I->getOpcode() == Instruction::UIToFP ||
434
119
        
I->getOpcode() == Instruction::SIToFP91
) {
435
35
      NewOperands.push_back(V);
436
84
    } else if (Instruction *VI = dyn_cast<Instruction>(V)) {
437
65
      NewOperands.push_back(convert(VI, ToTy));
438
65
    } else 
if (ConstantFP *19
CF19
= dyn_cast<ConstantFP>(V)) {
439
19
      APSInt Val(ToTy->getPrimitiveSizeInBits(), /*isUnsigned=*/false);
440
19
      bool Exact;
441
19
      CF->getValueAPF().convertToInteger(Val,
442
19
                                         APFloat::rmNearestTiesToEven,
443
19
                                         &Exact);
444
19
      NewOperands.push_back(ConstantInt::get(ToTy, Val));
445
19
    } else {
446
0
      llvm_unreachable("Unhandled operand type?");
447
0
    }
448
119
  }
449
91
450
91
  // Now create a new instruction.
451
91
  IRBuilder<> IRB(I);
452
91
  Value *NewV = nullptr;
453
91
  switch (I->getOpcode()) {
454
91
  
default: 0
llvm_unreachable0
("Unhandled instruction!");
455
91
456
91
  case Instruction::FPToUI:
457
20
    NewV = IRB.CreateZExtOrTrunc(NewOperands[0], I->getType());
458
20
    break;
459
91
460
91
  case Instruction::FPToSI:
461
4
    NewV = IRB.CreateSExtOrTrunc(NewOperands[0], I->getType());
462
4
    break;
463
91
464
91
  case Instruction::FCmp: {
465
2
    CmpInst::Predicate P = mapFCmpPred(cast<CmpInst>(I)->getPredicate());
466
2
    assert(P != CmpInst::BAD_ICMP_PREDICATE && "Unhandled predicate!");
467
2
    NewV = IRB.CreateICmp(P, NewOperands[0], NewOperands[1], I->getName());
468
2
    break;
469
91
  }
470
91
471
91
  case Instruction::UIToFP:
472
28
    NewV = IRB.CreateZExtOrTrunc(NewOperands[0], ToTy);
473
28
    break;
474
91
475
91
  case Instruction::SIToFP:
476
7
    NewV = IRB.CreateSExtOrTrunc(NewOperands[0], ToTy);
477
7
    break;
478
91
479
91
  case Instruction::FNeg:
480
4
    NewV = IRB.CreateNeg(NewOperands[0], I->getName());
481
4
    break;
482
91
483
91
  case Instruction::FAdd:
484
26
  case Instruction::FSub:
485
26
  case Instruction::FMul:
486
26
    NewV = IRB.CreateBinOp(mapBinOpcode(I->getOpcode()),
487
26
                           NewOperands[0], NewOperands[1],
488
26
                           I->getName());
489
26
    break;
490
91
  }
491
91
492
91
  // If we're a root instruction, RAUW.
493
91
  if (Roots.count(I))
494
26
    I->replaceAllUsesWith(NewV);
495
91
496
91
  ConvertedInsts[I] = NewV;
497
91
  return NewV;
498
91
}
499
500
// Perform dead code elimination on the instructions we just modified.
501
25
void Float2IntPass::cleanup() {
502
25
  for (auto &I : reverse(ConvertedInsts))
503
91
    I.first->eraseFromParent();
504
25
}
505
506
279k
bool Float2IntPass::runImpl(Function &F) {
507
279k
  LLVM_DEBUG(dbgs() << "F2I: Looking at function " << F.getName() << "\n");
508
279k
  // Clear out all state.
509
279k
  ECs = EquivalenceClasses<Instruction*>();
510
279k
  SeenInsts.clear();
511
279k
  ConvertedInsts.clear();
512
279k
  Roots.clear();
513
279k
514
279k
  Ctx = &F.getParent()->getContext();
515
279k
516
279k
  findRoots(F, Roots);
517
279k
518
279k
  walkBackwards(Roots);
519
279k
  walkForwards();
520
279k
521
279k
  bool Modified = validateAndTransform();
522
279k
  if (Modified)
523
25
    cleanup();
524
279k
  return Modified;
525
279k
}
526
527
namespace llvm {
528
12.9k
FunctionPass *createFloat2IntPass() { return new Float2IntLegacyPass(); }
529
530
880
PreservedAnalyses Float2IntPass::run(Function &F, FunctionAnalysisManager &) {
531
880
  if (!runImpl(F))
532
869
    return PreservedAnalyses::all();
533
11
534
11
  PreservedAnalyses PA;
535
11
  PA.preserveSet<CFGAnalyses>();
536
11
  PA.preserve<GlobalsAA>();
537
11
  return PA;
538
11
}
539
} // End namespace llvm