Skip to content

Commit

Permalink
Merge pull request #2135 from kinke/gh2131
Browse files Browse the repository at this point in the history
Add range metadata when loading bools
  • Loading branch information
dnadlinger committed Jun 3, 2017
2 parents 3ec35df + c9b2571 commit e659db6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
12 changes: 12 additions & 0 deletions gen/dvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/logger.h"
#include "gen/optimizer.h"
#include "gen/tollvm.h"
#include "llvm/IR/MDBuilder.h"

namespace {
bool isDefinedInFuncEntryBB(LLValue *v) {
Expand Down Expand Up @@ -121,6 +123,16 @@ DRValue *DLValue::getRVal() {
LLValue *rval = DtoLoad(val);
if (type->toBasetype()->ty == Tbool) {
assert(rval->getType() == llvm::Type::getInt8Ty(gIR->context()));

if (isOptimizationEnabled()) {
// attach range metadata for i8 being loaded: [0, 2)
llvm::MDBuilder mdBuilder(gIR->context());
llvm::cast<llvm::LoadInst>(rval)->setMetadata(
llvm::LLVMContext::MD_range,
mdBuilder.createRange(llvm::APInt(8, 0), llvm::APInt(8, 2)));
}

// truncate to i1
rval = gIR->ir->CreateTrunc(rval, llvm::Type::getInt1Ty(gIR->context()));
}

Expand Down
19 changes: 2 additions & 17 deletions gen/tollvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,9 @@ bool DtoIsReturnInArg(CallExp *ce) {

LLAttribute DtoShouldExtend(Type *type) {
type = type->toBasetype();
if (type->isintegral()) {
switch (type->ty) {
case Tint8:
case Tint16:
return LLAttribute::SExt;

case Tuns8:
case Tuns16:
case Tchar:
case Twchar:
return LLAttribute::ZExt;

default:
// Do not extend.
break;
}
if (type->isintegral() && type->ty != Tvector && type->size() <= 2) {
return type->isunsigned() ? LLAttribute::ZExt : LLAttribute::SExt;
}

return LLAttribute::None;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/codegen/gh2131.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %ldc -O3 -output-ll -of=%t.ll %s && FileCheck %s < %t.ll

// CHECK: define {{.*}}zeroext {{.*}}@{{.*}}_D6gh21313foo
// CHECK-SAME: i1 zeroext %x_arg
bool foo(bool x, ref bool o)
{
// CHECK-NOT: and i8
// CHECK: load i8{{.*}}, !range ![[META:[0-9]+]]
// CHECK-NOT: and i8
o |= x;
// CHECK: ret
return o;
}

// CHECK: ![[META]] = {{.*}}!{i8 0, i8 2}

0 comments on commit e659db6

Please sign in to comment.