James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 1 | //===- Float2Int.cpp - Demote floating point ops to work on integers ------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 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" |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 15 | |
| 16 | #include "llvm/Transforms/Scalar/Float2Int.h" |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/APInt.h" |
| 18 | #include "llvm/ADT/APSInt.h" |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | 08eebe2 | 2015-07-23 09:34:01 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/GlobalsModRef.h" |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 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 |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 43 | // as non-transformable. If we see an instruction that converts from the |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 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 { |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 53 | struct Float2IntLegacyPass : public FunctionPass { |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 54 | static char ID; // Pass identification, replacement for typeid |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 55 | Float2IntLegacyPass() : FunctionPass(ID) { |
| 56 | initializeFloat2IntLegacyPassPass(*PassRegistry::getPassRegistry()); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 59 | bool runOnFunction(Function &F) override { |
| 60 | if (skipFunction(F)) |
| 61 | return false; |
| 62 | |
Sanjay Patel | 13e71ce | 2019-09-19 16:31:17 +0000 | [diff] [blame] | 63 | const DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 64 | return Impl.runImpl(F, DT); |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 65 | } |
| 66 | |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 67 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 68 | AU.setPreservesCFG(); |
Sanjay Patel | 13e71ce | 2019-09-19 16:31:17 +0000 | [diff] [blame] | 69 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 70 | AU.addPreserved<GlobalsAAWrapperPass>(); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 73 | private: |
| 74 | Float2IntPass Impl; |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 75 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 76 | } |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 77 | |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 78 | char Float2IntLegacyPass::ID = 0; |
| 79 | INITIALIZE_PASS(Float2IntLegacyPass, "float2int", "Float to int", false, false) |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 80 | |
| 81 | // Given a FCmp predicate, return a matching ICmp predicate if one |
| 82 | // exists, otherwise return BAD_ICMP_PREDICATE. |
| 83 | static CmpInst::Predicate mapFCmpPred(CmpInst::Predicate P) { |
| 84 | switch (P) { |
| 85 | case CmpInst::FCMP_OEQ: |
| 86 | case CmpInst::FCMP_UEQ: |
| 87 | return CmpInst::ICMP_EQ; |
| 88 | case CmpInst::FCMP_OGT: |
| 89 | case CmpInst::FCMP_UGT: |
| 90 | return CmpInst::ICMP_SGT; |
| 91 | case CmpInst::FCMP_OGE: |
| 92 | case CmpInst::FCMP_UGE: |
| 93 | return CmpInst::ICMP_SGE; |
| 94 | case CmpInst::FCMP_OLT: |
| 95 | case CmpInst::FCMP_ULT: |
| 96 | return CmpInst::ICMP_SLT; |
| 97 | case CmpInst::FCMP_OLE: |
| 98 | case CmpInst::FCMP_ULE: |
| 99 | return CmpInst::ICMP_SLE; |
| 100 | case CmpInst::FCMP_ONE: |
| 101 | case CmpInst::FCMP_UNE: |
| 102 | return CmpInst::ICMP_NE; |
| 103 | default: |
| 104 | return CmpInst::BAD_ICMP_PREDICATE; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Given a floating point binary operator, return the matching |
| 109 | // integer version. |
| 110 | static Instruction::BinaryOps mapBinOpcode(unsigned Opcode) { |
| 111 | switch (Opcode) { |
| 112 | default: llvm_unreachable("Unhandled opcode!"); |
| 113 | case Instruction::FAdd: return Instruction::Add; |
| 114 | case Instruction::FSub: return Instruction::Sub; |
| 115 | case Instruction::FMul: return Instruction::Mul; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Find the roots - instructions that convert from the FP domain to |
| 120 | // integer domain. |
Sanjay Patel | 13e71ce | 2019-09-19 16:31:17 +0000 | [diff] [blame] | 121 | void Float2IntPass::findRoots(Function &F, const DominatorTree &DT, |
| 122 | SmallPtrSet<Instruction*,8> &Roots) { |
| 123 | for (BasicBlock &BB : F) { |
| 124 | // Unreachable code can take on strange forms that we are not prepared to |
| 125 | // handle. For example, an instruction may have itself as an operand. |
| 126 | if (!DT.isReachableFromEntry(&BB)) |
Reid Kleckner | 54ade23 | 2015-12-09 21:08:18 +0000 | [diff] [blame] | 127 | continue; |
Sanjay Patel | 13e71ce | 2019-09-19 16:31:17 +0000 | [diff] [blame] | 128 | |
| 129 | for (Instruction &I : BB) { |
| 130 | if (isa<VectorType>(I.getType())) |
| 131 | continue; |
| 132 | switch (I.getOpcode()) { |
| 133 | default: break; |
| 134 | case Instruction::FPToUI: |
| 135 | case Instruction::FPToSI: |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 136 | Roots.insert(&I); |
Sanjay Patel | 13e71ce | 2019-09-19 16:31:17 +0000 | [diff] [blame] | 137 | break; |
| 138 | case Instruction::FCmp: |
| 139 | if (mapFCmpPred(cast<CmpInst>(&I)->getPredicate()) != |
| 140 | CmpInst::BAD_ICMP_PREDICATE) |
| 141 | Roots.insert(&I); |
| 142 | break; |
| 143 | } |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Helper - mark I as having been traversed, having range R. |
Craig Topper | 5974dad | 2017-05-04 21:29:45 +0000 | [diff] [blame] | 149 | void Float2IntPass::seen(Instruction *I, ConstantRange R) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 150 | LLVM_DEBUG(dbgs() << "F2I: " << *I << ":" << R << "\n"); |
Craig Topper | fc481e5 | 2017-05-05 17:09:29 +0000 | [diff] [blame] | 151 | auto IT = SeenInsts.find(I); |
| 152 | if (IT != SeenInsts.end()) |
| 153 | IT->second = std::move(R); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 154 | else |
Craig Topper | fc481e5 | 2017-05-05 17:09:29 +0000 | [diff] [blame] | 155 | SeenInsts.insert(std::make_pair(I, std::move(R))); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // Helper - get a range representing a poison value. |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 159 | ConstantRange Float2IntPass::badRange() { |
Nikita Popov | 977934f | 2019-03-24 09:34:40 +0000 | [diff] [blame] | 160 | return ConstantRange::getFull(MaxIntegerBW + 1); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 161 | } |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 162 | ConstantRange Float2IntPass::unknownRange() { |
Nikita Popov | 977934f | 2019-03-24 09:34:40 +0000 | [diff] [blame] | 163 | return ConstantRange::getEmpty(MaxIntegerBW + 1); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 164 | } |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 165 | ConstantRange Float2IntPass::validateRange(ConstantRange R) { |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 166 | if (R.getBitWidth() > MaxIntegerBW + 1) |
| 167 | return badRange(); |
| 168 | return R; |
| 169 | } |
| 170 | |
| 171 | // The most obvious way to structure the search is a depth-first, eager |
| 172 | // search from each root. However, that require direct recursion and so |
| 173 | // can only handle small instruction sequences. Instead, we split the search |
| 174 | // up into two phases: |
| 175 | // - walkBackwards: A breadth-first walk of the use-def graph starting from |
| 176 | // the roots. Populate "SeenInsts" with interesting |
| 177 | // instructions and poison values if they're obvious and |
| 178 | // cheap to compute. Calculate the equivalance set structure |
| 179 | // while we're here too. |
| 180 | // - walkForwards: Iterate over SeenInsts in reverse order, so we visit |
| 181 | // defs before their uses. Calculate the real range info. |
| 182 | |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 183 | // Breadth-first walk of the use-def graph; determine the set of nodes |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 184 | // we care about and eagerly determine if some of them are poisonous. |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 185 | void Float2IntPass::walkBackwards(const SmallPtrSetImpl<Instruction*> &Roots) { |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 186 | std::deque<Instruction*> Worklist(Roots.begin(), Roots.end()); |
| 187 | while (!Worklist.empty()) { |
| 188 | Instruction *I = Worklist.back(); |
| 189 | Worklist.pop_back(); |
| 190 | |
| 191 | if (SeenInsts.find(I) != SeenInsts.end()) |
| 192 | // Seen already. |
| 193 | continue; |
| 194 | |
| 195 | switch (I->getOpcode()) { |
| 196 | // FIXME: Handle select and phi nodes. |
| 197 | default: |
| 198 | // Path terminated uncleanly. |
| 199 | seen(I, badRange()); |
| 200 | break; |
| 201 | |
Philip Reames | 4d00af1 | 2016-12-01 20:08:47 +0000 | [diff] [blame] | 202 | case Instruction::UIToFP: |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 203 | case Instruction::SIToFP: { |
Philip Reames | 4d00af1 | 2016-12-01 20:08:47 +0000 | [diff] [blame] | 204 | // Path terminated cleanly - use the type of the integer input to seed |
| 205 | // the analysis. |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 206 | unsigned BW = I->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
Nikita Popov | 977934f | 2019-03-24 09:34:40 +0000 | [diff] [blame] | 207 | auto Input = ConstantRange::getFull(BW); |
Philip Reames | 4d00af1 | 2016-12-01 20:08:47 +0000 | [diff] [blame] | 208 | auto CastOp = (Instruction::CastOps)I->getOpcode(); |
| 209 | seen(I, validateRange(Input.castOp(CastOp, MaxIntegerBW+1))); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 210 | continue; |
| 211 | } |
| 212 | |
Cameron McInally | 771769b | 2019-07-08 14:46:07 +0000 | [diff] [blame] | 213 | case Instruction::FNeg: |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 214 | case Instruction::FAdd: |
| 215 | case Instruction::FSub: |
| 216 | case Instruction::FMul: |
| 217 | case Instruction::FPToUI: |
| 218 | case Instruction::FPToSI: |
| 219 | case Instruction::FCmp: |
| 220 | seen(I, unknownRange()); |
| 221 | break; |
| 222 | } |
NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 223 | |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 224 | for (Value *O : I->operands()) { |
| 225 | if (Instruction *OI = dyn_cast<Instruction>(O)) { |
| 226 | // Unify def-use chains if they interfere. |
| 227 | ECs.unionSets(I, OI); |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 228 | if (SeenInsts.find(I)->second != badRange()) |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 229 | Worklist.push_back(OI); |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 230 | } else if (!isa<ConstantFP>(O)) { |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 231 | // Not an instruction or ConstantFP? we can't do anything. |
| 232 | seen(I, badRange()); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | // Walk forwards down the list of seen instructions, so we visit defs before |
| 239 | // uses. |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 240 | void Float2IntPass::walkForwards() { |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 241 | for (auto &It : reverse(SeenInsts)) { |
Pete Cooper | 7679afd | 2015-07-24 21:13:43 +0000 | [diff] [blame] | 242 | if (It.second != unknownRange()) |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 243 | continue; |
| 244 | |
Pete Cooper | 7679afd | 2015-07-24 21:13:43 +0000 | [diff] [blame] | 245 | Instruction *I = It.first; |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 246 | std::function<ConstantRange(ArrayRef<ConstantRange>)> Op; |
| 247 | switch (I->getOpcode()) { |
| 248 | // FIXME: Handle select and phi nodes. |
| 249 | default: |
| 250 | case Instruction::UIToFP: |
| 251 | case Instruction::SIToFP: |
| 252 | llvm_unreachable("Should have been handled in walkForwards!"); |
| 253 | |
Cameron McInally | 771769b | 2019-07-08 14:46:07 +0000 | [diff] [blame] | 254 | case Instruction::FNeg: |
| 255 | Op = [](ArrayRef<ConstantRange> Ops) { |
| 256 | assert(Ops.size() == 1 && "FNeg is a unary operator!"); |
| 257 | unsigned Size = Ops[0].getBitWidth(); |
| 258 | auto Zero = ConstantRange(APInt::getNullValue(Size)); |
| 259 | return Zero.sub(Ops[0]); |
| 260 | }; |
| 261 | break; |
| 262 | |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 263 | case Instruction::FAdd: |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 264 | case Instruction::FSub: |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 265 | case Instruction::FMul: |
Philip Reames | 4d00af1 | 2016-12-01 20:08:47 +0000 | [diff] [blame] | 266 | Op = [I](ArrayRef<ConstantRange> Ops) { |
| 267 | assert(Ops.size() == 2 && "its a binary operator!"); |
| 268 | auto BinOp = (Instruction::BinaryOps) I->getOpcode(); |
| 269 | return Ops[0].binaryOp(BinOp, Ops[1]); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 270 | }; |
| 271 | break; |
| 272 | |
| 273 | // |
| 274 | // Root-only instructions - we'll only see these if they're the |
| 275 | // first node in a walk. |
| 276 | // |
| 277 | case Instruction::FPToUI: |
| 278 | case Instruction::FPToSI: |
Philip Reames | 4d00af1 | 2016-12-01 20:08:47 +0000 | [diff] [blame] | 279 | Op = [I](ArrayRef<ConstantRange> Ops) { |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 280 | assert(Ops.size() == 1 && "FPTo[US]I is a unary operator!"); |
Philip Reames | 4d00af1 | 2016-12-01 20:08:47 +0000 | [diff] [blame] | 281 | // Note: We're ignoring the casts output size here as that's what the |
| 282 | // caller expects. |
| 283 | auto CastOp = (Instruction::CastOps)I->getOpcode(); |
| 284 | return Ops[0].castOp(CastOp, MaxIntegerBW+1); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 285 | }; |
| 286 | break; |
| 287 | |
| 288 | case Instruction::FCmp: |
| 289 | Op = [](ArrayRef<ConstantRange> Ops) { |
| 290 | assert(Ops.size() == 2 && "FCmp is a binary operator!"); |
| 291 | return Ops[0].unionWith(Ops[1]); |
| 292 | }; |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | bool Abort = false; |
| 297 | SmallVector<ConstantRange,4> OpRanges; |
| 298 | for (Value *O : I->operands()) { |
| 299 | if (Instruction *OI = dyn_cast<Instruction>(O)) { |
| 300 | assert(SeenInsts.find(OI) != SeenInsts.end() && |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 301 | "def not seen before use!"); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 302 | OpRanges.push_back(SeenInsts.find(OI)->second); |
| 303 | } else if (ConstantFP *CF = dyn_cast<ConstantFP>(O)) { |
| 304 | // Work out if the floating point number can be losslessly represented |
| 305 | // as an integer. |
| 306 | // APFloat::convertToInteger(&Exact) purports to do what we want, but |
| 307 | // the exactness can be too precise. For example, negative zero can |
| 308 | // never be exactly converted to an integer. |
| 309 | // |
| 310 | // Instead, we ask APFloat to round itself to an integral value - this |
| 311 | // preserves sign-of-zero - then compare the result with the original. |
| 312 | // |
Benjamin Kramer | 46e38f3 | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 313 | const APFloat &F = CF->getValueAPF(); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 314 | |
| 315 | // First, weed out obviously incorrect values. Non-finite numbers |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 316 | // can't be represented and neither can negative zero, unless |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 317 | // we're in fast math mode. |
| 318 | if (!F.isFinite() || |
| 319 | (F.isZero() && F.isNegative() && isa<FPMathOperator>(I) && |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 320 | !I->hasNoSignedZeros())) { |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 321 | seen(I, badRange()); |
| 322 | Abort = true; |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | APFloat NewF = F; |
| 327 | auto Res = NewF.roundToIntegral(APFloat::rmNearestTiesToEven); |
| 328 | if (Res != APFloat::opOK || NewF.compare(F) != APFloat::cmpEqual) { |
| 329 | seen(I, badRange()); |
| 330 | Abort = true; |
| 331 | break; |
| 332 | } |
| 333 | // OK, it's representable. Now get it. |
| 334 | APSInt Int(MaxIntegerBW+1, false); |
| 335 | bool Exact; |
| 336 | CF->getValueAPF().convertToInteger(Int, |
| 337 | APFloat::rmNearestTiesToEven, |
| 338 | &Exact); |
| 339 | OpRanges.push_back(ConstantRange(Int)); |
| 340 | } else { |
| 341 | llvm_unreachable("Should have already marked this as badRange!"); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // Reduce the operands' ranges to a single range and return. |
| 346 | if (!Abort) |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame] | 347 | seen(I, Op(OpRanges)); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | // If there is a valid transform to be done, do it. |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 352 | bool Float2IntPass::validateAndTransform() { |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 353 | bool MadeChange = false; |
| 354 | |
| 355 | // Iterate over every disjoint partition of the def-use graph. |
| 356 | for (auto It = ECs.begin(), E = ECs.end(); It != E; ++It) { |
| 357 | ConstantRange R(MaxIntegerBW + 1, false); |
| 358 | bool Fail = false; |
| 359 | Type *ConvertedToTy = nullptr; |
| 360 | |
| 361 | // For every member of the partition, union all the ranges together. |
| 362 | for (auto MI = ECs.member_begin(It), ME = ECs.member_end(); |
| 363 | MI != ME; ++MI) { |
| 364 | Instruction *I = *MI; |
| 365 | auto SeenI = SeenInsts.find(I); |
| 366 | if (SeenI == SeenInsts.end()) |
| 367 | continue; |
| 368 | |
| 369 | R = R.unionWith(SeenI->second); |
| 370 | // We need to ensure I has no users that have not been seen. |
| 371 | // If it does, transformation would be illegal. |
| 372 | // |
| 373 | // Don't count the roots, as they terminate the graphs. |
| 374 | if (Roots.count(I) == 0) { |
| 375 | // Set the type of the conversion while we're here. |
| 376 | if (!ConvertedToTy) |
| 377 | ConvertedToTy = I->getType(); |
| 378 | for (User *U : I->users()) { |
| 379 | Instruction *UI = dyn_cast<Instruction>(U); |
| 380 | if (!UI || SeenInsts.find(UI) == SeenInsts.end()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 381 | LLVM_DEBUG(dbgs() << "F2I: Failing because of " << *U << "\n"); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 382 | Fail = true; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | if (Fail) |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | // If the set was empty, or we failed, or the range is poisonous, |
| 392 | // bail out. |
| 393 | if (ECs.member_begin(It) == ECs.member_end() || Fail || |
| 394 | R.isFullSet() || R.isSignWrappedSet()) |
| 395 | continue; |
| 396 | assert(ConvertedToTy && "Must have set the convertedtoty by this point!"); |
NAKAMURA Takumi | a9cb538 | 2015-09-22 11:14:39 +0000 | [diff] [blame] | 397 | |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 398 | // The number of bits required is the maximum of the upper and |
| 399 | // lower limits, plus one so it can be signed. |
| 400 | unsigned MinBW = std::max(R.getLower().getMinSignedBits(), |
| 401 | R.getUpper().getMinSignedBits()) + 1; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 402 | LLVM_DEBUG(dbgs() << "F2I: MinBitwidth=" << MinBW << ", R: " << R << "\n"); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 403 | |
| 404 | // If we've run off the realms of the exactly representable integers, |
| 405 | // the floating point result will differ from an integer approximation. |
| 406 | |
| 407 | // Do we need more bits than are in the mantissa of the type we converted |
| 408 | // to? semanticsPrecision returns the number of mantissa bits plus one |
| 409 | // for the sign bit. |
| 410 | unsigned MaxRepresentableBits |
| 411 | = APFloat::semanticsPrecision(ConvertedToTy->getFltSemantics()) - 1; |
| 412 | if (MinBW > MaxRepresentableBits) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 413 | LLVM_DEBUG(dbgs() << "F2I: Value not guaranteed to be representable!\n"); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 414 | continue; |
| 415 | } |
| 416 | if (MinBW > 64) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 417 | LLVM_DEBUG( |
| 418 | dbgs() << "F2I: Value requires more than 64 bits to represent!\n"); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 419 | continue; |
| 420 | } |
| 421 | |
| 422 | // OK, R is known to be representable. Now pick a type for it. |
| 423 | // FIXME: Pick the smallest legal type that will fit. |
| 424 | Type *Ty = (MinBW > 32) ? Type::getInt64Ty(*Ctx) : Type::getInt32Ty(*Ctx); |
| 425 | |
| 426 | for (auto MI = ECs.member_begin(It), ME = ECs.member_end(); |
| 427 | MI != ME; ++MI) |
| 428 | convert(*MI, Ty); |
| 429 | MadeChange = true; |
| 430 | } |
| 431 | |
| 432 | return MadeChange; |
| 433 | } |
| 434 | |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 435 | Value *Float2IntPass::convert(Instruction *I, Type *ToTy) { |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 436 | if (ConvertedInsts.find(I) != ConvertedInsts.end()) |
| 437 | // Already converted this instruction. |
| 438 | return ConvertedInsts[I]; |
| 439 | |
| 440 | SmallVector<Value*,4> NewOperands; |
| 441 | for (Value *V : I->operands()) { |
| 442 | // Don't recurse if we're an instruction that terminates the path. |
| 443 | if (I->getOpcode() == Instruction::UIToFP || |
| 444 | I->getOpcode() == Instruction::SIToFP) { |
| 445 | NewOperands.push_back(V); |
| 446 | } else if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 447 | NewOperands.push_back(convert(VI, ToTy)); |
| 448 | } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) { |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 449 | APSInt Val(ToTy->getPrimitiveSizeInBits(), /*isUnsigned=*/false); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 450 | bool Exact; |
| 451 | CF->getValueAPF().convertToInteger(Val, |
| 452 | APFloat::rmNearestTiesToEven, |
| 453 | &Exact); |
| 454 | NewOperands.push_back(ConstantInt::get(ToTy, Val)); |
| 455 | } else { |
| 456 | llvm_unreachable("Unhandled operand type?"); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // Now create a new instruction. |
| 461 | IRBuilder<> IRB(I); |
| 462 | Value *NewV = nullptr; |
| 463 | switch (I->getOpcode()) { |
| 464 | default: llvm_unreachable("Unhandled instruction!"); |
| 465 | |
| 466 | case Instruction::FPToUI: |
| 467 | NewV = IRB.CreateZExtOrTrunc(NewOperands[0], I->getType()); |
| 468 | break; |
| 469 | |
| 470 | case Instruction::FPToSI: |
| 471 | NewV = IRB.CreateSExtOrTrunc(NewOperands[0], I->getType()); |
| 472 | break; |
| 473 | |
| 474 | case Instruction::FCmp: { |
| 475 | CmpInst::Predicate P = mapFCmpPred(cast<CmpInst>(I)->getPredicate()); |
| 476 | assert(P != CmpInst::BAD_ICMP_PREDICATE && "Unhandled predicate!"); |
| 477 | NewV = IRB.CreateICmp(P, NewOperands[0], NewOperands[1], I->getName()); |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | case Instruction::UIToFP: |
| 482 | NewV = IRB.CreateZExtOrTrunc(NewOperands[0], ToTy); |
| 483 | break; |
| 484 | |
| 485 | case Instruction::SIToFP: |
| 486 | NewV = IRB.CreateSExtOrTrunc(NewOperands[0], ToTy); |
| 487 | break; |
| 488 | |
Cameron McInally | 771769b | 2019-07-08 14:46:07 +0000 | [diff] [blame] | 489 | case Instruction::FNeg: |
| 490 | NewV = IRB.CreateNeg(NewOperands[0], I->getName()); |
| 491 | break; |
| 492 | |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 493 | case Instruction::FAdd: |
| 494 | case Instruction::FSub: |
| 495 | case Instruction::FMul: |
| 496 | NewV = IRB.CreateBinOp(mapBinOpcode(I->getOpcode()), |
| 497 | NewOperands[0], NewOperands[1], |
| 498 | I->getName()); |
| 499 | break; |
| 500 | } |
| 501 | |
| 502 | // If we're a root instruction, RAUW. |
| 503 | if (Roots.count(I)) |
| 504 | I->replaceAllUsesWith(NewV); |
| 505 | |
| 506 | ConvertedInsts[I] = NewV; |
| 507 | return NewV; |
| 508 | } |
| 509 | |
| 510 | // Perform dead code elimination on the instructions we just modified. |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 511 | void Float2IntPass::cleanup() { |
David Majnemer | d770877 | 2016-06-24 04:05:21 +0000 | [diff] [blame] | 512 | for (auto &I : reverse(ConvertedInsts)) |
Pete Cooper | 7679afd | 2015-07-24 21:13:43 +0000 | [diff] [blame] | 513 | I.first->eraseFromParent(); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Sanjay Patel | 13e71ce | 2019-09-19 16:31:17 +0000 | [diff] [blame] | 516 | bool Float2IntPass::runImpl(Function &F, const DominatorTree &DT) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 517 | LLVM_DEBUG(dbgs() << "F2I: Looking at function " << F.getName() << "\n"); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 518 | // Clear out all state. |
| 519 | ECs = EquivalenceClasses<Instruction*>(); |
| 520 | SeenInsts.clear(); |
| 521 | ConvertedInsts.clear(); |
| 522 | Roots.clear(); |
| 523 | |
| 524 | Ctx = &F.getParent()->getContext(); |
| 525 | |
Sanjay Patel | 13e71ce | 2019-09-19 16:31:17 +0000 | [diff] [blame] | 526 | findRoots(F, DT, Roots); |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 527 | |
| 528 | walkBackwards(Roots); |
| 529 | walkForwards(); |
| 530 | |
| 531 | bool Modified = validateAndTransform(); |
| 532 | if (Modified) |
| 533 | cleanup(); |
| 534 | return Modified; |
| 535 | } |
| 536 | |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 537 | namespace llvm { |
| 538 | FunctionPass *createFloat2IntPass() { return new Float2IntLegacyPass(); } |
| 539 | |
Sanjay Patel | 13e71ce | 2019-09-19 16:31:17 +0000 | [diff] [blame] | 540 | PreservedAnalyses Float2IntPass::run(Function &F, FunctionAnalysisManager &AM) { |
| 541 | const DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 542 | if (!runImpl(F, DT)) |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 543 | return PreservedAnalyses::all(); |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 544 | |
| 545 | PreservedAnalyses PA; |
| 546 | PA.preserveSet<CFGAnalyses>(); |
| 547 | PA.preserve<GlobalsAA>(); |
| 548 | return PA; |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 549 | } |
| 550 | } // End namespace llvm |