Skip to content

Commit

Permalink
Revise DtoGEP() helpers and mark most GEPs inbounds
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Dec 13, 2015
1 parent 5efb6bd commit d0019b9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 58 deletions.
4 changes: 2 additions & 2 deletions gen/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ static void DtoArrayInit(Loc &loc, LLValue *ptr, LLValue *length,
LLValue *itr_val = DtoLoad(itr);
// assign array element value
DValue *arrayelem = new DVarValue(
dvalue->type->toBasetype(), DtoGEP1(ptr, itr_val, "arrayinit.arrayelem"));
dvalue->type->toBasetype(), DtoGEP1(ptr, itr_val, true, "arrayinit.arrayelem"));
DtoAssign(loc, arrayelem, dvalue, op);

// increment iterator
Expand Down Expand Up @@ -761,7 +761,7 @@ void DtoCatAssignElement(Loc &loc, Type *arrayType, DValue *array,
appendedArray = DtoAggrPaint(appendedArray, DtoType(arrayType));

LLValue *val = DtoArrayPtr(array);
val = DtoGEP1(val, oldLength, ".lastElem");
val = DtoGEP1(val, oldLength, true, ".lastElem");
DtoAssign(loc, new DVarValue(arrayType->nextOf(), val), expVal, TOKblit);
callPostblit(loc, exp, val);
}
Expand Down
4 changes: 2 additions & 2 deletions gen/complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ void DtoGetComplexParts(Loc &loc, Type *to, DValue *val, DValue *&re,
if (to->iscomplex()) {
if (v->isLVal()) {
LLValue *reVal =
DtoGEP(v->getLVal(), DtoConstInt(0), DtoConstInt(0), ".re_part");
DtoGEPi(v->getLVal(), 0, 0, ".re_part");
LLValue *imVal =
DtoGEP(v->getLVal(), DtoConstInt(0), DtoConstInt(1), ".im_part");
DtoGEPi(v->getLVal(), 0, 1, ".im_part");
re = new DVarValue(baserety, reVal);
im = new DVarValue(baseimty, imVal);
} else {
Expand Down
2 changes: 1 addition & 1 deletion gen/statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ class ToIRVisitor : public Visitor {

// get value for this iteration
LLValue *loadedKey = irs->ir->CreateLoad(keyvar);
LLValue *gep = DtoGEP1(val, loadedKey);
LLValue *gep = DtoGEP1(val, loadedKey, true);

if (!stmt->value->isRef() && !stmt->value->isOut()) {
// Copy value to local variable, and use it as the value variable.
Expand Down
2 changes: 1 addition & 1 deletion gen/tocall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ bool DtoLowerMagicIntrinsic(IRState *p, FuncDeclaration *fndecl, CallExp *e,
assert(bitmask == 31 || bitmask == 63);
// auto q = cast(size_t*)ptr + (bitnum >> (64bit ? 6 : 5));
LLValue *q = DtoBitCast(ptr, DtoSize_t()->getPointerTo());
q = DtoGEP1(q, p->ir->CreateLShr(bitnum, bitmask == 63 ? 6 : 5), "bitop.q");
q = DtoGEP1(q, p->ir->CreateLShr(bitnum, bitmask == 63 ? 6 : 5), true, "bitop.q");

// auto mask = 1 << (bitnum & bitmask);
LLValue *mask =
Expand Down
13 changes: 6 additions & 7 deletions gen/toir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ class ToElemVisitor : public Visitor {
noStrideInc = p->ir->CreateNeg(noStrideInc);
}
return new DImValue(
base->type, DtoGEP1(base->getRVal(), noStrideInc, "", p->scopebb()));
base->type, DtoGEP1(base->getRVal(), noStrideInc, false));
}

// This might not actually be generated by the frontend, just to be
Expand All @@ -750,7 +750,7 @@ class ToElemVisitor : public Visitor {
inc = p->ir->CreateNeg(inc);
}
llvm::Value *bytePtr = DtoBitCast(base->getRVal(), getVoidPtrType());
DValue *result = new DImValue(Type::tvoidptr, DtoGEP1(bytePtr, inc));
DValue *result = new DImValue(Type::tvoidptr, DtoGEP1(bytePtr, inc, false));
return DtoCast(loc, result, resultType);
}

Expand Down Expand Up @@ -1284,18 +1284,17 @@ class ToElemVisitor : public Visitor {

LLValue *arrptr = nullptr;
if (e1type->ty == Tpointer) {
arrptr = DtoGEP1(l->getRVal(), r->getRVal());
arrptr = DtoGEP1(l->getRVal(), r->getRVal(), false);
} else if (e1type->ty == Tsarray) {
if (p->emitArrayBoundsChecks() && !e->indexIsInBounds) {
DtoIndexBoundsCheck(e->loc, l, r);
}
arrptr = DtoGEP(l->getRVal(), zero, r->getRVal());
arrptr = DtoGEP(l->getRVal(), zero, r->getRVal(), e->indexIsInBounds);
} else if (e1type->ty == Tarray) {
if (p->emitArrayBoundsChecks() && !e->indexIsInBounds) {
DtoIndexBoundsCheck(e->loc, l, r);
}
arrptr = DtoArrayPtr(l);
arrptr = DtoGEP1(arrptr, r->getRVal());
arrptr = DtoGEP1(DtoArrayPtr(l), r->getRVal(), e->indexIsInBounds);
} else if (e1type->ty == Taarray) {
result = DtoAAIndex(e->loc, e->type, l, r, e->modifiable);
return;
Expand Down Expand Up @@ -1382,7 +1381,7 @@ class ToElemVisitor : public Visitor {
}

// offset by lower
eptr = DtoGEP1(eptr, vlo, "lowerbound");
eptr = DtoGEP1(eptr, vlo, !needCheckLower, "lowerbound");

// adjust length
elen = p->ir->CreateSub(vup, vlo);
Expand Down
64 changes: 24 additions & 40 deletions gen/tollvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,69 +257,53 @@ LLIntegerType *DtoSize_t() {

////////////////////////////////////////////////////////////////////////////////

LLValue *DtoGEP1(LLValue *ptr, LLValue *i0, const char *var,
llvm::BasicBlock *bb) {
namespace {
llvm::GetElementPtrInst *DtoGEP(LLValue *ptr, llvm::ArrayRef<LLValue *> indices,
bool inBounds, const char *name,
llvm::BasicBlock *bb) {
LLPointerType *p = isaPointer(ptr);
assert(p && "GEP expects a pointer type");
return llvm::GetElementPtrInst::Create(
auto gep = llvm::GetElementPtrInst::Create(
#if LDC_LLVM_VER >= 307
p->getElementType(),
#endif
ptr, i0, var, bb ? bb : gIR->scopebb());
ptr, indices, name, bb ? bb : gIR->scopebb());
gep->setIsInBounds(inBounds);
return gep;
}
}

////////////////////////////////////////////////////////////////////////////////

LLValue *DtoGEP(LLValue *ptr, LLValue *i0, LLValue *i1, const char *var,
llvm::BasicBlock *bb) {
LLPointerType *p = isaPointer(ptr);
assert(p && "GEP expects a pointer type");
LLValue *v[] = {i0, i1};
return llvm::GetElementPtrInst::Create(
#if LDC_LLVM_VER >= 307
p->getElementType(),
#endif
ptr, v, var, bb ? bb : gIR->scopebb());
LLValue *DtoGEP1(LLValue *ptr, LLValue *i0, bool inBounds, const char *name,
llvm::BasicBlock *bb) {
return DtoGEP(ptr, i0, inBounds, name, bb);
}

////////////////////////////////////////////////////////////////////////////////
LLValue *DtoGEP(LLValue *ptr, LLValue *i0, LLValue *i1, bool inBounds,
const char *name, llvm::BasicBlock *bb) {
LLValue *indices[] = {i0, i1};
return DtoGEP(ptr, indices, inBounds, name, bb);
}

LLValue *DtoGEPi1(LLValue *ptr, unsigned i, const char *var,
LLValue *DtoGEPi1(LLValue *ptr, unsigned i0, const char *name,
llvm::BasicBlock *bb) {
LLPointerType *p = isaPointer(ptr);
assert(p && "GEP expects a pointer type");
return llvm::GetElementPtrInst::Create(
#if LDC_LLVM_VER >= 307
p->getElementType(),
#endif
ptr, DtoConstUint(i), var, bb ? bb : gIR->scopebb());
return DtoGEP(ptr, DtoConstUint(i0), /* inBounds = */ true, name, bb);
}

////////////////////////////////////////////////////////////////////////////////

LLValue *DtoGEPi(LLValue *ptr, unsigned i0, unsigned i1, const char *var,
LLValue *DtoGEPi(LLValue *ptr, unsigned i0, unsigned i1, const char *name,
llvm::BasicBlock *bb) {
LLPointerType *p = isaPointer(ptr);
assert(p && "GEP expects a pointer type");
LLValue *v[] = {DtoConstUint(i0), DtoConstUint(i1)};
return llvm::GetElementPtrInst::Create(
#if LDC_LLVM_VER >= 307
p->getElementType(),
#endif
ptr, v, var, bb ? bb : gIR->scopebb());
LLValue *indices[] = {DtoConstUint(i0), DtoConstUint(i1)};
return DtoGEP(ptr, indices, /* inBounds = */ true, name, bb);
}

////////////////////////////////////////////////////////////////////////////////

LLConstant *DtoGEPi(LLConstant *ptr, unsigned i0, unsigned i1) {
LLPointerType *p = isaPointer(ptr);
assert(p && "GEP expects a pointer type");
LLValue *v[] = {DtoConstUint(i0), DtoConstUint(i1)};
LLValue *indices[] = {DtoConstUint(i0), DtoConstUint(i1)};
return llvm::ConstantExpr::getGetElementPtr(
#if LDC_LLVM_VER >= 307
p->getElementType(),
#endif
ptr, v, true);
ptr, indices, /* InBounds = */ true);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions gen/tollvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ LLStructType *DtoMutexType();
LLStructType *DtoModuleReferenceType();

// getelementptr helpers
LLValue *DtoGEP1(LLValue *ptr, LLValue *i0, const char *name = "",
llvm::BasicBlock *bb = nullptr);
LLValue *DtoGEP(LLValue *ptr, LLValue *i0, LLValue *i1, const char *name = "",
llvm::BasicBlock *bb = nullptr);
LLValue *DtoGEP1(LLValue *ptr, LLValue *i0, bool inBounds,
const char *name = "", llvm::BasicBlock *bb = nullptr);
LLValue *DtoGEP(LLValue *ptr, LLValue *i0, LLValue *i1, bool inBounds,
const char *name = "", llvm::BasicBlock *bb = nullptr);

LLValue *DtoGEPi1(LLValue *ptr, unsigned i0, const char *name = "",
llvm::BasicBlock *bb = nullptr);
Expand Down
2 changes: 1 addition & 1 deletion ir/irclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ llvm::GlobalVariable *IrAggr::getInterfaceVtbl(BaseClass *b, bool new_instance,
: 1];
LLType *targetThisType = thisArg->getType();
thisArg = DtoBitCast(thisArg, getVoidPtrType());
thisArg = DtoGEP1(thisArg, DtoConstInt(-b->offset));
thisArg = DtoGEP1(thisArg, DtoConstInt(-b->offset), true);
thisArg = DtoBitCast(thisArg, targetThisType);

// call the real vtbl function.
Expand Down

0 comments on commit d0019b9

Please sign in to comment.