Skip to content

Commit e47a75a

Browse files
committed
Address review feedback.
- LangRef.rst: missing ':', stale example - LLParser.cpp: auto * - Bitcode: remove ptrty/addrdiscty - AsmWriter.cpp: ListSeparator - Constants.h: cast<> - Constants.cpp: stale NDEBUG - Constants.cpp: stale comments - Constants.cpp: use getTypeSizeInBits for offset accumulation, to allow pointer/integer types without fiddling. - ConstantFold.cpp: remove FIXME, should be addressed separately - ValueTracking.cpp: remove from isGuaranteedNotToBeUndefOrPoison
1 parent 42df2c5 commit e47a75a

File tree

10 files changed

+32
-52
lines changed

10 files changed

+32
-52
lines changed

llvm/docs/LangRef.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4750,7 +4750,7 @@ reference to the CFI jump table in the ``LowerTypeTests`` pass. These constants
47504750
may be useful in low-level programs, such as operating system kernels, which
47514751
need to refer to the actual function body.
47524752

4753-
.. _ptrauth
4753+
.. _ptrauth:
47544754

47554755
Authenticated Pointers
47564756
----------------------
@@ -4775,8 +4775,6 @@ If the address discriminator is present, then it is
47754775
%tmp2 = call i64 @llvm.ptrauth.sign.i64(i64 ptrtoint (ptr CST to i64), i64 %tmp1)
47764776
%val = inttoptr i64 %tmp2 to ptr
47774777

4778-
%tmp = call i64 @llvm.ptrauth.blend.i64
4779-
47804778
.. _constantexprs:
47814779

47824780
Constant Expressions

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ enum ConstantsCodes {
412412
// asmdialect|unwind,
413413
// asmstr,conststr]
414414
CST_CODE_CE_GEP_WITH_INRANGE = 31, // [opty, flags, range, n x operands]
415-
CST_CODE_SIGNED_PTR = 32, // SIGNED_PTR: [ptrty, ptr, key,
416-
// addrdiscty, addrdisc,
417-
// disc]
415+
CST_CODE_SIGNED_PTR = 32, // [ptr, key, addrdisc, disc]
418416
};
419417

420418
/// CastOpcodes - These are values used in the bitcode files to encode which

llvm/include/llvm/IR/Constants.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,18 +1034,22 @@ class ConstantPtrAuth final : public Constant {
10341034
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
10351035

10361036
/// The pointer that is authenticated in this authenticated global reference.
1037-
Constant *getPointer() const { return (Constant *)Op<0>().get(); }
1037+
Constant *getPointer() const { return cast<Constant>(Op<0>().get()); }
10381038

10391039
/// The Key ID, an i32 constant.
1040-
ConstantInt *getKey() const { return (ConstantInt *)Op<1>().get(); }
1040+
ConstantInt *getKey() const { return cast<ConstantInt>(Op<1>().get()); }
10411041

10421042
/// The address discriminator if any, or the null constant.
10431043
/// If present, this must be a value equivalent to the storage location of
10441044
/// the only user of the authenticated ptrauth global.
1045-
Constant *getAddrDiscriminator() const { return (Constant *)Op<2>().get(); }
1045+
Constant *getAddrDiscriminator() const {
1046+
return cast<Constant>(Op<2>().get());
1047+
}
10461048

10471049
/// The discriminator.
1048-
ConstantInt *getDiscriminator() const { return (ConstantInt *)Op<3>().get(); }
1050+
ConstantInt *getDiscriminator() const {
1051+
return cast<ConstantInt>(Op<3>().get());
1052+
}
10491053

10501054
/// Whether there is any non-null address discriminator.
10511055
bool hasAddressDiversity() const {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7226,9 +7226,6 @@ static bool isGuaranteedNotToBeUndefOrPoison(
72267226
isa<ConstantPointerNull>(C) || isa<Function>(C))
72277227
return true;
72287228

7229-
if (isa<ConstantPtrAuth>(C))
7230-
return true;
7231-
72327229
if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C))
72337230
return (!includesUndef(Kind) ? !C->containsPoisonElement()
72347231
: !C->containsUndefOrPoisonElement()) &&

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4069,15 +4069,15 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
40694069
if (!Ptr->getType()->isPointerTy())
40704070
return error(ID.Loc, "signed pointer must be a pointer");
40714071

4072-
auto KeyC = dyn_cast<ConstantInt>(Key);
4072+
auto *KeyC = dyn_cast<ConstantInt>(Key);
40734073
if (!KeyC || KeyC->getBitWidth() != 32)
40744074
return error(ID.Loc, "signed pointer key must be i32 constant integer");
40754075

40764076
if (!AddrDisc->getType()->isPointerTy())
40774077
return error(ID.Loc,
40784078
"signed pointer address discriminator must be a pointer");
40794079

4080-
auto DiscC = dyn_cast<ConstantInt>(Disc);
4080+
auto *DiscC = dyn_cast<ConstantInt>(Disc);
40814081
if (!DiscC || DiscC->getBitWidth() != 64)
40824082
return error(ID.Loc,
40834083
"signed pointer discriminator must be i64 constant integer");

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,18 +3643,13 @@ Error BitcodeReader::parseConstants() {
36433643
break;
36443644
}
36453645
case bitc::CST_CODE_SIGNED_PTR: {
3646-
if (Record.size() < 6)
3647-
return error("Invalid record");
3648-
Type *PtrTy = getTypeByID(Record[0]);
3649-
if (!PtrTy)
3650-
return error("Invalid record");
3651-
3652-
// PtrTy, Ptr, Key, AddrDiscTy, AddrDisc, Disc
3646+
if (Record.size() < 4)
3647+
return error("Invalid ptrauth record");
3648+
// Ptr, Key, AddrDisc, Disc
36533649
V = BitcodeConstant::create(
36543650
Alloc, CurTy, BitcodeConstant::ConstantPtrAuthOpcode,
3655-
{(unsigned)Record[1], (unsigned)Record[2], (unsigned)Record[4],
3656-
(unsigned)Record[5]});
3657-
3651+
{(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2],
3652+
(unsigned)Record[3]});
36583653
break;
36593654
}
36603655
}

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,14 +2822,6 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
28222822
Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
28232823
Record.push_back(VE.getValueID(BA->getFunction()));
28242824
Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
2825-
} else if (const ConstantPtrAuth *SP = dyn_cast<ConstantPtrAuth>(C)) {
2826-
Code = bitc::CST_CODE_SIGNED_PTR;
2827-
Record.push_back(VE.getTypeID(SP->getPointer()->getType()));
2828-
Record.push_back(VE.getValueID(SP->getPointer()));
2829-
Record.push_back(VE.getValueID(SP->getKey()));
2830-
Record.push_back(VE.getTypeID(SP->getAddrDiscriminator()->getType()));
2831-
Record.push_back(VE.getValueID(SP->getAddrDiscriminator()));
2832-
Record.push_back(VE.getValueID(SP->getDiscriminator()));
28332825
} else if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C)) {
28342826
Code = bitc::CST_CODE_DSO_LOCAL_EQUIVALENT;
28352827
Record.push_back(VE.getTypeID(Equiv->getGlobalValue()->getType()));
@@ -2838,6 +2830,12 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
28382830
Code = bitc::CST_CODE_NO_CFI_VALUE;
28392831
Record.push_back(VE.getTypeID(NC->getGlobalValue()->getType()));
28402832
Record.push_back(VE.getValueID(NC->getGlobalValue()));
2833+
} else if (const auto *CPA = dyn_cast<ConstantPtrAuth>(C)) {
2834+
Code = bitc::CST_CODE_SIGNED_PTR;
2835+
Record.push_back(VE.getValueID(CPA->getPointer()));
2836+
Record.push_back(VE.getValueID(CPA->getKey()));
2837+
Record.push_back(VE.getValueID(CPA->getAddrDiscriminator()));
2838+
Record.push_back(VE.getValueID(CPA->getDiscriminator()));
28412839
} else {
28422840
#ifndef NDEBUG
28432841
C->dump();

llvm/lib/IR/AsmWriter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,15 +1592,13 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
15921592

15931593
if (const ConstantPtrAuth *SP = dyn_cast<ConstantPtrAuth>(CV)) {
15941594
Out << "ptrauth (";
1595-
1595+
ListSeparator LS;
15961596
for (unsigned i = 0; i < SP->getNumOperands(); ++i) {
1597+
Out << LS;
15971598
WriterCtx.TypePrinter->print(SP->getOperand(i)->getType(), Out);
15981599
Out << ' ';
15991600
WriteAsOperandInternal(Out, SP->getOperand(i), WriterCtx);
1600-
if (i != SP->getNumOperands() - 1)
1601-
Out << ", ";
16021601
}
1603-
16041602
Out << ')';
16051603
return;
16061604
}

llvm/lib/IR/ConstantFold.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,9 +1154,6 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) {
11541154
GV->getType()->getAddressSpace()))
11551155
return ICmpInst::ICMP_UGT;
11561156
}
1157-
} else if (const ConstantPtrAuth *SP = dyn_cast<ConstantPtrAuth>(V1)) {
1158-
// FIXME: ahmedbougacha: implement ptrauth cst comparison
1159-
return ICmpInst::BAD_ICMP_PREDICATE;
11601157
} else if (auto *CE1 = dyn_cast<ConstantExpr>(V1)) {
11611158
// Ok, the LHS is known to be a constantexpr. The RHS can be any of a
11621159
// constantexpr, a global, block address, or a simple constant.

llvm/lib/IR/Constants.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,18 +2023,18 @@ Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) {
20232023

20242024
static bool areEquivalentAddrDiscriminators(const Value *V1, const Value *V2,
20252025
const DataLayout &DL) {
2026-
APInt V1Off(DL.getPointerSizeInBits(), 0);
2027-
APInt V2Off(DL.getPointerSizeInBits(), 0);
2026+
APInt Off1(DL.getTypeSizeInBits(V1->getType()), 0);
2027+
APInt Off2(DL.getTypeSizeInBits(V2->getType()), 0);
20282028

20292029
if (auto *V1Cast = dyn_cast<PtrToIntOperator>(V1))
20302030
V1 = V1Cast->getPointerOperand();
20312031
if (auto *V2Cast = dyn_cast<PtrToIntOperator>(V2))
20322032
V2 = V2Cast->getPointerOperand();
20332033
auto *V1Base = V1->stripAndAccumulateConstantOffsets(
2034-
DL, V1Off, /*AllowNonInbounds=*/true);
2034+
DL, Off1, /*AllowNonInbounds=*/true);
20352035
auto *V2Base = V2->stripAndAccumulateConstantOffsets(
2036-
DL, V2Off, /*AllowNonInbounds=*/true);
2037-
return V1Base == V2Base && V1Off == V2Off;
2036+
DL, Off2, /*AllowNonInbounds=*/true);
2037+
return V1Base == V2Base && Off1 == Off2;
20382038
}
20392039

20402040
bool ConstantPtrAuth::isCompatibleWith(const Value *Key,
@@ -2085,12 +2085,10 @@ ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key,
20852085
ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key,
20862086
Constant *AddrDisc, ConstantInt *Disc)
20872087
: Constant(Ptr->getType(), Value::ConstantPtrAuthVal, &Op<0>(), 4) {
2088-
#ifndef NDEBUG
20892088
assert(Ptr->getType()->isPointerTy());
20902089
assert(Key->getBitWidth() == 32);
20912090
assert(AddrDisc->getType()->isPointerTy());
20922091
assert(Disc->getBitWidth() == 64);
2093-
#endif
20942092
setOperand(0, Ptr);
20952093
setOperand(1, Key);
20962094
setOperand(2, AddrDisc);
@@ -2106,11 +2104,9 @@ Value *ConstantPtrAuth::handleOperandChangeImpl(Value *From, Value *ToV) {
21062104
assert(isa<Constant>(ToV) && "Cannot make Constant refer to non-constant!");
21072105
Constant *To = cast<Constant>(ToV);
21082106

2109-
SmallVector<Constant *, 8> Values;
2110-
Values.reserve(getNumOperands()); // Build replacement array.
2107+
SmallVector<Constant *, 4> Values;
2108+
Values.reserve(getNumOperands());
21112109

2112-
// Fill values with the modified operands of the constant array. Also,
2113-
// compute whether this turns into an all-zeros array.
21142110
unsigned NumUpdated = 0;
21152111

21162112
Use *OperandList = getOperandList();
@@ -2125,7 +2121,6 @@ Value *ConstantPtrAuth::handleOperandChangeImpl(Value *From, Value *ToV) {
21252121
Values.push_back(Val);
21262122
}
21272123

2128-
// FIXME: shouldn't we check it's not already there?
21292124
return getContext().pImpl->ConstantPtrAuths.replaceOperandsInPlace(
21302125
Values, this, From, To, NumUpdated, OperandNo);
21312126
}

0 commit comments

Comments
 (0)