From 6efc9a3324c7fcaae9b4844a374c37ea4a47d5eb Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Sat, 22 Nov 2008 18:35:52 +0100 Subject: [PATCH] Get rid of DtoBoolean - use DtoCast(... Type::tbool) instead. Fixed some casts to bool that were using truncation. --- gen/arrays.cpp | 6 ++++ gen/classes.cpp | 9 ++++- gen/complex.cpp | 3 ++ gen/llvmhelpers.cpp | 85 +++++++++++---------------------------------- gen/llvmhelpers.h | 3 -- gen/statements.cpp | 8 ++--- gen/toir.cpp | 16 ++++----- 7 files changed, 50 insertions(+), 80 deletions(-) diff --git a/gen/arrays.cpp b/gen/arrays.cpp index 7e8009b7..60912c6a 100644 --- a/gen/arrays.cpp +++ b/gen/arrays.cpp @@ -945,6 +945,12 @@ DValue* DtoCastArray(Loc& loc, DValue* u, Type* to) Logger::cout() << "to sarray" << '\n'; assert(0); } + else if (totype->ty == Tbool) { + // return (arr.ptr !is null) + LLValue* ptr = DtoArrayPtr(u); + LLConstant* nul = getNullPtr(ptr->getType()); + rval = gIR->ir->CreateICmpNE(ptr, nul, "tmp"); + } else { assert(0); } diff --git a/gen/classes.cpp b/gen/classes.cpp index df6d94ec..7884efba 100644 --- a/gen/classes.cpp +++ b/gen/classes.cpp @@ -924,10 +924,17 @@ DValue* DtoCastClass(DValue* val, Type* _to) Type* to = _to->toBasetype(); if (to->ty == Tpointer) { + Logger::println("to pointer"); const LLType* tolltype = DtoType(_to); LLValue* rval = DtoBitCast(val->getRVal(), tolltype); return new DImValue(_to, rval); } + else if (to->ty == Tbool) { + Logger::println("to bool"); + LLValue* llval = val->getRVal(); + LLValue* zero = LLConstant::getNullValue(llval->getType()); + return new DImValue(_to, gIR->ir->CreateICmpNE(llval, zero, "tmp")); + } assert(to->ty == Tclass); TypeClass* tc = (TypeClass*)to; @@ -947,7 +954,7 @@ DValue* DtoCastClass(DValue* val, Type* _to) } } else { - Logger::println("to object"); + Logger::println("to class"); int poffset; if (fc->sym->isInterfaceDeclaration()) { Logger::println("interface cast"); diff --git a/gen/complex.cpp b/gen/complex.cpp index ea41d289..b1bca2bd 100644 --- a/gen/complex.cpp +++ b/gen/complex.cpp @@ -454,6 +454,9 @@ DValue* DtoCastComplex(Loc& loc, DValue* val, Type* _to) DImValue* re = new DImValue(to, repart); return DtoCastFloat(loc, re, to); } + else if (to->ty == Tbool) { + return new DImValue(_to, DtoComplexEquals(loc, TOKnotequal, val, DtoNullValue(vty))); + } else assert(0); } diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index 7bb8c899..9b3b722b 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -569,7 +569,11 @@ DValue* DtoCastInt(Loc& loc, DValue* val, Type* _to) return new DImValue(_to, rval); } - if (to->isintegral()) { + if (to->ty == Tbool) { + LLValue* zero = LLConstantInt::get(rval->getType(), 0, false); + rval = gIR->ir->CreateICmpNE(rval, zero, "tmp"); + } + else if (to->isintegral()) { if (fromsz < tosz || from->ty == Tbool) { if (Logger::enabled()) Logger::cout() << "cast to: " << *tolltype << '\n'; @@ -579,7 +583,7 @@ DValue* DtoCastInt(Loc& loc, DValue* val, Type* _to) rval = new llvm::SExtInst(rval, tolltype, "tmp", gIR->scopebb()); } } - else if (fromsz > tosz || to->ty == Tbool) { + else if (fromsz > tosz) { rval = new llvm::TruncInst(rval, tolltype, "tmp", gIR->scopebb()); } else { @@ -626,6 +630,11 @@ DValue* DtoCastPtr(Loc& loc, DValue* val, Type* to) Logger::cout() << "src: " << *src << "to type: " << *tolltype << '\n'; rval = DtoBitCast(src, tolltype); } + else if (totype->ty == Tbool) { + LLValue* src = val->getRVal(); + LLValue* zero = LLConstant::getNullValue(src->getType()); + rval = gIR->ir->CreateICmpNE(src, zero, "tmp"); + } else if (totype->isintegral()) { rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); } @@ -653,7 +662,12 @@ DValue* DtoCastFloat(Loc& loc, DValue* val, Type* to) LLValue* rval; - if (totype->iscomplex()) { + if (totype->ty == Tbool) { + rval = val->getRVal(); + LLValue* zero = LLConstant::getNullValue(rval->getType()); + rval = gIR->ir->CreateFCmpONE(rval, zero, "tmp"); + } + else if (totype->iscomplex()) { return DtoComplex(loc, to, val); } else if (totype->isfloating()) { @@ -694,6 +708,10 @@ DValue* DtoCastDelegate(Loc& loc, DValue* val, Type* to) { return DtoPaintType(loc, val, to); } + else if (to->toBasetype()->ty == Tbool) + { + return new DImValue(to, DtoDelegateEquals(TOKnotequal, val->getRVal(), NULL)); + } else { error(loc, "invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars()); @@ -1614,67 +1632,6 @@ LLConstant* DtoTypeInfoOf(Type* type, bool base) ////////////////////////////////////////////////////////////////////////////////////////// -LLValue* DtoBoolean(Loc& loc, 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()) - { - return DtoComplexEquals(loc, TOKnotequal, dval, DtoNullValue(dtype)); - } - // 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()); - if (Logger::enabled()) - { - Logger::cout() << "val: " << *val << '\n'; - Logger::cout() << "zero: " << *zero << '\n'; - } - return gIR->ir->CreateICmpNE(val, zero, "tmp"); - } - // dynamic array - else if (ty == Tarray) - { - // return (arr.ptr !is null) - LLValue* ptr = DtoArrayPtr(dval); - LLConstant* nul = getNullPtr(ptr->getType()); - return gIR->ir->CreateICmpNE(ptr, nul, "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; -} - -////////////////////////////////////////////////////////////////////////////////////////// - void DtoOverloadedIntrinsicName(TemplateInstance* ti, TemplateDeclaration* td, std::string& name) { Logger::println("DtoOverloadedIntrinsicName"); diff --git a/gen/llvmhelpers.h b/gen/llvmhelpers.h index fa1406d4..3bd68fb6 100644 --- a/gen/llvmhelpers.h +++ b/gen/llvmhelpers.h @@ -108,9 +108,6 @@ DValue* DtoBinRem(Type* resulttype, DValue* lhs, DValue* rhs); // target stuff void findDefaultTarget(); -/// Converts any value to a boolean (llvm i1) -LLValue* DtoBoolean(Loc& loc, DValue* dval); - /// get the default initializer of the type LLConstant* DtoDefaultInit(Type* t); diff --git a/gen/statements.cpp b/gen/statements.cpp index 6e8f878f..40caf645 100644 --- a/gen/statements.cpp +++ b/gen/statements.cpp @@ -168,7 +168,7 @@ void IfStatement::toIR(IRState* p) if (cond_val->getType() != LLType::Int1Ty) { if (Logger::enabled()) Logger::cout() << "if conditional: " << *cond_val << '\n'; - cond_val = DtoBoolean(loc, cond_e); + cond_val = DtoCast(loc, cond_e, Type::tbool)->getRVal(); } LLValue* ifgoback = llvm::BranchInst::Create(ifbb, elsebb, cond_val, gIR->scopebb()); @@ -256,7 +256,7 @@ void WhileStatement::toIR(IRState* p) // create the condition DValue* cond_e = condition->toElem(p); - LLValue* cond_val = DtoBoolean(loc, cond_e); + LLValue* cond_val = DtoCast(loc, cond_e, Type::tbool)->getRVal(); delete cond_e; // conditional branch @@ -312,7 +312,7 @@ void DoStatement::toIR(IRState* p) // create the condition DValue* cond_e = condition->toElem(p); - LLValue* cond_val = DtoBoolean(loc, cond_e); + LLValue* cond_val = DtoCast(loc, cond_e, Type::tbool)->getRVal(); delete cond_e; // conditional branch @@ -357,7 +357,7 @@ void ForStatement::toIR(IRState* p) if (condition) { DValue* cond_e = condition->toElem(p); - cond_val = DtoBoolean(loc, cond_e); + cond_val = DtoCast(loc, cond_e, Type::tbool)->getRVal(); delete cond_e; } else diff --git a/gen/toir.cpp b/gen/toir.cpp index 4038ab49..a1ea56bb 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1649,7 +1649,7 @@ DValue* AssertExp::toElem(IRState* p) llvm::BasicBlock* endbb = llvm::BasicBlock::Create("noassert", p->topfunc(), oldend); // test condition - LLValue* condval = DtoBoolean(loc, cond); + LLValue* condval = DtoCast(loc, cond, Type::tbool)->getRVal(); // branch llvm::BranchInst::Create(endbb, assertbb, condval, p->scopebb()); @@ -1675,7 +1675,7 @@ DValue* NotExp::toElem(IRState* p) DValue* u = e1->toElem(p); - LLValue* b = DtoBoolean(loc, u); + LLValue* b = DtoCast(loc, u, Type::tbool)->getRVal(); LLConstant* zero = DtoConstBool(false); b = p->ir->CreateICmpEQ(b,zero); @@ -1701,14 +1701,14 @@ DValue* AndAndExp::toElem(IRState* p) llvm::BasicBlock* andand = llvm::BasicBlock::Create("andand", gIR->topfunc(), oldend); llvm::BasicBlock* andandend = llvm::BasicBlock::Create("andandend", gIR->topfunc(), oldend); - LLValue* ubool = DtoBoolean(loc, u); + LLValue* ubool = DtoCast(loc, u, Type::tbool)->getRVal(); DtoStore(ubool,resval); llvm::BranchInst::Create(andand,andandend,ubool,p->scopebb()); p->scope() = IRScope(andand, andandend); DValue* v = e2->toElem(p); - LLValue* vbool = DtoBoolean(loc, v); + LLValue* vbool = DtoCast(loc, v, Type::tbool)->getRVal(); LLValue* uandvbool = llvm::BinaryOperator::Create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb()); DtoStore(uandvbool,resval); llvm::BranchInst::Create(andandend,p->scopebb()); @@ -1737,14 +1737,14 @@ DValue* OrOrExp::toElem(IRState* p) llvm::BasicBlock* oror = llvm::BasicBlock::Create("oror", gIR->topfunc(), oldend); llvm::BasicBlock* ororend = llvm::BasicBlock::Create("ororend", gIR->topfunc(), oldend); - LLValue* ubool = DtoBoolean(loc, u); + LLValue* ubool = DtoCast(loc, u, Type::tbool)->getRVal(); DtoStore(ubool,resval); llvm::BranchInst::Create(ororend,oror,ubool,p->scopebb()); p->scope() = IRScope(oror, ororend); DValue* v = e2->toElem(p); - LLValue* vbool = DtoBoolean(loc, v); + LLValue* vbool = DtoCast(loc, v, Type::tbool)->getRVal(); DtoStore(vbool,resval); llvm::BranchInst::Create(ororend,p->scopebb()); @@ -1999,7 +1999,7 @@ DValue* CondExp::toElem(IRState* p) llvm::BasicBlock* condend = llvm::BasicBlock::Create("condend", gIR->topfunc(), oldend); DValue* c = econd->toElem(p); - LLValue* cond_val = DtoBoolean(loc, c); + LLValue* cond_val = DtoCast(loc, c, Type::tbool)->getRVal(); llvm::BranchInst::Create(condtrue,condfalse,cond_val,p->scopebb()); p->scope() = IRScope(condtrue, condfalse); @@ -2420,7 +2420,7 @@ DValue* GEPExp::toElem(IRState* p) DValue* BoolExp::toElem(IRState* p) { - return new DImValue(type, DtoBoolean(loc, e1->toElem(p))); + return new DImValue(type, DtoCast(loc, e1->toElem(p), Type::tbool)->getRVal()); } //////////////////////////////////////////////////////////////////////////////////////////