44 #define DEBUG_TYPE "amdgpu-codegenprepare"
55 bool HasUnsafeFPMath =
false;
62 unsigned getBaseElementBitWidth(
const Type *
T)
const;
79 bool needsPromotionToI32(
const Type *T)
const;
101 bool promoteUniformOpToI32(
ICmpInst &I)
const;
112 bool promoteUniformOpToI32(
SelectInst &I)
const;
134 bool visitInstruction(
Instruction &I) {
return false; }
142 bool doInitialization(
Module &M)
override;
143 bool runOnFunction(
Function &
F)
override;
145 StringRef getPassName()
const override {
return "AMDGPU IR optimizations"; }
155 unsigned AMDGPUCodeGenPrepare::getBaseElementBitWidth(
const Type *
T)
const {
156 assert(needsPromotionToI32(T) &&
"T does not need promotion to i32");
160 return cast<VectorType>(
T)->getElementType()->getIntegerBitWidth();
164 assert(needsPromotionToI32(T) &&
"T does not need promotion to i32");
172 return I.
getOpcode() == Instruction::AShr ||
176 bool AMDGPUCodeGenPrepare::isSigned(
const SelectInst &I)
const {
181 bool AMDGPUCodeGenPrepare::needsPromotionToI32(
const Type *T)
const {
186 if (
const VectorType *VT = dyn_cast<VectorType>(T)) {
189 if (
ST->hasVOP3PInsts())
192 return needsPromotionToI32(VT->getElementType());
201 case Instruction::Shl:
203 case Instruction::Sub:
205 case Instruction::Mul:
215 case Instruction::Shl:
217 case Instruction::Mul:
219 case Instruction::Sub:
226 bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(
BinaryOperator &I)
const {
228 "I does not need promotion to i32");
230 if (I.
getOpcode() == Instruction::SDiv ||
238 Value *ExtOp0 =
nullptr;
239 Value *ExtOp1 =
nullptr;
240 Value *ExtRes =
nullptr;
241 Value *TruncRes =
nullptr;
244 ExtOp0 = Builder.CreateSExt(I.
getOperand(0), I32Ty);
245 ExtOp1 = Builder.CreateSExt(I.
getOperand(1), I32Ty);
247 ExtOp0 = Builder.CreateZExt(I.
getOperand(0), I32Ty);
248 ExtOp1 = Builder.CreateZExt(I.
getOperand(1), I32Ty);
251 ExtRes = Builder.CreateBinOp(I.
getOpcode(), ExtOp0, ExtOp1);
252 if (
Instruction *Inst = dyn_cast<Instruction>(ExtRes)) {
254 Inst->setHasNoSignedWrap();
257 Inst->setHasNoUnsignedWrap();
259 if (
const auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
260 Inst->setIsExact(ExactOp->isExact());
263 TruncRes = Builder.CreateTrunc(ExtRes, I.
getType());
271 bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(
ICmpInst &I)
const {
273 "I does not need promotion to i32");
279 Value *ExtOp0 =
nullptr;
280 Value *ExtOp1 =
nullptr;
281 Value *NewICmp =
nullptr;
284 ExtOp0 = Builder.CreateSExt(I.
getOperand(0), I32Ty);
285 ExtOp1 = Builder.CreateSExt(I.
getOperand(1), I32Ty);
287 ExtOp0 = Builder.CreateZExt(I.
getOperand(0), I32Ty);
288 ExtOp1 = Builder.CreateZExt(I.
getOperand(1), I32Ty);
290 NewICmp = Builder.CreateICmp(I.
getPredicate(), ExtOp0, ExtOp1);
298 bool AMDGPUCodeGenPrepare::promoteUniformOpToI32(
SelectInst &I)
const {
300 "I does not need promotion to i32");
306 Value *ExtOp1 =
nullptr;
307 Value *ExtOp2 =
nullptr;
308 Value *ExtRes =
nullptr;
309 Value *TruncRes =
nullptr;
312 ExtOp1 = Builder.CreateSExt(I.
getOperand(1), I32Ty);
313 ExtOp2 = Builder.CreateSExt(I.
getOperand(2), I32Ty);
315 ExtOp1 = Builder.CreateZExt(I.
getOperand(1), I32Ty);
316 ExtOp2 = Builder.CreateZExt(I.
getOperand(2), I32Ty);
318 ExtRes = Builder.CreateSelect(I.
getOperand(0), ExtOp1, ExtOp2);
319 TruncRes = Builder.CreateTrunc(ExtRes, I.
getType());
327 bool AMDGPUCodeGenPrepare::promoteUniformBitreverseToI32(
330 "I must be bitreverse intrinsic");
332 "I does not need promotion to i32");
341 Value *ExtRes = Builder.CreateCall(I32, { ExtOp });
343 Builder.CreateLShr(ExtRes, 32 - getBaseElementBitWidth(I.
getType()));
345 Builder.CreateTrunc(LShrOp, I.
getType());
385 if (
ST->hasFP32Denormals() || UnsafeDiv)
389 Builder.setFastMathFlags(FMF);
390 Builder.SetCurrentDebugLocation(FDiv.
getDebugLoc());
397 Value *NewFDiv =
nullptr;
399 if (
VectorType *VT = dyn_cast<VectorType>(Ty)) {
404 for (
unsigned I = 0,
E = VT->getNumElements(); I !=
E; ++
I) {
405 Value *NumEltI = Builder.CreateExtractElement(Num, I);
406 Value *DenEltI = Builder.CreateExtractElement(Den, I);
410 NewElt = Builder.CreateFDiv(NumEltI, DenEltI);
412 NewElt = Builder.CreateCall(Decl, { NumEltI, DenEltI });
415 NewFDiv = Builder.CreateInsertElement(NewFDiv, NewElt, I);
419 NewFDiv = Builder.CreateCall(Decl, { Num, Den });
436 bool AMDGPUCodeGenPrepare::visitBinaryOperator(
BinaryOperator &I) {
437 bool Changed =
false;
439 if (
ST->has16BitInsts() && needsPromotionToI32(I.
getType()) &&
441 Changed |= promoteUniformOpToI32(I);
446 bool AMDGPUCodeGenPrepare::visitICmpInst(
ICmpInst &I) {
447 bool Changed =
false;
451 Changed |= promoteUniformOpToI32(I);
456 bool AMDGPUCodeGenPrepare::visitSelectInst(
SelectInst &I) {
457 bool Changed =
false;
459 if (
ST->has16BitInsts() && needsPromotionToI32(I.
getType()) &&
461 Changed |= promoteUniformOpToI32(I);
466 bool AMDGPUCodeGenPrepare::visitIntrinsicInst(
IntrinsicInst &I) {
468 case Intrinsic::bitreverse:
469 return visitBitreverseIntrinsicInst(I);
475 bool AMDGPUCodeGenPrepare::visitBitreverseIntrinsicInst(
IntrinsicInst &I) {
476 bool Changed =
false;
478 if (
ST->has16BitInsts() && needsPromotionToI32(I.
getType()) &&
480 Changed |= promoteUniformBitreverseToI32(I);
485 bool AMDGPUCodeGenPrepare::doInitialization(
Module &M) {
490 bool AMDGPUCodeGenPrepare::runOnFunction(
Function &
F) {
494 auto *
TPC = getAnalysisIfAvailable<TargetPassConfig>();
500 DA = &getAnalysis<DivergenceAnalysis>();
503 bool MadeChange =
false;
509 MadeChange |= visit(*I);
517 "AMDGPU IR optimizations",
false,
false)
522 char AMDGPUCodeGenPrepare::
ID = 0;
525 return new AMDGPUCodeGenPrepare();
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Base class for instruction visitors.
AMDGPU specific subclass of TargetSubtarget.
A Module instance is used to store all the information related to an LLVM module. ...
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
FastMathFlags getFastMathFlags() const
Convenience function for getting all the fast-math flags.
bool isSigned() const
Determine if this instruction is using a signed comparison.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
FunctionPass * createAMDGPUCodeGenPreparePass()
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
static bool promotedOpIsNUW(const Instruction &I)
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
This class represents the LLVM 'select' instruction.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
This file contains the simple types necessary to represent the attributes associated with functions a...
static bool shouldKeepFDivF32(Value *Num, bool UnsafeDiv)
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool allowReciprocal() const
void takeName(Value *V)
Transfer the name from V to this value.
bool unsafeAlgebra() const
static bool promotedOpIsNSW(const Instruction &I)
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
ConstantFP - Floating Point Values [float, double].
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Represent the analysis usage information of a pass.
This instruction compares its operands according to the predicate given to the constructor.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
self_iterator getIterator()
unsigned getIntegerBitWidth() const
Class to represent integer types.
Predicate getPredicate() const
Return the predicate for this instruction.
The AMDGPU TargetMachine interface definition for hw codgen targets.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
float getFPAccuracy() const
Get the maximum error permitted by this operation in ULPs.
BinaryOps getOpcode() const
Iterator for intrusive lists based on ilist_node.
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
Utility class for floating point operations which can have information about relaxed accuracy require...
Type * getType() const
All values are typed, get the type of this value.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
static bool hasUnsafeFPMath(const Function &F)
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
Class to represent vector types.
bool isIntegerTy() const
True if this is an instance of IntegerType.
void setPreservesAll()
Set by analyses that do not transform their input at all.
INITIALIZE_PASS_BEGIN(AMDGPUCodeGenPrepare, DEBUG_TYPE,"AMDGPU IR optimizations", false, false) INITIALIZE_PASS_END(AMDGPUCodeGenPrepare
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool isExactlyValue(const APFloat &V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
StringRef getValueAsString() const
Return the attribute's value as a string.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Primary interface to the complete machine description for the target machine.
Convenience struct for specifying and reasoning about fast-math flags.
StringRef - Represent a constant reference to a string, i.e.
Statically lint checks LLVM IR
const BasicBlock * getParent() const
A wrapper class for inspecting calls to intrinsic functions.