Skip to content

Commit 2a6c871

Browse files
committed
[InstCombine] Move target-specific inst combining
For a long time, the InstCombine pass handled target specific intrinsics. Having target specific code in general passes was noted as an area for improvement for a long time. D81728 moves most target specific code out of the InstCombine pass. Applying the target specific combinations in an extra pass would probably result in inferior optimizations compared to the current fixed-point iteration, therefore the InstCombine pass resorts to newly introduced functions in the TargetTransformInfo when it encounters unknown intrinsics. The patch should not have any effect on generated code (under the assumption that code never uses intrinsics from a foreign target). This introduces three new functions: TargetTransformInfo::instCombineIntrinsic TargetTransformInfo::simplifyDemandedUseBitsIntrinsic TargetTransformInfo::simplifyDemandedVectorEltsIntrinsic A few target specific parts are left in the InstCombine folder, where it makes sense to share code. The largest left-over part in InstCombineCalls.cpp is the code shared between arm and aarch64. This allows to move about 3000 lines out from InstCombine to the targets. Differential Revision: https://reviews.llvm.org/D81728
1 parent 5623da5 commit 2a6c871

File tree

66 files changed

+4725
-4026
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+4725
-4026
lines changed

clang/test/CodeGen/thinlto-distributed-newpm.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@
6262
; CHECK-O: Running pass: InstCombinePass on main
6363
; CHECK-O: Running analysis: TargetLibraryAnalysis on main
6464
; CHECK-O: Running analysis: OptimizationRemarkEmitterAnalysis on main
65+
; CHECK-O: Running analysis: TargetIRAnalysis on main
6566
; CHECK-O: Running analysis: AAManager on main
6667
; CHECK-O: Running analysis: BasicAA on main
6768
; CHECK-O: Running analysis: ScopedNoAliasAA on main
6869
; CHECK-O: Running analysis: TypeBasedAA on main
6970
; CHECK-O: Running analysis: OuterAnalysisManagerProxy
7071
; CHECK-O: Running pass: SimplifyCFGPass on main
71-
; CHECK-O: Running analysis: TargetIRAnalysis on main
7272
; CHECK-O: Finished {{.*}}Function pass manager run.
7373
; CHECK-O: Running pass: RequireAnalysisPass<{{.*}}GlobalsAA
7474
; CHECK-O: Running analysis: GlobalsAA

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CallBase;
4242
class ExtractElementInst;
4343
class Function;
4444
class GlobalValue;
45+
class InstCombiner;
4546
class IntrinsicInst;
4647
class LoadInst;
4748
class LoopAccessInfo;
@@ -56,6 +57,7 @@ class TargetLibraryInfo;
5657
class Type;
5758
class User;
5859
class Value;
60+
struct KnownBits;
5961
template <typename T> class Optional;
6062

6163
/// Information about a load/store intrinsic defined by the target.
@@ -542,6 +544,29 @@ class TargetTransformInfo {
542544
/// target-independent defaults with information from \p L and \p SE.
543545
void getPeelingPreferences(Loop *L, ScalarEvolution &SE,
544546
PeelingPreferences &PP) const;
547+
548+
/// Targets can implement their own combinations for target-specific
549+
/// intrinsics. This function will be called from the InstCombine pass every
550+
/// time a target-specific intrinsic is encountered.
551+
///
552+
/// \returns None to not do anything target specific or a value that will be
553+
/// returned from the InstCombiner. It is possible to return null and stop
554+
/// further processing of the intrinsic by returning nullptr.
555+
Optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
556+
IntrinsicInst &II) const;
557+
/// Can be used to implement target-specific instruction combining.
558+
/// \see instCombineIntrinsic
559+
Optional<Value *>
560+
simplifyDemandedUseBitsIntrinsic(InstCombiner &IC, IntrinsicInst &II,
561+
APInt DemandedMask, KnownBits &Known,
562+
bool &KnownBitsComputed) const;
563+
/// Can be used to implement target-specific instruction combining.
564+
/// \see instCombineIntrinsic
565+
Optional<Value *> simplifyDemandedVectorEltsIntrinsic(
566+
InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts,
567+
APInt &UndefElts2, APInt &UndefElts3,
568+
std::function<void(Instruction *, unsigned, APInt, APInt &)>
569+
SimplifyAndSetOp) const;
545570
/// @}
546571

547572
/// \name Scalar Target Information
@@ -1301,6 +1326,17 @@ class TargetTransformInfo::Concept {
13011326
AssumptionCache &AC, TargetLibraryInfo *TLI,
13021327
DominatorTree *DT, const LoopAccessInfo *LAI) = 0;
13031328
virtual bool emitGetActiveLaneMask() = 0;
1329+
virtual Optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
1330+
IntrinsicInst &II) = 0;
1331+
virtual Optional<Value *>
1332+
simplifyDemandedUseBitsIntrinsic(InstCombiner &IC, IntrinsicInst &II,
1333+
APInt DemandedMask, KnownBits &Known,
1334+
bool &KnownBitsComputed) = 0;
1335+
virtual Optional<Value *> simplifyDemandedVectorEltsIntrinsic(
1336+
InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts,
1337+
APInt &UndefElts2, APInt &UndefElts3,
1338+
std::function<void(Instruction *, unsigned, APInt, APInt &)>
1339+
SimplifyAndSetOp) = 0;
13041340
virtual bool isLegalAddImmediate(int64_t Imm) = 0;
13051341
virtual bool isLegalICmpImmediate(int64_t Imm) = 0;
13061342
virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
@@ -1588,6 +1624,26 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
15881624
bool emitGetActiveLaneMask() override {
15891625
return Impl.emitGetActiveLaneMask();
15901626
}
1627+
Optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
1628+
IntrinsicInst &II) override {
1629+
return Impl.instCombineIntrinsic(IC, II);
1630+
}
1631+
Optional<Value *>
1632+
simplifyDemandedUseBitsIntrinsic(InstCombiner &IC, IntrinsicInst &II,
1633+
APInt DemandedMask, KnownBits &Known,
1634+
bool &KnownBitsComputed) override {
1635+
return Impl.simplifyDemandedUseBitsIntrinsic(IC, II, DemandedMask, Known,
1636+
KnownBitsComputed);
1637+
}
1638+
Optional<Value *> simplifyDemandedVectorEltsIntrinsic(
1639+
InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts,
1640+
APInt &UndefElts2, APInt &UndefElts3,
1641+
std::function<void(Instruction *, unsigned, APInt, APInt &)>
1642+
SimplifyAndSetOp) override {
1643+
return Impl.simplifyDemandedVectorEltsIntrinsic(
1644+
IC, II, DemandedElts, UndefElts, UndefElts2, UndefElts3,
1645+
SimplifyAndSetOp);
1646+
}
15911647
bool isLegalAddImmediate(int64_t Imm) override {
15921648
return Impl.isLegalAddImmediate(Imm);
15931649
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ class TargetTransformInfoImplBase {
147147
return false;
148148
}
149149

150+
Optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
151+
IntrinsicInst &II) const {
152+
return None;
153+
}
154+
155+
Optional<Value *>
156+
simplifyDemandedUseBitsIntrinsic(InstCombiner &IC, IntrinsicInst &II,
157+
APInt DemandedMask, KnownBits &Known,
158+
bool &KnownBitsComputed) const {
159+
return None;
160+
}
161+
162+
Optional<Value *> simplifyDemandedVectorEltsIntrinsic(
163+
InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts,
164+
APInt &UndefElts2, APInt &UndefElts3,
165+
std::function<void(Instruction *, unsigned, APInt, APInt &)>
166+
SimplifyAndSetOp) const {
167+
return None;
168+
}
169+
150170
void getUnrollingPreferences(Loop *, ScalarEvolution &,
151171
TTI::UnrollingPreferences &) {}
152172

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,30 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
477477
return BaseT::emitGetActiveLaneMask();
478478
}
479479

480+
Optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
481+
IntrinsicInst &II) {
482+
return BaseT::instCombineIntrinsic(IC, II);
483+
}
484+
485+
Optional<Value *> simplifyDemandedUseBitsIntrinsic(InstCombiner &IC,
486+
IntrinsicInst &II,
487+
APInt DemandedMask,
488+
KnownBits &Known,
489+
bool &KnownBitsComputed) {
490+
return BaseT::simplifyDemandedUseBitsIntrinsic(IC, II, DemandedMask, Known,
491+
KnownBitsComputed);
492+
}
493+
494+
Optional<Value *> simplifyDemandedVectorEltsIntrinsic(
495+
InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts,
496+
APInt &UndefElts2, APInt &UndefElts3,
497+
std::function<void(Instruction *, unsigned, APInt, APInt &)>
498+
SimplifyAndSetOp) {
499+
return BaseT::simplifyDemandedVectorEltsIntrinsic(
500+
IC, II, DemandedElts, UndefElts, UndefElts2, UndefElts3,
501+
SimplifyAndSetOp);
502+
}
503+
480504
int getInstructionLatency(const Instruction *I) {
481505
if (isa<LoadInst>(I))
482506
return getST()->getSchedModel().DefaultLoadLatency;
@@ -1605,7 +1629,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
16051629
}
16061630
}
16071631

1608-
auto MinLegalCostI = std::min_element(LegalCost.begin(), LegalCost.end());
1632+
auto *MinLegalCostI = std::min_element(LegalCost.begin(), LegalCost.end());
16091633
if (MinLegalCostI != LegalCost.end())
16101634
return *MinLegalCostI;
16111635

llvm/include/llvm/IR/Function.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ class Function : public GlobalObject, public ilist_node<Function> {
199199
/// returns Intrinsic::not_intrinsic!
200200
bool isIntrinsic() const { return HasLLVMReservedName; }
201201

202+
/// isTargetIntrinsic - Returns true if this function is an intrinsic and the
203+
/// intrinsic is specific to a certain target. If this is not an intrinsic
204+
/// or a generic intrinsic, false is returned.
205+
bool isTargetIntrinsic() const;
206+
202207
/// Returns true if the function is one of the "Constrained Floating-Point
203208
/// Intrinsics". Returns false if not, and returns false when
204209
/// getIntrinsicID() returns Intrinsic::not_intrinsic.

0 commit comments

Comments
 (0)