xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp (revision 8bcb0991864975618c09697b1aca10683346d9f0)
10b57cec5SDimitry Andric //===-- AMDGPUCodeGenPrepare.cpp ------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This pass does misc. AMDGPU optimizations on IR before instruction
110b57cec5SDimitry Andric /// selection.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "AMDGPU.h"
160b57cec5SDimitry Andric #include "AMDGPUSubtarget.h"
170b57cec5SDimitry Andric #include "AMDGPUTargetMachine.h"
180b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
190b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
200b57cec5SDimitry Andric #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
210b57cec5SDimitry Andric #include "llvm/Analysis/Loads.h"
220b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
250b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
260b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
270b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
280b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
290b57cec5SDimitry Andric #include "llvm/IR/Function.h"
300b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
310b57cec5SDimitry Andric #include "llvm/IR/InstVisitor.h"
320b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
330b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
340b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
350b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
360b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
370b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
380b57cec5SDimitry Andric #include "llvm/IR/Operator.h"
390b57cec5SDimitry Andric #include "llvm/IR/Type.h"
400b57cec5SDimitry Andric #include "llvm/IR/Value.h"
410b57cec5SDimitry Andric #include "llvm/Pass.h"
420b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
430b57cec5SDimitry Andric #include <cassert>
440b57cec5SDimitry Andric #include <iterator>
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric #define DEBUG_TYPE "amdgpu-codegenprepare"
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric using namespace llvm;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric namespace {
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric static cl::opt<bool> WidenLoads(
530b57cec5SDimitry Andric   "amdgpu-codegenprepare-widen-constant-loads",
540b57cec5SDimitry Andric   cl::desc("Widen sub-dword constant address space loads in AMDGPUCodeGenPrepare"),
550b57cec5SDimitry Andric   cl::ReallyHidden,
560b57cec5SDimitry Andric   cl::init(true));
570b57cec5SDimitry Andric 
58*8bcb0991SDimitry Andric static cl::opt<bool> UseMul24Intrin(
59*8bcb0991SDimitry Andric   "amdgpu-codegenprepare-mul24",
60*8bcb0991SDimitry Andric   cl::desc("Introduce mul24 intrinsics in AMDGPUCodeGenPrepare"),
61*8bcb0991SDimitry Andric   cl::ReallyHidden,
62*8bcb0991SDimitry Andric   cl::init(true));
63*8bcb0991SDimitry Andric 
640b57cec5SDimitry Andric class AMDGPUCodeGenPrepare : public FunctionPass,
650b57cec5SDimitry Andric                              public InstVisitor<AMDGPUCodeGenPrepare, bool> {
660b57cec5SDimitry Andric   const GCNSubtarget *ST = nullptr;
670b57cec5SDimitry Andric   AssumptionCache *AC = nullptr;
680b57cec5SDimitry Andric   LegacyDivergenceAnalysis *DA = nullptr;
690b57cec5SDimitry Andric   Module *Mod = nullptr;
700b57cec5SDimitry Andric   const DataLayout *DL = nullptr;
710b57cec5SDimitry Andric   bool HasUnsafeFPMath = false;
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   /// Copies exact/nsw/nuw flags (if any) from binary operation \p I to
740b57cec5SDimitry Andric   /// binary operation \p V.
750b57cec5SDimitry Andric   ///
760b57cec5SDimitry Andric   /// \returns Binary operation \p V.
770b57cec5SDimitry Andric   /// \returns \p T's base element bit width.
780b57cec5SDimitry Andric   unsigned getBaseElementBitWidth(const Type *T) const;
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   /// \returns Equivalent 32 bit integer type for given type \p T. For example,
810b57cec5SDimitry Andric   /// if \p T is i7, then i32 is returned; if \p T is <3 x i12>, then <3 x i32>
820b57cec5SDimitry Andric   /// is returned.
830b57cec5SDimitry Andric   Type *getI32Ty(IRBuilder<> &B, const Type *T) const;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   /// \returns True if binary operation \p I is a signed binary operation, false
860b57cec5SDimitry Andric   /// otherwise.
870b57cec5SDimitry Andric   bool isSigned(const BinaryOperator &I) const;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   /// \returns True if the condition of 'select' operation \p I comes from a
900b57cec5SDimitry Andric   /// signed 'icmp' operation, false otherwise.
910b57cec5SDimitry Andric   bool isSigned(const SelectInst &I) const;
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   /// \returns True if type \p T needs to be promoted to 32 bit integer type,
940b57cec5SDimitry Andric   /// false otherwise.
950b57cec5SDimitry Andric   bool needsPromotionToI32(const Type *T) const;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   /// Promotes uniform binary operation \p I to equivalent 32 bit binary
980b57cec5SDimitry Andric   /// operation.
990b57cec5SDimitry Andric   ///
1000b57cec5SDimitry Andric   /// \details \p I's base element bit width must be greater than 1 and less
1010b57cec5SDimitry Andric   /// than or equal 16. Promotion is done by sign or zero extending operands to
1020b57cec5SDimitry Andric   /// 32 bits, replacing \p I with equivalent 32 bit binary operation, and
1030b57cec5SDimitry Andric   /// truncating the result of 32 bit binary operation back to \p I's original
1040b57cec5SDimitry Andric   /// type. Division operation is not promoted.
1050b57cec5SDimitry Andric   ///
1060b57cec5SDimitry Andric   /// \returns True if \p I is promoted to equivalent 32 bit binary operation,
1070b57cec5SDimitry Andric   /// false otherwise.
1080b57cec5SDimitry Andric   bool promoteUniformOpToI32(BinaryOperator &I) const;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   /// Promotes uniform 'icmp' operation \p I to 32 bit 'icmp' operation.
1110b57cec5SDimitry Andric   ///
1120b57cec5SDimitry Andric   /// \details \p I's base element bit width must be greater than 1 and less
1130b57cec5SDimitry Andric   /// than or equal 16. Promotion is done by sign or zero extending operands to
1140b57cec5SDimitry Andric   /// 32 bits, and replacing \p I with 32 bit 'icmp' operation.
1150b57cec5SDimitry Andric   ///
1160b57cec5SDimitry Andric   /// \returns True.
1170b57cec5SDimitry Andric   bool promoteUniformOpToI32(ICmpInst &I) const;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   /// Promotes uniform 'select' operation \p I to 32 bit 'select'
1200b57cec5SDimitry Andric   /// operation.
1210b57cec5SDimitry Andric   ///
1220b57cec5SDimitry Andric   /// \details \p I's base element bit width must be greater than 1 and less
1230b57cec5SDimitry Andric   /// than or equal 16. Promotion is done by sign or zero extending operands to
1240b57cec5SDimitry Andric   /// 32 bits, replacing \p I with 32 bit 'select' operation, and truncating the
1250b57cec5SDimitry Andric   /// result of 32 bit 'select' operation back to \p I's original type.
1260b57cec5SDimitry Andric   ///
1270b57cec5SDimitry Andric   /// \returns True.
1280b57cec5SDimitry Andric   bool promoteUniformOpToI32(SelectInst &I) const;
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   /// Promotes uniform 'bitreverse' intrinsic \p I to 32 bit 'bitreverse'
1310b57cec5SDimitry Andric   /// intrinsic.
1320b57cec5SDimitry Andric   ///
1330b57cec5SDimitry Andric   /// \details \p I's base element bit width must be greater than 1 and less
1340b57cec5SDimitry Andric   /// than or equal 16. Promotion is done by zero extending the operand to 32
1350b57cec5SDimitry Andric   /// bits, replacing \p I with 32 bit 'bitreverse' intrinsic, shifting the
1360b57cec5SDimitry Andric   /// result of 32 bit 'bitreverse' intrinsic to the right with zero fill (the
1370b57cec5SDimitry Andric   /// shift amount is 32 minus \p I's base element bit width), and truncating
1380b57cec5SDimitry Andric   /// the result of the shift operation back to \p I's original type.
1390b57cec5SDimitry Andric   ///
1400b57cec5SDimitry Andric   /// \returns True.
1410b57cec5SDimitry Andric   bool promoteUniformBitreverseToI32(IntrinsicInst &I) const;
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   unsigned numBitsUnsigned(Value *Op, unsigned ScalarSize) const;
1450b57cec5SDimitry Andric   unsigned numBitsSigned(Value *Op, unsigned ScalarSize) const;
1460b57cec5SDimitry Andric   bool isI24(Value *V, unsigned ScalarSize) const;
1470b57cec5SDimitry Andric   bool isU24(Value *V, unsigned ScalarSize) const;
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   /// Replace mul instructions with llvm.amdgcn.mul.u24 or llvm.amdgcn.mul.s24.
1500b57cec5SDimitry Andric   /// SelectionDAG has an issue where an and asserting the bits are known
1510b57cec5SDimitry Andric   bool replaceMulWithMul24(BinaryOperator &I) const;
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   /// Expands 24 bit div or rem.
1540b57cec5SDimitry Andric   Value* expandDivRem24(IRBuilder<> &Builder, BinaryOperator &I,
1550b57cec5SDimitry Andric                         Value *Num, Value *Den,
1560b57cec5SDimitry Andric                         bool IsDiv, bool IsSigned) const;
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   /// Expands 32 bit div or rem.
1590b57cec5SDimitry Andric   Value* expandDivRem32(IRBuilder<> &Builder, BinaryOperator &I,
1600b57cec5SDimitry Andric                         Value *Num, Value *Den) const;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   /// Widen a scalar load.
1630b57cec5SDimitry Andric   ///
1640b57cec5SDimitry Andric   /// \details \p Widen scalar load for uniform, small type loads from constant
1650b57cec5SDimitry Andric   //  memory / to a full 32-bits and then truncate the input to allow a scalar
1660b57cec5SDimitry Andric   //  load instead of a vector load.
1670b57cec5SDimitry Andric   //
1680b57cec5SDimitry Andric   /// \returns True.
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   bool canWidenScalarExtLoad(LoadInst &I) const;
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric public:
1730b57cec5SDimitry Andric   static char ID;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   AMDGPUCodeGenPrepare() : FunctionPass(ID) {}
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   bool visitFDiv(BinaryOperator &I);
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   bool visitInstruction(Instruction &I) { return false; }
1800b57cec5SDimitry Andric   bool visitBinaryOperator(BinaryOperator &I);
1810b57cec5SDimitry Andric   bool visitLoadInst(LoadInst &I);
1820b57cec5SDimitry Andric   bool visitICmpInst(ICmpInst &I);
1830b57cec5SDimitry Andric   bool visitSelectInst(SelectInst &I);
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   bool visitIntrinsicInst(IntrinsicInst &I);
1860b57cec5SDimitry Andric   bool visitBitreverseIntrinsicInst(IntrinsicInst &I);
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   bool doInitialization(Module &M) override;
1890b57cec5SDimitry Andric   bool runOnFunction(Function &F) override;
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   StringRef getPassName() const override { return "AMDGPU IR optimizations"; }
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
1940b57cec5SDimitry Andric     AU.addRequired<AssumptionCacheTracker>();
1950b57cec5SDimitry Andric     AU.addRequired<LegacyDivergenceAnalysis>();
1960b57cec5SDimitry Andric     AU.setPreservesAll();
1970b57cec5SDimitry Andric  }
1980b57cec5SDimitry Andric };
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric } // end anonymous namespace
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric unsigned AMDGPUCodeGenPrepare::getBaseElementBitWidth(const Type *T) const {
2030b57cec5SDimitry Andric   assert(needsPromotionToI32(T) && "T does not need promotion to i32");
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   if (T->isIntegerTy())
2060b57cec5SDimitry Andric     return T->getIntegerBitWidth();
2070b57cec5SDimitry Andric   return cast<VectorType>(T)->getElementType()->getIntegerBitWidth();
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric Type *AMDGPUCodeGenPrepare::getI32Ty(IRBuilder<> &B, const Type *T) const {
2110b57cec5SDimitry Andric   assert(needsPromotionToI32(T) && "T does not need promotion to i32");
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   if (T->isIntegerTy())
2140b57cec5SDimitry Andric     return B.getInt32Ty();
2150b57cec5SDimitry Andric   return VectorType::get(B.getInt32Ty(), cast<VectorType>(T)->getNumElements());
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::isSigned(const BinaryOperator &I) const {
2190b57cec5SDimitry Andric   return I.getOpcode() == Instruction::AShr ||
2200b57cec5SDimitry Andric       I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::SRem;
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::isSigned(const SelectInst &I) const {
2240b57cec5SDimitry Andric   return isa<ICmpInst>(I.getOperand(0)) ?
2250b57cec5SDimitry Andric       cast<ICmpInst>(I.getOperand(0))->isSigned() : false;
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::needsPromotionToI32(const Type *T) const {
2290b57cec5SDimitry Andric   const IntegerType *IntTy = dyn_cast<IntegerType>(T);
2300b57cec5SDimitry Andric   if (IntTy && IntTy->getBitWidth() > 1 && IntTy->getBitWidth() <= 16)
2310b57cec5SDimitry Andric     return true;
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric   if (const VectorType *VT = dyn_cast<VectorType>(T)) {
2340b57cec5SDimitry Andric     // TODO: The set of packed operations is more limited, so may want to
2350b57cec5SDimitry Andric     // promote some anyway.
2360b57cec5SDimitry Andric     if (ST->hasVOP3PInsts())
2370b57cec5SDimitry Andric       return false;
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric     return needsPromotionToI32(VT->getElementType());
2400b57cec5SDimitry Andric   }
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   return false;
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric // Return true if the op promoted to i32 should have nsw set.
2460b57cec5SDimitry Andric static bool promotedOpIsNSW(const Instruction &I) {
2470b57cec5SDimitry Andric   switch (I.getOpcode()) {
2480b57cec5SDimitry Andric   case Instruction::Shl:
2490b57cec5SDimitry Andric   case Instruction::Add:
2500b57cec5SDimitry Andric   case Instruction::Sub:
2510b57cec5SDimitry Andric     return true;
2520b57cec5SDimitry Andric   case Instruction::Mul:
2530b57cec5SDimitry Andric     return I.hasNoUnsignedWrap();
2540b57cec5SDimitry Andric   default:
2550b57cec5SDimitry Andric     return false;
2560b57cec5SDimitry Andric   }
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric // Return true if the op promoted to i32 should have nuw set.
2600b57cec5SDimitry Andric static bool promotedOpIsNUW(const Instruction &I) {
2610b57cec5SDimitry Andric   switch (I.getOpcode()) {
2620b57cec5SDimitry Andric   case Instruction::Shl:
2630b57cec5SDimitry Andric   case Instruction::Add:
2640b57cec5SDimitry Andric   case Instruction::Mul:
2650b57cec5SDimitry Andric     return true;
2660b57cec5SDimitry Andric   case Instruction::Sub:
2670b57cec5SDimitry Andric     return I.hasNoUnsignedWrap();
2680b57cec5SDimitry Andric   default:
2690b57cec5SDimitry Andric     return false;
2700b57cec5SDimitry Andric   }
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::canWidenScalarExtLoad(LoadInst &I) const {
2740b57cec5SDimitry Andric   Type *Ty = I.getType();
2750b57cec5SDimitry Andric   const DataLayout &DL = Mod->getDataLayout();
2760b57cec5SDimitry Andric   int TySize = DL.getTypeSizeInBits(Ty);
2770b57cec5SDimitry Andric   unsigned Align = I.getAlignment() ?
2780b57cec5SDimitry Andric                    I.getAlignment() : DL.getABITypeAlignment(Ty);
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   return I.isSimple() && TySize < 32 && Align >= 4 && DA->isUniform(&I);
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(BinaryOperator &I) const {
2840b57cec5SDimitry Andric   assert(needsPromotionToI32(I.getType()) &&
2850b57cec5SDimitry Andric          "I does not need promotion to i32");
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   if (I.getOpcode() == Instruction::SDiv ||
2880b57cec5SDimitry Andric       I.getOpcode() == Instruction::UDiv ||
2890b57cec5SDimitry Andric       I.getOpcode() == Instruction::SRem ||
2900b57cec5SDimitry Andric       I.getOpcode() == Instruction::URem)
2910b57cec5SDimitry Andric     return false;
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   IRBuilder<> Builder(&I);
2940b57cec5SDimitry Andric   Builder.SetCurrentDebugLocation(I.getDebugLoc());
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   Type *I32Ty = getI32Ty(Builder, I.getType());
2970b57cec5SDimitry Andric   Value *ExtOp0 = nullptr;
2980b57cec5SDimitry Andric   Value *ExtOp1 = nullptr;
2990b57cec5SDimitry Andric   Value *ExtRes = nullptr;
3000b57cec5SDimitry Andric   Value *TruncRes = nullptr;
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric   if (isSigned(I)) {
3030b57cec5SDimitry Andric     ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
3040b57cec5SDimitry Andric     ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
3050b57cec5SDimitry Andric   } else {
3060b57cec5SDimitry Andric     ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
3070b57cec5SDimitry Andric     ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
3080b57cec5SDimitry Andric   }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   ExtRes = Builder.CreateBinOp(I.getOpcode(), ExtOp0, ExtOp1);
3110b57cec5SDimitry Andric   if (Instruction *Inst = dyn_cast<Instruction>(ExtRes)) {
3120b57cec5SDimitry Andric     if (promotedOpIsNSW(cast<Instruction>(I)))
3130b57cec5SDimitry Andric       Inst->setHasNoSignedWrap();
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric     if (promotedOpIsNUW(cast<Instruction>(I)))
3160b57cec5SDimitry Andric       Inst->setHasNoUnsignedWrap();
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric     if (const auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3190b57cec5SDimitry Andric       Inst->setIsExact(ExactOp->isExact());
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   I.replaceAllUsesWith(TruncRes);
3250b57cec5SDimitry Andric   I.eraseFromParent();
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric   return true;
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(ICmpInst &I) const {
3310b57cec5SDimitry Andric   assert(needsPromotionToI32(I.getOperand(0)->getType()) &&
3320b57cec5SDimitry Andric          "I does not need promotion to i32");
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   IRBuilder<> Builder(&I);
3350b57cec5SDimitry Andric   Builder.SetCurrentDebugLocation(I.getDebugLoc());
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   Type *I32Ty = getI32Ty(Builder, I.getOperand(0)->getType());
3380b57cec5SDimitry Andric   Value *ExtOp0 = nullptr;
3390b57cec5SDimitry Andric   Value *ExtOp1 = nullptr;
3400b57cec5SDimitry Andric   Value *NewICmp  = nullptr;
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   if (I.isSigned()) {
3430b57cec5SDimitry Andric     ExtOp0 = Builder.CreateSExt(I.getOperand(0), I32Ty);
3440b57cec5SDimitry Andric     ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
3450b57cec5SDimitry Andric   } else {
3460b57cec5SDimitry Andric     ExtOp0 = Builder.CreateZExt(I.getOperand(0), I32Ty);
3470b57cec5SDimitry Andric     ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
3480b57cec5SDimitry Andric   }
3490b57cec5SDimitry Andric   NewICmp = Builder.CreateICmp(I.getPredicate(), ExtOp0, ExtOp1);
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric   I.replaceAllUsesWith(NewICmp);
3520b57cec5SDimitry Andric   I.eraseFromParent();
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric   return true;
3550b57cec5SDimitry Andric }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(SelectInst &I) const {
3580b57cec5SDimitry Andric   assert(needsPromotionToI32(I.getType()) &&
3590b57cec5SDimitry Andric          "I does not need promotion to i32");
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   IRBuilder<> Builder(&I);
3620b57cec5SDimitry Andric   Builder.SetCurrentDebugLocation(I.getDebugLoc());
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   Type *I32Ty = getI32Ty(Builder, I.getType());
3650b57cec5SDimitry Andric   Value *ExtOp1 = nullptr;
3660b57cec5SDimitry Andric   Value *ExtOp2 = nullptr;
3670b57cec5SDimitry Andric   Value *ExtRes = nullptr;
3680b57cec5SDimitry Andric   Value *TruncRes = nullptr;
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   if (isSigned(I)) {
3710b57cec5SDimitry Andric     ExtOp1 = Builder.CreateSExt(I.getOperand(1), I32Ty);
3720b57cec5SDimitry Andric     ExtOp2 = Builder.CreateSExt(I.getOperand(2), I32Ty);
3730b57cec5SDimitry Andric   } else {
3740b57cec5SDimitry Andric     ExtOp1 = Builder.CreateZExt(I.getOperand(1), I32Ty);
3750b57cec5SDimitry Andric     ExtOp2 = Builder.CreateZExt(I.getOperand(2), I32Ty);
3760b57cec5SDimitry Andric   }
3770b57cec5SDimitry Andric   ExtRes = Builder.CreateSelect(I.getOperand(0), ExtOp1, ExtOp2);
3780b57cec5SDimitry Andric   TruncRes = Builder.CreateTrunc(ExtRes, I.getType());
3790b57cec5SDimitry Andric 
3800b57cec5SDimitry Andric   I.replaceAllUsesWith(TruncRes);
3810b57cec5SDimitry Andric   I.eraseFromParent();
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   return true;
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::promoteUniformBitreverseToI32(
3870b57cec5SDimitry Andric     IntrinsicInst &I) const {
3880b57cec5SDimitry Andric   assert(I.getIntrinsicID() == Intrinsic::bitreverse &&
3890b57cec5SDimitry Andric          "I must be bitreverse intrinsic");
3900b57cec5SDimitry Andric   assert(needsPromotionToI32(I.getType()) &&
3910b57cec5SDimitry Andric          "I does not need promotion to i32");
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   IRBuilder<> Builder(&I);
3940b57cec5SDimitry Andric   Builder.SetCurrentDebugLocation(I.getDebugLoc());
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   Type *I32Ty = getI32Ty(Builder, I.getType());
3970b57cec5SDimitry Andric   Function *I32 =
3980b57cec5SDimitry Andric       Intrinsic::getDeclaration(Mod, Intrinsic::bitreverse, { I32Ty });
3990b57cec5SDimitry Andric   Value *ExtOp = Builder.CreateZExt(I.getOperand(0), I32Ty);
4000b57cec5SDimitry Andric   Value *ExtRes = Builder.CreateCall(I32, { ExtOp });
4010b57cec5SDimitry Andric   Value *LShrOp =
4020b57cec5SDimitry Andric       Builder.CreateLShr(ExtRes, 32 - getBaseElementBitWidth(I.getType()));
4030b57cec5SDimitry Andric   Value *TruncRes =
4040b57cec5SDimitry Andric       Builder.CreateTrunc(LShrOp, I.getType());
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric   I.replaceAllUsesWith(TruncRes);
4070b57cec5SDimitry Andric   I.eraseFromParent();
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   return true;
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric unsigned AMDGPUCodeGenPrepare::numBitsUnsigned(Value *Op,
4130b57cec5SDimitry Andric                                                unsigned ScalarSize) const {
4140b57cec5SDimitry Andric   KnownBits Known = computeKnownBits(Op, *DL, 0, AC);
4150b57cec5SDimitry Andric   return ScalarSize - Known.countMinLeadingZeros();
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric unsigned AMDGPUCodeGenPrepare::numBitsSigned(Value *Op,
4190b57cec5SDimitry Andric                                              unsigned ScalarSize) const {
4200b57cec5SDimitry Andric   // In order for this to be a signed 24-bit value, bit 23, must
4210b57cec5SDimitry Andric   // be a sign bit.
4220b57cec5SDimitry Andric   return ScalarSize - ComputeNumSignBits(Op, *DL, 0, AC);
4230b57cec5SDimitry Andric }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::isI24(Value *V, unsigned ScalarSize) const {
4260b57cec5SDimitry Andric   return ScalarSize >= 24 && // Types less than 24-bit should be treated
4270b57cec5SDimitry Andric                                      // as unsigned 24-bit values.
4280b57cec5SDimitry Andric     numBitsSigned(V, ScalarSize) < 24;
4290b57cec5SDimitry Andric }
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::isU24(Value *V, unsigned ScalarSize) const {
4320b57cec5SDimitry Andric   return numBitsUnsigned(V, ScalarSize) <= 24;
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric static void extractValues(IRBuilder<> &Builder,
4360b57cec5SDimitry Andric                           SmallVectorImpl<Value *> &Values, Value *V) {
4370b57cec5SDimitry Andric   VectorType *VT = dyn_cast<VectorType>(V->getType());
4380b57cec5SDimitry Andric   if (!VT) {
4390b57cec5SDimitry Andric     Values.push_back(V);
4400b57cec5SDimitry Andric     return;
4410b57cec5SDimitry Andric   }
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric   for (int I = 0, E = VT->getNumElements(); I != E; ++I)
4440b57cec5SDimitry Andric     Values.push_back(Builder.CreateExtractElement(V, I));
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric static Value *insertValues(IRBuilder<> &Builder,
4480b57cec5SDimitry Andric                            Type *Ty,
4490b57cec5SDimitry Andric                            SmallVectorImpl<Value *> &Values) {
4500b57cec5SDimitry Andric   if (Values.size() == 1)
4510b57cec5SDimitry Andric     return Values[0];
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric   Value *NewVal = UndefValue::get(Ty);
4540b57cec5SDimitry Andric   for (int I = 0, E = Values.size(); I != E; ++I)
4550b57cec5SDimitry Andric     NewVal = Builder.CreateInsertElement(NewVal, Values[I], I);
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   return NewVal;
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::replaceMulWithMul24(BinaryOperator &I) const {
4610b57cec5SDimitry Andric   if (I.getOpcode() != Instruction::Mul)
4620b57cec5SDimitry Andric     return false;
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric   Type *Ty = I.getType();
4650b57cec5SDimitry Andric   unsigned Size = Ty->getScalarSizeInBits();
4660b57cec5SDimitry Andric   if (Size <= 16 && ST->has16BitInsts())
4670b57cec5SDimitry Andric     return false;
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric   // Prefer scalar if this could be s_mul_i32
4700b57cec5SDimitry Andric   if (DA->isUniform(&I))
4710b57cec5SDimitry Andric     return false;
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric   Value *LHS = I.getOperand(0);
4740b57cec5SDimitry Andric   Value *RHS = I.getOperand(1);
4750b57cec5SDimitry Andric   IRBuilder<> Builder(&I);
4760b57cec5SDimitry Andric   Builder.SetCurrentDebugLocation(I.getDebugLoc());
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric   Intrinsic::ID IntrID = Intrinsic::not_intrinsic;
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   // TODO: Should this try to match mulhi24?
4810b57cec5SDimitry Andric   if (ST->hasMulU24() && isU24(LHS, Size) && isU24(RHS, Size)) {
4820b57cec5SDimitry Andric     IntrID = Intrinsic::amdgcn_mul_u24;
4830b57cec5SDimitry Andric   } else if (ST->hasMulI24() && isI24(LHS, Size) && isI24(RHS, Size)) {
4840b57cec5SDimitry Andric     IntrID = Intrinsic::amdgcn_mul_i24;
4850b57cec5SDimitry Andric   } else
4860b57cec5SDimitry Andric     return false;
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric   SmallVector<Value *, 4> LHSVals;
4890b57cec5SDimitry Andric   SmallVector<Value *, 4> RHSVals;
4900b57cec5SDimitry Andric   SmallVector<Value *, 4> ResultVals;
4910b57cec5SDimitry Andric   extractValues(Builder, LHSVals, LHS);
4920b57cec5SDimitry Andric   extractValues(Builder, RHSVals, RHS);
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric   IntegerType *I32Ty = Builder.getInt32Ty();
4960b57cec5SDimitry Andric   FunctionCallee Intrin = Intrinsic::getDeclaration(Mod, IntrID);
4970b57cec5SDimitry Andric   for (int I = 0, E = LHSVals.size(); I != E; ++I) {
4980b57cec5SDimitry Andric     Value *LHS, *RHS;
4990b57cec5SDimitry Andric     if (IntrID == Intrinsic::amdgcn_mul_u24) {
5000b57cec5SDimitry Andric       LHS = Builder.CreateZExtOrTrunc(LHSVals[I], I32Ty);
5010b57cec5SDimitry Andric       RHS = Builder.CreateZExtOrTrunc(RHSVals[I], I32Ty);
5020b57cec5SDimitry Andric     } else {
5030b57cec5SDimitry Andric       LHS = Builder.CreateSExtOrTrunc(LHSVals[I], I32Ty);
5040b57cec5SDimitry Andric       RHS = Builder.CreateSExtOrTrunc(RHSVals[I], I32Ty);
5050b57cec5SDimitry Andric     }
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric     Value *Result = Builder.CreateCall(Intrin, {LHS, RHS});
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric     if (IntrID == Intrinsic::amdgcn_mul_u24) {
5100b57cec5SDimitry Andric       ResultVals.push_back(Builder.CreateZExtOrTrunc(Result,
5110b57cec5SDimitry Andric                                                      LHSVals[I]->getType()));
5120b57cec5SDimitry Andric     } else {
5130b57cec5SDimitry Andric       ResultVals.push_back(Builder.CreateSExtOrTrunc(Result,
5140b57cec5SDimitry Andric                                                      LHSVals[I]->getType()));
5150b57cec5SDimitry Andric     }
5160b57cec5SDimitry Andric   }
5170b57cec5SDimitry Andric 
518*8bcb0991SDimitry Andric   Value *NewVal = insertValues(Builder, Ty, ResultVals);
519*8bcb0991SDimitry Andric   NewVal->takeName(&I);
520*8bcb0991SDimitry Andric   I.replaceAllUsesWith(NewVal);
5210b57cec5SDimitry Andric   I.eraseFromParent();
5220b57cec5SDimitry Andric 
5230b57cec5SDimitry Andric   return true;
5240b57cec5SDimitry Andric }
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric static bool shouldKeepFDivF32(Value *Num, bool UnsafeDiv, bool HasDenormals) {
5270b57cec5SDimitry Andric   const ConstantFP *CNum = dyn_cast<ConstantFP>(Num);
5280b57cec5SDimitry Andric   if (!CNum)
5290b57cec5SDimitry Andric     return HasDenormals;
5300b57cec5SDimitry Andric 
5310b57cec5SDimitry Andric   if (UnsafeDiv)
5320b57cec5SDimitry Andric     return true;
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   bool IsOne = CNum->isExactlyValue(+1.0) || CNum->isExactlyValue(-1.0);
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric   // Reciprocal f32 is handled separately without denormals.
5370b57cec5SDimitry Andric   return HasDenormals ^ IsOne;
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric // Insert an intrinsic for fast fdiv for safe math situations where we can
5410b57cec5SDimitry Andric // reduce precision. Leave fdiv for situations where the generic node is
5420b57cec5SDimitry Andric // expected to be optimized.
5430b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::visitFDiv(BinaryOperator &FDiv) {
5440b57cec5SDimitry Andric   Type *Ty = FDiv.getType();
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric   if (!Ty->getScalarType()->isFloatTy())
5470b57cec5SDimitry Andric     return false;
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric   MDNode *FPMath = FDiv.getMetadata(LLVMContext::MD_fpmath);
5500b57cec5SDimitry Andric   if (!FPMath)
5510b57cec5SDimitry Andric     return false;
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric   const FPMathOperator *FPOp = cast<const FPMathOperator>(&FDiv);
5540b57cec5SDimitry Andric   float ULP = FPOp->getFPAccuracy();
5550b57cec5SDimitry Andric   if (ULP < 2.5f)
5560b57cec5SDimitry Andric     return false;
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric   FastMathFlags FMF = FPOp->getFastMathFlags();
5590b57cec5SDimitry Andric   bool UnsafeDiv = HasUnsafeFPMath || FMF.isFast() ||
5600b57cec5SDimitry Andric                                       FMF.allowReciprocal();
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   // With UnsafeDiv node will be optimized to just rcp and mul.
5630b57cec5SDimitry Andric   if (UnsafeDiv)
5640b57cec5SDimitry Andric     return false;
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric   IRBuilder<> Builder(FDiv.getParent(), std::next(FDiv.getIterator()), FPMath);
5670b57cec5SDimitry Andric   Builder.setFastMathFlags(FMF);
5680b57cec5SDimitry Andric   Builder.SetCurrentDebugLocation(FDiv.getDebugLoc());
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric   Function *Decl = Intrinsic::getDeclaration(Mod, Intrinsic::amdgcn_fdiv_fast);
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   Value *Num = FDiv.getOperand(0);
5730b57cec5SDimitry Andric   Value *Den = FDiv.getOperand(1);
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric   Value *NewFDiv = nullptr;
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric   bool HasDenormals = ST->hasFP32Denormals();
5780b57cec5SDimitry Andric   if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
5790b57cec5SDimitry Andric     NewFDiv = UndefValue::get(VT);
5800b57cec5SDimitry Andric 
5810b57cec5SDimitry Andric     // FIXME: Doesn't do the right thing for cases where the vector is partially
5820b57cec5SDimitry Andric     // constant. This works when the scalarizer pass is run first.
5830b57cec5SDimitry Andric     for (unsigned I = 0, E = VT->getNumElements(); I != E; ++I) {
5840b57cec5SDimitry Andric       Value *NumEltI = Builder.CreateExtractElement(Num, I);
5850b57cec5SDimitry Andric       Value *DenEltI = Builder.CreateExtractElement(Den, I);
5860b57cec5SDimitry Andric       Value *NewElt;
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric       if (shouldKeepFDivF32(NumEltI, UnsafeDiv, HasDenormals)) {
5890b57cec5SDimitry Andric         NewElt = Builder.CreateFDiv(NumEltI, DenEltI);
5900b57cec5SDimitry Andric       } else {
5910b57cec5SDimitry Andric         NewElt = Builder.CreateCall(Decl, { NumEltI, DenEltI });
5920b57cec5SDimitry Andric       }
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric       NewFDiv = Builder.CreateInsertElement(NewFDiv, NewElt, I);
5950b57cec5SDimitry Andric     }
5960b57cec5SDimitry Andric   } else {
5970b57cec5SDimitry Andric     if (!shouldKeepFDivF32(Num, UnsafeDiv, HasDenormals))
5980b57cec5SDimitry Andric       NewFDiv = Builder.CreateCall(Decl, { Num, Den });
5990b57cec5SDimitry Andric   }
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric   if (NewFDiv) {
6020b57cec5SDimitry Andric     FDiv.replaceAllUsesWith(NewFDiv);
6030b57cec5SDimitry Andric     NewFDiv->takeName(&FDiv);
6040b57cec5SDimitry Andric     FDiv.eraseFromParent();
6050b57cec5SDimitry Andric   }
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric   return !!NewFDiv;
6080b57cec5SDimitry Andric }
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric static bool hasUnsafeFPMath(const Function &F) {
6110b57cec5SDimitry Andric   Attribute Attr = F.getFnAttribute("unsafe-fp-math");
6120b57cec5SDimitry Andric   return Attr.getValueAsString() == "true";
6130b57cec5SDimitry Andric }
6140b57cec5SDimitry Andric 
6150b57cec5SDimitry Andric static std::pair<Value*, Value*> getMul64(IRBuilder<> &Builder,
6160b57cec5SDimitry Andric                                           Value *LHS, Value *RHS) {
6170b57cec5SDimitry Andric   Type *I32Ty = Builder.getInt32Ty();
6180b57cec5SDimitry Andric   Type *I64Ty = Builder.getInt64Ty();
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric   Value *LHS_EXT64 = Builder.CreateZExt(LHS, I64Ty);
6210b57cec5SDimitry Andric   Value *RHS_EXT64 = Builder.CreateZExt(RHS, I64Ty);
6220b57cec5SDimitry Andric   Value *MUL64 = Builder.CreateMul(LHS_EXT64, RHS_EXT64);
6230b57cec5SDimitry Andric   Value *Lo = Builder.CreateTrunc(MUL64, I32Ty);
6240b57cec5SDimitry Andric   Value *Hi = Builder.CreateLShr(MUL64, Builder.getInt64(32));
6250b57cec5SDimitry Andric   Hi = Builder.CreateTrunc(Hi, I32Ty);
6260b57cec5SDimitry Andric   return std::make_pair(Lo, Hi);
6270b57cec5SDimitry Andric }
6280b57cec5SDimitry Andric 
6290b57cec5SDimitry Andric static Value* getMulHu(IRBuilder<> &Builder, Value *LHS, Value *RHS) {
6300b57cec5SDimitry Andric   return getMul64(Builder, LHS, RHS).second;
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric // The fractional part of a float is enough to accurately represent up to
6340b57cec5SDimitry Andric // a 24-bit signed integer.
6350b57cec5SDimitry Andric Value* AMDGPUCodeGenPrepare::expandDivRem24(IRBuilder<> &Builder,
6360b57cec5SDimitry Andric                                             BinaryOperator &I,
6370b57cec5SDimitry Andric                                             Value *Num, Value *Den,
6380b57cec5SDimitry Andric                                             bool IsDiv, bool IsSigned) const {
6390b57cec5SDimitry Andric   assert(Num->getType()->isIntegerTy(32));
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric   const DataLayout &DL = Mod->getDataLayout();
6420b57cec5SDimitry Andric   unsigned LHSSignBits = ComputeNumSignBits(Num, DL, 0, AC, &I);
6430b57cec5SDimitry Andric   if (LHSSignBits < 9)
6440b57cec5SDimitry Andric     return nullptr;
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric   unsigned RHSSignBits = ComputeNumSignBits(Den, DL, 0, AC, &I);
6470b57cec5SDimitry Andric   if (RHSSignBits < 9)
6480b57cec5SDimitry Andric     return nullptr;
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric   unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
6520b57cec5SDimitry Andric   unsigned DivBits = 32 - SignBits;
6530b57cec5SDimitry Andric   if (IsSigned)
6540b57cec5SDimitry Andric     ++DivBits;
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric   Type *Ty = Num->getType();
6570b57cec5SDimitry Andric   Type *I32Ty = Builder.getInt32Ty();
6580b57cec5SDimitry Andric   Type *F32Ty = Builder.getFloatTy();
6590b57cec5SDimitry Andric   ConstantInt *One = Builder.getInt32(1);
6600b57cec5SDimitry Andric   Value *JQ = One;
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   if (IsSigned) {
6630b57cec5SDimitry Andric     // char|short jq = ia ^ ib;
6640b57cec5SDimitry Andric     JQ = Builder.CreateXor(Num, Den);
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric     // jq = jq >> (bitsize - 2)
6670b57cec5SDimitry Andric     JQ = Builder.CreateAShr(JQ, Builder.getInt32(30));
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric     // jq = jq | 0x1
6700b57cec5SDimitry Andric     JQ = Builder.CreateOr(JQ, One);
6710b57cec5SDimitry Andric   }
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   // int ia = (int)LHS;
6740b57cec5SDimitry Andric   Value *IA = Num;
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric   // int ib, (int)RHS;
6770b57cec5SDimitry Andric   Value *IB = Den;
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric   // float fa = (float)ia;
6800b57cec5SDimitry Andric   Value *FA = IsSigned ? Builder.CreateSIToFP(IA, F32Ty)
6810b57cec5SDimitry Andric                        : Builder.CreateUIToFP(IA, F32Ty);
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   // float fb = (float)ib;
6840b57cec5SDimitry Andric   Value *FB = IsSigned ? Builder.CreateSIToFP(IB,F32Ty)
6850b57cec5SDimitry Andric                        : Builder.CreateUIToFP(IB,F32Ty);
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   Value *RCP = Builder.CreateFDiv(ConstantFP::get(F32Ty, 1.0), FB);
6880b57cec5SDimitry Andric   Value *FQM = Builder.CreateFMul(FA, RCP);
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   // fq = trunc(fqm);
6910b57cec5SDimitry Andric   CallInst *FQ = Builder.CreateUnaryIntrinsic(Intrinsic::trunc, FQM);
6920b57cec5SDimitry Andric   FQ->copyFastMathFlags(Builder.getFastMathFlags());
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric   // float fqneg = -fq;
6950b57cec5SDimitry Andric   Value *FQNeg = Builder.CreateFNeg(FQ);
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric   // float fr = mad(fqneg, fb, fa);
6980b57cec5SDimitry Andric   Value *FR = Builder.CreateIntrinsic(Intrinsic::amdgcn_fmad_ftz,
6990b57cec5SDimitry Andric                                       {FQNeg->getType()}, {FQNeg, FB, FA}, FQ);
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric   // int iq = (int)fq;
7020b57cec5SDimitry Andric   Value *IQ = IsSigned ? Builder.CreateFPToSI(FQ, I32Ty)
7030b57cec5SDimitry Andric                        : Builder.CreateFPToUI(FQ, I32Ty);
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric   // fr = fabs(fr);
7060b57cec5SDimitry Andric   FR = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FR, FQ);
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric   // fb = fabs(fb);
7090b57cec5SDimitry Andric   FB = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FB, FQ);
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric   // int cv = fr >= fb;
7120b57cec5SDimitry Andric   Value *CV = Builder.CreateFCmpOGE(FR, FB);
7130b57cec5SDimitry Andric 
7140b57cec5SDimitry Andric   // jq = (cv ? jq : 0);
7150b57cec5SDimitry Andric   JQ = Builder.CreateSelect(CV, JQ, Builder.getInt32(0));
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric   // dst = iq + jq;
7180b57cec5SDimitry Andric   Value *Div = Builder.CreateAdd(IQ, JQ);
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric   Value *Res = Div;
7210b57cec5SDimitry Andric   if (!IsDiv) {
7220b57cec5SDimitry Andric     // Rem needs compensation, it's easier to recompute it
7230b57cec5SDimitry Andric     Value *Rem = Builder.CreateMul(Div, Den);
7240b57cec5SDimitry Andric     Res = Builder.CreateSub(Num, Rem);
7250b57cec5SDimitry Andric   }
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric   // Truncate to number of bits this divide really is.
7280b57cec5SDimitry Andric   if (IsSigned) {
7290b57cec5SDimitry Andric     Res = Builder.CreateTrunc(Res, Builder.getIntNTy(DivBits));
7300b57cec5SDimitry Andric     Res = Builder.CreateSExt(Res, Ty);
7310b57cec5SDimitry Andric   } else {
7320b57cec5SDimitry Andric     ConstantInt *TruncMask = Builder.getInt32((UINT64_C(1) << DivBits) - 1);
7330b57cec5SDimitry Andric     Res = Builder.CreateAnd(Res, TruncMask);
7340b57cec5SDimitry Andric   }
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric   return Res;
7370b57cec5SDimitry Andric }
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric Value* AMDGPUCodeGenPrepare::expandDivRem32(IRBuilder<> &Builder,
7400b57cec5SDimitry Andric                                             BinaryOperator &I,
7410b57cec5SDimitry Andric                                             Value *Num, Value *Den) const {
7420b57cec5SDimitry Andric   Instruction::BinaryOps Opc = I.getOpcode();
7430b57cec5SDimitry Andric   assert(Opc == Instruction::URem || Opc == Instruction::UDiv ||
7440b57cec5SDimitry Andric          Opc == Instruction::SRem || Opc == Instruction::SDiv);
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric   FastMathFlags FMF;
7470b57cec5SDimitry Andric   FMF.setFast();
7480b57cec5SDimitry Andric   Builder.setFastMathFlags(FMF);
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric   if (isa<Constant>(Den))
7510b57cec5SDimitry Andric     return nullptr; // Keep it for optimization
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric   bool IsDiv = Opc == Instruction::UDiv || Opc == Instruction::SDiv;
7540b57cec5SDimitry Andric   bool IsSigned = Opc == Instruction::SRem || Opc == Instruction::SDiv;
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric   Type *Ty = Num->getType();
7570b57cec5SDimitry Andric   Type *I32Ty = Builder.getInt32Ty();
7580b57cec5SDimitry Andric   Type *F32Ty = Builder.getFloatTy();
7590b57cec5SDimitry Andric 
7600b57cec5SDimitry Andric   if (Ty->getScalarSizeInBits() < 32) {
7610b57cec5SDimitry Andric     if (IsSigned) {
7620b57cec5SDimitry Andric       Num = Builder.CreateSExt(Num, I32Ty);
7630b57cec5SDimitry Andric       Den = Builder.CreateSExt(Den, I32Ty);
7640b57cec5SDimitry Andric     } else {
7650b57cec5SDimitry Andric       Num = Builder.CreateZExt(Num, I32Ty);
7660b57cec5SDimitry Andric       Den = Builder.CreateZExt(Den, I32Ty);
7670b57cec5SDimitry Andric     }
7680b57cec5SDimitry Andric   }
7690b57cec5SDimitry Andric 
7700b57cec5SDimitry Andric   if (Value *Res = expandDivRem24(Builder, I, Num, Den, IsDiv, IsSigned)) {
7710b57cec5SDimitry Andric     Res = Builder.CreateTrunc(Res, Ty);
7720b57cec5SDimitry Andric     return Res;
7730b57cec5SDimitry Andric   }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric   ConstantInt *Zero = Builder.getInt32(0);
7760b57cec5SDimitry Andric   ConstantInt *One = Builder.getInt32(1);
7770b57cec5SDimitry Andric   ConstantInt *MinusOne = Builder.getInt32(~0);
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric   Value *Sign = nullptr;
7800b57cec5SDimitry Andric   if (IsSigned) {
7810b57cec5SDimitry Andric     ConstantInt *K31 = Builder.getInt32(31);
7820b57cec5SDimitry Andric     Value *LHSign = Builder.CreateAShr(Num, K31);
7830b57cec5SDimitry Andric     Value *RHSign = Builder.CreateAShr(Den, K31);
7840b57cec5SDimitry Andric     // Remainder sign is the same as LHS
7850b57cec5SDimitry Andric     Sign = IsDiv ? Builder.CreateXor(LHSign, RHSign) : LHSign;
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric     Num = Builder.CreateAdd(Num, LHSign);
7880b57cec5SDimitry Andric     Den = Builder.CreateAdd(Den, RHSign);
7890b57cec5SDimitry Andric 
7900b57cec5SDimitry Andric     Num = Builder.CreateXor(Num, LHSign);
7910b57cec5SDimitry Andric     Den = Builder.CreateXor(Den, RHSign);
7920b57cec5SDimitry Andric   }
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric   // RCP =  URECIP(Den) = 2^32 / Den + e
7950b57cec5SDimitry Andric   // e is rounding error.
7960b57cec5SDimitry Andric   Value *DEN_F32 = Builder.CreateUIToFP(Den, F32Ty);
7970b57cec5SDimitry Andric   Value *RCP_F32 = Builder.CreateFDiv(ConstantFP::get(F32Ty, 1.0), DEN_F32);
7980b57cec5SDimitry Andric   Constant *UINT_MAX_PLUS_1 = ConstantFP::get(F32Ty, BitsToFloat(0x4f800000));
7990b57cec5SDimitry Andric   Value *RCP_SCALE = Builder.CreateFMul(RCP_F32, UINT_MAX_PLUS_1);
8000b57cec5SDimitry Andric   Value *RCP = Builder.CreateFPToUI(RCP_SCALE, I32Ty);
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric   // RCP_LO, RCP_HI = mul(RCP, Den) */
8030b57cec5SDimitry Andric   Value *RCP_LO, *RCP_HI;
8040b57cec5SDimitry Andric   std::tie(RCP_LO, RCP_HI) = getMul64(Builder, RCP, Den);
8050b57cec5SDimitry Andric 
8060b57cec5SDimitry Andric   // NEG_RCP_LO = -RCP_LO
8070b57cec5SDimitry Andric   Value *NEG_RCP_LO = Builder.CreateNeg(RCP_LO);
8080b57cec5SDimitry Andric 
8090b57cec5SDimitry Andric   // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
8100b57cec5SDimitry Andric   Value *RCP_HI_0_CC = Builder.CreateICmpEQ(RCP_HI, Zero);
8110b57cec5SDimitry Andric   Value *ABS_RCP_LO = Builder.CreateSelect(RCP_HI_0_CC, NEG_RCP_LO, RCP_LO);
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric   // Calculate the rounding error from the URECIP instruction
8140b57cec5SDimitry Andric   // E = mulhu(ABS_RCP_LO, RCP)
8150b57cec5SDimitry Andric   Value *E = getMulHu(Builder, ABS_RCP_LO, RCP);
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric   // RCP_A_E = RCP + E
8180b57cec5SDimitry Andric   Value *RCP_A_E = Builder.CreateAdd(RCP, E);
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric   // RCP_S_E = RCP - E
8210b57cec5SDimitry Andric   Value *RCP_S_E = Builder.CreateSub(RCP, E);
8220b57cec5SDimitry Andric 
8230b57cec5SDimitry Andric   // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
8240b57cec5SDimitry Andric   Value *Tmp0 = Builder.CreateSelect(RCP_HI_0_CC, RCP_A_E, RCP_S_E);
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric   // Quotient = mulhu(Tmp0, Num)
8270b57cec5SDimitry Andric   Value *Quotient = getMulHu(Builder, Tmp0, Num);
8280b57cec5SDimitry Andric 
8290b57cec5SDimitry Andric   // Num_S_Remainder = Quotient * Den
8300b57cec5SDimitry Andric   Value *Num_S_Remainder = Builder.CreateMul(Quotient, Den);
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric   // Remainder = Num - Num_S_Remainder
8330b57cec5SDimitry Andric   Value *Remainder = Builder.CreateSub(Num, Num_S_Remainder);
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric   // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
8360b57cec5SDimitry Andric   Value *Rem_GE_Den_CC = Builder.CreateICmpUGE(Remainder, Den);
8370b57cec5SDimitry Andric   Value *Remainder_GE_Den = Builder.CreateSelect(Rem_GE_Den_CC, MinusOne, Zero);
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric   // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
8400b57cec5SDimitry Andric   Value *Num_GE_Num_S_Rem_CC = Builder.CreateICmpUGE(Num, Num_S_Remainder);
8410b57cec5SDimitry Andric   Value *Remainder_GE_Zero = Builder.CreateSelect(Num_GE_Num_S_Rem_CC,
8420b57cec5SDimitry Andric                                                   MinusOne, Zero);
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric   // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
8450b57cec5SDimitry Andric   Value *Tmp1 = Builder.CreateAnd(Remainder_GE_Den, Remainder_GE_Zero);
8460b57cec5SDimitry Andric   Value *Tmp1_0_CC = Builder.CreateICmpEQ(Tmp1, Zero);
8470b57cec5SDimitry Andric 
8480b57cec5SDimitry Andric   Value *Res;
8490b57cec5SDimitry Andric   if (IsDiv) {
8500b57cec5SDimitry Andric     // Quotient_A_One = Quotient + 1
8510b57cec5SDimitry Andric     Value *Quotient_A_One = Builder.CreateAdd(Quotient, One);
8520b57cec5SDimitry Andric 
8530b57cec5SDimitry Andric     // Quotient_S_One = Quotient - 1
8540b57cec5SDimitry Andric     Value *Quotient_S_One = Builder.CreateSub(Quotient, One);
8550b57cec5SDimitry Andric 
8560b57cec5SDimitry Andric     // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
8570b57cec5SDimitry Andric     Value *Div = Builder.CreateSelect(Tmp1_0_CC, Quotient, Quotient_A_One);
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric     // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
8600b57cec5SDimitry Andric     Res = Builder.CreateSelect(Num_GE_Num_S_Rem_CC, Div, Quotient_S_One);
8610b57cec5SDimitry Andric   } else {
8620b57cec5SDimitry Andric     // Remainder_S_Den = Remainder - Den
8630b57cec5SDimitry Andric     Value *Remainder_S_Den = Builder.CreateSub(Remainder, Den);
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric     // Remainder_A_Den = Remainder + Den
8660b57cec5SDimitry Andric     Value *Remainder_A_Den = Builder.CreateAdd(Remainder, Den);
8670b57cec5SDimitry Andric 
8680b57cec5SDimitry Andric     // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
8690b57cec5SDimitry Andric     Value *Rem = Builder.CreateSelect(Tmp1_0_CC, Remainder, Remainder_S_Den);
8700b57cec5SDimitry Andric 
8710b57cec5SDimitry Andric     // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
8720b57cec5SDimitry Andric     Res = Builder.CreateSelect(Num_GE_Num_S_Rem_CC, Rem, Remainder_A_Den);
8730b57cec5SDimitry Andric   }
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   if (IsSigned) {
8760b57cec5SDimitry Andric     Res = Builder.CreateXor(Res, Sign);
8770b57cec5SDimitry Andric     Res = Builder.CreateSub(Res, Sign);
8780b57cec5SDimitry Andric   }
8790b57cec5SDimitry Andric 
8800b57cec5SDimitry Andric   Res = Builder.CreateTrunc(Res, Ty);
8810b57cec5SDimitry Andric 
8820b57cec5SDimitry Andric   return Res;
8830b57cec5SDimitry Andric }
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::visitBinaryOperator(BinaryOperator &I) {
8860b57cec5SDimitry Andric   if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
8870b57cec5SDimitry Andric       DA->isUniform(&I) && promoteUniformOpToI32(I))
8880b57cec5SDimitry Andric     return true;
8890b57cec5SDimitry Andric 
890*8bcb0991SDimitry Andric   if (UseMul24Intrin && replaceMulWithMul24(I))
8910b57cec5SDimitry Andric     return true;
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   bool Changed = false;
8940b57cec5SDimitry Andric   Instruction::BinaryOps Opc = I.getOpcode();
8950b57cec5SDimitry Andric   Type *Ty = I.getType();
8960b57cec5SDimitry Andric   Value *NewDiv = nullptr;
8970b57cec5SDimitry Andric   if ((Opc == Instruction::URem || Opc == Instruction::UDiv ||
8980b57cec5SDimitry Andric        Opc == Instruction::SRem || Opc == Instruction::SDiv) &&
8990b57cec5SDimitry Andric       Ty->getScalarSizeInBits() <= 32) {
9000b57cec5SDimitry Andric     Value *Num = I.getOperand(0);
9010b57cec5SDimitry Andric     Value *Den = I.getOperand(1);
9020b57cec5SDimitry Andric     IRBuilder<> Builder(&I);
9030b57cec5SDimitry Andric     Builder.SetCurrentDebugLocation(I.getDebugLoc());
9040b57cec5SDimitry Andric 
9050b57cec5SDimitry Andric     if (VectorType *VT = dyn_cast<VectorType>(Ty)) {
9060b57cec5SDimitry Andric       NewDiv = UndefValue::get(VT);
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric       for (unsigned N = 0, E = VT->getNumElements(); N != E; ++N) {
9090b57cec5SDimitry Andric         Value *NumEltN = Builder.CreateExtractElement(Num, N);
9100b57cec5SDimitry Andric         Value *DenEltN = Builder.CreateExtractElement(Den, N);
9110b57cec5SDimitry Andric         Value *NewElt = expandDivRem32(Builder, I, NumEltN, DenEltN);
9120b57cec5SDimitry Andric         if (!NewElt)
9130b57cec5SDimitry Andric           NewElt = Builder.CreateBinOp(Opc, NumEltN, DenEltN);
9140b57cec5SDimitry Andric         NewDiv = Builder.CreateInsertElement(NewDiv, NewElt, N);
9150b57cec5SDimitry Andric       }
9160b57cec5SDimitry Andric     } else {
9170b57cec5SDimitry Andric       NewDiv = expandDivRem32(Builder, I, Num, Den);
9180b57cec5SDimitry Andric     }
9190b57cec5SDimitry Andric 
9200b57cec5SDimitry Andric     if (NewDiv) {
9210b57cec5SDimitry Andric       I.replaceAllUsesWith(NewDiv);
9220b57cec5SDimitry Andric       I.eraseFromParent();
9230b57cec5SDimitry Andric       Changed = true;
9240b57cec5SDimitry Andric     }
9250b57cec5SDimitry Andric   }
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric   return Changed;
9280b57cec5SDimitry Andric }
9290b57cec5SDimitry Andric 
9300b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::visitLoadInst(LoadInst &I) {
9310b57cec5SDimitry Andric   if (!WidenLoads)
9320b57cec5SDimitry Andric     return false;
9330b57cec5SDimitry Andric 
9340b57cec5SDimitry Andric   if ((I.getPointerAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS ||
9350b57cec5SDimitry Andric        I.getPointerAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT) &&
9360b57cec5SDimitry Andric       canWidenScalarExtLoad(I)) {
9370b57cec5SDimitry Andric     IRBuilder<> Builder(&I);
9380b57cec5SDimitry Andric     Builder.SetCurrentDebugLocation(I.getDebugLoc());
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric     Type *I32Ty = Builder.getInt32Ty();
9410b57cec5SDimitry Andric     Type *PT = PointerType::get(I32Ty, I.getPointerAddressSpace());
9420b57cec5SDimitry Andric     Value *BitCast= Builder.CreateBitCast(I.getPointerOperand(), PT);
9430b57cec5SDimitry Andric     LoadInst *WidenLoad = Builder.CreateLoad(I32Ty, BitCast);
9440b57cec5SDimitry Andric     WidenLoad->copyMetadata(I);
9450b57cec5SDimitry Andric 
9460b57cec5SDimitry Andric     // If we have range metadata, we need to convert the type, and not make
9470b57cec5SDimitry Andric     // assumptions about the high bits.
9480b57cec5SDimitry Andric     if (auto *Range = WidenLoad->getMetadata(LLVMContext::MD_range)) {
9490b57cec5SDimitry Andric       ConstantInt *Lower =
9500b57cec5SDimitry Andric         mdconst::extract<ConstantInt>(Range->getOperand(0));
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric       if (Lower->getValue().isNullValue()) {
9530b57cec5SDimitry Andric         WidenLoad->setMetadata(LLVMContext::MD_range, nullptr);
9540b57cec5SDimitry Andric       } else {
9550b57cec5SDimitry Andric         Metadata *LowAndHigh[] = {
9560b57cec5SDimitry Andric           ConstantAsMetadata::get(ConstantInt::get(I32Ty, Lower->getValue().zext(32))),
9570b57cec5SDimitry Andric           // Don't make assumptions about the high bits.
9580b57cec5SDimitry Andric           ConstantAsMetadata::get(ConstantInt::get(I32Ty, 0))
9590b57cec5SDimitry Andric         };
9600b57cec5SDimitry Andric 
9610b57cec5SDimitry Andric         WidenLoad->setMetadata(LLVMContext::MD_range,
9620b57cec5SDimitry Andric                                MDNode::get(Mod->getContext(), LowAndHigh));
9630b57cec5SDimitry Andric       }
9640b57cec5SDimitry Andric     }
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric     int TySize = Mod->getDataLayout().getTypeSizeInBits(I.getType());
9670b57cec5SDimitry Andric     Type *IntNTy = Builder.getIntNTy(TySize);
9680b57cec5SDimitry Andric     Value *ValTrunc = Builder.CreateTrunc(WidenLoad, IntNTy);
9690b57cec5SDimitry Andric     Value *ValOrig = Builder.CreateBitCast(ValTrunc, I.getType());
9700b57cec5SDimitry Andric     I.replaceAllUsesWith(ValOrig);
9710b57cec5SDimitry Andric     I.eraseFromParent();
9720b57cec5SDimitry Andric     return true;
9730b57cec5SDimitry Andric   }
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   return false;
9760b57cec5SDimitry Andric }
9770b57cec5SDimitry Andric 
9780b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::visitICmpInst(ICmpInst &I) {
9790b57cec5SDimitry Andric   bool Changed = false;
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric   if (ST->has16BitInsts() && needsPromotionToI32(I.getOperand(0)->getType()) &&
9820b57cec5SDimitry Andric       DA->isUniform(&I))
9830b57cec5SDimitry Andric     Changed |= promoteUniformOpToI32(I);
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   return Changed;
9860b57cec5SDimitry Andric }
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::visitSelectInst(SelectInst &I) {
9890b57cec5SDimitry Andric   bool Changed = false;
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
9920b57cec5SDimitry Andric       DA->isUniform(&I))
9930b57cec5SDimitry Andric     Changed |= promoteUniformOpToI32(I);
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric   return Changed;
9960b57cec5SDimitry Andric }
9970b57cec5SDimitry Andric 
9980b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::visitIntrinsicInst(IntrinsicInst &I) {
9990b57cec5SDimitry Andric   switch (I.getIntrinsicID()) {
10000b57cec5SDimitry Andric   case Intrinsic::bitreverse:
10010b57cec5SDimitry Andric     return visitBitreverseIntrinsicInst(I);
10020b57cec5SDimitry Andric   default:
10030b57cec5SDimitry Andric     return false;
10040b57cec5SDimitry Andric   }
10050b57cec5SDimitry Andric }
10060b57cec5SDimitry Andric 
10070b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::visitBitreverseIntrinsicInst(IntrinsicInst &I) {
10080b57cec5SDimitry Andric   bool Changed = false;
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric   if (ST->has16BitInsts() && needsPromotionToI32(I.getType()) &&
10110b57cec5SDimitry Andric       DA->isUniform(&I))
10120b57cec5SDimitry Andric     Changed |= promoteUniformBitreverseToI32(I);
10130b57cec5SDimitry Andric 
10140b57cec5SDimitry Andric   return Changed;
10150b57cec5SDimitry Andric }
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::doInitialization(Module &M) {
10180b57cec5SDimitry Andric   Mod = &M;
10190b57cec5SDimitry Andric   DL = &Mod->getDataLayout();
10200b57cec5SDimitry Andric   return false;
10210b57cec5SDimitry Andric }
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric bool AMDGPUCodeGenPrepare::runOnFunction(Function &F) {
10240b57cec5SDimitry Andric   if (skipFunction(F))
10250b57cec5SDimitry Andric     return false;
10260b57cec5SDimitry Andric 
10270b57cec5SDimitry Andric   auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
10280b57cec5SDimitry Andric   if (!TPC)
10290b57cec5SDimitry Andric     return false;
10300b57cec5SDimitry Andric 
10310b57cec5SDimitry Andric   const AMDGPUTargetMachine &TM = TPC->getTM<AMDGPUTargetMachine>();
10320b57cec5SDimitry Andric   ST = &TM.getSubtarget<GCNSubtarget>(F);
10330b57cec5SDimitry Andric   AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
10340b57cec5SDimitry Andric   DA = &getAnalysis<LegacyDivergenceAnalysis>();
10350b57cec5SDimitry Andric   HasUnsafeFPMath = hasUnsafeFPMath(F);
10360b57cec5SDimitry Andric 
10370b57cec5SDimitry Andric   bool MadeChange = false;
10380b57cec5SDimitry Andric 
10390b57cec5SDimitry Andric   for (BasicBlock &BB : F) {
10400b57cec5SDimitry Andric     BasicBlock::iterator Next;
10410b57cec5SDimitry Andric     for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; I = Next) {
10420b57cec5SDimitry Andric       Next = std::next(I);
10430b57cec5SDimitry Andric       MadeChange |= visit(*I);
10440b57cec5SDimitry Andric     }
10450b57cec5SDimitry Andric   }
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric   return MadeChange;
10480b57cec5SDimitry Andric }
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(AMDGPUCodeGenPrepare, DEBUG_TYPE,
10510b57cec5SDimitry Andric                       "AMDGPU IR optimizations", false, false)
10520b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
10530b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
10540b57cec5SDimitry Andric INITIALIZE_PASS_END(AMDGPUCodeGenPrepare, DEBUG_TYPE, "AMDGPU IR optimizations",
10550b57cec5SDimitry Andric                     false, false)
10560b57cec5SDimitry Andric 
10570b57cec5SDimitry Andric char AMDGPUCodeGenPrepare::ID = 0;
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric FunctionPass *llvm::createAMDGPUCodeGenPreparePass() {
10600b57cec5SDimitry Andric   return new AMDGPUCodeGenPrepare();
10610b57cec5SDimitry Andric }
1062