llvm.org GIT mirror llvm / 1060082
[IR] Make use of Type::isPtrOrPtrVectorTy/isIntOrIntVectorTy/isFPOrFPVectorTy to shorten code. NFC git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307491 91177308-0d34-0410-b5e6-96231b3b80d8 Craig Topper 2 years ago
15 changed file(s) with 49 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
427427
428428 std::pair LT = TLI->getTypeLegalizationCost(DL, Ty);
429429
430 bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
430 bool IsFloat = Ty->isFPOrFPVectorTy();
431431 // Assume that floating point arithmetic operations cost twice as much as
432432 // integer operations.
433433 unsigned OpCost = (IsFloat ? 2 : 1);
15161516 const Twine &Name = "") {
15171517 if (V->getType() == DestTy)
15181518 return V;
1519 if (V->getType()->getScalarType()->isPointerTy() &&
1520 DestTy->getScalarType()->isIntegerTy())
1519 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
15211520 return CreatePtrToInt(V, DestTy, Name);
1522 if (V->getType()->getScalarType()->isIntegerTy() &&
1523 DestTy->getScalarType()->isPointerTy())
1521 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
15241522 return CreateIntToPtr(V, DestTy, Name);
15251523
15261524 return CreateBitCast(V, DestTy, Name);
597597 /// folding.
598598 static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
599599 bool AllowNonInbounds = false) {
600 assert(V->getType()->getScalarType()->isPointerTy());
600 assert(V->getType()->isPtrOrPtrVectorTy());
601601
602602 Type *IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType();
603603 APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth());
626626 }
627627 break;
628628 }
629 assert(V->getType()->getScalarType()->isPointerTy() &&
630 "Unexpected operand type!");
629 assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
631630 } while (Visited.insert(V).second);
632631
633632 Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset);
15001500 unsigned BitWidth = Known.getBitWidth();
15011501
15021502 assert((V->getType()->isIntOrIntVectorTy() ||
1503 V->getType()->getScalarType()->isPointerTy()) &&
1503 V->getType()->isPtrOrPtrVectorTy()) &&
15041504 "Not integer or pointer type!");
15051505 assert((Q.DL.getTypeSizeInBits(V->getType()->getScalarType()) == BitWidth) &&
15061506 (!V->getType()->isIntOrIntVectorTy() ||
30603060 } else {
30613061 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
30623062 if (!Val0->getType()->isIntOrIntVectorTy() &&
3063 !Val0->getType()->getScalarType()->isPointerTy())
3063 !Val0->getType()->isPtrOrPtrVectorTy())
30643064 return Error(ID.Loc, "icmp requires pointer or integer operands");
30653065 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
30663066 }
32093209
32103210 if (Opc == Instruction::GetElementPtr) {
32113211 if (Elts.size() == 0 ||
3212 !Elts[0]->getType()->getScalarType()->isPointerTy())
3212 !Elts[0]->getType()->isPtrOrPtrVectorTy())
32133213 return Error(ID.Loc, "base of getelementptr must be a pointer");
32143214
32153215 Type *BaseType = Elts[0]->getType();
32253225 ArrayRef Indices(Elts.begin() + 1, Elts.end());
32263226 for (Constant *Val : Indices) {
32273227 Type *ValTy = Val->getType();
3228 if (!ValTy->getScalarType()->isIntegerTy())
3228 if (!ValTy->isIntOrIntVectorTy())
32293229 return Error(ID.Loc, "getelementptr index must be an integer");
32303230 if (ValTy->isVectorTy()) {
32313231 unsigned ValNumEl = ValTy->getVectorNumElements();
56965696 } else {
56975697 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
56985698 if (!LHS->getType()->isIntOrIntVectorTy() &&
5699 !LHS->getType()->getScalarType()->isPointerTy())
5699 !LHS->getType()->isPtrOrPtrVectorTy())
57005700 return Error(Loc, "icmp requires integer operands");
57015701 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
57025702 }
63486348 break;
63496349 }
63506350 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
6351 if (!Val->getType()->getScalarType()->isIntegerTy())
6351 if (!Val->getType()->isIntOrIntVectorTy())
63526352 return Error(EltLoc, "getelementptr index must be an integer");
63536353
63546354 if (Val->getType()->isVectorTy()) {
16341634
16351635 Constant *ConstantExpr::getPtrToInt(Constant *C, Type *DstTy,
16361636 bool OnlyIfReduced) {
1637 assert(C->getType()->getScalarType()->isPointerTy() &&
1637 assert(C->getType()->isPtrOrPtrVectorTy() &&
16381638 "PtrToInt source must be pointer or pointer vector");
1639 assert(DstTy->getScalarType()->isIntegerTy() &&
1639 assert(DstTy->isIntOrIntVectorTy() &&
16401640 "PtrToInt destination must be integer or integer vector");
16411641 assert(isa(C->getType()) == isa(DstTy));
16421642 if (isa(C->getType()))
16471647
16481648 Constant *ConstantExpr::getIntToPtr(Constant *C, Type *DstTy,
16491649 bool OnlyIfReduced) {
1650 assert(C->getType()->getScalarType()->isIntegerTy() &&
1650 assert(C->getType()->isIntOrIntVectorTy() &&
16511651 "IntToPtr source must be integer or integer vector");
1652 assert(DstTy->getScalarType()->isPointerTy() &&
1652 assert(DstTy->isPtrOrPtrVectorTy() &&
16531653 "IntToPtr destination must be a pointer or pointer vector");
16541654 assert(isa(C->getType()) == isa(DstTy));
16551655 if (isa(C->getType()))
30633063 if (VectorType *VT = dyn_cast(SrcTy))
30643064 if (VT->getNumElements() != cast(DstTy)->getNumElements())
30653065 return false;
3066 return SrcTy->getScalarType()->isPointerTy() &&
3067 DstTy->getScalarType()->isIntegerTy();
3066 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
30683067 case Instruction::IntToPtr:
30693068 if (isa(SrcTy) != isa(DstTy))
30703069 return false;
30713070 if (VectorType *VT = dyn_cast(SrcTy))
30723071 if (VT->getNumElements() != cast(DstTy)->getNumElements())
30733072 return false;
3074 return SrcTy->getScalarType()->isIntegerTy() &&
3075 DstTy->getScalarType()->isPointerTy();
3073 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
30763074 case Instruction::BitCast: {
30773075 PointerType *SrcPtrTy = dyn_cast(SrcTy->getScalarType());
30783076 PointerType *DstPtrTy = dyn_cast(DstTy->getScalarType());
25032503 Type *SrcTy = I.getOperand(0)->getType();
25042504 Type *DestTy = I.getType();
25052505
2506 Assert(SrcTy->getScalarType()->isPointerTy(),
2507 "PtrToInt source must be pointer", &I);
2506 Assert(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
25082507
25092508 if (auto *PTy = dyn_cast(SrcTy->getScalarType()))
25102509 Assert(!DL.isNonIntegralPointerType(PTy),
25112510 "ptrtoint not supported for non-integral pointers");
25122511
2513 Assert(DestTy->getScalarType()->isIntegerTy(),
2514 "PtrToInt result must be integral", &I);
2512 Assert(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
25152513 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
25162514 &I);
25172515
25302528 Type *SrcTy = I.getOperand(0)->getType();
25312529 Type *DestTy = I.getType();
25322530
2533 Assert(SrcTy->getScalarType()->isIntegerTy(),
2531 Assert(SrcTy->isIntOrIntVectorTy(),
25342532 "IntToPtr source must be an integral", &I);
2535 Assert(DestTy->getScalarType()->isPointerTy(),
2536 "IntToPtr result must be a pointer", &I);
2533 Assert(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
25372534
25382535 if (auto *PTy = dyn_cast(DestTy->getScalarType()))
25392536 Assert(!DL.isNonIntegralPointerType(PTy),
29512948 Assert(Op0Ty == Op1Ty,
29522949 "Both operands to ICmp instruction are not of the same type!", &IC);
29532950 // Check that the operands are the right type
2954 Assert(Op0Ty->isIntOrIntVectorTy() || Op0Ty->getScalarType()->isPointerTy(),
2951 Assert(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
29552952 "Invalid operand types for ICmp instruction", &IC);
29562953 // Check that the predicate is valid.
29572954 Assert(IC.isIntPredicate(),
30083005 GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
30093006 Assert(ElTy, "Invalid indices for GEP pointer type!", &GEP);
30103007
3011 Assert(GEP.getType()->getScalarType()->isPointerTy() &&
3008 Assert(GEP.getType()->isPtrOrPtrVectorTy() &&
30123009 GEP.getResultElementType() == ElTy,
30133010 "GEP is not of right type for indices!", &GEP, ElTy);
30143011
30243021 unsigned IndexWidth = IndexTy->getVectorNumElements();
30253022 Assert(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
30263023 }
3027 Assert(IndexTy->getScalarType()->isIntegerTy(),
3024 Assert(IndexTy->isIntOrIntVectorTy(),
30283025 "All GEP indices should be of integer type");
30293026 }
30303027 }
42504247 // relocated pointer. It can be casted to the correct type later if it's
42514248 // desired. However, they must have the same address space and 'vectorness'
42524249 GCRelocateInst &Relocate = cast(*CS.getInstruction());
4253 Assert(Relocate.getDerivedPtr()->getType()->getScalarType()->isPointerTy(),
4250 Assert(Relocate.getDerivedPtr()->getType()->isPtrOrPtrVectorTy(),
42544251 "gc.relocate: relocated value must be a gc pointer", CS);
42554252
42564253 auto ResultType = CS.getType();
786786 int Cost = ((Val->getScalarSizeInBits() == 1) ? 2 /*+test-under-mask*/ : 1);
787787
788788 // Give a slight penalty for moving out of vector pipeline to FXU unit.
789 if (Index == 0 && Val->getScalarType()->isIntegerTy())
789 if (Index == 0 && Val->isIntOrIntVectorTy())
790790 Cost += 1;
791791
792792 return Cost;
18651865 InstCombiner::BuilderTy &Builder) {
18661866 Type *DestTy = BitCast.getType();
18671867 BinaryOperator *BO;
1868 if (!DestTy->getScalarType()->isIntegerTy() ||
1868 if (!DestTy->isIntOrIntVectorTy() ||
18691869 !match(BitCast.getOperand(0), m_OneUse(m_BinOp(BO))) ||
18701870 !BO->isBitwiseLogicOp())
18711871 return nullptr;
12011201 V->takeName(LI);
12021202 if (Instruction *I = dyn_cast(V))
12031203 I->setDebugLoc(LI->getDebugLoc());
1204 if (V->getType()->getScalarType()->isPointerTy())
1204 if (V->getType()->isPtrOrPtrVectorTy())
12051205 MD->invalidateCachedPointerInfo(V);
12061206 markInstructionForDeletion(LI);
12071207 ORE->emit(OptimizationRemark(DEBUG_TYPE, "LoadPRE", LI)
12881288 // to propagate LI's DebugLoc because LI may not post-dominate I.
12891289 if (LI->getDebugLoc() && LI->getParent() == I->getParent())
12901290 I->setDebugLoc(LI->getDebugLoc());
1291 if (V->getType()->getScalarType()->isPointerTy())
1291 if (V->getType()->isPtrOrPtrVectorTy())
12921292 MD->invalidateCachedPointerInfo(V);
12931293 markInstructionForDeletion(LI);
12941294 ++NumGVNLoad;
14421442 reportLoadElim(L, AvailableValue, ORE);
14431443 // Tell MDA to rexamine the reused pointer since we might have more
14441444 // information after forwarding it.
1445 if (MD && AvailableValue->getType()->getScalarType()->isPointerTy())
1445 if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy())
14461446 MD->invalidateCachedPointerInfo(AvailableValue);
14471447 return true;
14481448 }
16971697 Changed = true;
16981698 }
16991699 if (Changed) {
1700 if (MD && V->getType()->getScalarType()->isPointerTy())
1700 if (MD && V->getType()->isPtrOrPtrVectorTy())
17011701 MD->invalidateCachedPointerInfo(V);
17021702 ++NumGVNSimpl;
17031703 return true;
18081808
18091809 // Remove it!
18101810 patchAndReplaceAllUsesWith(I, Repl);
1811 if (MD && Repl->getType()->getScalarType()->isPointerTy())
1811 if (MD && Repl->getType()->isPtrOrPtrVectorTy())
18121812 MD->invalidateCachedPointerInfo(Repl);
18131813 markInstructionForDeletion(I);
18141814 return true;
20822082 addToLeaderTable(ValNo, Phi, CurrentBlock);
20832083 Phi->setDebugLoc(CurInst->getDebugLoc());
20842084 CurInst->replaceAllUsesWith(Phi);
2085 if (MD && Phi->getType()->getScalarType()->isPointerTy())
2085 if (MD && Phi->getType()->isPtrOrPtrVectorTy())
20862086 MD->invalidateCachedPointerInfo(Phi);
20872087 VN.erase(CurInst);
20882088 removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
237237 &BB->front());
238238 NewPN->addIncoming(Opd1, S0->getParent());
239239 NewPN->addIncoming(Opd2, S1->getParent());
240 if (MD && NewPN->getType()->getScalarType()->isPointerTy())
240 if (MD && NewPN->getType()->isPtrOrPtrVectorTy())
241241 MD->invalidateCachedPointerInfo(NewPN);
242242 return NewPN;
243243 }
16721672
16731673 // See if we need inttoptr for this type pair. A cast involving both scalars
16741674 // and vectors requires and additional bitcast.
1675 if (OldTy->getScalarType()->isIntegerTy() &&
1676 NewTy->getScalarType()->isPointerTy()) {
1675 if (OldTy->isIntOrIntVectorTy() && NewTy->isPtrOrPtrVectorTy()) {
16771676 // Expand <2 x i32> to i8* --> <2 x i32> to i64 to i8*
16781677 if (OldTy->isVectorTy() && !NewTy->isVectorTy())
16791678 return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)),
16891688
16901689 // See if we need ptrtoint for this type pair. A cast involving both scalars
16911690 // and vectors requires and additional bitcast.
1692 if (OldTy->getScalarType()->isPointerTy() &&
1693 NewTy->getScalarType()->isIntegerTy()) {
1691 if (OldTy->isPtrOrPtrVectorTy() && NewTy->isIntOrIntVectorTy()) {
16941692 // Expand <2 x i8*> to i128 --> <2 x i8*> to <2 x i64> to i128
16951693 if (OldTy->isVectorTy() && !NewTy->isVectorTy())
16961694 return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)),
5050 // If the store and reload are the same size, we can always reuse it.
5151 if (StoredValSize == LoadedValSize) {
5252 // Pointer to Pointer -> use bitcast.
53 if (StoredValTy->getScalarType()->isPointerTy() &&
54 LoadedTy->getScalarType()->isPointerTy()) {
53 if (StoredValTy->isPtrOrPtrVectorTy() && LoadedTy->isPtrOrPtrVectorTy()) {
5554 StoredVal = Helper.CreateBitCast(StoredVal, LoadedTy);
5655 } else {
5756 // Convert source pointers to integers, which can be bitcast.
58 if (StoredValTy->getScalarType()->isPointerTy()) {
57 if (StoredValTy->isPtrOrPtrVectorTy()) {
5958 StoredValTy = DL.getIntPtrType(StoredValTy);
6059 StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
6160 }
6261
6362 Type *TypeToCastTo = LoadedTy;
64 if (TypeToCastTo->getScalarType()->isPointerTy())
63 if (TypeToCastTo->isPtrOrPtrVectorTy())
6564 TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
6665
6766 if (StoredValTy != TypeToCastTo)
6867 StoredVal = Helper.CreateBitCast(StoredVal, TypeToCastTo);
6968
7069 // Cast to pointer if the load needs a pointer type.
71 if (LoadedTy->getScalarType()->isPointerTy())
70 if (LoadedTy->isPtrOrPtrVectorTy())
7271 StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
7372 }
7473
8584 "canCoerceMustAliasedValueToLoad fail");
8685
8786 // Convert source pointers to integers, which can be manipulated.
88 if (StoredValTy->getScalarType()->isPointerTy()) {
87 if (StoredValTy->isPtrOrPtrVectorTy()) {
8988 StoredValTy = DL.getIntPtrType(StoredValTy);
9089 StoredVal = Helper.CreatePtrToInt(StoredVal, StoredValTy);
9190 }
111110
112111 if (LoadedTy != NewIntTy) {
113112 // If the result is a pointer, inttoptr.
114 if (LoadedTy->getScalarType()->isPointerTy())
113 if (LoadedTy->isPtrOrPtrVectorTy())
115114 StoredVal = Helper.CreateIntToPtr(StoredVal, LoadedTy);
116115 else
117116 // Otherwise, bitcast.
315314 uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
316315 // Compute which bits of the stored value are being used by the load. Convert
317316 // to an integer type to start with.
318 if (SrcVal->getType()->getScalarType()->isPointerTy())
317 if (SrcVal->getType()->isPtrOrPtrVectorTy())
319318 SrcVal = Helper.CreatePtrToInt(SrcVal, DL.getIntPtrType(SrcVal->getType()));
320319 if (!SrcVal->getType()->isIntegerTy())
321320 SrcVal = Helper.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize * 8));
384384
385385 if (Ty->isVectorTy()) {
386386 switch (getRandom() % 2) {
387 case 0: if (Ty->getScalarType()->isIntegerTy())
387 case 0: if (Ty->isIntOrIntVectorTy())
388388 return PT->push_back(ConstantVector::getAllOnesValue(Ty));
389389 break;
390 case 1: if (Ty->getScalarType()->isIntegerTy())
390 case 1: if (Ty->isIntOrIntVectorTy())
391391 return PT->push_back(ConstantVector::getNullValue(Ty));
392392 }
393393 }
530530 }
531531
532532 // Both types are integers:
533 if (VTy->getScalarType()->isIntegerTy() &&
534 DestTy->getScalarType()->isIntegerTy()) {
533 if (VTy->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy()) {
535534 if (VSize > DestSize) {
536535 return PT->push_back(
537536 new TruncInst(V, DestTy, "Tr", BB->getTerminator()));
545544 }
546545
547546 // Fp to int.
548 if (VTy->getScalarType()->isFloatingPointTy() &&
549 DestTy->getScalarType()->isIntegerTy()) {
547 if (VTy->isFPOrFPVectorTy() && DestTy->isIntOrIntVectorTy()) {
550548 if (getRandom() & 1)
551549 return PT->push_back(
552550 new FPToSIInst(V, DestTy, "FC", BB->getTerminator()));
554552 }
555553
556554 // Int to fp.
557 if (VTy->getScalarType()->isIntegerTy() &&
558 DestTy->getScalarType()->isFloatingPointTy()) {
555 if (VTy->isIntOrIntVectorTy() && DestTy->isFPOrFPVectorTy()) {
559556 if (getRandom() & 1)
560557 return PT->push_back(
561558 new SIToFPInst(V, DestTy, "FC", BB->getTerminator()));
564561 }
565562
566563 // Both floats.
567 if (VTy->getScalarType()->isFloatingPointTy() &&
568 DestTy->getScalarType()->isFloatingPointTy()) {
564 if (VTy->isFPOrFPVectorTy() && DestTy->isFPOrFPVectorTy()) {
569565 if (VSize > DestSize) {
570566 return PT->push_back(
571567 new FPTruncInst(V, DestTy, "Tr", BB->getTerminator()));