@@ -44,69 +44,6 @@ class TargetTransformInfoImplBase {
44
44
45
45
const DataLayout &getDataLayout () const { return DL; }
46
46
47
- unsigned getOperationCost (unsigned Opcode, Type *Ty, Type *OpTy) {
48
- switch (Opcode) {
49
- default :
50
- // By default, just classify everything as 'basic'.
51
- return TTI::TCC_Basic;
52
-
53
- case Instruction::GetElementPtr:
54
- llvm_unreachable (" Use getGEPCost for GEP operations!" );
55
-
56
- case Instruction::BitCast:
57
- assert (OpTy && " Cast instructions must provide the operand type" );
58
- if (Ty == OpTy || (Ty->isPointerTy () && OpTy->isPointerTy ()))
59
- // Identity and pointer-to-pointer casts are free.
60
- return TTI::TCC_Free;
61
-
62
- // Otherwise, the default basic cost is used.
63
- return TTI::TCC_Basic;
64
-
65
- case Instruction::Freeze:
66
- // Freeze operation is free because it should be lowered into a register
67
- // use without any register copy in assembly code.
68
- return TTI::TCC_Free;
69
-
70
- case Instruction::FDiv:
71
- case Instruction::FRem:
72
- case Instruction::SDiv:
73
- case Instruction::SRem:
74
- case Instruction::UDiv:
75
- case Instruction::URem:
76
- return TTI::TCC_Expensive;
77
-
78
- case Instruction::IntToPtr: {
79
- // An inttoptr cast is free so long as the input is a legal integer type
80
- // which doesn't contain values outside the range of a pointer.
81
- unsigned OpSize = OpTy->getScalarSizeInBits ();
82
- if (DL.isLegalInteger (OpSize) &&
83
- OpSize <= DL.getPointerTypeSizeInBits (Ty))
84
- return TTI::TCC_Free;
85
-
86
- // Otherwise it's not a no-op.
87
- return TTI::TCC_Basic;
88
- }
89
- case Instruction::PtrToInt: {
90
- // A ptrtoint cast is free so long as the result is large enough to store
91
- // the pointer, and a legal integer type.
92
- unsigned DestSize = Ty->getScalarSizeInBits ();
93
- if (DL.isLegalInteger (DestSize) &&
94
- DestSize >= DL.getPointerTypeSizeInBits (OpTy))
95
- return TTI::TCC_Free;
96
-
97
- // Otherwise it's not a no-op.
98
- return TTI::TCC_Basic;
99
- }
100
- case Instruction::Trunc:
101
- // trunc to a native type is free (assuming the target has compare and
102
- // shift-right of the same width).
103
- if (DL.isLegalInteger (DL.getTypeSizeInBits (Ty)))
104
- return TTI::TCC_Free;
105
-
106
- return TTI::TCC_Basic;
107
- }
108
- }
109
-
110
47
int getGEPCost (Type *PointeeType, const Value *Ptr,
111
48
ArrayRef<const Value *> Operands) {
112
49
// In the basic model, we just assume that all-constant GEPs will be folded
@@ -445,6 +382,35 @@ class TargetTransformInfoImplBase {
445
382
446
383
unsigned getCastInstrCost (unsigned Opcode, Type *Dst, Type *Src,
447
384
const Instruction *I) {
385
+ switch (Opcode) {
386
+ default :
387
+ break ;
388
+ case Instruction::IntToPtr: {
389
+ unsigned SrcSize = Src->getScalarSizeInBits ();
390
+ if (DL.isLegalInteger (SrcSize) &&
391
+ SrcSize <= DL.getPointerTypeSizeInBits (Dst))
392
+ return 0 ;
393
+ break ;
394
+ }
395
+ case Instruction::PtrToInt: {
396
+ unsigned DstSize = Dst->getScalarSizeInBits ();
397
+ if (DL.isLegalInteger (DstSize) &&
398
+ DstSize >= DL.getPointerTypeSizeInBits (Src))
399
+ return 0 ;
400
+ break ;
401
+ }
402
+ case Instruction::BitCast:
403
+ if (Dst == Src || (Dst->isPointerTy () && Src->isPointerTy ()))
404
+ // Identity and pointer-to-pointer casts are free.
405
+ return 0 ;
406
+ break ;
407
+ case Instruction::Trunc:
408
+ // trunc to a native type is free (assuming the target has compare and
409
+ // shift-right of the same width).
410
+ if (DL.isLegalInteger (DL.getTypeSizeInBits (Dst)))
411
+ return 0 ;
412
+ break ;
413
+ }
448
414
return 1 ;
449
415
}
450
416
@@ -828,27 +794,8 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
828
794
}
829
795
830
796
unsigned getUserCost (const User *U, ArrayRef<const Value *> Operands) {
831
- if (isa<PHINode>(U))
832
- return TTI::TCC_Free; // Model all PHI nodes as free.
833
-
834
- if (isa<ExtractValueInst>(U))
835
- return TTI::TCC_Free; // Model all ExtractValue nodes as free.
836
-
837
- if (isa<FreezeInst>(U))
838
- return TTI::TCC_Free; // Model all Freeze nodes as free.
839
-
840
- // Static alloca doesn't generate target instructions.
841
- if (auto *A = dyn_cast<AllocaInst>(U))
842
- if (A->isStaticAlloca ())
843
- return TTI::TCC_Free;
844
-
845
797
auto *TargetTTI = static_cast <T *>(this );
846
798
847
- if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U))
848
- return TargetTTI->getGEPCost (GEP->getSourceElementType (),
849
- GEP->getPointerOperand (),
850
- Operands.drop_front ());
851
-
852
799
if (auto CS = ImmutableCallSite (U)) {
853
800
const Function *F = CS.getCalledFunction ();
854
801
if (F) {
@@ -867,14 +814,55 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
867
814
return TTI::TCC_Basic * (CS.arg_size () + 1 );
868
815
}
869
816
870
- if (isa<SExtInst>(U) || isa<ZExtInst>(U) || isa<FPExtInst>(U))
871
- // The old behaviour of generally treating extensions of icmp to be free
872
- // has been removed. A target that needs it should override getUserCost().
873
- return TargetTTI->getExtCost (cast<Instruction>(U), Operands.back ());
874
-
875
- return TargetTTI->getOperationCost (
876
- Operator::getOpcode (U), U->getType (),
877
- U->getNumOperands () == 1 ? U->getOperand (0 )->getType () : nullptr );
817
+ Type *Ty = U->getType ();
818
+ Type *OpTy =
819
+ U->getNumOperands () == 1 ? U->getOperand (0 )->getType () : nullptr ;
820
+ unsigned Opcode = Operator::getOpcode (U);
821
+ auto *I = dyn_cast<Instruction>(U);
822
+ switch (Opcode) {
823
+ default :
824
+ break ;
825
+ case Instruction::PHI:
826
+ case Instruction::ExtractValue:
827
+ case Instruction::Freeze:
828
+ return TTI::TCC_Free;
829
+ case Instruction::Alloca:
830
+ if (cast<AllocaInst>(U)->isStaticAlloca ())
831
+ return TTI::TCC_Free;
832
+ break ;
833
+ case Instruction::GetElementPtr: {
834
+ const GEPOperator *GEP = cast<GEPOperator>(U);
835
+ return TargetTTI->getGEPCost (GEP->getSourceElementType (),
836
+ GEP->getPointerOperand (),
837
+ Operands.drop_front ());
838
+ }
839
+ case Instruction::FDiv:
840
+ case Instruction::FRem:
841
+ case Instruction::SDiv:
842
+ case Instruction::SRem:
843
+ case Instruction::UDiv:
844
+ case Instruction::URem:
845
+ return TTI::TCC_Expensive;
846
+ case Instruction::IntToPtr:
847
+ case Instruction::PtrToInt:
848
+ case Instruction::Trunc:
849
+ if (getCastInstrCost (Opcode, Ty, OpTy, I) == TTI::TCC_Free ||
850
+ TargetTTI->getCastInstrCost (Opcode, Ty, OpTy, I) == TTI::TCC_Free)
851
+ return TTI::TCC_Free;
852
+ break ;
853
+ case Instruction::BitCast:
854
+ if (getCastInstrCost (Opcode, Ty, OpTy, I) == TTI::TCC_Free)
855
+ return TTI::TCC_Free;
856
+ break ;
857
+ case Instruction::FPExt:
858
+ case Instruction::SExt:
859
+ case Instruction::ZExt:
860
+ if (TargetTTI->getExtCost (I, Operands.back ()) == TTI::TCC_Free)
861
+ return TTI::TCC_Free;
862
+ break ;
863
+ }
864
+ // By default, just classify everything as 'basic'.
865
+ return TTI::TCC_Basic;
878
866
}
879
867
880
868
int getInstructionLatency (const Instruction *I) {
0 commit comments