blob: 0652dacd9b0a07f12559a679f331d16ba849a73e [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 {
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000184 const IntegerType *IntTy = dyn_cast<IntegerType>(T);
185 if (IntTy && IntTy->getBitWidth() > 1 && IntTy->getBitWidth() <= 16)
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000186 return true;
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000187
188 if (const VectorType *VT = dyn_cast<VectorType>(T)) {
189 // TODO: The set of packed operations is more limited, so may want to
190 // promote some anyway.
191 if (ST->hasVOP3PInsts())
192 return false;
193
194 return needsPromotionToI32(VT->getElementType());
195 }
196
197 return false;
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000198}
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000199
Matt Arsenaultd59e6402017-02-01 16:25:23 +0000200// Return true if the op promoted to i32 should have nsw set.
201static bool promotedOpIsNSW(const Instruction &I) {
202 switch (I.getOpcode()) {
203 case Instruction::Shl:
204 case Instruction::Add:
205 case Instruction::Sub:
206 return true;
207 case Instruction::Mul:
208 return I.hasNoUnsignedWrap();
209 default:
210 return false;
211 }
212}
213
214// Return true if the op promoted to i32 should have nuw set.
215static bool promotedOpIsNUW(const Instruction &I) {
216 switch (I.getOpcode()) {
217 case Instruction::Shl:
218 case Instruction::Add:
219 case Instruction::Mul:
220 return true;
221 case Instruction::Sub:
222 return I.hasNoUnsignedWrap();
223 default:
224 return false;
225 }
226}
227
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000228bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(BinaryOperator &I) const {
229 assert(needsPromotionToI32(I.getType()) &&
230 "I does not need promotion to i32");
231
232 if (I.getOpcode() == Instruction::SDiv ||
233 I.getOpcode() == Instruction::UDiv)
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000234 return false;
235
236 IRBuilder<> Builder(&I);
237 Builder.SetCurrentDebugLocation(I.getDebugLoc());
238
239 Type *I32Ty = getI32Ty(Builder, I.getType());
240 Value *ExtOp0 = nullptr;
241 Value *ExtOp1 = nullptr;
242 Value *ExtRes = nullptr;
243 Value *TruncRes = nullptr;
244
245 if (isSigned(I)) {
246 ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
247 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
248 } else {
249 ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
250 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
251 }
Matt Arsenaultd59e6402017-02-01 16:25:23 +0000252
253 ExtRes = Builder.CreateBinOp(I.getOpcode(), ExtOp0, ExtOp1);
254 if (Instruction *Inst = dyn_cast<Instruction>(ExtRes)) {
255 if (promotedOpIsNSW(cast<Instruction>(I)))
256 Inst->setHasNoSignedWrap();
257
258 if (promotedOpIsNUW(cast<Instruction>(I)))
259 Inst->setHasNoUnsignedWrap();
260
261 if (const auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
262 Inst->setIsExact(ExactOp->isExact());
263 }
264
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000265 TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000266
267 I.replaceAllUsesWith(TruncRes);
268 I.eraseFromParent();
269
270 return true;
271}
272
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000273bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(ICmpInst &I) const {
274 assert(needsPromotionToI32(I.getOperand(0)->getType()) &&
275 "I does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000276
277 IRBuilder<> Builder(&I);
278 Builder.SetCurrentDebugLocation(I.getDebugLoc());
279
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000280 Type *I32Ty = getI32Ty(Builder, I.getOperand(0)->getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000281 Value *ExtOp0 = nullptr;
282 Value *ExtOp1 = nullptr;
283 Value *NewICmp = nullptr;
284
285 if (I.isSigned()) {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000286 ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
287 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000288 } else {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000289 ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
290 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000291 }
292 NewICmp = Builder.CreateICmp(I.getPredicate(), ExtOp0, ExtOp1);
293
294 I.replaceAllUsesWith(NewICmp);
295 I.eraseFromParent();
296
297 return true;
298}
299
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000300bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(SelectInst &I) const {
301 assert(needsPromotionToI32(I.getType()) &&
302 "I does not need promotion to i32");
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000303
304 IRBuilder<> Builder(&I);
305 Builder.SetCurrentDebugLocation(I.getDebugLoc());
306
307 Type *I32Ty = getI32Ty(Builder, I.getType());
308 Value *ExtOp1 = nullptr;
309 Value *ExtOp2 = nullptr;
310 Value *ExtRes = nullptr;
311 Value *TruncRes = nullptr;
312
313 if (isSigned(I)) {
314 ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
315 ExtOp2 = Builder.CreateSExt(I.getOperand(2), I32Ty);
316 } else {
317 ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
318 ExtOp2 = Builder.CreateZExt(I.getOperand(2), I32Ty);
319 }
320 ExtRes = Builder.CreateSelect(I.getOperand(0), ExtOp1, ExtOp2);
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000321 TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000322
323 I.replaceAllUsesWith(TruncRes);
324 I.eraseFromParent();
325
326 return true;
327}
328
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000329bool AMDGPUCodeGenPrepare::promoteUniformBitreverseToI32(
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000330 IntrinsicInst &I) const {
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000331 assert(I.getIntrinsicID() == Intrinsic::bitreverse &&
332 "I must be bitreverse intrinsic");
333 assert(needsPromotionToI32(I.getType()) &&
334 "I does not need promotion to i32");
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000335
336 IRBuilder<> Builder(&I);
337 Builder.SetCurrentDebugLocation(I.getDebugLoc());
338
339 Type *I32Ty = getI32Ty(Builder, I.getType());
340 Function *I32 =
Konstantin Zhuravlyovc09e2d72016-10-07 14:39:53 +0000341 Intrinsic::getDeclaration(Mod, Intrinsic::bitreverse, { I32Ty });
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000342 Value *ExtOp = Builder.CreateZExt(I.getOperand(0), I32Ty);
343 Value *ExtRes = Builder.CreateCall(I32, { ExtOp });
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000344 Value *LShrOp =
345 Builder.CreateLShr(ExtRes, 32 - getBaseElementBitWidth(I.getType()));
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000346 Value *TruncRes =
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000347 Builder.CreateTrunc(LShrOp, I.getType());
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000348
349 I.replaceAllUsesWith(TruncRes);
350 I.eraseFromParent();
351
352 return true;
353}
354
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000355static bool shouldKeepFDivF32(Value *Num, bool UnsafeDiv) {
356 const ConstantFP *CNum = dyn_cast<ConstantFP>(Num);
357 if (!CNum)
358 return false;
359
360 // Reciprocal f32 is handled separately without denormals.
Matt Arsenaulte3862cd2016-07-26 23:25:44 +0000361 return UnsafeDiv || CNum->isExactlyValue(+1.0);
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000362}
363
364// Insert an intrinsic for fast fdiv for safe math situations where we can
365// reduce precision. Leave fdiv for situations where the generic node is
366// expected to be optimized.
367bool AMDGPUCodeGenPrepare::visitFDiv(BinaryOperator &FDiv) {
368 Type *Ty = FDiv.getType();
369
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000370 if (!Ty->getScalarType()->isFloatTy())
371 return false;
372
373 MDNode *FPMath = FDiv.getMetadata(LLVMContext::MD_fpmath);
374 if (!FPMath)
375 return false;
376
377 const FPMathOperator *FPOp = cast<const FPMathOperator>(&FDiv);
378 float ULP = FPOp->getFPAccuracy();
379 if (ULP < 2.5f)
380 return false;
381
382 FastMathFlags FMF = FPOp->getFastMathFlags();
383 bool UnsafeDiv = HasUnsafeFPMath || FMF.unsafeAlgebra() ||
384 FMF.allowReciprocal();
385 if (ST->hasFP32Denormals() && !UnsafeDiv)
386 return false;
387
388 IRBuilder<> Builder(FDiv.getParent(), std::next(FDiv.getIterator()), FPMath);
389 Builder.setFastMathFlags(FMF);
390 Builder.SetCurrentDebugLocation(FDiv.getDebugLoc());
391
392 const AMDGPUIntrinsicInfo *II = TM->getIntrinsicInfo();
393 Function *Decl
394 = II->getDeclaration(Mod, AMDGPUIntrinsic::amdgcn_fdiv_fast, {});
395
396 Value *Num = FDiv.getOperand(0);
397 Value *Den = FDiv.getOperand(1);
398
399 Value *NewFDiv = nullptr;
400
401 if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
402 NewFDiv = UndefValue::get(VT);
403
404 // FIXME: Doesn't do the right thing for cases where the vector is partially
405 // constant. This works when the scalarizer pass is run first.
406 for (unsigned I = 0, E = VT->getNumElements(); I != E; ++I) {
407 Value *NumEltI = Builder.CreateExtractElement(Num, I);
408 Value *DenEltI = Builder.CreateExtractElement(Den, I);
409 Value *NewElt;
410
411 if (shouldKeepFDivF32(NumEltI, UnsafeDiv)) {
412 NewElt = Builder.CreateFDiv(NumEltI, DenEltI);
413 } else {
414 NewElt = Builder.CreateCall(Decl, { NumEltI, DenEltI });
415 }
416
417 NewFDiv = Builder.CreateInsertElement(NewFDiv, NewElt, I);
418 }
419 } else {
420 if (!shouldKeepFDivF32(Num, UnsafeDiv))
421 NewFDiv = Builder.CreateCall(Decl, { Num, Den });
422 }
423
424 if (NewFDiv) {
425 FDiv.replaceAllUsesWith(NewFDiv);
426 NewFDiv->takeName(&FDiv);
427 FDiv.eraseFromParent();
428 }
429
430 return true;
431}
432
433static bool hasUnsafeFPMath(const Function &F) {
434 Attribute Attr = F.getFnAttribute("unsafe-fp-math");
435 return Attr.getValueAsString() == "true";
436}
437
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000438bool AMDGPUCodeGenPrepare::visitBinaryOperator(BinaryOperator &I) {
439 bool Changed = false;
440
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000441 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
442 DA->isUniform(&I))
443 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000444
445 return Changed;
446}
447
448bool AMDGPUCodeGenPrepare::visitICmpInst(ICmpInst &I) {
449 bool Changed = false;
450
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000451 if (ST->has16BitInsts() && needsPromotionToI32(I.getOperand(0)->getType()) &&
452 DA->isUniform(&I))
453 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000454
455 return Changed;
456}
457
458bool AMDGPUCodeGenPrepare::visitSelectInst(SelectInst &I) {
459 bool Changed = false;
460
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000461 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
462 DA->isUniform(&I))
463 Changed |= promoteUniformOpToI32(I);
Konstantin Zhuravlyovb4eb5d52016-10-06 02:20:46 +0000464
465 return Changed;
466}
467
468bool AMDGPUCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
469 switch (I.getIntrinsicID()) {
470 case Intrinsic::bitreverse:
471 return visitBitreverseIntrinsicInst(I);
472 default:
473 return false;
474 }
475}
476
477bool AMDGPUCodeGenPrepare::visitBitreverseIntrinsicInst(IntrinsicInst &I) {
478 bool Changed = false;
479
Konstantin Zhuravlyovf74fc602016-10-07 14:22:58 +0000480 if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
481 DA->isUniform(&I))
482 Changed |= promoteUniformBitreverseToI32(I);
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000483
484 return Changed;
485}
486
Matt Arsenault86de4862016-06-24 07:07:55 +0000487bool AMDGPUCodeGenPrepare::doInitialization(Module &M) {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000488 Mod = &M;
Matt Arsenault86de4862016-06-24 07:07:55 +0000489 return false;
490}
491
492bool AMDGPUCodeGenPrepare::runOnFunction(Function &F) {
493 if (!TM || skipFunction(F))
494 return false;
495
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000496 ST = &TM->getSubtarget<SISubtarget>(F);
Matt Arsenault86de4862016-06-24 07:07:55 +0000497 DA = &getAnalysis<DivergenceAnalysis>();
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000498 HasUnsafeFPMath = hasUnsafeFPMath(F);
Matt Arsenault86de4862016-06-24 07:07:55 +0000499
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000500 bool MadeChange = false;
501
502 for (BasicBlock &BB : F) {
503 BasicBlock::iterator Next;
504 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; I = Next) {
505 Next = std::next(I);
506 MadeChange |= visit(*I);
507 }
508 }
509
510 return MadeChange;
Matt Arsenault86de4862016-06-24 07:07:55 +0000511}
512
513INITIALIZE_TM_PASS_BEGIN(AMDGPUCodeGenPrepare, DEBUG_TYPE,
514 "AMDGPU IR optimizations", false, false)
515INITIALIZE_PASS_DEPENDENCY(DivergenceAnalysis)
516INITIALIZE_TM_PASS_END(AMDGPUCodeGenPrepare, DEBUG_TYPE,
517 "AMDGPU IR optimizations", false, false)
518
519char AMDGPUCodeGenPrepare::ID = 0;
520
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +0000521FunctionPass *llvm::createAMDGPUCodeGenPreparePass(const GCNTargetMachine *TM) {
Matt Arsenault86de4862016-06-24 07:07:55 +0000522 return new AMDGPUCodeGenPrepare(TM);
523}