LLVM  7.0.0
AMDGPUCodeGenPrepare.cpp
Go to the documentation of this file.
1 //===-- 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"
18 #include "AMDGPUTargetMachine.h"
19 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Analysis/Loads.h"
24 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/InstVisitor.h"
33 #include "llvm/IR/InstrTypes.h"
34 #include "llvm/IR/Instruction.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/IntrinsicInst.h"
37 #include "llvm/IR/Intrinsics.h"
38 #include "llvm/IR/LLVMContext.h"
39 #include "llvm/IR/Operator.h"
40 #include "llvm/IR/Type.h"
41 #include "llvm/IR/Value.h"
42 #include "llvm/Pass.h"
43 #include "llvm/Support/Casting.h"
44 #include <cassert>
45 #include <iterator>
46 
47 #define DEBUG_TYPE "amdgpu-codegenprepare"
48 
49 using namespace llvm;
50 
51 namespace {
52 
53 static cl::opt<bool> WidenLoads(
54  "amdgpu-codegenprepare-widen-constant-loads",
55  cl::desc("Widen sub-dword constant address space loads in AMDGPUCodeGenPrepare"),
57  cl::init(true));
58 
59 class AMDGPUCodeGenPrepare : public FunctionPass,
60  public InstVisitor<AMDGPUCodeGenPrepare, bool> {
61  const GCNSubtarget *ST = nullptr;
62  AssumptionCache *AC = nullptr;
63  DivergenceAnalysis *DA = nullptr;
64  Module *Mod = nullptr;
65  bool HasUnsafeFPMath = false;
66  AMDGPUAS AMDGPUASI;
67 
68  /// Copies exact/nsw/nuw flags (if any) from binary operation \p I to
69  /// binary operation \p V.
70  ///
71  /// \returns Binary operation \p V.
72  /// \returns \p T's base element bit width.
73  unsigned getBaseElementBitWidth(const Type *T) const;
74 
75  /// \returns Equivalent 32 bit integer type for given type \p T. For example,
76  /// if \p T is i7, then i32 is returned; if \p T is <3 x i12>, then <3 x i32>
77  /// is returned.
78  Type *getI32Ty(IRBuilder<> &B, const Type *T) const;
79 
80  /// \returns True if binary operation \p I is a signed binary operation, false
81  /// otherwise.
82  bool isSigned(const BinaryOperator &I) const;
83 
84  /// \returns True if the condition of 'select' operation \p I comes from a
85  /// signed 'icmp' operation, false otherwise.
86  bool isSigned(const SelectInst &I) const;
87 
88  /// \returns True if type \p T needs to be promoted to 32 bit integer type,
89  /// false otherwise.
90  bool needsPromotionToI32(const Type *T) const;
91 
92  /// Promotes uniform binary operation \p I to equivalent 32 bit binary
93  /// operation.
94  ///
95  /// \details \p I's base element bit width must be greater than 1 and less
96  /// than or equal 16. Promotion is done by sign or zero extending operands to
97  /// 32 bits, replacing \p I with equivalent 32 bit binary operation, and
98  /// truncating the result of 32 bit binary operation back to \p I's original
99  /// type. Division operation is not promoted.
100  ///
101  /// \returns True if \p I is promoted to equivalent 32 bit binary operation,
102  /// false otherwise.
103  bool promoteUniformOpToI32(BinaryOperator &I) const;
104 
105  /// Promotes uniform 'icmp' operation \p I to 32 bit 'icmp' 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, and replacing \p I with 32 bit 'icmp' operation.
110  ///
111  /// \returns True.
112  bool promoteUniformOpToI32(ICmpInst &I) const;
113 
114  /// Promotes uniform 'select' operation \p I to 32 bit 'select'
115  /// operation.
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 sign or zero extending operands to
119  /// 32 bits, replacing \p I with 32 bit 'select' operation, and truncating the
120  /// result of 32 bit 'select' operation back to \p I's original type.
121  ///
122  /// \returns True.
123  bool promoteUniformOpToI32(SelectInst &I) const;
124 
125  /// Promotes uniform 'bitreverse' intrinsic \p I to 32 bit 'bitreverse'
126  /// intrinsic.
127  ///
128  /// \details \p I's base element bit width must be greater than 1 and less
129  /// than or equal 16. Promotion is done by zero extending the operand to 32
130  /// bits, replacing \p I with 32 bit 'bitreverse' intrinsic, shifting the
131  /// result of 32 bit 'bitreverse' intrinsic to the right with zero fill (the
132  /// shift amount is 32 minus \p I's base element bit width), and truncating
133  /// the result of the shift operation back to \p I's original type.
134  ///
135  /// \returns True.
136  bool promoteUniformBitreverseToI32(IntrinsicInst &I) const;
137 
138  /// Expands 24 bit div or rem.
139  Value* expandDivRem24(IRBuilder<> &Builder, BinaryOperator &I,
140  Value *Num, Value *Den,
141  bool IsDiv, bool IsSigned) const;
142 
143  /// Expands 32 bit div or rem.
144  Value* expandDivRem32(IRBuilder<> &Builder, BinaryOperator &I,
145  Value *Num, Value *Den) const;
146 
147  /// Widen a scalar load.
148  ///
149  /// \details \p Widen scalar load for uniform, small type loads from constant
150  // memory / to a full 32-bits and then truncate the input to allow a scalar
151  // load instead of a vector load.
152  //
153  /// \returns True.
154 
155  bool canWidenScalarExtLoad(LoadInst &I) const;
156 
157 public:
158  static char ID;
159 
160  AMDGPUCodeGenPrepare() : FunctionPass(ID) {}
161 
162  bool visitFDiv(BinaryOperator &I);
163 
164  bool visitInstruction(Instruction &I) { return false; }
165  bool visitBinaryOperator(BinaryOperator &I);
166  bool visitLoadInst(LoadInst &I);
167  bool visitICmpInst(ICmpInst &I);
168  bool visitSelectInst(SelectInst &I);
169 
170  bool visitIntrinsicInst(IntrinsicInst &I);
171  bool visitBitreverseIntrinsicInst(IntrinsicInst &I);
172 
173  bool doInitialization(Module &M) override;
174  bool runOnFunction(Function &F) override;
175 
176  StringRef getPassName() const override { return "AMDGPU IR optimizations"; }
177 
178  void getAnalysisUsage(AnalysisUsage &AU) const override {
181  AU.setPreservesAll();
182  }
183 };
184 
185 } // end anonymous namespace
186 
187 unsigned AMDGPUCodeGenPrepare::getBaseElementBitWidth(const Type *T) const {
188  assert(needsPromotionToI32(T) && "T does not need promotion to i32");
189 
190  if (T->isIntegerTy())
191  return T->getIntegerBitWidth();
192  return cast<VectorType>(T)->getElementType()->getIntegerBitWidth();
193 }
194 
195 Type *AMDGPUCodeGenPrepare::getI32Ty(IRBuilder<> &B, const Type *T) const {
196  assert(needsPromotionToI32(T) && "T does not need promotion to i32");
197 
198  if (T->isIntegerTy())
199  return B.getInt32Ty();
200  return VectorType::get(B.getInt32Ty(), cast<VectorType>(T)->getNumElements());
201 }
202 
204  return I.getOpcode() == Instruction::AShr ||
205  I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::SRem;
206 }
207 
208 bool AMDGPUCodeGenPrepare::isSigned(const SelectInst &I) const {
209  return isa<ICmpInst>(I.getOperand(0)) ?
210  cast<ICmpInst>(I.getOperand(0))->isSigned() : false;
211 }
212 
213 bool AMDGPUCodeGenPrepare::needsPromotionToI32(const Type *T) const {
214  const IntegerType *IntTy = dyn_cast<IntegerType>(T);
215  if (IntTy && IntTy->getBitWidth() > 1 && IntTy->getBitWidth() <= 16)
216  return true;
217 
218  if (const VectorType *VT = dyn_cast<VectorType>(T)) {
219  // TODO: The set of packed operations is more limited, so may want to
220  // promote some anyway.
221  if (ST->hasVOP3PInsts())
222  return false;
223 
224  return needsPromotionToI32(VT->getElementType());
225  }
226 
227  return false;
228 }
229 
230 // Return true if the op promoted to i32 should have nsw set.
231 static bool promotedOpIsNSW(const Instruction &I) {
232  switch (I.getOpcode()) {
233  case Instruction::Shl:
234  case Instruction::Add:
235  case Instruction::Sub:
236  return true;
237  case Instruction::Mul:
238  return I.hasNoUnsignedWrap();
239  default:
240  return false;
241  }
242 }
243 
244 // Return true if the op promoted to i32 should have nuw set.
245 static bool promotedOpIsNUW(const Instruction &I) {
246  switch (I.getOpcode()) {
247  case Instruction::Shl:
248  case Instruction::Add:
249  case Instruction::Mul:
250  return true;
251  case Instruction::Sub:
252  return I.hasNoUnsignedWrap();
253  default:
254  return false;
255  }
256 }
257 
258 bool AMDGPUCodeGenPrepare::canWidenScalarExtLoad(LoadInst &I) const {
259  Type *Ty = I.getType();
260  const DataLayout &DL = Mod->getDataLayout();
261  int TySize = DL.getTypeSizeInBits(Ty);
262  unsigned Align = I.getAlignment() ?
263  I.getAlignment() : DL.getABITypeAlignment(Ty);
264 
265  return I.isSimple() && TySize < 32 && Align >= 4 && DA->isUniform(&I);
266 }
267 
268 bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(BinaryOperator &I) const {
269  assert(needsPromotionToI32(I.getType()) &&
270  "I does not need promotion to i32");
271 
272  if (I.getOpcode() == Instruction::SDiv ||
273  I.getOpcode() == Instruction::UDiv ||
274  I.getOpcode() == Instruction::SRem ||
275  I.getOpcode() == Instruction::URem)
276  return false;
277 
278  IRBuilder<> Builder(&I);
280 
281  Type *I32Ty = getI32Ty(Builder, I.getType());
282  Value *ExtOp0 = nullptr;
283  Value *ExtOp1 = nullptr;
284  Value *ExtRes = nullptr;
285  Value *TruncRes = nullptr;
286 
287  if (isSigned(I)) {
288  ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
289  ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
290  } else {
291  ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
292  ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
293  }
294 
295  ExtRes = Builder.CreateBinOp(I.getOpcode(), ExtOp0, ExtOp1);
296  if (Instruction *Inst = dyn_cast<Instruction>(ExtRes)) {
297  if (promotedOpIsNSW(cast<Instruction>(I)))
298  Inst->setHasNoSignedWrap();
299 
300  if (promotedOpIsNUW(cast<Instruction>(I)))
301  Inst->setHasNoUnsignedWrap();
302 
303  if (const auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
304  Inst->setIsExact(ExactOp->isExact());
305  }
306 
307  TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
308 
309  I.replaceAllUsesWith(TruncRes);
310  I.eraseFromParent();
311 
312  return true;
313 }
314 
315 bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(ICmpInst &I) const {
316  assert(needsPromotionToI32(I.getOperand(0)->getType()) &&
317  "I does not need promotion to i32");
318 
319  IRBuilder<> Builder(&I);
321 
322  Type *I32Ty = getI32Ty(Builder, I.getOperand(0)->getType());
323  Value *ExtOp0 = nullptr;
324  Value *ExtOp1 = nullptr;
325  Value *NewICmp = nullptr;
326 
327  if (I.isSigned()) {
328  ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
329  ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
330  } else {
331  ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
332  ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
333  }
334  NewICmp = Builder.CreateICmp(I.getPredicate(), ExtOp0, ExtOp1);
335 
336  I.replaceAllUsesWith(NewICmp);
337  I.eraseFromParent();
338 
339  return true;
340 }
341 
342 bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(SelectInst &I) const {
343  assert(needsPromotionToI32(I.getType()) &&
344  "I does not need promotion to i32");
345 
346  IRBuilder<> Builder(&I);
348 
349  Type *I32Ty = getI32Ty(Builder, I.getType());
350  Value *ExtOp1 = nullptr;
351  Value *ExtOp2 = nullptr;
352  Value *ExtRes = nullptr;
353  Value *TruncRes = nullptr;
354 
355  if (isSigned(I)) {
356  ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
357  ExtOp2 = Builder.CreateSExt(I.getOperand(2), I32Ty);
358  } else {
359  ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
360  ExtOp2 = Builder.CreateZExt(I.getOperand(2), I32Ty);
361  }
362  ExtRes = Builder.CreateSelect(I.getOperand(0), ExtOp1, ExtOp2);
363  TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
364 
365  I.replaceAllUsesWith(TruncRes);
366  I.eraseFromParent();
367 
368  return true;
369 }
370 
371 bool AMDGPUCodeGenPrepare::promoteUniformBitreverseToI32(
372  IntrinsicInst &I) const {
373  assert(I.getIntrinsicID() == Intrinsic::bitreverse &&
374  "I must be bitreverse intrinsic");
375  assert(needsPromotionToI32(I.getType()) &&
376  "I does not need promotion to i32");
377 
378  IRBuilder<> Builder(&I);
380 
381  Type *I32Ty = getI32Ty(Builder, I.getType());
382  Function *I32 =
383  Intrinsic::getDeclaration(Mod, Intrinsic::bitreverse, { I32Ty });
384  Value *ExtOp = Builder.CreateZExt(I.getOperand(0), I32Ty);
385  Value *ExtRes = Builder.CreateCall(I32, { ExtOp });
386  Value *LShrOp =
387  Builder.CreateLShr(ExtRes, 32 - getBaseElementBitWidth(I.getType()));
388  Value *TruncRes =
389  Builder.CreateTrunc(LShrOp, I.getType());
390 
391  I.replaceAllUsesWith(TruncRes);
392  I.eraseFromParent();
393 
394  return true;
395 }
396 
397 static bool shouldKeepFDivF32(Value *Num, bool UnsafeDiv, bool HasDenormals) {
398  const ConstantFP *CNum = dyn_cast<ConstantFP>(Num);
399  if (!CNum)
400  return HasDenormals;
401 
402  if (UnsafeDiv)
403  return true;
404 
405  bool IsOne = CNum->isExactlyValue(+1.0) || CNum->isExactlyValue(-1.0);
406 
407  // Reciprocal f32 is handled separately without denormals.
408  return HasDenormals ^ IsOne;
409 }
410 
411 // Insert an intrinsic for fast fdiv for safe math situations where we can
412 // reduce precision. Leave fdiv for situations where the generic node is
413 // expected to be optimized.
414 bool AMDGPUCodeGenPrepare::visitFDiv(BinaryOperator &FDiv) {
415  Type *Ty = FDiv.getType();
416 
417  if (!Ty->getScalarType()->isFloatTy())
418  return false;
419 
420  MDNode *FPMath = FDiv.getMetadata(LLVMContext::MD_fpmath);
421  if (!FPMath)
422  return false;
423 
424  const FPMathOperator *FPOp = cast<const FPMathOperator>(&FDiv);
425  float ULP = FPOp->getFPAccuracy();
426  if (ULP < 2.5f)
427  return false;
428 
429  FastMathFlags FMF = FPOp->getFastMathFlags();
430  bool UnsafeDiv = HasUnsafeFPMath || FMF.isFast() ||
431  FMF.allowReciprocal();
432 
433  // With UnsafeDiv node will be optimized to just rcp and mul.
434  if (UnsafeDiv)
435  return false;
436 
437  IRBuilder<> Builder(FDiv.getParent(), std::next(FDiv.getIterator()), FPMath);
438  Builder.setFastMathFlags(FMF);
439  Builder.SetCurrentDebugLocation(FDiv.getDebugLoc());
440 
441  Function *Decl = Intrinsic::getDeclaration(Mod, Intrinsic::amdgcn_fdiv_fast);
442 
443  Value *Num = FDiv.getOperand(0);
444  Value *Den = FDiv.getOperand(1);
445 
446  Value *NewFDiv = nullptr;
447 
448  bool HasDenormals = ST->hasFP32Denormals();
449  if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
450  NewFDiv = UndefValue::get(VT);
451 
452  // FIXME: Doesn't do the right thing for cases where the vector is partially
453  // constant. This works when the scalarizer pass is run first.
454  for (unsigned I = 0, E = VT->getNumElements(); I != E; ++I) {
455  Value *NumEltI = Builder.CreateExtractElement(Num, I);
456  Value *DenEltI = Builder.CreateExtractElement(Den, I);
457  Value *NewElt;
458 
459  if (shouldKeepFDivF32(NumEltI, UnsafeDiv, HasDenormals)) {
460  NewElt = Builder.CreateFDiv(NumEltI, DenEltI);
461  } else {
462  NewElt = Builder.CreateCall(Decl, { NumEltI, DenEltI });
463  }
464 
465  NewFDiv = Builder.CreateInsertElement(NewFDiv, NewElt, I);
466  }
467  } else {
468  if (!shouldKeepFDivF32(Num, UnsafeDiv, HasDenormals))
469  NewFDiv = Builder.CreateCall(Decl, { Num, Den });
470  }
471 
472  if (NewFDiv) {
473  FDiv.replaceAllUsesWith(NewFDiv);
474  NewFDiv->takeName(&FDiv);
475  FDiv.eraseFromParent();
476  }
477 
478  return !!NewFDiv;
479 }
480 
481 static bool hasUnsafeFPMath(const Function &F) {
482  Attribute Attr = F.getFnAttribute("unsafe-fp-math");
483  return Attr.getValueAsString() == "true";
484 }
485 
486 static std::pair<Value*, Value*> getMul64(IRBuilder<> &Builder,
487  Value *LHS, Value *RHS) {
488  Type *I32Ty = Builder.getInt32Ty();
489  Type *I64Ty = Builder.getInt64Ty();
490 
491  Value *LHS_EXT64 = Builder.CreateZExt(LHS, I64Ty);
492  Value *RHS_EXT64 = Builder.CreateZExt(RHS, I64Ty);
493  Value *MUL64 = Builder.CreateMul(LHS_EXT64, RHS_EXT64);
494  Value *Lo = Builder.CreateTrunc(MUL64, I32Ty);
495  Value *Hi = Builder.CreateLShr(MUL64, Builder.getInt64(32));
496  Hi = Builder.CreateTrunc(Hi, I32Ty);
497  return std::make_pair(Lo, Hi);
498 }
499 
500 static Value* getMulHu(IRBuilder<> &Builder, Value *LHS, Value *RHS) {
501  return getMul64(Builder, LHS, RHS).second;
502 }
503 
504 // The fractional part of a float is enough to accurately represent up to
505 // a 24-bit signed integer.
506 Value* AMDGPUCodeGenPrepare::expandDivRem24(IRBuilder<> &Builder,
507  BinaryOperator &I,
508  Value *Num, Value *Den,
509  bool IsDiv, bool IsSigned) const {
510  assert(Num->getType()->isIntegerTy(32));
511 
512  const DataLayout &DL = Mod->getDataLayout();
513  unsigned LHSSignBits = ComputeNumSignBits(Num, DL, 0, AC, &I);
514  if (LHSSignBits < 9)
515  return nullptr;
516 
517  unsigned RHSSignBits = ComputeNumSignBits(Den, DL, 0, AC, &I);
518  if (RHSSignBits < 9)
519  return nullptr;
520 
521 
522  unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
523  unsigned DivBits = 32 - SignBits;
524  if (IsSigned)
525  ++DivBits;
526 
527  Type *Ty = Num->getType();
528  Type *I32Ty = Builder.getInt32Ty();
529  Type *F32Ty = Builder.getFloatTy();
530  ConstantInt *One = Builder.getInt32(1);
531  Value *JQ = One;
532 
533  if (IsSigned) {
534  // char|short jq = ia ^ ib;
535  JQ = Builder.CreateXor(Num, Den);
536 
537  // jq = jq >> (bitsize - 2)
538  JQ = Builder.CreateAShr(JQ, Builder.getInt32(30));
539 
540  // jq = jq | 0x1
541  JQ = Builder.CreateOr(JQ, One);
542  }
543 
544  // int ia = (int)LHS;
545  Value *IA = Num;
546 
547  // int ib, (int)RHS;
548  Value *IB = Den;
549 
550  // float fa = (float)ia;
551  Value *FA = IsSigned ? Builder.CreateSIToFP(IA, F32Ty)
552  : Builder.CreateUIToFP(IA, F32Ty);
553 
554  // float fb = (float)ib;
555  Value *FB = IsSigned ? Builder.CreateSIToFP(IB,F32Ty)
556  : Builder.CreateUIToFP(IB,F32Ty);
557 
558  Value *RCP = Builder.CreateFDiv(ConstantFP::get(F32Ty, 1.0), FB);
559  Value *FQM = Builder.CreateFMul(FA, RCP);
560 
561  // fq = trunc(fqm);
562  CallInst* FQ = Builder.CreateIntrinsic(Intrinsic::trunc, { FQM });
563  FQ->copyFastMathFlags(Builder.getFastMathFlags());
564 
565  // float fqneg = -fq;
566  Value *FQNeg = Builder.CreateFNeg(FQ);
567 
568  // float fr = mad(fqneg, fb, fa);
569  Value *FR = Builder.CreateIntrinsic(Intrinsic::amdgcn_fmad_ftz,
570  { FQNeg, FB, FA }, FQ);
571 
572  // int iq = (int)fq;
573  Value *IQ = IsSigned ? Builder.CreateFPToSI(FQ, I32Ty)
574  : Builder.CreateFPToUI(FQ, I32Ty);
575 
576  // fr = fabs(fr);
577  FR = Builder.CreateIntrinsic(Intrinsic::fabs, { FR }, FQ);
578 
579  // fb = fabs(fb);
580  FB = Builder.CreateIntrinsic(Intrinsic::fabs, { FB }, FQ);
581 
582  // int cv = fr >= fb;
583  Value *CV = Builder.CreateFCmpOGE(FR, FB);
584 
585  // jq = (cv ? jq : 0);
586  JQ = Builder.CreateSelect(CV, JQ, Builder.getInt32(0));
587 
588  // dst = iq + jq;
589  Value *Div = Builder.CreateAdd(IQ, JQ);
590 
591  Value *Res = Div;
592  if (!IsDiv) {
593  // Rem needs compensation, it's easier to recompute it
594  Value *Rem = Builder.CreateMul(Div, Den);
595  Res = Builder.CreateSub(Num, Rem);
596  }
597 
598  // Truncate to number of bits this divide really is.
599  if (IsSigned) {
600  Res = Builder.CreateTrunc(Res, Builder.getIntNTy(DivBits));
601  Res = Builder.CreateSExt(Res, Ty);
602  } else {
603  ConstantInt *TruncMask = Builder.getInt32((UINT64_C(1) << DivBits) - 1);
604  Res = Builder.CreateAnd(Res, TruncMask);
605  }
606 
607  return Res;
608 }
609 
610 Value* AMDGPUCodeGenPrepare::expandDivRem32(IRBuilder<> &Builder,
611  BinaryOperator &I,
612  Value *Num, Value *Den) const {
614  assert(Opc == Instruction::URem || Opc == Instruction::UDiv ||
615  Opc == Instruction::SRem || Opc == Instruction::SDiv);
616 
617  FastMathFlags FMF;
618  FMF.setFast();
619  Builder.setFastMathFlags(FMF);
620 
621  if (isa<Constant>(Den))
622  return nullptr; // Keep it for optimization
623 
624  bool IsDiv = Opc == Instruction::UDiv || Opc == Instruction::SDiv;
625  bool IsSigned = Opc == Instruction::SRem || Opc == Instruction::SDiv;
626 
627  Type *Ty = Num->getType();
628  Type *I32Ty = Builder.getInt32Ty();
629  Type *F32Ty = Builder.getFloatTy();
630 
631  if (Ty->getScalarSizeInBits() < 32) {
632  if (IsSigned) {
633  Num = Builder.CreateSExt(Num, I32Ty);
634  Den = Builder.CreateSExt(Den, I32Ty);
635  } else {
636  Num = Builder.CreateZExt(Num, I32Ty);
637  Den = Builder.CreateZExt(Den, I32Ty);
638  }
639  }
640 
641  if (Value *Res = expandDivRem24(Builder, I, Num, Den, IsDiv, IsSigned)) {
642  Res = Builder.CreateTrunc(Res, Ty);
643  return Res;
644  }
645 
646  ConstantInt *Zero = Builder.getInt32(0);
647  ConstantInt *One = Builder.getInt32(1);
648  ConstantInt *MinusOne = Builder.getInt32(~0);
649 
650  Value *Sign = nullptr;
651  if (IsSigned) {
652  ConstantInt *K31 = Builder.getInt32(31);
653  Value *LHSign = Builder.CreateAShr(Num, K31);
654  Value *RHSign = Builder.CreateAShr(Den, K31);
655  // Remainder sign is the same as LHS
656  Sign = IsDiv ? Builder.CreateXor(LHSign, RHSign) : LHSign;
657 
658  Num = Builder.CreateAdd(Num, LHSign);
659  Den = Builder.CreateAdd(Den, RHSign);
660 
661  Num = Builder.CreateXor(Num, LHSign);
662  Den = Builder.CreateXor(Den, RHSign);
663  }
664 
665  // RCP = URECIP(Den) = 2^32 / Den + e
666  // e is rounding error.
667  Value *DEN_F32 = Builder.CreateUIToFP(Den, F32Ty);
668  Value *RCP_F32 = Builder.CreateFDiv(ConstantFP::get(F32Ty, 1.0), DEN_F32);
669  Constant *UINT_MAX_PLUS_1 = ConstantFP::get(F32Ty, BitsToFloat(0x4f800000));
670  Value *RCP_SCALE = Builder.CreateFMul(RCP_F32, UINT_MAX_PLUS_1);
671  Value *RCP = Builder.CreateFPToUI(RCP_SCALE, I32Ty);
672 
673  // RCP_LO, RCP_HI = mul(RCP, Den) */
674  Value *RCP_LO, *RCP_HI;
675  std::tie(RCP_LO, RCP_HI) = getMul64(Builder, RCP, Den);
676 
677  // NEG_RCP_LO = -RCP_LO
678  Value *NEG_RCP_LO = Builder.CreateNeg(RCP_LO);
679 
680  // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
681  Value *RCP_HI_0_CC = Builder.CreateICmpEQ(RCP_HI, Zero);
682  Value *ABS_RCP_LO = Builder.CreateSelect(RCP_HI_0_CC, NEG_RCP_LO, RCP_LO);
683 
684  // Calculate the rounding error from the URECIP instruction
685  // E = mulhu(ABS_RCP_LO, RCP)
686  Value *E = getMulHu(Builder, ABS_RCP_LO, RCP);
687 
688  // RCP_A_E = RCP + E
689  Value *RCP_A_E = Builder.CreateAdd(RCP, E);
690 
691  // RCP_S_E = RCP - E
692  Value *RCP_S_E = Builder.CreateSub(RCP, E);
693 
694  // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
695  Value *Tmp0 = Builder.CreateSelect(RCP_HI_0_CC, RCP_A_E, RCP_S_E);
696 
697  // Quotient = mulhu(Tmp0, Num)
698  Value *Quotient = getMulHu(Builder, Tmp0, Num);
699 
700  // Num_S_Remainder = Quotient * Den
701  Value *Num_S_Remainder = Builder.CreateMul(Quotient, Den);
702 
703  // Remainder = Num - Num_S_Remainder
704  Value *Remainder = Builder.CreateSub(Num, Num_S_Remainder);
705 
706  // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
707  Value *Rem_GE_Den_CC = Builder.CreateICmpUGE(Remainder, Den);
708  Value *Remainder_GE_Den = Builder.CreateSelect(Rem_GE_Den_CC, MinusOne, Zero);
709 
710  // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
711  Value *Num_GE_Num_S_Rem_CC = Builder.CreateICmpUGE(Num, Num_S_Remainder);
712  Value *Remainder_GE_Zero = Builder.CreateSelect(Num_GE_Num_S_Rem_CC,
713  MinusOne, Zero);
714 
715  // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
716  Value *Tmp1 = Builder.CreateAnd(Remainder_GE_Den, Remainder_GE_Zero);
717  Value *Tmp1_0_CC = Builder.CreateICmpEQ(Tmp1, Zero);
718 
719  Value *Res;
720  if (IsDiv) {
721  // Quotient_A_One = Quotient + 1
722  Value *Quotient_A_One = Builder.CreateAdd(Quotient, One);
723 
724  // Quotient_S_One = Quotient - 1
725  Value *Quotient_S_One = Builder.CreateSub(Quotient, One);
726 
727  // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
728  Value *Div = Builder.CreateSelect(Tmp1_0_CC, Quotient, Quotient_A_One);
729 
730  // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
731  Res = Builder.CreateSelect(Num_GE_Num_S_Rem_CC, Div, Quotient_S_One);
732  } else {
733  // Remainder_S_Den = Remainder - Den
734  Value *Remainder_S_Den = Builder.CreateSub(Remainder, Den);
735 
736  // Remainder_A_Den = Remainder + Den
737  Value *Remainder_A_Den = Builder.CreateAdd(Remainder, Den);
738 
739  // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
740  Value *Rem = Builder.CreateSelect(Tmp1_0_CC, Remainder, Remainder_S_Den);
741 
742  // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
743  Res = Builder.CreateSelect(Num_GE_Num_S_Rem_CC, Rem, Remainder_A_Den);
744  }
745 
746  if (IsSigned) {
747  Res = Builder.CreateXor(Res, Sign);
748  Res = Builder.CreateSub(Res, Sign);
749  }
750 
751  Res = Builder.CreateTrunc(Res, Ty);
752 
753  return Res;
754 }
755 
756 bool AMDGPUCodeGenPrepare::visitBinaryOperator(BinaryOperator &I) {
757  if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
758  DA->isUniform(&I) && promoteUniformOpToI32(I))
759  return true;
760 
761  bool Changed = false;
763  Type *Ty = I.getType();
764  Value *NewDiv = nullptr;
765  if ((Opc == Instruction::URem || Opc == Instruction::UDiv ||
766  Opc == Instruction::SRem || Opc == Instruction::SDiv) &&
767  Ty->getScalarSizeInBits() <= 32) {
768  Value *Num = I.getOperand(0);
769  Value *Den = I.getOperand(1);
770  IRBuilder<> Builder(&I);
772 
773  if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
774  NewDiv = UndefValue::get(VT);
775 
776  for (unsigned N = 0, E = VT->getNumElements(); N != E; ++N) {
777  Value *NumEltN = Builder.CreateExtractElement(Num, N);
778  Value *DenEltN = Builder.CreateExtractElement(Den, N);
779  Value *NewElt = expandDivRem32(Builder, I, NumEltN, DenEltN);
780  if (!NewElt)
781  NewElt = Builder.CreateBinOp(Opc, NumEltN, DenEltN);
782  NewDiv = Builder.CreateInsertElement(NewDiv, NewElt, N);
783  }
784  } else {
785  NewDiv = expandDivRem32(Builder, I, Num, Den);
786  }
787 
788  if (NewDiv) {
789  I.replaceAllUsesWith(NewDiv);
790  I.eraseFromParent();
791  Changed = true;
792  }
793  }
794 
795  return Changed;
796 }
797 
798 bool AMDGPUCodeGenPrepare::visitLoadInst(LoadInst &I) {
799  if (!WidenLoads)
800  return false;
801 
802  if ((I.getPointerAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS ||
803  I.getPointerAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS_32BIT) &&
804  canWidenScalarExtLoad(I)) {
805  IRBuilder<> Builder(&I);
807 
808  Type *I32Ty = Builder.getInt32Ty();
809  Type *PT = PointerType::get(I32Ty, I.getPointerAddressSpace());
810  Value *BitCast= Builder.CreateBitCast(I.getPointerOperand(), PT);
811  LoadInst *WidenLoad = Builder.CreateLoad(BitCast);
812  WidenLoad->copyMetadata(I);
813 
814  // If we have range metadata, we need to convert the type, and not make
815  // assumptions about the high bits.
816  if (auto *Range = WidenLoad->getMetadata(LLVMContext::MD_range)) {
817  ConstantInt *Lower =
818  mdconst::extract<ConstantInt>(Range->getOperand(0));
819 
820  if (Lower->getValue().isNullValue()) {
821  WidenLoad->setMetadata(LLVMContext::MD_range, nullptr);
822  } else {
823  Metadata *LowAndHigh[] = {
825  // Don't make assumptions about the high bits.
827  };
828 
830  MDNode::get(Mod->getContext(), LowAndHigh));
831  }
832  }
833 
834  int TySize = Mod->getDataLayout().getTypeSizeInBits(I.getType());
835  Type *IntNTy = Builder.getIntNTy(TySize);
836  Value *ValTrunc = Builder.CreateTrunc(WidenLoad, IntNTy);
837  Value *ValOrig = Builder.CreateBitCast(ValTrunc, I.getType());
838  I.replaceAllUsesWith(ValOrig);
839  I.eraseFromParent();
840  return true;
841  }
842 
843  return false;
844 }
845 
846 bool AMDGPUCodeGenPrepare::visitICmpInst(ICmpInst &I) {
847  bool Changed = false;
848 
849  if (ST->has16BitInsts() && needsPromotionToI32(I.getOperand(0)->getType()) &&
850  DA->isUniform(&I))
851  Changed |= promoteUniformOpToI32(I);
852 
853  return Changed;
854 }
855 
856 bool AMDGPUCodeGenPrepare::visitSelectInst(SelectInst &I) {
857  bool Changed = false;
858 
859  if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
860  DA->isUniform(&I))
861  Changed |= promoteUniformOpToI32(I);
862 
863  return Changed;
864 }
865 
866 bool AMDGPUCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
867  switch (I.getIntrinsicID()) {
868  case Intrinsic::bitreverse:
869  return visitBitreverseIntrinsicInst(I);
870  default:
871  return false;
872  }
873 }
874 
875 bool AMDGPUCodeGenPrepare::visitBitreverseIntrinsicInst(IntrinsicInst &I) {
876  bool Changed = false;
877 
878  if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
879  DA->isUniform(&I))
880  Changed |= promoteUniformBitreverseToI32(I);
881 
882  return Changed;
883 }
884 
885 bool AMDGPUCodeGenPrepare::doInitialization(Module &M) {
886  Mod = &M;
887  return false;
888 }
889 
891  if (skipFunction(F))
892  return false;
893 
894  auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
895  if (!TPC)
896  return false;
897 
898  const AMDGPUTargetMachine &TM = TPC->getTM<AMDGPUTargetMachine>();
899  ST = &TM.getSubtarget<GCNSubtarget>(F);
900  AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
901  DA = &getAnalysis<DivergenceAnalysis>();
902  HasUnsafeFPMath = hasUnsafeFPMath(F);
903  AMDGPUASI = TM.getAMDGPUAS();
904 
905  bool MadeChange = false;
906 
907  for (BasicBlock &BB : F) {
909  for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; I = Next) {
910  Next = std::next(I);
911  MadeChange |= visit(*I);
912  }
913  }
914 
915  return MadeChange;
916 }
917 
918 INITIALIZE_PASS_BEGIN(AMDGPUCodeGenPrepare, DEBUG_TYPE,
919  "AMDGPU IR optimizations", false, false)
923  false, false)
924 
925 char AMDGPUCodeGenPrepare::ID = 0;
926 
928  return new AMDGPUCodeGenPrepare();
929 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:68
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction, which must be an operator which supports these flags.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1846
bool isSimple() const
Definition: Instructions.h:266
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1246
FastMathFlags getFastMathFlags() const
Get the flags to be applied to created floating point ops.
Definition: IRBuilder.h:211
Base class for instruction visitors.
Definition: InstVisitor.h:81
AMDGPU specific subclass of TargetSubtarget.
void setFast(bool B=true)
Definition: Operator.h:231
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
BinaryOps getOpcode() const
Definition: InstrTypes.h:555
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1148
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:864
This class represents a function call, abstracting a target machine&#39;s calling convention.
An immutable pass that tracks lazily created AssumptionCache objects.
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:617
float BitsToFloat(uint32_t Bits)
This function takes a 32-bit integer and returns the bit equivalent float.
Definition: MathExtras.h:581
A cache of @llvm.assume calls within a function.
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1560
FunctionPass * createAMDGPUCodeGenPreparePass()
Metadata node.
Definition: Metadata.h:862
F(f)
An instruction for reading from memory.
Definition: Instructions.h:168
static bool promotedOpIsNUW(const Instruction &I)
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:347
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
bool isSigned() const
Definition: InstrTypes.h:1054
This class represents the LLVM &#39;select&#39; instruction.
OpenCL uses address spaces to differentiate between various memory regions on the hardware...
Definition: AMDGPU.h:224
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition: IRBuilder.h:352
AMDGPU IR optimizations
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:731
This file contains the simple types necessary to represent the attributes associated with functions a...
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:962
Metadata * LowAndHigh[]
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1629
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
#define T
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:200
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:138
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:126
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:979
Value * CreateSIToFP(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1606
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
Definition: IRBuilder.h:151
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:439
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1556
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
Definition: DerivedTypes.h:66
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:301
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:408
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1007
Value * getOperand(unsigned i) const
Definition: User.h:170
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1130
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return &#39;this&#39;.
Definition: Type.h:304
Value * getOperand(unsigned i_nocapture) const
bool isFloatTy() const
Return true if this is &#39;float&#39;, a 32-bit IEEE fp type.
Definition: Type.h:147
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool&#39; fo...
Definition: IRBuilder.h:1305
static bool promotedOpIsNSW(const Instruction &I)
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags.
Definition: Operator.h:358
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1164
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Value * CreateFMul(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1195
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1092
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
CallInst * CreateIntrinsic(Intrinsic::ID ID, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with no operands.
Definition: IRBuilder.cpp:741
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.h:1901
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:264
Value * CreateFPToUI(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1594
bool isFast() const
&#39;Fast&#39; means all bits are set.
Definition: Operator.h:207
Represent the analysis usage information of a pass.
Value * CreateNeg(Value *V, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1256
This instruction compares its operands according to the predicate given to the constructor.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
Value * getPointerOperand()
Definition: Instructions.h:274
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1736
bool allowReciprocal() const
Definition: Operator.h:203
self_iterator getIterator()
Definition: ilist_node.h:82
Class to represent integer types.
Definition: DerivedTypes.h:40
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
Definition: IRBuilder.h:312
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:360
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1921
The AMDGPU TargetMachine interface definition for hw codgen targets.
static bool shouldKeepFDivF32(Value *Num, bool UnsafeDiv, bool HasDenormals)
static Value * getMulHu(IRBuilder<> &Builder, Value *LHS, Value *RHS)
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1392
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define DEBUG_TYPE
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1226
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:996
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1552
Value * CreateUIToFP(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1602
Value * CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1786
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return the number of times the sign bit of the register is replicated into the other bits...
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:130
Value * CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1748
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:240
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1934
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:722
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:307
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:621
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:684
static bool hasUnsafeFPMath(const Function &F)
The access may modify the value stored in memory.
static std::pair< Value *, Value * > getMul64(IRBuilder<> &Builder, Value *LHS, Value *RHS)
Class to represent vector types.
Definition: DerivedTypes.h:393
void setPreservesAll()
Set by analyses that do not transform their input at all.
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:560
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:959
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:290
unsigned getAlignment() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:230
unsigned getIntegerBitWidth() const
Definition: DerivedTypes.h:97
StringRef getValueAsString() const
Return the attribute&#39;s value as a string.
Definition: Attributes.cpp:195
float getFPAccuracy() const
Get the maximum error permitted by this operation in ULPs.
static bool isSigned(Value *V)
Can the given value generate sign bits.
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
Value * CreateFPToSI(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1598
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
Type * getFloatTy()
Fetch the type representing a 32-bit floating point value.
Definition: IRBuilder.h:370
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1112
Value * CreateFDiv(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1212
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:280
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:593
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Definition: IRBuilder.h:220
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:317
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1072
bool isExactlyValue(const APFloat &V) const
We don&#39;t rely on operator== working on double values, as it returns true for things that are clearly ...
Definition: Constants.cpp:801
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:160
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
INITIALIZE_PASS_BEGIN(AMDGPUCodeGenPrepare, DEBUG_TYPE, "AMDGPU IR optimizations", false, false) INITIALIZE_PASS_END(AMDGPUCodeGenPrepare
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
Root of the metadata hierarchy.
Definition: Metadata.h:58
Statically lint checks LLVM IR
Definition: Lint.cpp:193
Value * CreateFNeg(Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1274
bool isNullValue() const
Determine if all bits are clear.
Definition: APInt.h:405
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
const BasicBlock * getParent() const
Definition: Instruction.h:67
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1871