blob: 23f124b637ff02cf1ba7089d87d3a4a03f7c62e5 [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"
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +000017#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault86de4862016-06-24 07:07:55 +000018#include "AMDGPUSubtarget.h"
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +000019#include "AMDGPUTargetMachine.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000020#include "llvm/ADT/StringRef.h"
Matt Arsenault86de4862016-06-24 07:07:55 +000021#include "llvm/Analysis/DivergenceAnalysis.h"
22#include "llvm/CodeGen/Passes.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"
28#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
30#include "llvm/IR/Instructions.h"
Matt Arsenault86de4862016-06-24 07:07:55 +000031#include "llvm/IR/InstVisitor.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000032#include "llvm/IR/IntrinsicInst.h"
33#include "llvm/IR/Intrinsics.h"
Matt Arsenault86de4862016-06-24 07:07:55 +000034#include "llvm/IR/IRBuilder.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> {
52 const GCNTargetMachine *TM;
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000053 const SISubtarget *ST = nullptr;
54 DivergenceAnalysis *DA = nullptr;
55 Module *Mod = nullptr;
56 bool HasUnsafeFPMath = false;
Matt Arsenault86de4862016-06-24 07:07:55 +000057
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000058 /// \brief Copies exact/nsw/nuw flags (if any) from binary operation \p I to
59 /// binary operation \p V.
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000060 ///
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000061 /// \returns Binary operation \p V.
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000062 /// \returns \p T's base element bit width.
63 unsigned getBaseElementBitWidth(const Type *T) const;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000064
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000065 /// \returns Equivalent 32 bit integer type for given type \p T. For example,
66 /// if \p T is i7, then i32 is returned; if \p T is <3 x i12>, then <3 x i32>
67 /// is returned.
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000068 Type *getI32Ty(IRBuilder<> &B, const Type *T) const;
69
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000070 /// \returns True if binary operation \p I is a signed binary operation, false
71 /// otherwise.
72 bool isSigned(const BinaryOperator &I) const;
73
74 /// \returns True if the condition of 'select' operation \p I comes from a
75 /// signed 'icmp' operation, false otherwise.
76 bool isSigned(const SelectInst &I) const;
77
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000078 /// \returns True if type \p T needs to be promoted to 32 bit integer type,
79 /// false otherwise.
80 bool needsPromotionToI32(const Type *T) const;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +000081
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +000082 /// \brief Promotes uniform binary operation \p I to equivalent 32 bit binary
83 /// operation.
84 ///
85 /// \details \p I's base element bit width must be greater than 1 and less
86 /// than or equal 16. Promotion is done by sign or zero extending operands to
87 /// 32 bits, replacing \p I with equivalent 32 bit binary operation, and
88 /// truncating the result of 32 bit binary operation back to \p I's original
89 /// type. Division operation is not promoted.
90 ///
91 /// \returns True if \p I is promoted to equivalent 32 bit binary operation,
92 /// false otherwise.
93 bool promoteUniformOpToI32(BinaryOperator &I) const;
94
95 /// \brief Promotes uniform 'icmp' operation \p I to 32 bit 'icmp' operation.
96 ///
97 /// \details \p I's base element bit width must be greater than 1 and less
98 /// than or equal 16. Promotion is done by sign or zero extending operands to
99 /// 32 bits, and replacing \p I with 32 bit 'icmp' operation.
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000100 ///
101 /// \returns True.
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000102 bool promoteUniformOpToI32(ICmpInst &I) const;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000103
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000104 /// \brief Promotes uniform 'select' operation \p I to 32 bit 'select'
105 /// operation.
106 ///
107 /// \details \p I's base element bit width must be greater than 1 and less
108 /// than or equal 16. Promotion is done by sign or zero extending operands to
109 /// 32 bits, replacing \p I with 32 bit 'select' operation, and truncating the
110 /// result of 32 bit 'select' operation back to \p I's original type.
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000111 ///
112 /// \returns True.
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000113 bool promoteUniformOpToI32(SelectInst &I) const;
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000114
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000115 /// \brief Promotes uniform 'bitreverse' intrinsic \p I to 32 bit 'bitreverse'
116 /// intrinsic.
117 ///
118 /// \details \p I's base element bit width must be greater than 1 and less
119 /// than or equal 16. Promotion is done by zero extending the operand to 32
120 /// bits, replacing \p I with 32 bit 'bitreverse' intrinsic, shifting the
121 /// result of 32 bit 'bitreverse' intrinsic to the right with zero fill (the
122 /// shift amount is 32 minus \p I's base element bit width), and truncating
123 /// the result of the shift operation back to \p I's original type.
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000124 ///
125 /// \returns True.
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000126 bool promoteUniformBitreverseToI32(IntrinsicInst &I) const;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000127
Matt Arsenault86de4862016-06-24 07:07:55 +0000128public:
129 static char ID;
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000130
Matt Arsenault86de4862016-06-24 07:07:55 +0000131 AMDGPUCodeGenPrepare(const TargetMachine *TM = nullptr) :
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000132 FunctionPass(ID), TM(static_cast<const GCNTargetMachine *>(TM)) {}
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000133
134 bool visitFDiv(BinaryOperator &I);
135
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000136 bool visitInstruction(Instruction &I) { return false; }
137 bool visitBinaryOperator(BinaryOperator &I);
138 bool visitICmpInst(ICmpInst &I);
139 bool visitSelectInst(SelectInst &I);
Matt Arsenault86de4862016-06-24 07:07:55 +0000140
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000141 bool visitIntrinsicInst(IntrinsicInst &I);
142 bool visitBitreverseIntrinsicInst(IntrinsicInst &I);
143
Matt Arsenault86de4862016-06-24 07:07:55 +0000144 bool doInitialization(Module &M) override;
145 bool runOnFunction(Function &F) override;
146
Mehdi Amini117296c2016-10-01 02:56:57 +0000147 StringRef getPassName() const override { return "AMDGPU IR optimizations"; }
Matt Arsenault86de4862016-06-24 07:07:55 +0000148
149 void getAnalysisUsage(AnalysisUsage &AU) const override {
150 AU.addRequired<DivergenceAnalysis>();
151 AU.setPreservesAll();
152 }
153};
154
Eugene Zelenko734bb7b2017-01-20 17:52:16 +0000155} // end anonymous namespace
Matt Arsenault86de4862016-06-24 07:07:55 +0000156
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000157unsigned AMDGPUCodeGenPrepare::getBaseElementBitWidth(const Type *T) const {
158 assert(needsPromotionToI32(T) && "T does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000159
160 if (T->isIntegerTy())
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000161 return T->getIntegerBitWidth();
162 return cast<VectorType>(T)->getElementType()->getIntegerBitWidth();
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000163}
164
165Type *AMDGPUCodeGenPrepare::getI32Ty(IRBuilder<> &B, const Type *T) const {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000166 assert(needsPromotionToI32(T) && "T does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000167
168 if (T->isIntegerTy())
169 return B.getInt32Ty();
170 return VectorType::get(B.getInt32Ty(), cast<VectorType>(T)->getNumElements());
171}
172
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000173bool AMDGPUCodeGenPrepare::isSigned(const BinaryOperator &I) const {
Konstantin Zhuravlyov691e2e02016-10-03 18:29:01 +0000174 return I.getOpcode() == Instruction::AShr ||
175 I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::SRem;
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000176}
177
178bool AMDGPUCodeGenPrepare::isSigned(const SelectInst &I) const {
179 return isa<ICmpInst>(I.getOperand(0)) ?
180 cast<ICmpInst>(I.getOperand(0))->isSigned() : false;
181}
182
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000183bool AMDGPUCodeGenPrepare::needsPromotionToI32(const Type *T) const {
184 if (T->isIntegerTy() && T->getIntegerBitWidth() > 1 &&
185 T->getIntegerBitWidth() <= 16)
186 return true;
187 if (!T->isVectorTy())
188 return false;
189 return needsPromotionToI32(cast<VectorType>(T)->getElementType());
190}
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000191
Matt Arsenaultd59e6402017-02-01 16:25:23 +0000192// Return true if the op promoted to i32 should have nsw set.
193static bool promotedOpIsNSW(const Instruction &I) {
194 switch (I.getOpcode()) {
195 case Instruction::Shl:
196 case Instruction::Add:
197 case Instruction::Sub:
198 return true;
199 case Instruction::Mul:
200 return I.hasNoUnsignedWrap();
201 default:
202 return false;
203 }
204}
205
206// Return true if the op promoted to i32 should have nuw set.
207static bool promotedOpIsNUW(const Instruction &I) {
208 switch (I.getOpcode()) {
209 case Instruction::Shl:
210 case Instruction::Add:
211 case Instruction::Mul:
212 return true;
213 case Instruction::Sub:
214 return I.hasNoUnsignedWrap();
215 default:
216 return false;
217 }
218}
219
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000220bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(BinaryOperator &I) const {
221 assert(needsPromotionToI32(I.getType()) &&
222 "I does not need promotion to i32");
223
224 if (I.getOpcode() == Instruction::SDiv ||
225 I.getOpcode() == Instruction::UDiv)
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000226 return false;
227
228 IRBuilder<> Builder(&I);
229 Builder.SetCurrentDebugLocation(I.getDebugLoc());
230
231 Type *I32Ty = getI32Ty(Builder, I.getType());
232 Value *ExtOp0 = nullptr;
233 Value *ExtOp1 = nullptr;
234 Value *ExtRes = nullptr;
235 Value *TruncRes = nullptr;
236
237 if (isSigned(I)) {
238 ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
239 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
240 } else {
241 ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
242 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
243 }
Matt Arsenaultd59e6402017-02-01 16:25:23 +0000244
245 ExtRes = Builder.CreateBinOp(I.getOpcode(), ExtOp0, ExtOp1);
246 if (Instruction *Inst = dyn_cast<Instruction>(ExtRes)) {
247 if (promotedOpIsNSW(cast<Instruction>(I)))
248 Inst->setHasNoSignedWrap();
249
250 if (promotedOpIsNUW(cast<Instruction>(I)))
251 Inst->setHasNoUnsignedWrap();
252
253 if (const auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
254 Inst->setIsExact(ExactOp->isExact());
255 }
256
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000257 TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000258
259 I.replaceAllUsesWith(TruncRes);
260 I.eraseFromParent();
261
262 return true;
263}
264
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000265bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(ICmpInst &I) const {
266 assert(needsPromotionToI32(I.getOperand(0)->getType()) &&
267 "I does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000268
269 IRBuilder<> Builder(&I);
270 Builder.SetCurrentDebugLocation(I.getDebugLoc());
271
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000272 Type *I32Ty = getI32Ty(Builder, I.getOperand(0)->getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000273 Value *ExtOp0 = nullptr;
274 Value *ExtOp1 = nullptr;
275 Value *NewICmp = nullptr;
276
277 if (I.isSigned()) {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000278 ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
279 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000280 } else {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000281 ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
282 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000283 }
284 NewICmp = Builder.CreateICmp(I.getPredicate(), ExtOp0, ExtOp1);
285
286 I.replaceAllUsesWith(NewICmp);
287 I.eraseFromParent();
288
289 return true;
290}
291
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000292bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(SelectInst &I) const {
293 assert(needsPromotionToI32(I.getType()) &&
294 "I does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000295
296 IRBuilder<> Builder(&I);
297 Builder.SetCurrentDebugLocation(I.getDebugLoc());
298
299 Type *I32Ty = getI32Ty(Builder, I.getType());
300 Value *ExtOp1 = nullptr;
301 Value *ExtOp2 = nullptr;
302 Value *ExtRes = nullptr;
303 Value *TruncRes = nullptr;
304
305 if (isSigned(I)) {
306 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
307 ExtOp2 = Builder.CreateSExt(I.getOperand(2), I32Ty);
308 } else {
309 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
310 ExtOp2 = Builder.CreateZExt(I.getOperand(2), I32Ty);
311 }
312 ExtRes = Builder.CreateSelect(I.getOperand(0), ExtOp1, ExtOp2);
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000313 TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000314
315 I.replaceAllUsesWith(TruncRes);
316 I.eraseFromParent();
317
318 return true;
319}
320
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000321bool AMDGPUCodeGenPrepare::promoteUniformBitreverseToI32(
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000322 IntrinsicInst &I) const {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000323 assert(I.getIntrinsicID() == Intrinsic::bitreverse &&
324 "I must be bitreverse intrinsic");
325 assert(needsPromotionToI32(I.getType()) &&
326 "I does not need promotion to i32");
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000327
328 IRBuilder<> Builder(&I);
329 Builder.SetCurrentDebugLocation(I.getDebugLoc());
330
331 Type *I32Ty = getI32Ty(Builder, I.getType());
332 Function *I32 =
Konstantin Zhuravlyovc09e2d72016-10-07 14:39:53 +0000333 Intrinsic::getDeclaration(Mod, Intrinsic::bitreverse, { I32Ty });
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000334 Value *ExtOp = Builder.CreateZExt(I.getOperand(0), I32Ty);
335 Value *ExtRes = Builder.CreateCall(I32, { ExtOp });
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000336 Value *LShrOp =
337 Builder.CreateLShr(ExtRes, 32 - getBaseElementBitWidth(I.getType()));
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000338 Value *TruncRes =
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000339 Builder.CreateTrunc(LShrOp, I.getType());
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000340
341 I.replaceAllUsesWith(TruncRes);
342 I.eraseFromParent();
343
344 return true;
345}
346
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000347static bool shouldKeepFDivF32(Value *Num, bool UnsafeDiv) {
348 const ConstantFP *CNum = dyn_cast<ConstantFP>(Num);
349 if (!CNum)
350 return false;
351
352 // Reciprocal f32 is handled separately without denormals.
Matt Arsenaulte3862cd2016-07-26 23:25:44 +0000353 return UnsafeDiv || CNum->isExactlyValue(+1.0);
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000354}
355
356// Insert an intrinsic for fast fdiv for safe math situations where we can
357// reduce precision. Leave fdiv for situations where the generic node is
358// expected to be optimized.
359bool AMDGPUCodeGenPrepare::visitFDiv(BinaryOperator &FDiv) {
360 Type *Ty = FDiv.getType();
361
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000362 if (!Ty->getScalarType()->isFloatTy())
363 return false;
364
365 MDNode *FPMath = FDiv.getMetadata(LLVMContext::MD_fpmath);
366 if (!FPMath)
367 return false;
368
369 const FPMathOperator *FPOp = cast<const FPMathOperator>(&FDiv);
370 float ULP = FPOp->getFPAccuracy();
371 if (ULP < 2.5f)
372 return false;
373
374 FastMathFlags FMF = FPOp->getFastMathFlags();
375 bool UnsafeDiv = HasUnsafeFPMath || FMF.unsafeAlgebra() ||
376 FMF.allowReciprocal();
377 if (ST->hasFP32Denormals() && !UnsafeDiv)
378 return false;
379
380 IRBuilder<> Builder(FDiv.getParent(), std::next(FDiv.getIterator()), FPMath);
381 Builder.setFastMathFlags(FMF);
382 Builder.SetCurrentDebugLocation(FDiv.getDebugLoc());
383
384 const AMDGPUIntrinsicInfo *II = TM->getIntrinsicInfo();
385 Function *Decl
386 = II->getDeclaration(Mod, AMDGPUIntrinsic::amdgcn_fdiv_fast, {});
387
388 Value *Num = FDiv.getOperand(0);
389 Value *Den = FDiv.getOperand(1);
390
391 Value *NewFDiv = nullptr;
392
393 if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
394 NewFDiv = UndefValue::get(VT);
395
396 // FIXME: Doesn't do the right thing for cases where the vector is partially
397 // constant. This works when the scalarizer pass is run first.
398 for (unsigned I = 0, E = VT->getNumElements(); I != E; ++I) {
399 Value *NumEltI = Builder.CreateExtractElement(Num, I);
400 Value *DenEltI = Builder.CreateExtractElement(Den, I);
401 Value *NewElt;
402
403 if (shouldKeepFDivF32(NumEltI, UnsafeDiv)) {
404 NewElt = Builder.CreateFDiv(NumEltI, DenEltI);
405 } else {
406 NewElt = Builder.CreateCall(Decl, { NumEltI, DenEltI });
407 }
408
409 NewFDiv = Builder.CreateInsertElement(NewFDiv, NewElt, I);
410 }
411 } else {
412 if (!shouldKeepFDivF32(Num, UnsafeDiv))
413 NewFDiv = Builder.CreateCall(Decl, { Num, Den });
414 }
415
416 if (NewFDiv) {
417 FDiv.replaceAllUsesWith(NewFDiv);
418 NewFDiv->takeName(&FDiv);
419 FDiv.eraseFromParent();
420 }
421
422 return true;
423}
424
425static bool hasUnsafeFPMath(const Function &F) {
426 Attribute Attr = F.getFnAttribute("unsafe-fp-math");
427 return Attr.getValueAsString() == "true";
428}
429
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000430bool AMDGPUCodeGenPrepare::visitBinaryOperator(BinaryOperator &I) {
431 bool Changed = false;
432
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000433 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
434 DA->isUniform(&I))
435 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000436
437 return Changed;
438}
439
440bool AMDGPUCodeGenPrepare::visitICmpInst(ICmpInst &I) {
441 bool Changed = false;
442
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000443 if (ST->has16BitInsts() && needsPromotionToI32(I.getOperand(0)->getType()) &&
444 DA->isUniform(&I))
445 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000446
447 return Changed;
448}
449
450bool AMDGPUCodeGenPrepare::visitSelectInst(SelectInst &I) {
451 bool Changed = false;
452
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000453 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
454 DA->isUniform(&I))
455 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000456
457 return Changed;
458}
459
460bool AMDGPUCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
461 switch (I.getIntrinsicID()) {
462 case Intrinsic::bitreverse:
463 return visitBitreverseIntrinsicInst(I);
464 default:
465 return false;
466 }
467}
468
469bool AMDGPUCodeGenPrepare::visitBitreverseIntrinsicInst(IntrinsicInst &I) {
470 bool Changed = false;
471
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000472 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
473 DA->isUniform(&I))
474 Changed |= promoteUniformBitreverseToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000475
476 return Changed;
477}
478
Matt Arsenault86de4862016-06-24 07:07:55 +0000479bool AMDGPUCodeGenPrepare::doInitialization(Module &M) {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000480 Mod = &M;
Matt Arsenault86de4862016-06-24 07:07:55 +0000481 return false;
482}
483
484bool AMDGPUCodeGenPrepare::runOnFunction(Function &F) {
485 if (!TM || skipFunction(F))
486 return false;
487
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000488 ST = &TM->getSubtarget<SISubtarget>(F);
Matt Arsenault86de4862016-06-24 07:07:55 +0000489 DA = &getAnalysis<DivergenceAnalysis>();
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000490 HasUnsafeFPMath = hasUnsafeFPMath(F);
Matt Arsenault86de4862016-06-24 07:07:55 +0000491
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000492 bool MadeChange = false;
493
494 for (BasicBlock &BB : F) {
495 BasicBlock::iterator Next;
496 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; I = Next) {
497 Next = std::next(I);
498 MadeChange |= visit(*I);
499 }
500 }
501
502 return MadeChange;
Matt Arsenault86de4862016-06-24 07:07:55 +0000503}
504
505INITIALIZE_TM_PASS_BEGIN(AMDGPUCodeGenPrepare, DEBUG_TYPE,
506 "AMDGPU IR optimizations", false, false)
507INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
508INITIALIZE_TM_PASS_END(AMDGPUCodeGenPrepare, DEBUG_TYPE,
509 "AMDGPU IR optimizations", false, false)
510
511char AMDGPUCodeGenPrepare::ID = 0;
512
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000513FunctionPass *llvm::createAMDGPUCodeGenPreparePass(const GCNTargetMachine *TM) {
Matt Arsenault86de4862016-06-24 07:07:55 +0000514 return new AMDGPUCodeGenPrepare(TM);
515}