[svn r386] Fixed broken DtoBoolean.

Some code cleanup.
This commit is contained in:
Tomas Lindquist Olsen 2008-07-15 00:17:03 +02:00
parent 25dea7a16e
commit 86a3f53cfe
7 changed files with 81 additions and 58 deletions

View file

@ -758,7 +758,7 @@ static LLValue* DtoArrayEqCmp_impl(const char* func, DValue* l, DValue* r, bool
LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r) LLValue* DtoArrayEquals(TOK op, DValue* l, DValue* r)
{ {
LLValue* res = DtoArrayEqCmp_impl("_adEq", l, r, true); LLValue* res = DtoArrayEqCmp_impl("_adEq", l, r, true);
res = new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, res, DtoConstInt(0), "tmp", gIR->scopebb()); res = gIR->ir->CreateICmpNE(res, DtoConstInt(0), "tmp");
if (op == TOKnotequal) if (op == TOKnotequal)
res = gIR->ir->CreateNot(res, "tmp"); res = gIR->ir->CreateNot(res, "tmp");
@ -817,7 +817,7 @@ LLValue* DtoArrayCompare(TOK op, DValue* l, DValue* r)
res = DtoArrayEqCmp_impl("_adCmpChar", l, r, false); res = DtoArrayEqCmp_impl("_adCmpChar", l, r, false);
else else
res = DtoArrayEqCmp_impl("_adCmp", l, r, true); res = DtoArrayEqCmp_impl("_adCmp", l, r, true);
res = new llvm::ICmpInst(cmpop, res, DtoConstInt(0), "tmp", gIR->scopebb()); res = gIR->ir->CreateICmp(cmpop, res, DtoConstInt(0), "tmp");
} }
assert(res); assert(res);
@ -859,12 +859,12 @@ LLValue* DtoDynArrayIs(TOK op, DValue* l, DValue* r)
// compare lengths // compare lengths
len1 = DtoArrayLen(l); len1 = DtoArrayLen(l);
len2 = DtoArrayLen(r); len2 = DtoArrayLen(r);
LLValue* b1 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,len1,len2,"tmp"); LLValue* b1 = gIR->ir->CreateICmpEQ(len1,len2,"tmp");
// compare pointers // compare pointers
ptr1 = DtoArrayPtr(l); ptr1 = DtoArrayPtr(l);
ptr2 = DtoArrayPtr(r); ptr2 = DtoArrayPtr(r);
LLValue* b2 = gIR->ir->CreateICmp(llvm::ICmpInst::ICMP_EQ,ptr1,ptr2,"tmp"); LLValue* b2 = gIR->ir->CreateICmpEQ(ptr1,ptr2,"tmp");
// combine // combine
LLValue* res = gIR->ir->CreateAnd(b1,b2,"tmp"); LLValue* res = gIR->ir->CreateAnd(b1,b2,"tmp");

View file

@ -4,6 +4,7 @@
#include "mars.h" #include "mars.h"
#include "init.h" #include "init.h"
#include "id.h" #include "id.h"
#include "expression.h"
#include "gen/tollvm.h" #include "gen/tollvm.h"
#include "gen/llvmhelpers.h" #include "gen/llvmhelpers.h"
@ -1312,3 +1313,58 @@ void findDefaultTarget()
global.params.llvmArch = const_cast<char*>(e->Name); global.params.llvmArch = const_cast<char*>(e->Name);
} }
} }
//////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoBoolean(DValue* dval)
{
Type* dtype = dval->getType()->toBasetype();
TY ty = dtype->ty;
// integer
if (dtype->isintegral())
{
LLValue* val = dval->getRVal();
if (val->getType() == LLType::Int1Ty)
return val;
else {
LLValue* zero = LLConstantInt::get(val->getType(), 0, false);
return gIR->ir->CreateICmpNE(val, zero, "tmp");
}
}
// complex
else if (dtype->iscomplex())
{
// huh?
return DtoComplexEquals(TOKnotequal, dval, DtoComplex(dtype, new DNullValue(Type::tint8, llvm::ConstantInt::get(LLType::Int8Ty, 0))));
}
// floating point
else if (dtype->isfloating())
{
LLValue* val = dval->getRVal();
LLValue* zero = LLConstant::getNullValue(val->getType());
return gIR->ir->CreateFCmpONE(val, zero, "tmp");
}
// pointer/class
else if (ty == Tpointer || ty == Tclass) {
LLValue* val = dval->getRVal();
LLValue* zero = LLConstant::getNullValue(val->getType());
return gIR->ir->CreateICmpNE(val, zero, "tmp");
}
// dynamic array
else if (ty == Tarray)
{
// return (arr.length != 0)
return gIR->ir->CreateICmpNE(DtoArrayLen(dval), DtoConstSize_t(0), "tmp");
}
// delegate
else if (ty == Tdelegate)
{
// return (dg !is null)
return DtoDelegateEquals(TOKnotequal, dval->getRVal(), NULL);
}
// unknown
std::cout << "unsupported -> bool : " << dtype->toChars() << '\n';
assert(0);
return 0;
}

View file

@ -98,4 +98,7 @@ void findDefaultTarget();
*/ */
DValue* DtoCallDFunc(FuncDeclaration* fdecl, Array* arguments, TypeClass* type=0, LLValue* thismem=0); DValue* DtoCallDFunc(FuncDeclaration* fdecl, Array* arguments, TypeClass* type=0, LLValue* thismem=0);
/// Converts any value to a boolean (llvm i1)
LLValue* DtoBoolean(DValue* dval);
#endif #endif

View file

@ -967,12 +967,12 @@ void ForeachStatement::toIR(IRState* p)
LLValue* done = 0; LLValue* done = 0;
LLValue* load = DtoLoad(keyvar); LLValue* load = DtoLoad(keyvar);
if (op == TOKforeach) { if (op == TOKforeach) {
done = new llvm::ICmpInst(llvm::ICmpInst::ICMP_ULT, load, niters, "tmp", p->scopebb()); done = p->ir->CreateICmpULT(load, niters, "tmp");
} }
else if (op == TOKforeach_reverse) { else if (op == TOKforeach_reverse) {
done = new llvm::ICmpInst(llvm::ICmpInst::ICMP_UGT, load, zerokey, "tmp", p->scopebb()); done = p->ir->CreateICmpUGT(load, zerokey, "tmp");
load = llvm::BinaryOperator::createSub(load,llvm::ConstantInt::get(keytype, 1, false),"tmp",p->scopebb()); load = p->ir->CreateSub(load, llvm::ConstantInt::get(keytype, 1, false), "tmp");
new llvm::StoreInst(load, keyvar, p->scopebb()); DtoStore(load, keyvar);
} }
llvm::BranchInst::Create(bodybb, endbb, done, p->scopebb()); llvm::BranchInst::Create(bodybb, endbb, done, p->scopebb());

View file

@ -1610,7 +1610,7 @@ DValue* CmpExp::toElem(IRState* p)
LLValue* b = r->getRVal(); LLValue* b = r->getRVal();
Logger::cout() << "type 1: " << *a << '\n'; Logger::cout() << "type 1: " << *a << '\n';
Logger::cout() << "type 2: " << *b << '\n'; Logger::cout() << "type 2: " << *b << '\n';
eval = new llvm::ICmpInst(cmpop, a, b, "tmp", p->scopebb()); eval = p->ir->CreateICmp(cmpop, a, b, "tmp");
} }
} }
else if (t->isfloating()) else if (t->isfloating())
@ -1646,7 +1646,7 @@ DValue* CmpExp::toElem(IRState* p)
default: default:
assert(0); assert(0);
} }
eval = new llvm::FCmpInst(cmpop, l->getRVal(), r->getRVal(), "tmp", p->scopebb()); eval = p->ir->CreateFCmp(cmpop, l->getRVal(), r->getRVal(), "tmp");
} }
else if (t->ty == Tsarray || t->ty == Tarray) else if (t->ty == Tsarray || t->ty == Tarray)
{ {
@ -1697,7 +1697,7 @@ DValue* EqualExp::toElem(IRState* p)
if (rv->getType() != lv->getType()) { if (rv->getType() != lv->getType()) {
rv = DtoBitCast(rv, lv->getType()); rv = DtoBitCast(rv, lv->getType());
} }
eval = new llvm::ICmpInst(cmpop, lv, rv, "tmp", p->scopebb()); eval = p->ir->CreateICmp(cmpop, lv, rv, "tmp");
} }
else if (t->iscomplex()) else if (t->iscomplex())
{ {
@ -1719,7 +1719,7 @@ DValue* EqualExp::toElem(IRState* p)
default: default:
assert(0); assert(0);
} }
eval = new llvm::FCmpInst(cmpop, l->getRVal(), r->getRVal(), "tmp", p->scopebb()); eval = p->ir->CreateFCmp(cmpop, l->getRVal(), r->getRVal(), "tmp");
} }
else if (t->ty == Tsarray || t->ty == Tarray) else if (t->ty == Tsarray || t->ty == Tarray)
{ {
@ -1996,7 +1996,7 @@ DValue* NotExp::toElem(IRState* p)
LLValue* b = DtoBoolean(u); LLValue* b = DtoBoolean(u);
LLConstant* zero = llvm::ConstantInt::get(LLType::Int1Ty, 0, true); LLConstant* zero = DtoConstBool(false);
b = p->ir->CreateICmpEQ(b,zero); b = p->ir->CreateICmpEQ(b,zero);
return new DImValue(type, b); return new DImValue(type, b);
@ -2262,8 +2262,9 @@ DValue* IdentityExp::toElem(IRState* p)
} }
else if (t1->isfloating()) else if (t1->isfloating())
{ {
llvm::FCmpInst::Predicate pred = (op == TOKidentity) ? llvm::FCmpInst::FCMP_OEQ : llvm::FCmpInst::FCMP_ONE; eval = (op == TOKidentity)
eval = new llvm::FCmpInst(pred, l, r, "tmp", p->scopebb()); ? p->ir->CreateFCmpOEQ(l,r,"tmp")
: p->ir->CreateFCmpONE(l,r,"tmp");
} }
else if (t1->ty == Tpointer) else if (t1->ty == Tpointer)
{ {
@ -2273,13 +2274,14 @@ DValue* IdentityExp::toElem(IRState* p)
else else
r = DtoBitCast(r, l->getType()); r = DtoBitCast(r, l->getType());
} }
llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; eval = (op == TOKidentity)
eval = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); ? p->ir->CreateICmpEQ(l,r,"tmp")
: p->ir->CreateICmpNE(l,r,"tmp");
} }
else { else {
llvm::ICmpInst::Predicate pred = (op == TOKidentity) ? llvm::ICmpInst::ICMP_EQ : llvm::ICmpInst::ICMP_NE; eval = (op == TOKidentity)
//Logger::cout() << "l = " << *l << " r = " << *r << '\n'; ? p->ir->CreateICmpEQ(l,r,"tmp")
eval = new llvm::ICmpInst(pred, l, r, "tmp", p->scopebb()); : p->ir->CreateICmpNE(l,r,"tmp");
} }
return new DImValue(type, eval); return new DImValue(type, eval);
} }

View file

@ -326,41 +326,6 @@ LLValue* DtoPointedType(LLValue* ptr, LLValue* val)
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
LLValue* DtoBoolean(DValue* dval)
{
Type* dtype = dval->getType()->toBasetype();
LLValue* val = dval->getRVal();
const LLType* t = val->getType();
if (dtype->isintegral())
{
if (t == LLType::Int1Ty)
return val;
else {
LLValue* zero = llvm::ConstantInt::get(t, 0, false);
return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb());
}
}
else if (dtype->iscomplex())
{
return DtoComplexEquals(TOKnotequal, dval, DtoComplex(dtype, new DNullValue(Type::tint8, llvm::ConstantInt::get(LLType::Int8Ty, 0))));
}
else if (dtype->isfloating())
{
LLValue* zero = llvm::Constant::getNullValue(t);
return new llvm::FCmpInst(llvm::FCmpInst::FCMP_ONE, val, zero, "tmp", gIR->scopebb());
}
else if (dtype->ty == Tpointer) {
LLValue* zero = llvm::Constant::getNullValue(t);
return new llvm::ICmpInst(llvm::ICmpInst::ICMP_NE, val, zero, "tmp", gIR->scopebb());
}
std::cout << "unsupported -> bool : " << dtype->toChars() << " " << *t << '\n';
assert(0);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////
const LLType* DtoSize_t() const LLType* DtoSize_t()
{ {
// the type of size_t does not change once set // the type of size_t does not change once set

View file

@ -40,9 +40,6 @@ unsigned DtoCallingConv(LINK l);
// TODO: this one should be removed!!! // TODO: this one should be removed!!!
LLValue* DtoPointedType(LLValue* ptr, LLValue* val); LLValue* DtoPointedType(LLValue* ptr, LLValue* val);
// casts any value to a boolean
LLValue* DtoBoolean(DValue* dval);
// some types // some types
const LLType* DtoSize_t(); const LLType* DtoSize_t();
const LLStructType* DtoInterfaceInfoType(); const LLStructType* DtoInterfaceInfoType();