blob: b312dbc8d14d65b32b9b651bfe73fec2ef6677c4 [file] [log] [blame]
Matt Arsenault86de4862016-06-24 07:07:55 +00001//===-- AMDGPUCodeGenPrepare.cpp ------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// This pass does misc. AMDGPU optimizations on IR before instruction
12/// selection.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AMDGPU.h"
17#include "AMDGPUSubtarget.h"
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +000018#include "AMDGPUTargetMachine.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000019#include "llvm/ADT/StringRef.h"
Matt Arsenault86de4862016-06-24 07:07:55 +000020#include "llvm/Analysis/DivergenceAnalysis.h"
21#include "llvm/CodeGen/Passes.h"
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +000022#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000023#include "llvm/IR/Attributes.h"
24#include "llvm/IR/BasicBlock.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000028#include "llvm/IR/IRBuilder.h"
29#include "llvm/IR/InstVisitor.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000030#include "llvm/IR/InstrTypes.h"
31#include "llvm/IR/Instruction.h"
32#include "llvm/IR/Instructions.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000033#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/Intrinsics.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000035#include "llvm/IR/LLVMContext.h"
36#include "llvm/IR/Operator.h"
37#include "llvm/IR/Type.h"
38#include "llvm/IR/Value.h"
39#include "llvm/Pass.h"
40#include "llvm/Support/Casting.h"
41#include <cassert>
42#include <iterator>
Matt Arsenault86de4862016-06-24 07:07:55 +000043
44#define DEBUG_TYPE "amdgpu-codegenprepare"
45
46using namespace llvm;
47
48namespace {
49
50class AMDGPUCodeGenPrepare : public FunctionPass,
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +000051 public InstVisitor<AMDGPUCodeGenPrepare, bool> {
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000052 const SISubtarget *ST = nullptr;
53 DivergenceAnalysis *DA = nullptr;
54 Module *Mod = nullptr;
55 bool HasUnsafeFPMath = false;
Matt Arsenault86de4862016-06-24 07:07:55 +000056
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000057 /// \brief Copies exact/nsw/nuw flags (if any) from binary operation \p I to
58 /// binary operation \p V.
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000059 ///
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000060 /// \returns Binary operation \p V.
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000061 /// \returns \p T's base element bit width.
62 unsigned getBaseElementBitWidth(const Type *T) const;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000063
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000064 /// \returns Equivalent 32 bit integer type for given type \p T. For example,
65 /// if \p T is i7, then i32 is returned; if \p T is <3 x i12>, then <3 x i32>
66 /// is returned.
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000067 Type *getI32Ty(IRBuilder<> &B, const Type *T) const;
68
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000069 /// \returns True if binary operation \p I is a signed binary operation, false
70 /// otherwise.
71 bool isSigned(const BinaryOperator &I) const;
72
73 /// \returns True if the condition of 'select' operation \p I comes from a
74 /// signed 'icmp' operation, false otherwise.
75 bool isSigned(const SelectInst &I) const;
76
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000077 /// \returns True if type \p T needs to be promoted to 32 bit integer type,
78 /// false otherwise.
79 bool needsPromotionToI32(const Type *T) const;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000080
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000081 /// \brief Promotes uniform binary operation \p I to equivalent 32 bit binary
82 /// operation.
83 ///
84 /// \details \p I's base element bit width must be greater than 1 and less
85 /// than or equal 16. Promotion is done by sign or zero extending operands to
86 /// 32 bits, replacing \p I with equivalent 32 bit binary operation, and
87 /// truncating the result of 32 bit binary operation back to \p I's original
88 /// type. Division operation is not promoted.
89 ///
90 /// \returns True if \p I is promoted to equivalent 32 bit binary operation,
91 /// false otherwise.
92 bool promoteUniformOpToI32(BinaryOperator &I) const;
93
94 /// \brief Promotes uniform 'icmp' operation \p I to 32 bit 'icmp' operation.
95 ///
96 /// \details \p I's base element bit width must be greater than 1 and less
97 /// than or equal 16. Promotion is done by sign or zero extending operands to
98 /// 32 bits, and replacing \p I with 32 bit 'icmp' operation.
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000099 ///
100 /// \returns True.
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000101 bool promoteUniformOpToI32(ICmpInst &I) const;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000102
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000103 /// \brief Promotes uniform 'select' operation \p I to 32 bit 'select'
104 /// operation.
105 ///
106 /// \details \p I's base element bit width must be greater than 1 and less
107 /// than or equal 16. Promotion is done by sign or zero extending operands to
108 /// 32 bits, replacing \p I with 32 bit 'select' operation, and truncating the
109 /// result of 32 bit 'select' operation back to \p I's original type.
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000110 ///
111 /// \returns True.
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000112 bool promoteUniformOpToI32(SelectInst &I) const;
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000113
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000114 /// \brief Promotes uniform 'bitreverse' intrinsic \p I to 32 bit 'bitreverse'
115 /// intrinsic.
116 ///
117 /// \details \p I's base element bit width must be greater than 1 and less
118 /// than or equal 16. Promotion is done by zero extending the operand to 32
119 /// bits, replacing \p I with 32 bit 'bitreverse' intrinsic, shifting the
120 /// result of 32 bit 'bitreverse' intrinsic to the right with zero fill (the
121 /// shift amount is 32 minus \p I's base element bit width), and truncating
122 /// the result of the shift operation back to \p I's original type.
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000123 ///
124 /// \returns True.
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000125 bool promoteUniformBitreverseToI32(IntrinsicInst &I) const;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000126
Matt Arsenault86de4862016-06-24 07:07:55 +0000127public:
128 static char ID;
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000129
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000130 AMDGPUCodeGenPrepare() : FunctionPass(ID) {}
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000131
132 bool visitFDiv(BinaryOperator &I);
133
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000134 bool visitInstruction(Instruction &I) { return false; }
135 bool visitBinaryOperator(BinaryOperator &I);
136 bool visitICmpInst(ICmpInst &I);
137 bool visitSelectInst(SelectInst &I);
Matt Arsenault86de4862016-06-24 07:07:55 +0000138
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000139 bool visitIntrinsicInst(IntrinsicInst &I);
140 bool visitBitreverseIntrinsicInst(IntrinsicInst &I);
141
Matt Arsenault86de4862016-06-24 07:07:55 +0000142 bool doInitialization(Module &M) override;
143 bool runOnFunction(Function &F) override;
144
Mehdi Amini117296c2016-10-01 02:56:57 +0000145 StringRef getPassName() const override { return "AMDGPU IR optimizations"; }
Matt Arsenault86de4862016-06-24 07:07:55 +0000146
147 void getAnalysisUsage(AnalysisUsage &AU) const override {
148 AU.addRequired<DivergenceAnalysis>();
149 AU.setPreservesAll();
150 }
151};
152
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000153} // end anonymous namespace
Matt Arsenault86de4862016-06-24 07:07:55 +0000154
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000155unsigned AMDGPUCodeGenPrepare::getBaseElementBitWidth(const Type *T) const {
156 assert(needsPromotionToI32(T) && "T does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000157
158 if (T->isIntegerTy())
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000159 return T->getIntegerBitWidth();
160 return cast<VectorType>(T)->getElementType()->getIntegerBitWidth();
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000161}
162
163Type *AMDGPUCodeGenPrepare::getI32Ty(IRBuilder<> &B, const Type *T) const {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000164 assert(needsPromotionToI32(T) && "T does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000165
166 if (T->isIntegerTy())
167 return B.getInt32Ty();
168 return VectorType::get(B.getInt32Ty(), cast<VectorType>(T)->getNumElements());
169}
170
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000171bool AMDGPUCodeGenPrepare::isSigned(const BinaryOperator &I) const {
Konstantin Zhuravlyov691e2e02016-10-03 18:29:01 +0000172 return I.getOpcode() == Instruction::AShr ||
173 I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::SRem;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000174}
175
176bool AMDGPUCodeGenPrepare::isSigned(const SelectInst &I) const {
177 return isa<ICmpInst>(I.getOperand(0)) ?
178 cast<ICmpInst>(I.getOperand(0))->isSigned() : false;
179}
180
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000181bool AMDGPUCodeGenPrepare::needsPromotionToI32(const Type *T) const {
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000182 const IntegerType *IntTy = dyn_cast<IntegerType>(T);
183 if (IntTy && IntTy->getBitWidth() > 1 && IntTy->getBitWidth() <= 16)
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000184 return true;
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000185
186 if (const VectorType *VT = dyn_cast<VectorType>(T)) {
187 // TODO: The set of packed operations is more limited, so may want to
188 // promote some anyway.
189 if (ST->hasVOP3PInsts())
190 return false;
191
192 return needsPromotionToI32(VT->getElementType());
193 }
194
195 return false;
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000196}
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000197
Matt Arsenaultd59e6402017-02-01 16:25:23 +0000198// Return true if the op promoted to i32 should have nsw set.
199static bool promotedOpIsNSW(const Instruction &I) {
200 switch (I.getOpcode()) {
201 case Instruction::Shl:
202 case Instruction::Add:
203 case Instruction::Sub:
204 return true;
205 case Instruction::Mul:
206 return I.hasNoUnsignedWrap();
207 default:
208 return false;
209 }
210}
211
212// Return true if the op promoted to i32 should have nuw set.
213static bool promotedOpIsNUW(const Instruction &I) {
214 switch (I.getOpcode()) {
215 case Instruction::Shl:
216 case Instruction::Add:
217 case Instruction::Mul:
218 return true;
219 case Instruction::Sub:
220 return I.hasNoUnsignedWrap();
221 default:
222 return false;
223 }
224}
225
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000226bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(BinaryOperator &I) const {
227 assert(needsPromotionToI32(I.getType()) &&
228 "I does not need promotion to i32");
229
230 if (I.getOpcode() == Instruction::SDiv ||
231 I.getOpcode() == Instruction::UDiv)
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000232 return false;
233
234 IRBuilder<> Builder(&I);
235 Builder.SetCurrentDebugLocation(I.getDebugLoc());
236
237 Type *I32Ty = getI32Ty(Builder, I.getType());
238 Value *ExtOp0 = nullptr;
239 Value *ExtOp1 = nullptr;
240 Value *ExtRes = nullptr;
241 Value *TruncRes = nullptr;
242
243 if (isSigned(I)) {
244 ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
245 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
246 } else {
247 ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
248 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
249 }
Matt Arsenaultd59e6402017-02-01 16:25:23 +0000250
251 ExtRes = Builder.CreateBinOp(I.getOpcode(), ExtOp0, ExtOp1);
252 if (Instruction *Inst = dyn_cast<Instruction>(ExtRes)) {
253 if (promotedOpIsNSW(cast<Instruction>(I)))
254 Inst->setHasNoSignedWrap();
255
256 if (promotedOpIsNUW(cast<Instruction>(I)))
257 Inst->setHasNoUnsignedWrap();
258
259 if (const auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
260 Inst->setIsExact(ExactOp->isExact());
261 }
262
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000263 TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000264
265 I.replaceAllUsesWith(TruncRes);
266 I.eraseFromParent();
267
268 return true;
269}
270
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000271bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(ICmpInst &I) const {
272 assert(needsPromotionToI32(I.getOperand(0)->getType()) &&
273 "I does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000274
275 IRBuilder<> Builder(&I);
276 Builder.SetCurrentDebugLocation(I.getDebugLoc());
277
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000278 Type *I32Ty = getI32Ty(Builder, I.getOperand(0)->getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000279 Value *ExtOp0 = nullptr;
280 Value *ExtOp1 = nullptr;
281 Value *NewICmp = nullptr;
282
283 if (I.isSigned()) {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000284 ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
285 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000286 } else {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000287 ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
288 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000289 }
290 NewICmp = Builder.CreateICmp(I.getPredicate(), ExtOp0, ExtOp1);
291
292 I.replaceAllUsesWith(NewICmp);
293 I.eraseFromParent();
294
295 return true;
296}
297
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000298bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(SelectInst &I) const {
299 assert(needsPromotionToI32(I.getType()) &&
300 "I does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000301
302 IRBuilder<> Builder(&I);
303 Builder.SetCurrentDebugLocation(I.getDebugLoc());
304
305 Type *I32Ty = getI32Ty(Builder, I.getType());
306 Value *ExtOp1 = nullptr;
307 Value *ExtOp2 = nullptr;
308 Value *ExtRes = nullptr;
309 Value *TruncRes = nullptr;
310
311 if (isSigned(I)) {
312 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
313 ExtOp2 = Builder.CreateSExt(I.getOperand(2), I32Ty);
314 } else {
315 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
316 ExtOp2 = Builder.CreateZExt(I.getOperand(2), I32Ty);
317 }
318 ExtRes = Builder.CreateSelect(I.getOperand(0), ExtOp1, ExtOp2);
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000319 TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000320
321 I.replaceAllUsesWith(TruncRes);
322 I.eraseFromParent();
323
324 return true;
325}
326
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000327bool AMDGPUCodeGenPrepare::promoteUniformBitreverseToI32(
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000328 IntrinsicInst &I) const {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000329 assert(I.getIntrinsicID() == Intrinsic::bitreverse &&
330 "I must be bitreverse intrinsic");
331 assert(needsPromotionToI32(I.getType()) &&
332 "I does not need promotion to i32");
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000333
334 IRBuilder<> Builder(&I);
335 Builder.SetCurrentDebugLocation(I.getDebugLoc());
336
337 Type *I32Ty = getI32Ty(Builder, I.getType());
338 Function *I32 =
Konstantin Zhuravlyovc09e2d72016-10-07 14:39:53 +0000339 Intrinsic::getDeclaration(Mod, Intrinsic::bitreverse, { I32Ty });
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000340 Value *ExtOp = Builder.CreateZExt(I.getOperand(0), I32Ty);
341 Value *ExtRes = Builder.CreateCall(I32, { ExtOp });
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000342 Value *LShrOp =
343 Builder.CreateLShr(ExtRes, 32 - getBaseElementBitWidth(I.getType()));
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000344 Value *TruncRes =
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000345 Builder.CreateTrunc(LShrOp, I.getType());
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000346
347 I.replaceAllUsesWith(TruncRes);
348 I.eraseFromParent();
349
350 return true;
351}
352
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000353static bool shouldKeepFDivF32(Value *Num, bool UnsafeDiv) {
354 const ConstantFP *CNum = dyn_cast<ConstantFP>(Num);
355 if (!CNum)
356 return false;
357
358 // Reciprocal f32 is handled separately without denormals.
Matt Arsenaulte3862cd2016-07-26 23:25:44 +0000359 return UnsafeDiv || CNum->isExactlyValue(+1.0);
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000360}
361
362// Insert an intrinsic for fast fdiv for safe math situations where we can
363// reduce precision. Leave fdiv for situations where the generic node is
364// expected to be optimized.
365bool AMDGPUCodeGenPrepare::visitFDiv(BinaryOperator &FDiv) {
366 Type *Ty = FDiv.getType();
367
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000368 if (!Ty->getScalarType()->isFloatTy())
369 return false;
370
371 MDNode *FPMath = FDiv.getMetadata(LLVMContext::MD_fpmath);
372 if (!FPMath)
373 return false;
374
375 const FPMathOperator *FPOp = cast<const FPMathOperator>(&FDiv);
376 float ULP = FPOp->getFPAccuracy();
377 if (ULP < 2.5f)
378 return false;
379
380 FastMathFlags FMF = FPOp->getFastMathFlags();
381 bool UnsafeDiv = HasUnsafeFPMath || FMF.unsafeAlgebra() ||
382 FMF.allowReciprocal();
383 if (ST->hasFP32Denormals() && !UnsafeDiv)
384 return false;
385
386 IRBuilder<> Builder(FDiv.getParent(), std::next(FDiv.getIterator()), FPMath);
387 Builder.setFastMathFlags(FMF);
388 Builder.SetCurrentDebugLocation(FDiv.getDebugLoc());
389
Matt Arsenaultc5b641a2017-03-17 20:41:45 +0000390 Function *Decl = Intrinsic::getDeclaration(Mod, Intrinsic::amdgcn_fdiv_fast);
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000391
392 Value *Num = FDiv.getOperand(0);
393 Value *Den = FDiv.getOperand(1);
394
395 Value *NewFDiv = nullptr;
396
397 if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
398 NewFDiv = UndefValue::get(VT);
399
400 // FIXME: Doesn't do the right thing for cases where the vector is partially
401 // constant. This works when the scalarizer pass is run first.
402 for (unsigned I = 0, E = VT->getNumElements(); I != E; ++I) {
403 Value *NumEltI = Builder.CreateExtractElement(Num, I);
404 Value *DenEltI = Builder.CreateExtractElement(Den, I);
405 Value *NewElt;
406
407 if (shouldKeepFDivF32(NumEltI, UnsafeDiv)) {
408 NewElt = Builder.CreateFDiv(NumEltI, DenEltI);
409 } else {
410 NewElt = Builder.CreateCall(Decl, { NumEltI, DenEltI });
411 }
412
413 NewFDiv = Builder.CreateInsertElement(NewFDiv, NewElt, I);
414 }
415 } else {
416 if (!shouldKeepFDivF32(Num, UnsafeDiv))
417 NewFDiv = Builder.CreateCall(Decl, { Num, Den });
418 }
419
420 if (NewFDiv) {
421 FDiv.replaceAllUsesWith(NewFDiv);
422 NewFDiv->takeName(&FDiv);
423 FDiv.eraseFromParent();
424 }
425
426 return true;
427}
428
429static bool hasUnsafeFPMath(const Function &F) {
430 Attribute Attr = F.getFnAttribute("unsafe-fp-math");
431 return Attr.getValueAsString() == "true";
432}
433
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000434bool AMDGPUCodeGenPrepare::visitBinaryOperator(BinaryOperator &I) {
435 bool Changed = false;
436
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000437 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
438 DA->isUniform(&I))
439 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000440
441 return Changed;
442}
443
444bool AMDGPUCodeGenPrepare::visitICmpInst(ICmpInst &I) {
445 bool Changed = false;
446
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000447 if (ST->has16BitInsts() && needsPromotionToI32(I.getOperand(0)->getType()) &&
448 DA->isUniform(&I))
449 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000450
451 return Changed;
452}
453
454bool AMDGPUCodeGenPrepare::visitSelectInst(SelectInst &I) {
455 bool Changed = false;
456
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000457 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
458 DA->isUniform(&I))
459 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000460
461 return Changed;
462}
463
464bool AMDGPUCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
465 switch (I.getIntrinsicID()) {
466 case Intrinsic::bitreverse:
467 return visitBitreverseIntrinsicInst(I);
468 default:
469 return false;
470 }
471}
472
473bool AMDGPUCodeGenPrepare::visitBitreverseIntrinsicInst(IntrinsicInst &I) {
474 bool Changed = false;
475
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000476 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
477 DA->isUniform(&I))
478 Changed |= promoteUniformBitreverseToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000479
480 return Changed;
481}
482
Matt Arsenault86de4862016-06-24 07:07:55 +0000483bool AMDGPUCodeGenPrepare::doInitialization(Module &M) {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000484 Mod = &M;
Matt Arsenault86de4862016-06-24 07:07:55 +0000485 return false;
486}
487
488bool AMDGPUCodeGenPrepare::runOnFunction(Function &F) {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000489 if (skipFunction(F))
Matt Arsenault86de4862016-06-24 07:07:55 +0000490 return false;
491
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000492 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
493 if (!TPC)
494 return false;
495
496 const TargetMachine &TM = TPC->getTM<TargetMachine>();
497 ST = &TM.getSubtarget<SISubtarget>(F);
Matt Arsenault86de4862016-06-24 07:07:55 +0000498 DA = &getAnalysis<DivergenceAnalysis>();
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000499 HasUnsafeFPMath = hasUnsafeFPMath(F);
Matt Arsenault86de4862016-06-24 07:07:55 +0000500
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000501 bool MadeChange = false;
502
503 for (BasicBlock &BB : F) {
504 BasicBlock::iterator Next;
505 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; I = Next) {
506 Next = std::next(I);
507 MadeChange |= visit(*I);
508 }
509 }
510
511 return MadeChange;
Matt Arsenault86de4862016-06-24 07:07:55 +0000512}
513
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000514INITIALIZE_PASS_BEGIN(AMDGPUCodeGenPrepare, DEBUG_TYPE,
Matt Arsenault86de4862016-06-24 07:07:55 +0000515 "AMDGPU IR optimizations", false, false)
516INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000517INITIALIZE_PASS_END(AMDGPUCodeGenPrepare, DEBUG_TYPE, "AMDGPU IR optimizations",
518 false, false)
Matt Arsenault86de4862016-06-24 07:07:55 +0000519
520char AMDGPUCodeGenPrepare::ID = 0;
521
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000522FunctionPass *llvm::createAMDGPUCodeGenPreparePass() {
523 return new AMDGPUCodeGenPrepare();
Matt Arsenault86de4862016-06-24 07:07:55 +0000524}