This is an archive of the discontinued LLVM Phabricator instance.

[X86] Bugfix for nullptr check by klocwork
ClosedPublic

Authored by xiangzhangllvm on Feb 18 2019, 5:36 PM.

Details

Summary

klocwork critical issues in CG files:

#10087: Pointer 'ReferenceDI' returned from call to function 'get' at line 87 may be NULL and will be dereferenced at line 90. llvm/lib/Target/X86/X86DiscriminateMemOps.cpp:87

#13373: Pointer 'DI' returned from call to function 'cloneWithDiscriminator' at line 145 may be NULL and will be dereferenced at line 150. llvm/lib/Target/X86/X86DiscriminateMemOps.cpp:145

#17178: Pointer 'RegRB' returned from call to function 'getRegBank' at line 1603 may be NULL and will be dereferenced at line 1604. llvm/lib/Target/X86/X86InstructionSelector.cpp:1603

Diff Detail

Repository
rL LLVM

Event Timeline

xiangzhangllvm created this revision.Feb 18 2019, 5:36 PM
craig.topper added inline comments.Feb 18 2019, 6:35 PM
lib/Target/X86/X86InstructionSelector.cpp
1606 ↗(On Diff #187286)

Get rid of this reference variable and just use the pointer with an -> below. Rename it back to RegRB.

craig.topper retitled this revision from Bugfix for nullptr check by klocwork to [X86] Bugfix for nullptr check by klocwork.Feb 18 2019, 6:36 PM

Ok , I'll update it like this:

1 diff --git a/lib/Target/X86/X86InstructionSelector.cpp b/lib/Target/X86/X86InstructionSelector.cpp

2 index c3c41fe..92de59b 100644
3 --- a/lib/Target/X86/X86InstructionSelector.cpp
4 +++ b/lib/Target/X86/X86InstructionSelector.cpp
5 @@ -1600,11 +1600,8 @@ bool X86InstructionSelector::selectDivRem(MachineInstr &I,
6    assert(RegTy == MRI.getType(Op1Reg) && RegTy == MRI.getType(Op2Reg) &&
7           "Arguments and return value types must match");
8
9 -  const RegisterBank *PtrRegRB = RBI.getRegBank(DstReg, MRI, TRI);

10 - if (!PtrRegRB)
11 - return false;
12 - const RegisterBank &RegRB = *PtrRegRB;
13 - if (RegRB.getID() != X86::GPRRegBankID)
14 + const RegisterBank *RegRB = RBI.getRegBank(DstReg, MRI, TRI);
15 + if (!RegRB && RegRB->getID() != X86::GPRRegBankID)
16 return false;
17
18 const static unsigned NumTypes = 4; // i8, i16, i32, i64
19 @@ -1702,7 +1699,7 @@ bool X86InstructionSelector::selectDivRem(MachineInstr &I,
20 const DivRemEntry &TypeEntry = *OpEntryIt;
21 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
22
23 - const TargetRegisterClass *RegRC = getRegClass(RegTy, RegRB);
24 + const TargetRegisterClass *RegRC = getRegClass(RegTy, *RegRB);
25 if (!RBI.constrainGenericRegister(Op1Reg, *RegRC, MRI) ||
26 !RBI.constrainGenericRegister(Op2Reg, *RegRC, MRI) ||
27 !RBI.constrainGenericRegister(DstReg, *RegRC, MRI)) {

LuoYuanke added inline comments.Feb 18 2019, 9:03 PM
lib/Target/X86/X86InstructionSelector.cpp
1604 ↗(On Diff #187290)

Use || instead of &&?

Yes! Should be '||'! Thank you!

This revision is now accepted and ready to land.Feb 18 2019, 9:31 PM
This revision was automatically updated to reflect the committed changes.