1 | //===--- ExpandLargeFpConvert.cpp - Expand large fp convert----------------===// |
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 | |
10 | // This pass expands ‘fptoui .. to’, ‘fptosi .. to’, ‘uitofp .. to’, |
11 | // ‘sitofp .. to’ instructions with a bitwidth above a threshold into |
12 | // auto-generated functions. This is useful for targets like x86_64 that cannot |
13 | // lower fp convertions with more than 128 bits. |
14 | // |
15 | //===----------------------------------------------------------------------===// |
16 | |
17 | #include "llvm/CodeGen/ExpandLargeFpConvert.h" |
18 | #include "llvm/ADT/SmallVector.h" |
19 | #include "llvm/ADT/StringExtras.h" |
20 | #include "llvm/Analysis/GlobalsModRef.h" |
21 | #include "llvm/CodeGen/Passes.h" |
22 | #include "llvm/CodeGen/TargetLowering.h" |
23 | #include "llvm/CodeGen/TargetPassConfig.h" |
24 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
25 | #include "llvm/IR/IRBuilder.h" |
26 | #include "llvm/IR/InstIterator.h" |
27 | #include "llvm/IR/PassManager.h" |
28 | #include "llvm/InitializePasses.h" |
29 | #include "llvm/Pass.h" |
30 | #include "llvm/Support/CommandLine.h" |
31 | #include "llvm/Target/TargetMachine.h" |
32 | |
33 | using namespace llvm; |
34 | |
35 | static cl::opt<unsigned> |
36 | ExpandFpConvertBits("expand-fp-convert-bits" , cl::Hidden, |
37 | cl::init(Val: llvm::IntegerType::MAX_INT_BITS), |
38 | cl::desc("fp convert instructions on integers with " |
39 | "more than <N> bits are expanded." )); |
40 | |
41 | /// Generate code to convert a fp number to integer, replacing FPToS(U)I with |
42 | /// the generated code. This currently generates code similarly to compiler-rt's |
43 | /// implementations. |
44 | /// |
45 | /// An example IR generated from compiler-rt/fixsfdi.c looks like below: |
46 | /// define dso_local i64 @foo(float noundef %a) local_unnamed_addr #0 { |
47 | /// entry: |
48 | /// %0 = bitcast float %a to i32 |
49 | /// %conv.i = zext i32 %0 to i64 |
50 | /// %tobool.not = icmp sgt i32 %0, -1 |
51 | /// %conv = select i1 %tobool.not, i64 1, i64 -1 |
52 | /// %and = lshr i64 %conv.i, 23 |
53 | /// %shr = and i64 %and, 255 |
54 | /// %and2 = and i64 %conv.i, 8388607 |
55 | /// %or = or i64 %and2, 8388608 |
56 | /// %cmp = icmp ult i64 %shr, 127 |
57 | /// br i1 %cmp, label %cleanup, label %if.end |
58 | /// |
59 | /// if.end: ; preds = %entry |
60 | /// %sub = add nuw nsw i64 %shr, 4294967169 |
61 | /// %conv5 = and i64 %sub, 4294967232 |
62 | /// %cmp6.not = icmp eq i64 %conv5, 0 |
63 | /// br i1 %cmp6.not, label %if.end12, label %if.then8 |
64 | /// |
65 | /// if.then8: ; preds = %if.end |
66 | /// %cond11 = select i1 %tobool.not, i64 9223372036854775807, i64 -9223372036854775808 |
67 | /// br label %cleanup |
68 | /// |
69 | /// if.end12: ; preds = %if.end |
70 | /// %cmp13 = icmp ult i64 %shr, 150 |
71 | /// br i1 %cmp13, label %if.then15, label %if.else |
72 | /// |
73 | /// if.then15: ; preds = %if.end12 |
74 | /// %sub16 = sub nuw nsw i64 150, %shr |
75 | /// %shr17 = lshr i64 %or, %sub16 |
76 | /// %mul = mul nsw i64 %shr17, %conv |
77 | /// br label %cleanup |
78 | /// |
79 | /// if.else: ; preds = %if.end12 |
80 | /// %sub18 = add nsw i64 %shr, -150 |
81 | /// %shl = shl i64 %or, %sub18 |
82 | /// %mul19 = mul nsw i64 %shl, %conv |
83 | /// br label %cleanup |
84 | /// |
85 | /// cleanup: ; preds = %entry, %if.else, %if.then15, %if.then8 |
86 | /// %retval.0 = phi i64 [ %cond11, %if.then8 ], [ %mul, %if.then15 ], [ %mul19, %if.else ], [ 0, %entry ] |
87 | /// ret i64 %retval.0 |
88 | /// } |
89 | /// |
90 | /// Replace fp to integer with generated code. |
91 | static void expandFPToI(Instruction *FPToI) { |
92 | IRBuilder<> Builder(FPToI); |
93 | auto *FloatVal = FPToI->getOperand(i: 0); |
94 | IntegerType *IntTy = cast<IntegerType>(Val: FPToI->getType()); |
95 | |
96 | unsigned BitWidth = FPToI->getType()->getIntegerBitWidth(); |
97 | unsigned FPMantissaWidth = FloatVal->getType()->getFPMantissaWidth() - 1; |
98 | |
99 | // FIXME: fp16's range is covered by i32. So `fptoi half` can convert |
100 | // to i32 first following a sext/zext to target integer type. |
101 | Value *A1 = nullptr; |
102 | if (FloatVal->getType()->isHalfTy()) { |
103 | if (FPToI->getOpcode() == Instruction::FPToUI) { |
104 | Value *A0 = Builder.CreateFPToUI(V: FloatVal, DestTy: Builder.getIntNTy(N: 32)); |
105 | A1 = Builder.CreateZExt(V: A0, DestTy: IntTy); |
106 | } else { // FPToSI |
107 | Value *A0 = Builder.CreateFPToSI(V: FloatVal, DestTy: Builder.getIntNTy(N: 32)); |
108 | A1 = Builder.CreateSExt(V: A0, DestTy: IntTy); |
109 | } |
110 | FPToI->replaceAllUsesWith(V: A1); |
111 | FPToI->dropAllReferences(); |
112 | FPToI->eraseFromParent(); |
113 | return; |
114 | } |
115 | |
116 | // fp80 conversion is implemented by fpext to fp128 first then do the |
117 | // conversion. |
118 | FPMantissaWidth = FPMantissaWidth == 63 ? 112 : FPMantissaWidth; |
119 | unsigned FloatWidth = |
120 | PowerOf2Ceil(A: FloatVal->getType()->getScalarSizeInBits()); |
121 | unsigned ExponentWidth = FloatWidth - FPMantissaWidth - 1; |
122 | unsigned ExponentBias = (1 << (ExponentWidth - 1)) - 1; |
123 | Value *ImplicitBit = Builder.CreateShl( |
124 | LHS: Builder.getIntN(N: BitWidth, C: 1), RHS: Builder.getIntN(N: BitWidth, C: FPMantissaWidth)); |
125 | Value *SignificandMask = |
126 | Builder.CreateSub(LHS: ImplicitBit, RHS: Builder.getIntN(N: BitWidth, C: 1)); |
127 | Value *NegOne = Builder.CreateSExt( |
128 | V: ConstantInt::getSigned(Ty: Builder.getInt32Ty(), V: -1), DestTy: IntTy); |
129 | Value *NegInf = |
130 | Builder.CreateShl(LHS: ConstantInt::getSigned(Ty: IntTy, V: 1), |
131 | RHS: ConstantInt::getSigned(Ty: IntTy, V: BitWidth - 1)); |
132 | |
133 | BasicBlock *Entry = Builder.GetInsertBlock(); |
134 | Function *F = Entry->getParent(); |
135 | Entry->setName(Twine(Entry->getName(), "fp-to-i-entry" )); |
136 | BasicBlock *End = |
137 | Entry->splitBasicBlock(I: Builder.GetInsertPoint(), BBName: "fp-to-i-cleanup" ); |
138 | BasicBlock *IfEnd = |
139 | BasicBlock::Create(Context&: Builder.getContext(), Name: "fp-to-i-if-end" , Parent: F, InsertBefore: End); |
140 | BasicBlock *IfThen5 = |
141 | BasicBlock::Create(Context&: Builder.getContext(), Name: "fp-to-i-if-then5" , Parent: F, InsertBefore: End); |
142 | BasicBlock *IfEnd9 = |
143 | BasicBlock::Create(Context&: Builder.getContext(), Name: "fp-to-i-if-end9" , Parent: F, InsertBefore: End); |
144 | BasicBlock *IfThen12 = |
145 | BasicBlock::Create(Context&: Builder.getContext(), Name: "fp-to-i-if-then12" , Parent: F, InsertBefore: End); |
146 | BasicBlock *IfElse = |
147 | BasicBlock::Create(Context&: Builder.getContext(), Name: "fp-to-i-if-else" , Parent: F, InsertBefore: End); |
148 | |
149 | Entry->getTerminator()->eraseFromParent(); |
150 | |
151 | // entry: |
152 | Builder.SetInsertPoint(Entry); |
153 | Value *FloatVal0 = FloatVal; |
154 | // fp80 conversion is implemented by fpext to fp128 first then do the |
155 | // conversion. |
156 | if (FloatVal->getType()->isX86_FP80Ty()) |
157 | FloatVal0 = |
158 | Builder.CreateFPExt(V: FloatVal, DestTy: Type::getFP128Ty(C&: Builder.getContext())); |
159 | Value *ARep0 = |
160 | Builder.CreateBitCast(V: FloatVal0, DestTy: Builder.getIntNTy(N: FloatWidth)); |
161 | Value *ARep = Builder.CreateZExt(V: ARep0, DestTy: FPToI->getType()); |
162 | Value *PosOrNeg = Builder.CreateICmpSGT( |
163 | LHS: ARep0, RHS: ConstantInt::getSigned(Ty: Builder.getIntNTy(N: FloatWidth), V: -1)); |
164 | Value *Sign = Builder.CreateSelect(C: PosOrNeg, True: ConstantInt::getSigned(Ty: IntTy, V: 1), |
165 | False: ConstantInt::getSigned(Ty: IntTy, V: -1)); |
166 | Value *And = |
167 | Builder.CreateLShr(LHS: ARep, RHS: Builder.getIntN(N: BitWidth, C: FPMantissaWidth)); |
168 | Value *And2 = Builder.CreateAnd( |
169 | LHS: And, RHS: Builder.getIntN(N: BitWidth, C: (1 << ExponentWidth) - 1)); |
170 | Value *Abs = Builder.CreateAnd(LHS: ARep, RHS: SignificandMask); |
171 | Value *Or = Builder.CreateOr(LHS: Abs, RHS: ImplicitBit); |
172 | Value *Cmp = |
173 | Builder.CreateICmpULT(LHS: And2, RHS: Builder.getIntN(N: BitWidth, C: ExponentBias)); |
174 | Builder.CreateCondBr(Cond: Cmp, True: End, False: IfEnd); |
175 | |
176 | // if.end: |
177 | Builder.SetInsertPoint(IfEnd); |
178 | Value *Add1 = Builder.CreateAdd( |
179 | LHS: And2, RHS: ConstantInt::getSigned( |
180 | Ty: IntTy, V: -static_cast<int64_t>(ExponentBias + BitWidth))); |
181 | Value *Cmp3 = Builder.CreateICmpULT( |
182 | LHS: Add1, RHS: ConstantInt::getSigned(Ty: IntTy, V: -static_cast<int64_t>(BitWidth))); |
183 | Builder.CreateCondBr(Cond: Cmp3, True: IfThen5, False: IfEnd9); |
184 | |
185 | // if.then5: |
186 | Builder.SetInsertPoint(IfThen5); |
187 | Value *PosInf = Builder.CreateXor(LHS: NegOne, RHS: NegInf); |
188 | Value *Cond8 = Builder.CreateSelect(C: PosOrNeg, True: PosInf, False: NegInf); |
189 | Builder.CreateBr(Dest: End); |
190 | |
191 | // if.end9: |
192 | Builder.SetInsertPoint(IfEnd9); |
193 | Value *Cmp10 = Builder.CreateICmpULT( |
194 | LHS: And2, RHS: Builder.getIntN(N: BitWidth, C: ExponentBias + FPMantissaWidth)); |
195 | Builder.CreateCondBr(Cond: Cmp10, True: IfThen12, False: IfElse); |
196 | |
197 | // if.then12: |
198 | Builder.SetInsertPoint(IfThen12); |
199 | Value *Sub13 = Builder.CreateSub( |
200 | LHS: Builder.getIntN(N: BitWidth, C: ExponentBias + FPMantissaWidth), RHS: And2); |
201 | Value *Shr14 = Builder.CreateLShr(LHS: Or, RHS: Sub13); |
202 | Value *Mul = Builder.CreateMul(LHS: Shr14, RHS: Sign); |
203 | Builder.CreateBr(Dest: End); |
204 | |
205 | // if.else: |
206 | Builder.SetInsertPoint(IfElse); |
207 | Value *Sub15 = Builder.CreateAdd( |
208 | LHS: And2, RHS: ConstantInt::getSigned( |
209 | Ty: IntTy, V: -static_cast<int64_t>(ExponentBias + FPMantissaWidth))); |
210 | Value *Shl = Builder.CreateShl(LHS: Or, RHS: Sub15); |
211 | Value *Mul16 = Builder.CreateMul(LHS: Shl, RHS: Sign); |
212 | Builder.CreateBr(Dest: End); |
213 | |
214 | // cleanup: |
215 | Builder.SetInsertPoint(TheBB: End, IP: End->begin()); |
216 | PHINode *Retval0 = Builder.CreatePHI(Ty: FPToI->getType(), NumReservedValues: 4); |
217 | |
218 | Retval0->addIncoming(V: Cond8, BB: IfThen5); |
219 | Retval0->addIncoming(V: Mul, BB: IfThen12); |
220 | Retval0->addIncoming(V: Mul16, BB: IfElse); |
221 | Retval0->addIncoming(V: Builder.getIntN(N: BitWidth, C: 0), BB: Entry); |
222 | |
223 | FPToI->replaceAllUsesWith(V: Retval0); |
224 | FPToI->dropAllReferences(); |
225 | FPToI->eraseFromParent(); |
226 | } |
227 | |
228 | /// Generate code to convert a fp number to integer, replacing S(U)IToFP with |
229 | /// the generated code. This currently generates code similarly to compiler-rt's |
230 | /// implementations. This implementation has an implicit assumption that integer |
231 | /// width is larger than fp. |
232 | /// |
233 | /// An example IR generated from compiler-rt/floatdisf.c looks like below: |
234 | /// define dso_local float @__floatdisf(i64 noundef %a) local_unnamed_addr #0 { |
235 | /// entry: |
236 | /// %cmp = icmp eq i64 %a, 0 |
237 | /// br i1 %cmp, label %return, label %if.end |
238 | /// |
239 | /// if.end: ; preds = %entry |
240 | /// %shr = ashr i64 %a, 63 |
241 | /// %xor = xor i64 %shr, %a |
242 | /// %sub = sub nsw i64 %xor, %shr |
243 | /// %0 = tail call i64 @llvm.ctlz.i64(i64 %sub, i1 true), !range !5 |
244 | /// %cast = trunc i64 %0 to i32 |
245 | /// %sub1 = sub nuw nsw i32 64, %cast |
246 | /// %sub2 = xor i32 %cast, 63 |
247 | /// %cmp3 = icmp ult i32 %cast, 40 |
248 | /// br i1 %cmp3, label %if.then4, label %if.else |
249 | /// |
250 | /// if.then4: ; preds = %if.end |
251 | /// switch i32 %sub1, label %sw.default [ |
252 | /// i32 25, label %sw.bb |
253 | /// i32 26, label %sw.epilog |
254 | /// ] |
255 | /// |
256 | /// sw.bb: ; preds = %if.then4 |
257 | /// %shl = shl i64 %sub, 1 |
258 | /// br label %sw.epilog |
259 | /// |
260 | /// sw.default: ; preds = %if.then4 |
261 | /// %sub5 = sub nsw i64 38, %0 |
262 | /// %sh_prom = and i64 %sub5, 4294967295 |
263 | /// %shr6 = lshr i64 %sub, %sh_prom |
264 | /// %shr9 = lshr i64 274877906943, %0 |
265 | /// %and = and i64 %shr9, %sub |
266 | /// %cmp10 = icmp ne i64 %and, 0 |
267 | /// %conv11 = zext i1 %cmp10 to i64 |
268 | /// %or = or i64 %shr6, %conv11 |
269 | /// br label %sw.epilog |
270 | /// |
271 | /// sw.epilog: ; preds = %sw.default, %if.then4, %sw.bb |
272 | /// %a.addr.0 = phi i64 [ %or, %sw.default ], [ %sub, %if.then4 ], [ %shl, %sw.bb ] |
273 | /// %1 = lshr i64 %a.addr.0, 2 |
274 | /// %2 = and i64 %1, 1 |
275 | /// %or16 = or i64 %2, %a.addr.0 |
276 | /// %inc = add nsw i64 %or16, 1 |
277 | /// %3 = and i64 %inc, 67108864 |
278 | /// %tobool.not = icmp eq i64 %3, 0 |
279 | /// %spec.select.v = select i1 %tobool.not, i64 2, i64 3 |
280 | /// %spec.select = ashr i64 %inc, %spec.select.v |
281 | /// %spec.select56 = select i1 %tobool.not, i32 %sub2, i32 %sub1 |
282 | /// br label %if.end26 |
283 | /// |
284 | /// if.else: ; preds = %if.end |
285 | /// %sub23 = add nuw nsw i64 %0, 4294967256 |
286 | /// %sh_prom24 = and i64 %sub23, 4294967295 |
287 | /// %shl25 = shl i64 %sub, %sh_prom24 |
288 | /// br label %if.end26 |
289 | /// |
290 | /// if.end26: ; preds = %sw.epilog, %if.else |
291 | /// %a.addr.1 = phi i64 [ %shl25, %if.else ], [ %spec.select, %sw.epilog ] |
292 | /// %e.0 = phi i32 [ %sub2, %if.else ], [ %spec.select56, %sw.epilog ] |
293 | /// %conv27 = trunc i64 %shr to i32 |
294 | /// %and28 = and i32 %conv27, -2147483648 |
295 | /// %add = shl nuw nsw i32 %e.0, 23 |
296 | /// %shl29 = add nuw nsw i32 %add, 1065353216 |
297 | /// %conv31 = trunc i64 %a.addr.1 to i32 |
298 | /// %and32 = and i32 %conv31, 8388607 |
299 | /// %or30 = or i32 %and32, %and28 |
300 | /// %or33 = or i32 %or30, %shl29 |
301 | /// %4 = bitcast i32 %or33 to float |
302 | /// br label %return |
303 | /// |
304 | /// return: ; preds = %entry, %if.end26 |
305 | /// %retval.0 = phi float [ %4, %if.end26 ], [ 0.000000e+00, %entry ] |
306 | /// ret float %retval.0 |
307 | /// } |
308 | /// |
309 | /// Replace integer to fp with generated code. |
310 | static void expandIToFP(Instruction *IToFP) { |
311 | IRBuilder<> Builder(IToFP); |
312 | auto *IntVal = IToFP->getOperand(i: 0); |
313 | IntegerType *IntTy = cast<IntegerType>(Val: IntVal->getType()); |
314 | |
315 | unsigned BitWidth = IntVal->getType()->getIntegerBitWidth(); |
316 | unsigned FPMantissaWidth = IToFP->getType()->getFPMantissaWidth() - 1; |
317 | // fp80 conversion is implemented by conversion tp fp128 first following |
318 | // a fptrunc to fp80. |
319 | FPMantissaWidth = FPMantissaWidth == 63 ? 112 : FPMantissaWidth; |
320 | // FIXME: As there is no related builtins added in compliler-rt, |
321 | // here currently utilized the fp32 <-> fp16 lib calls to implement. |
322 | FPMantissaWidth = FPMantissaWidth == 10 ? 23 : FPMantissaWidth; |
323 | FPMantissaWidth = FPMantissaWidth == 7 ? 23 : FPMantissaWidth; |
324 | unsigned FloatWidth = PowerOf2Ceil(A: FPMantissaWidth); |
325 | bool IsSigned = IToFP->getOpcode() == Instruction::SIToFP; |
326 | |
327 | assert(BitWidth > FloatWidth && "Unexpected conversion. expandIToFP() " |
328 | "assumes integer width is larger than fp." ); |
329 | |
330 | Value *Temp1 = |
331 | Builder.CreateShl(LHS: Builder.getIntN(N: BitWidth, C: 1), |
332 | RHS: Builder.getIntN(N: BitWidth, C: FPMantissaWidth + 3)); |
333 | |
334 | BasicBlock *Entry = Builder.GetInsertBlock(); |
335 | Function *F = Entry->getParent(); |
336 | Entry->setName(Twine(Entry->getName(), "itofp-entry" )); |
337 | BasicBlock *End = |
338 | Entry->splitBasicBlock(I: Builder.GetInsertPoint(), BBName: "itofp-return" ); |
339 | BasicBlock *IfEnd = |
340 | BasicBlock::Create(Context&: Builder.getContext(), Name: "itofp-if-end" , Parent: F, InsertBefore: End); |
341 | BasicBlock *IfThen4 = |
342 | BasicBlock::Create(Context&: Builder.getContext(), Name: "itofp-if-then4" , Parent: F, InsertBefore: End); |
343 | BasicBlock *SwBB = |
344 | BasicBlock::Create(Context&: Builder.getContext(), Name: "itofp-sw-bb" , Parent: F, InsertBefore: End); |
345 | BasicBlock *SwDefault = |
346 | BasicBlock::Create(Context&: Builder.getContext(), Name: "itofp-sw-default" , Parent: F, InsertBefore: End); |
347 | BasicBlock *SwEpilog = |
348 | BasicBlock::Create(Context&: Builder.getContext(), Name: "itofp-sw-epilog" , Parent: F, InsertBefore: End); |
349 | BasicBlock *IfThen20 = |
350 | BasicBlock::Create(Context&: Builder.getContext(), Name: "itofp-if-then20" , Parent: F, InsertBefore: End); |
351 | BasicBlock *IfElse = |
352 | BasicBlock::Create(Context&: Builder.getContext(), Name: "itofp-if-else" , Parent: F, InsertBefore: End); |
353 | BasicBlock *IfEnd26 = |
354 | BasicBlock::Create(Context&: Builder.getContext(), Name: "itofp-if-end26" , Parent: F, InsertBefore: End); |
355 | |
356 | Entry->getTerminator()->eraseFromParent(); |
357 | |
358 | Function *CTLZ = |
359 | Intrinsic::getDeclaration(M: F->getParent(), id: Intrinsic::ctlz, Tys: IntTy); |
360 | ConstantInt *True = Builder.getTrue(); |
361 | |
362 | // entry: |
363 | Builder.SetInsertPoint(Entry); |
364 | Value *Cmp = Builder.CreateICmpEQ(LHS: IntVal, RHS: ConstantInt::getSigned(Ty: IntTy, V: 0)); |
365 | Builder.CreateCondBr(Cond: Cmp, True: End, False: IfEnd); |
366 | |
367 | // if.end: |
368 | Builder.SetInsertPoint(IfEnd); |
369 | Value *Shr = |
370 | Builder.CreateAShr(LHS: IntVal, RHS: Builder.getIntN(N: BitWidth, C: BitWidth - 1)); |
371 | Value *Xor = Builder.CreateXor(LHS: Shr, RHS: IntVal); |
372 | Value *Sub = Builder.CreateSub(LHS: Xor, RHS: Shr); |
373 | Value *Call = Builder.CreateCall(Callee: CTLZ, Args: {IsSigned ? Sub : IntVal, True}); |
374 | Value *Cast = Builder.CreateTrunc(V: Call, DestTy: Builder.getInt32Ty()); |
375 | int BitWidthNew = FloatWidth == 128 ? BitWidth : 32; |
376 | Value *Sub1 = Builder.CreateSub(LHS: Builder.getIntN(N: BitWidthNew, C: BitWidth), |
377 | RHS: FloatWidth == 128 ? Call : Cast); |
378 | Value *Sub2 = Builder.CreateSub(LHS: Builder.getIntN(N: BitWidthNew, C: BitWidth - 1), |
379 | RHS: FloatWidth == 128 ? Call : Cast); |
380 | Value *Cmp3 = Builder.CreateICmpSGT( |
381 | LHS: Sub1, RHS: Builder.getIntN(N: BitWidthNew, C: FPMantissaWidth + 1)); |
382 | Builder.CreateCondBr(Cond: Cmp3, True: IfThen4, False: IfElse); |
383 | |
384 | // if.then4: |
385 | Builder.SetInsertPoint(IfThen4); |
386 | llvm::SwitchInst *SI = Builder.CreateSwitch(V: Sub1, Dest: SwDefault); |
387 | SI->addCase(OnVal: Builder.getIntN(N: BitWidthNew, C: FPMantissaWidth + 2), Dest: SwBB); |
388 | SI->addCase(OnVal: Builder.getIntN(N: BitWidthNew, C: FPMantissaWidth + 3), Dest: SwEpilog); |
389 | |
390 | // sw.bb: |
391 | Builder.SetInsertPoint(SwBB); |
392 | Value *Shl = |
393 | Builder.CreateShl(LHS: IsSigned ? Sub : IntVal, RHS: Builder.getIntN(N: BitWidth, C: 1)); |
394 | Builder.CreateBr(Dest: SwEpilog); |
395 | |
396 | // sw.default: |
397 | Builder.SetInsertPoint(SwDefault); |
398 | Value *Sub5 = Builder.CreateSub( |
399 | LHS: Builder.getIntN(N: BitWidthNew, C: BitWidth - FPMantissaWidth - 3), |
400 | RHS: FloatWidth == 128 ? Call : Cast); |
401 | Value *ShProm = Builder.CreateZExt(V: Sub5, DestTy: IntTy); |
402 | Value *Shr6 = Builder.CreateLShr(LHS: IsSigned ? Sub : IntVal, |
403 | RHS: FloatWidth == 128 ? Sub5 : ShProm); |
404 | Value *Sub8 = |
405 | Builder.CreateAdd(LHS: FloatWidth == 128 ? Call : Cast, |
406 | RHS: Builder.getIntN(N: BitWidthNew, C: FPMantissaWidth + 3)); |
407 | Value *ShProm9 = Builder.CreateZExt(V: Sub8, DestTy: IntTy); |
408 | Value *Shr9 = Builder.CreateLShr(LHS: ConstantInt::getSigned(Ty: IntTy, V: -1), |
409 | RHS: FloatWidth == 128 ? Sub8 : ShProm9); |
410 | Value *And = Builder.CreateAnd(LHS: Shr9, RHS: IsSigned ? Sub : IntVal); |
411 | Value *Cmp10 = Builder.CreateICmpNE(LHS: And, RHS: Builder.getIntN(N: BitWidth, C: 0)); |
412 | Value *Conv11 = Builder.CreateZExt(V: Cmp10, DestTy: IntTy); |
413 | Value *Or = Builder.CreateOr(LHS: Shr6, RHS: Conv11); |
414 | Builder.CreateBr(Dest: SwEpilog); |
415 | |
416 | // sw.epilog: |
417 | Builder.SetInsertPoint(SwEpilog); |
418 | PHINode *AAddr0 = Builder.CreatePHI(Ty: IntTy, NumReservedValues: 3); |
419 | AAddr0->addIncoming(V: Or, BB: SwDefault); |
420 | AAddr0->addIncoming(V: IsSigned ? Sub : IntVal, BB: IfThen4); |
421 | AAddr0->addIncoming(V: Shl, BB: SwBB); |
422 | Value *A0 = Builder.CreateTrunc(V: AAddr0, DestTy: Builder.getInt32Ty()); |
423 | Value *A1 = Builder.CreateLShr(LHS: A0, RHS: Builder.getIntN(N: 32, C: 2)); |
424 | Value *A2 = Builder.CreateAnd(LHS: A1, RHS: Builder.getIntN(N: 32, C: 1)); |
425 | Value *Conv16 = Builder.CreateZExt(V: A2, DestTy: IntTy); |
426 | Value *Or17 = Builder.CreateOr(LHS: AAddr0, RHS: Conv16); |
427 | Value *Inc = Builder.CreateAdd(LHS: Or17, RHS: Builder.getIntN(N: BitWidth, C: 1)); |
428 | Value *Shr18 = nullptr; |
429 | if (IsSigned) |
430 | Shr18 = Builder.CreateAShr(LHS: Inc, RHS: Builder.getIntN(N: BitWidth, C: 2)); |
431 | else |
432 | Shr18 = Builder.CreateLShr(LHS: Inc, RHS: Builder.getIntN(N: BitWidth, C: 2)); |
433 | Value *A3 = Builder.CreateAnd(LHS: Inc, RHS: Temp1, Name: "a3" ); |
434 | Value *PosOrNeg = Builder.CreateICmpEQ(LHS: A3, RHS: Builder.getIntN(N: BitWidth, C: 0)); |
435 | Value * = Builder.CreateTrunc(V: Shr18, DestTy: Builder.getIntNTy(N: FloatWidth)); |
436 | Value * = Builder.CreateLShr(LHS: Shr18, RHS: Builder.getIntN(N: BitWidth, C: 32)); |
437 | Value * = nullptr; |
438 | if (FloatWidth > 80) |
439 | ExtractT64 = Builder.CreateTrunc(V: Sub2, DestTy: Builder.getInt64Ty()); |
440 | else |
441 | ExtractT64 = Builder.CreateTrunc(V: Extract63, DestTy: Builder.getInt32Ty()); |
442 | Builder.CreateCondBr(Cond: PosOrNeg, True: IfEnd26, False: IfThen20); |
443 | |
444 | // if.then20 |
445 | Builder.SetInsertPoint(IfThen20); |
446 | Value *Shr21 = nullptr; |
447 | if (IsSigned) |
448 | Shr21 = Builder.CreateAShr(LHS: Inc, RHS: Builder.getIntN(N: BitWidth, C: 3)); |
449 | else |
450 | Shr21 = Builder.CreateLShr(LHS: Inc, RHS: Builder.getIntN(N: BitWidth, C: 3)); |
451 | Value * = Builder.CreateTrunc(V: Shr21, DestTy: Builder.getIntNTy(N: FloatWidth)); |
452 | Value * = Builder.CreateLShr(LHS: Shr21, RHS: Builder.getIntN(N: BitWidth, C: 32)); |
453 | Value * = nullptr; |
454 | if (FloatWidth > 80) |
455 | ExtractT62 = Builder.CreateTrunc(V: Sub1, DestTy: Builder.getIntNTy(N: 64)); |
456 | else |
457 | ExtractT62 = Builder.CreateTrunc(V: Extract, DestTy: Builder.getIntNTy(N: 32)); |
458 | Builder.CreateBr(Dest: IfEnd26); |
459 | |
460 | // if.else: |
461 | Builder.SetInsertPoint(IfElse); |
462 | Value *Sub24 = Builder.CreateAdd( |
463 | LHS: FloatWidth == 128 ? Call : Cast, |
464 | RHS: ConstantInt::getSigned(Ty: Builder.getIntNTy(N: BitWidthNew), |
465 | V: -(BitWidth - FPMantissaWidth - 1))); |
466 | Value *ShProm25 = Builder.CreateZExt(V: Sub24, DestTy: IntTy); |
467 | Value *Shl26 = Builder.CreateShl(LHS: IsSigned ? Sub : IntVal, |
468 | RHS: FloatWidth == 128 ? Sub24 : ShProm25); |
469 | Value * = Builder.CreateTrunc(V: Shl26, DestTy: Builder.getIntNTy(N: FloatWidth)); |
470 | Value * = Builder.CreateLShr(LHS: Shl26, RHS: Builder.getIntN(N: BitWidth, C: 32)); |
471 | Value * = nullptr; |
472 | if (FloatWidth > 80) |
473 | ExtractT66 = Builder.CreateTrunc(V: Sub2, DestTy: Builder.getIntNTy(N: 64)); |
474 | else |
475 | ExtractT66 = Builder.CreateTrunc(V: Extract65, DestTy: Builder.getInt32Ty()); |
476 | Builder.CreateBr(Dest: IfEnd26); |
477 | |
478 | // if.end26: |
479 | Builder.SetInsertPoint(IfEnd26); |
480 | PHINode *AAddr1Off0 = Builder.CreatePHI(Ty: Builder.getIntNTy(N: FloatWidth), NumReservedValues: 3); |
481 | AAddr1Off0->addIncoming(V: ExtractT, BB: IfThen20); |
482 | AAddr1Off0->addIncoming(V: ExtractT60, BB: SwEpilog); |
483 | AAddr1Off0->addIncoming(V: ExtractT61, BB: IfElse); |
484 | PHINode *AAddr1Off32 = nullptr; |
485 | if (FloatWidth > 32) { |
486 | AAddr1Off32 = |
487 | Builder.CreatePHI(Ty: Builder.getIntNTy(N: FloatWidth > 80 ? 64 : 32), NumReservedValues: 3); |
488 | AAddr1Off32->addIncoming(V: ExtractT62, BB: IfThen20); |
489 | AAddr1Off32->addIncoming(V: ExtractT64, BB: SwEpilog); |
490 | AAddr1Off32->addIncoming(V: ExtractT66, BB: IfElse); |
491 | } |
492 | PHINode *E0 = nullptr; |
493 | if (FloatWidth <= 80) { |
494 | E0 = Builder.CreatePHI(Ty: Builder.getIntNTy(N: BitWidthNew), NumReservedValues: 3); |
495 | E0->addIncoming(V: Sub1, BB: IfThen20); |
496 | E0->addIncoming(V: Sub2, BB: SwEpilog); |
497 | E0->addIncoming(V: Sub2, BB: IfElse); |
498 | } |
499 | Value *And29 = nullptr; |
500 | if (FloatWidth > 80) { |
501 | Value *Temp2 = Builder.CreateShl(LHS: Builder.getIntN(N: BitWidth, C: 1), |
502 | RHS: Builder.getIntN(N: BitWidth, C: 63)); |
503 | And29 = Builder.CreateAnd(LHS: Shr, RHS: Temp2, Name: "and29" ); |
504 | } else { |
505 | Value *Conv28 = Builder.CreateTrunc(V: Shr, DestTy: Builder.getIntNTy(N: 32)); |
506 | And29 = Builder.CreateAnd( |
507 | LHS: Conv28, RHS: ConstantInt::getSigned(Ty: Builder.getIntNTy(N: 32), V: 0x80000000)); |
508 | } |
509 | unsigned TempMod = FPMantissaWidth % 32; |
510 | Value *And34 = nullptr; |
511 | Value *Shl30 = nullptr; |
512 | if (FloatWidth > 80) { |
513 | TempMod += 32; |
514 | Value *Add = Builder.CreateShl(LHS: AAddr1Off32, RHS: Builder.getIntN(N: 64, C: TempMod)); |
515 | Shl30 = Builder.CreateAdd( |
516 | LHS: Add, |
517 | RHS: Builder.getIntN(N: 64, C: ((1ull << (62ull - TempMod)) - 1ull) << TempMod)); |
518 | And34 = Builder.CreateZExt(V: Shl30, DestTy: Builder.getIntNTy(N: 128)); |
519 | } else { |
520 | Value *Add = Builder.CreateShl(LHS: E0, RHS: Builder.getIntN(N: 32, C: TempMod)); |
521 | Shl30 = Builder.CreateAdd( |
522 | LHS: Add, RHS: Builder.getIntN(N: 32, C: ((1 << (30 - TempMod)) - 1) << TempMod)); |
523 | And34 = Builder.CreateAnd(LHS: FloatWidth > 32 ? AAddr1Off32 : AAddr1Off0, |
524 | RHS: Builder.getIntN(N: 32, C: (1 << TempMod) - 1)); |
525 | } |
526 | Value *Or35 = nullptr; |
527 | if (FloatWidth > 80) { |
528 | Value *And29Trunc = Builder.CreateTrunc(V: And29, DestTy: Builder.getIntNTy(N: 128)); |
529 | Value *Or31 = Builder.CreateOr(LHS: And29Trunc, RHS: And34); |
530 | Value *Or34 = Builder.CreateShl(LHS: Or31, RHS: Builder.getIntN(N: 128, C: 64)); |
531 | Value *Temp3 = Builder.CreateShl(LHS: Builder.getIntN(N: 128, C: 1), |
532 | RHS: Builder.getIntN(N: 128, C: FPMantissaWidth)); |
533 | Value *Temp4 = Builder.CreateSub(LHS: Temp3, RHS: Builder.getIntN(N: 128, C: 1)); |
534 | Value *A6 = Builder.CreateAnd(LHS: AAddr1Off0, RHS: Temp4); |
535 | Or35 = Builder.CreateOr(LHS: Or34, RHS: A6); |
536 | } else { |
537 | Value *Or31 = Builder.CreateOr(LHS: And34, RHS: And29); |
538 | Or35 = Builder.CreateOr(LHS: IsSigned ? Or31 : And34, RHS: Shl30); |
539 | } |
540 | Value *A4 = nullptr; |
541 | if (IToFP->getType()->isDoubleTy()) { |
542 | Value *ZExt1 = Builder.CreateZExt(V: Or35, DestTy: Builder.getIntNTy(N: FloatWidth)); |
543 | Value *Shl1 = Builder.CreateShl(LHS: ZExt1, RHS: Builder.getIntN(N: FloatWidth, C: 32)); |
544 | Value *And1 = |
545 | Builder.CreateAnd(LHS: AAddr1Off0, RHS: Builder.getIntN(N: FloatWidth, C: 0xFFFFFFFF)); |
546 | Value *Or1 = Builder.CreateOr(LHS: Shl1, RHS: And1); |
547 | A4 = Builder.CreateBitCast(V: Or1, DestTy: IToFP->getType()); |
548 | } else if (IToFP->getType()->isX86_FP80Ty()) { |
549 | Value *A40 = |
550 | Builder.CreateBitCast(V: Or35, DestTy: Type::getFP128Ty(C&: Builder.getContext())); |
551 | A4 = Builder.CreateFPTrunc(V: A40, DestTy: IToFP->getType()); |
552 | } else if (IToFP->getType()->isHalfTy() || IToFP->getType()->isBFloatTy()) { |
553 | // Deal with "half" situation. This is a workaround since we don't have |
554 | // floattihf.c currently as referring. |
555 | Value *A40 = |
556 | Builder.CreateBitCast(V: Or35, DestTy: Type::getFloatTy(C&: Builder.getContext())); |
557 | A4 = Builder.CreateFPTrunc(V: A40, DestTy: IToFP->getType()); |
558 | } else // float type |
559 | A4 = Builder.CreateBitCast(V: Or35, DestTy: IToFP->getType()); |
560 | Builder.CreateBr(Dest: End); |
561 | |
562 | // return: |
563 | Builder.SetInsertPoint(TheBB: End, IP: End->begin()); |
564 | PHINode *Retval0 = Builder.CreatePHI(Ty: IToFP->getType(), NumReservedValues: 2); |
565 | Retval0->addIncoming(V: A4, BB: IfEnd26); |
566 | Retval0->addIncoming(V: ConstantFP::getZero(Ty: IToFP->getType(), Negative: false), BB: Entry); |
567 | |
568 | IToFP->replaceAllUsesWith(V: Retval0); |
569 | IToFP->dropAllReferences(); |
570 | IToFP->eraseFromParent(); |
571 | } |
572 | |
573 | static void scalarize(Instruction *I, SmallVectorImpl<Instruction *> &Replace) { |
574 | VectorType *VTy = cast<FixedVectorType>(Val: I->getType()); |
575 | |
576 | IRBuilder<> Builder(I); |
577 | |
578 | unsigned NumElements = VTy->getElementCount().getFixedValue(); |
579 | Value *Result = PoisonValue::get(T: VTy); |
580 | for (unsigned Idx = 0; Idx < NumElements; ++Idx) { |
581 | Value *Ext = Builder.CreateExtractElement(Vec: I->getOperand(i: 0), Idx); |
582 | Value *Cast = Builder.CreateCast(Op: cast<CastInst>(Val: I)->getOpcode(), V: Ext, |
583 | DestTy: I->getType()->getScalarType()); |
584 | Result = Builder.CreateInsertElement(Vec: Result, NewElt: Cast, Idx); |
585 | if (isa<Instruction>(Val: Cast)) |
586 | Replace.push_back(Elt: cast<Instruction>(Val: Cast)); |
587 | } |
588 | I->replaceAllUsesWith(V: Result); |
589 | I->dropAllReferences(); |
590 | I->eraseFromParent(); |
591 | } |
592 | |
593 | static bool runImpl(Function &F, const TargetLowering &TLI) { |
594 | SmallVector<Instruction *, 4> Replace; |
595 | SmallVector<Instruction *, 4> ReplaceVector; |
596 | bool Modified = false; |
597 | |
598 | unsigned MaxLegalFpConvertBitWidth = |
599 | TLI.getMaxLargeFPConvertBitWidthSupported(); |
600 | if (ExpandFpConvertBits != llvm::IntegerType::MAX_INT_BITS) |
601 | MaxLegalFpConvertBitWidth = ExpandFpConvertBits; |
602 | |
603 | if (MaxLegalFpConvertBitWidth >= llvm::IntegerType::MAX_INT_BITS) |
604 | return false; |
605 | |
606 | for (auto &I : instructions(F)) { |
607 | switch (I.getOpcode()) { |
608 | case Instruction::FPToUI: |
609 | case Instruction::FPToSI: { |
610 | // TODO: This pass doesn't handle scalable vectors. |
611 | if (I.getOperand(i: 0)->getType()->isScalableTy()) |
612 | continue; |
613 | |
614 | auto *IntTy = cast<IntegerType>(Val: I.getType()->getScalarType()); |
615 | if (IntTy->getIntegerBitWidth() <= MaxLegalFpConvertBitWidth) |
616 | continue; |
617 | |
618 | if (I.getOperand(i: 0)->getType()->isVectorTy()) |
619 | ReplaceVector.push_back(Elt: &I); |
620 | else |
621 | Replace.push_back(Elt: &I); |
622 | Modified = true; |
623 | break; |
624 | } |
625 | case Instruction::UIToFP: |
626 | case Instruction::SIToFP: { |
627 | // TODO: This pass doesn't handle scalable vectors. |
628 | if (I.getOperand(i: 0)->getType()->isScalableTy()) |
629 | continue; |
630 | |
631 | auto *IntTy = |
632 | cast<IntegerType>(Val: I.getOperand(i: 0)->getType()->getScalarType()); |
633 | if (IntTy->getIntegerBitWidth() <= MaxLegalFpConvertBitWidth) |
634 | continue; |
635 | |
636 | if (I.getOperand(i: 0)->getType()->isVectorTy()) |
637 | ReplaceVector.push_back(Elt: &I); |
638 | else |
639 | Replace.push_back(Elt: &I); |
640 | Modified = true; |
641 | break; |
642 | } |
643 | default: |
644 | break; |
645 | } |
646 | } |
647 | |
648 | while (!ReplaceVector.empty()) { |
649 | Instruction *I = ReplaceVector.pop_back_val(); |
650 | scalarize(I, Replace); |
651 | } |
652 | |
653 | if (Replace.empty()) |
654 | return false; |
655 | |
656 | while (!Replace.empty()) { |
657 | Instruction *I = Replace.pop_back_val(); |
658 | if (I->getOpcode() == Instruction::FPToUI || |
659 | I->getOpcode() == Instruction::FPToSI) { |
660 | expandFPToI(FPToI: I); |
661 | } else { |
662 | expandIToFP(IToFP: I); |
663 | } |
664 | } |
665 | |
666 | return Modified; |
667 | } |
668 | |
669 | namespace { |
670 | class ExpandLargeFpConvertLegacyPass : public FunctionPass { |
671 | public: |
672 | static char ID; |
673 | |
674 | ExpandLargeFpConvertLegacyPass() : FunctionPass(ID) { |
675 | initializeExpandLargeFpConvertLegacyPassPass( |
676 | *PassRegistry::getPassRegistry()); |
677 | } |
678 | |
679 | bool runOnFunction(Function &F) override { |
680 | auto *TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); |
681 | auto *TLI = TM->getSubtargetImpl(F)->getTargetLowering(); |
682 | return runImpl(F, TLI: *TLI); |
683 | } |
684 | |
685 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
686 | AU.addRequired<TargetPassConfig>(); |
687 | AU.addPreserved<AAResultsWrapperPass>(); |
688 | AU.addPreserved<GlobalsAAWrapperPass>(); |
689 | } |
690 | }; |
691 | } // namespace |
692 | |
693 | PreservedAnalyses ExpandLargeFpConvertPass::run(Function &F, |
694 | FunctionAnalysisManager &FAM) { |
695 | const TargetSubtargetInfo *STI = TM->getSubtargetImpl(F); |
696 | return runImpl(F, TLI: *STI->getTargetLowering()) ? PreservedAnalyses::none() |
697 | : PreservedAnalyses::all(); |
698 | } |
699 | |
700 | char ExpandLargeFpConvertLegacyPass::ID = 0; |
701 | INITIALIZE_PASS_BEGIN(ExpandLargeFpConvertLegacyPass, "expand-large-fp-convert" , |
702 | "Expand large fp convert" , false, false) |
703 | INITIALIZE_PASS_END(ExpandLargeFpConvertLegacyPass, "expand-large-fp-convert" , |
704 | "Expand large fp convert" , false, false) |
705 | |
706 | FunctionPass *llvm::createExpandLargeFpConvertPass() { |
707 | return new ExpandLargeFpConvertLegacyPass(); |
708 | } |
709 | |