Skip to content

Commit d4cb557

Browse files
committed
Address review comments
- Fix indentation. - Update comment for Address:ElementType. - Fix a bug where calls to getRawPointer in call argument expressions were causing the output to be dependent on the compiler.
1 parent cdcd3ec commit d4cb557

File tree

8 files changed

+80
-79
lines changed

8 files changed

+80
-79
lines changed

clang/lib/CodeGen/Address.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,9 @@ class Address {
114114
// The boolean flag indicates whether the pointer is known to be non-null.
115115
llvm::PointerIntPair<llvm::Value *, 1, bool> Pointer;
116116

117-
/// The expected IR type of the pointer. When the address is a raw pointer,
118-
/// this is currently redundant with the pointer's type, but for signed
119-
/// pointers it is useful if the pointer has been offsetted or cast from the
120-
/// original type. In the long run, when LLVM adopts opaque pointer types,
121-
/// this should become the notional element type of the address.
122-
///
123-
/// Carrying accurate element type information in Address makes it more
124-
/// convenient to work with Address values and allows frontend assertions to
125-
/// catch simple mistakes even after LLVM adopts opaque pointer types.
117+
/// The expected IR type of the pointer. Carrying accurate element type
118+
/// information in Address makes it more convenient to work with Address
119+
/// values and allows frontend assertions to catch simple mistakes.
126120
llvm::Type *ElementType = nullptr;
127121

128122
CharUnits Alignment;

clang/lib/CodeGen/CGAtomic.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,10 +1901,10 @@ std::pair<RValue, llvm::Value *> AtomicInfo::EmitAtomicCompareExchange(
19011901
if (shouldUseLibcall()) {
19021902
// Produce a source address.
19031903
Address ExpectedAddr = materializeRValue(Expected);
1904-
Address DesiredAddr = materializeRValue(Desired);
1905-
auto *Res = EmitAtomicCompareExchangeLibcall(
1906-
ExpectedAddr.getRawPointer(CGF), DesiredAddr.getRawPointer(CGF),
1907-
Success, Failure);
1904+
llvm::Value *ExpectedPtr = ExpectedAddr.getRawPointer(CGF);
1905+
llvm::Value *DesiredPtr = materializeRValue(Desired).getRawPointer(CGF);
1906+
auto *Res = EmitAtomicCompareExchangeLibcall(ExpectedPtr, DesiredPtr,
1907+
Success, Failure);
19081908
return std::make_pair(
19091909
convertAtomicTempToRValue(ExpectedAddr, AggValueSlot::ignored(),
19101910
SourceLocation(), /*AsValue=*/false),
@@ -1999,9 +1999,10 @@ void AtomicInfo::EmitAtomicUpdateLibcall(
19991999
AggValueSlot::ignored(),
20002000
SourceLocation(), /*AsValue=*/false);
20012001
EmitAtomicUpdateValue(CGF, *this, OldRVal, UpdateOp, DesiredAddr);
2002-
auto *Res = EmitAtomicCompareExchangeLibcall(ExpectedAddr.getRawPointer(CGF),
2003-
DesiredAddr.getRawPointer(CGF),
2004-
AO, Failure);
2002+
llvm::Value *ExpectedPtr = ExpectedAddr.getRawPointer(CGF);
2003+
llvm::Value *DesiredPtr = DesiredAddr.getRawPointer(CGF);
2004+
auto *Res =
2005+
EmitAtomicCompareExchangeLibcall(ExpectedPtr, DesiredPtr, AO, Failure);
20052006
CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
20062007
CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
20072008
}
@@ -2081,9 +2082,10 @@ void AtomicInfo::EmitAtomicUpdateLibcall(llvm::AtomicOrdering AO,
20812082
CGF.Builder.CreateStore(OldVal, DesiredAddr);
20822083
}
20832084
EmitAtomicUpdateValue(CGF, *this, UpdateRVal, DesiredAddr);
2084-
auto *Res = EmitAtomicCompareExchangeLibcall(ExpectedAddr.getRawPointer(CGF),
2085-
DesiredAddr.getRawPointer(CGF),
2086-
AO, Failure);
2085+
llvm::Value *ExpectedPtr = ExpectedAddr.getRawPointer(CGF);
2086+
llvm::Value *DesiredPtr = DesiredAddr.getRawPointer(CGF);
2087+
auto *Res =
2088+
EmitAtomicCompareExchangeLibcall(ExpectedPtr, DesiredPtr, AO, Failure);
20872089
CGF.Builder.CreateCondBr(Res, ExitBB, ContBB);
20882090
CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
20892091
}

clang/lib/CodeGen/CGBuilder.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,33 +363,33 @@ class CGBuilderTy : public CGBuilderBaseTy {
363363
using CGBuilderBaseTy::CreateMemCpy;
364364
llvm::CallInst *CreateMemCpy(Address Dest, Address Src, llvm::Value *Size,
365365
bool IsVolatile = false) {
366-
return CreateMemCpy(getRawPointerFromAddress(Dest),
367-
Dest.getAlignment().getAsAlign(),
368-
getRawPointerFromAddress(Src),
366+
llvm::Value *DestPtr = getRawPointerFromAddress(Dest);
367+
llvm::Value *SrcPtr = getRawPointerFromAddress(Src);
368+
return CreateMemCpy(DestPtr, Dest.getAlignment().getAsAlign(), SrcPtr,
369369
Src.getAlignment().getAsAlign(), Size, IsVolatile);
370370
}
371371
llvm::CallInst *CreateMemCpy(Address Dest, Address Src, uint64_t Size,
372372
bool IsVolatile = false) {
373-
return CreateMemCpy(getRawPointerFromAddress(Dest),
374-
Dest.getAlignment().getAsAlign(),
375-
getRawPointerFromAddress(Src),
373+
llvm::Value *DestPtr = getRawPointerFromAddress(Dest);
374+
llvm::Value *SrcPtr = getRawPointerFromAddress(Src);
375+
return CreateMemCpy(DestPtr, Dest.getAlignment().getAsAlign(), SrcPtr,
376376
Src.getAlignment().getAsAlign(), Size, IsVolatile);
377377
}
378378

379379
using CGBuilderBaseTy::CreateMemCpyInline;
380380
llvm::CallInst *CreateMemCpyInline(Address Dest, Address Src, uint64_t Size) {
381-
return CreateMemCpyInline(getRawPointerFromAddress(Dest),
382-
Dest.getAlignment().getAsAlign(),
383-
getRawPointerFromAddress(Src),
381+
llvm::Value *DestPtr = getRawPointerFromAddress(Dest);
382+
llvm::Value *SrcPtr = getRawPointerFromAddress(Src);
383+
return CreateMemCpyInline(DestPtr, Dest.getAlignment().getAsAlign(), SrcPtr,
384384
Src.getAlignment().getAsAlign(), getInt64(Size));
385385
}
386386

387387
using CGBuilderBaseTy::CreateMemMove;
388388
llvm::CallInst *CreateMemMove(Address Dest, Address Src, llvm::Value *Size,
389389
bool IsVolatile = false) {
390-
return CreateMemMove(getRawPointerFromAddress(Dest),
391-
Dest.getAlignment().getAsAlign(),
392-
getRawPointerFromAddress(Src),
390+
llvm::Value *DestPtr = getRawPointerFromAddress(Dest);
391+
llvm::Value *SrcPtr = getRawPointerFromAddress(Src);
392+
return CreateMemMove(DestPtr, Dest.getAlignment().getAsAlign(), SrcPtr,
393393
Src.getAlignment().getAsAlign(), Size, IsVolatile);
394394
}
395395

clang/lib/CodeGen/CGCall.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,9 +1144,10 @@ void CodeGenFunction::ExpandTypeToArgs(
11441144
}
11451145

11461146
/// Create a temporary allocation for the purposes of coercion.
1147-
static Address CreateTempAllocaForCoercion(CodeGenFunction &CGF, llvm::Type *Ty,
1148-
CharUnits MinAlign,
1149-
const Twine &Name = "tmp") {
1147+
static RawAddress CreateTempAllocaForCoercion(CodeGenFunction &CGF,
1148+
llvm::Type *Ty,
1149+
CharUnits MinAlign,
1150+
const Twine &Name = "tmp") {
11501151
// Don't use an alignment that's worse than what LLVM would prefer.
11511152
auto PrefAlign = CGF.CGM.getDataLayout().getPrefTypeAlign(Ty);
11521153
CharUnits Align = std::max(MinAlign, CharUnits::fromQuantity(PrefAlign));
@@ -1318,11 +1319,11 @@ static llvm::Value *CreateCoercedLoad(Address Src, llvm::Type *Ty,
13181319
}
13191320

13201321
// Otherwise do coercion through memory. This is stupid, but simple.
1321-
Address Tmp =
1322+
RawAddress Tmp =
13221323
CreateTempAllocaForCoercion(CGF, Ty, Src.getAlignment(), Src.getName());
13231324
CGF.Builder.CreateMemCpy(
1324-
Tmp.getRawPointer(CGF), Tmp.getAlignment().getAsAlign(),
1325-
Src.getRawPointer(CGF), Src.getAlignment().getAsAlign(),
1325+
Tmp.getPointer(), Tmp.getAlignment().getAsAlign(), Src.getRawPointer(CGF),
1326+
Src.getAlignment().getAsAlign(),
13261327
llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize.getKnownMinValue()));
13271328
return CGF.Builder.CreateLoad(Tmp);
13281329
}
@@ -1406,11 +1407,12 @@ static void CreateCoercedStore(llvm::Value *Src,
14061407
//
14071408
// FIXME: Assert that we aren't truncating non-padding bits when have access
14081409
// to that information.
1409-
Address Tmp = CreateTempAllocaForCoercion(CGF, SrcTy, Dst.getAlignment());
1410+
RawAddress Tmp =
1411+
CreateTempAllocaForCoercion(CGF, SrcTy, Dst.getAlignment());
14101412
CGF.Builder.CreateStore(Src, Tmp);
14111413
CGF.Builder.CreateMemCpy(
14121414
Dst.getRawPointer(CGF), Dst.getAlignment().getAsAlign(),
1413-
Tmp.getRawPointer(CGF), Tmp.getAlignment().getAsAlign(),
1415+
Tmp.getPointer(), Tmp.getAlignment().getAsAlign(),
14141416
llvm::ConstantInt::get(CGF.IntPtrTy, DstSize.getFixedValue()));
14151417
}
14161418
}
@@ -3004,7 +3006,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
30043006
// may be aliased, copy it to ensure that the parameter variable is
30053007
// mutable and has a unique adress, as C requires.
30063008
if (ArgI.getIndirectRealign() || ArgI.isIndirectAliased()) {
3007-
Address AlignedTemp = CreateMemTemp(Ty, "coerce");
3009+
RawAddress AlignedTemp = CreateMemTemp(Ty, "coerce");
30083010

30093011
// Copy from the incoming argument pointer to the temporary with the
30103012
// appropriate alignment.
@@ -3013,8 +3015,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
30133015
// copy.
30143016
CharUnits Size = getContext().getTypeSizeInChars(Ty);
30153017
Builder.CreateMemCpy(
3016-
AlignedTemp.getRawPointer(*this),
3017-
AlignedTemp.getAlignment().getAsAlign(),
3018+
AlignedTemp.getPointer(), AlignedTemp.getAlignment().getAsAlign(),
30183019
ParamAddr.getRawPointer(*this),
30193020
ParamAddr.getAlignment().getAsAlign(),
30203021
llvm::ConstantInt::get(IntPtrTy, Size.getQuantity()));

clang/lib/CodeGen/CGException.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -390,27 +390,27 @@ namespace {
390390
void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) {
391391
// Make sure the exception object is cleaned up if there's an
392392
// exception during initialization.
393-
pushFullExprCleanup<FreeException>(EHCleanup, addr.getRawPointer(*this));
394-
EHScopeStack::stable_iterator cleanup = EHStack.stable_begin();
395-
396-
// __cxa_allocate_exception returns a void*; we need to cast this
397-
// to the appropriate type for the object.
398-
llvm::Type *ty = ConvertTypeForMem(e->getType());
399-
Address typedAddr = addr.withElementType(ty);
400-
401-
// FIXME: this isn't quite right! If there's a final unelided call
402-
// to a copy constructor, then according to [except.terminate]p1 we
403-
// must call std::terminate() if that constructor throws, because
404-
// technically that copy occurs after the exception expression is
405-
// evaluated but before the exception is caught. But the best way
406-
// to handle that is to teach EmitAggExpr to do the final copy
407-
// differently if it can't be elided.
408-
EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
409-
/*IsInit*/ true);
410-
411-
// Deactivate the cleanup block.
412-
DeactivateCleanupBlock(
413-
cleanup, cast<llvm::Instruction>(typedAddr.getRawPointer(*this)));
393+
pushFullExprCleanup<FreeException>(EHCleanup, addr.getRawPointer(*this));
394+
EHScopeStack::stable_iterator cleanup = EHStack.stable_begin();
395+
396+
// __cxa_allocate_exception returns a void*; we need to cast this
397+
// to the appropriate type for the object.
398+
llvm::Type *ty = ConvertTypeForMem(e->getType());
399+
Address typedAddr = addr.withElementType(ty);
400+
401+
// FIXME: this isn't quite right! If there's a final unelided call
402+
// to a copy constructor, then according to [except.terminate]p1 we
403+
// must call std::terminate() if that constructor throws, because
404+
// technically that copy occurs after the exception expression is
405+
// evaluated but before the exception is caught. But the best way
406+
// to handle that is to teach EmitAggExpr to do the final copy
407+
// differently if it can't be elided.
408+
EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
409+
/*IsInit*/ true);
410+
411+
// Deactivate the cleanup block.
412+
DeactivateCleanupBlock(
413+
cleanup, cast<llvm::Instruction>(typedAddr.getRawPointer(*this)));
414414
}
415415

416416
Address CodeGenFunction::getExceptionSlot() {

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,9 @@ void CodeGenFunction::EmitNewArrayInitializer(
12611261

12621262
// Enter a partial-destruction Cleanup if necessary.
12631263
if (!CleanupDominator && needsEHCleanup(DtorKind)) {
1264-
pushRegularPartialArrayCleanup(BeginPtr.getRawPointer(*this),
1265-
CurPtr.getRawPointer(*this), ElementType,
1264+
llvm::Value *BeginPtrRaw = BeginPtr.getRawPointer(*this);
1265+
llvm::Value *CurPtrRaw = CurPtr.getRawPointer(*this);
1266+
pushRegularPartialArrayCleanup(BeginPtrRaw, CurPtrRaw, ElementType,
12661267
ElementAlign, getDestroyer(DtorKind));
12671268
Cleanup = EHStack.stable_begin();
12681269
CleanupDominator = Builder.CreateUnreachable();

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4475,12 +4475,12 @@ void CGOpenMPRuntime::emitUpdateClause(CodeGenFunction &CGF, LValue DepobjLVal,
44754475
FlagsLVal);
44764476

44774477
// Shift the address forward by one element.
4478-
Address ElementNext =
4479-
CGF.Builder.CreateConstGEP(Begin, /*Index=*/1, "omp.elementNext");
4480-
ElementPHI->addIncoming(ElementNext.getRawPointer(CGF),
4481-
CGF.Builder.GetInsertBlock());
4482-
llvm::Value *IsEmpty = CGF.Builder.CreateICmpEQ(
4483-
ElementNext.getRawPointer(CGF), End, "omp.isempty");
4478+
llvm::Value *ElementNext =
4479+
CGF.Builder.CreateConstGEP(Begin, /*Index=*/1, "omp.elementNext")
4480+
.getRawPointer(CGF);
4481+
ElementPHI->addIncoming(ElementNext, CGF.Builder.GetInsertBlock());
4482+
llvm::Value *IsEmpty =
4483+
CGF.Builder.CreateICmpEQ(ElementNext, End, "omp.isempty");
44844484
CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB);
44854485
// Done.
44864486
CGF.EmitBlock(DoneBB, /*IsFinished=*/true);
@@ -7304,9 +7304,10 @@ class MappableExprsHandler {
73047304
CGF.EmitOMPSharedLValue(MC.getAssociatedExpression())
73057305
.getAddress(CGF);
73067306
}
7307-
Size = CGF.Builder.CreatePtrDiff(CGF.Int8Ty,
7308-
ComponentLB.getRawPointer(CGF),
7309-
LB.getRawPointer(CGF));
7307+
llvm::Value *ComponentLBPtr = ComponentLB.getRawPointer(CGF);
7308+
llvm::Value *LBPtr = LB.getRawPointer(CGF);
7309+
Size = CGF.Builder.CreatePtrDiff(CGF.Int8Ty, ComponentLBPtr,
7310+
LBPtr);
73107311
break;
73117312
}
73127313
}
@@ -7329,9 +7330,10 @@ class MappableExprsHandler {
73297330
CombinedInfo.DevicePtrDecls.push_back(nullptr);
73307331
CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
73317332
CombinedInfo.Pointers.push_back(LB.getRawPointer(CGF));
7333+
llvm::Value *LBPtr = LB.getRawPointer(CGF);
73327334
Size = CGF.Builder.CreatePtrDiff(
73337335
CGF.Int8Ty, CGF.Builder.CreateConstGEP(HB, 1).getRawPointer(CGF),
7334-
LB.getRawPointer(CGF));
7336+
LBPtr);
73357337
CombinedInfo.Sizes.push_back(
73367338
CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
73377339
CombinedInfo.Types.push_back(Flags);

clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,11 @@ static void shuffleAndStore(CodeGenFunction &CGF, Address SrcAddr,
15111511
Ptr = Address(PhiSrc, Ptr.getElementType(), Ptr.getAlignment());
15121512
ElemPtr =
15131513
Address(PhiDest, ElemPtr.getElementType(), ElemPtr.getAlignment());
1514-
llvm::Value *PtrDiff =
1515-
Bld.CreatePtrDiff(CGF.Int8Ty, PtrEnd.getRawPointer(CGF),
1516-
Bld.CreatePointerBitCastOrAddrSpaceCast(
1517-
Ptr.getRawPointer(CGF), CGF.VoidPtrTy));
1514+
llvm::Value *PtrEndRaw = PtrEnd.getRawPointer(CGF);
1515+
llvm::Value *PtrRaw = Ptr.getRawPointer(CGF);
1516+
llvm::Value *PtrDiff = Bld.CreatePtrDiff(
1517+
CGF.Int8Ty, PtrEndRaw,
1518+
Bld.CreatePointerBitCastOrAddrSpaceCast(PtrRaw, CGF.VoidPtrTy));
15181519
Bld.CreateCondBr(Bld.CreateICmpSGT(PtrDiff, Bld.getInt64(IntSize - 1)),
15191520
ThenBB, ExitBB);
15201521
CGF.EmitBlock(ThenBB);

0 commit comments

Comments
 (0)