diff options
author | Reid Spencer <rspencer@reidspencer.com> | 2006-12-23 06:05:41 +0000 |
---|---|---|
committer | Reid Spencer <rspencer@reidspencer.com> | 2006-12-23 06:05:41 +0000 |
commit | e4d87aa2de6e52952dca73716386db09aad5a8fd (patch) | |
tree | ce8c6e6ddc845de3585020c856118892f4206593 /lib/Transforms | |
parent | add2bd7f5941537a97a41e037ae2277fbeed0b4f (diff) |
For PR950:
This patch removes the SetCC instructions and replaces them with the ICmp
and FCmp instructions. The SetCondInst instruction has been removed and
been replaced with ICmpInst and FCmpInst.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32751 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
21 files changed, 1831 insertions, 1037 deletions
diff --git a/lib/Transforms/ExprTypeConvert.cpp b/lib/Transforms/ExprTypeConvert.cpp index d59fb4db14..5f6b6aebf9 100644 --- a/lib/Transforms/ExprTypeConvert.cpp +++ b/lib/Transforms/ExprTypeConvert.cpp @@ -463,10 +463,13 @@ static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty, return ValueConvertibleToType(I, Ty, CTMap, TD) && ExpressionConvertibleToType(OtherOp, Ty, CTMap, TD); } - case Instruction::SetEQ: - case Instruction::SetNE: { - Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0); - return ExpressionConvertibleToType(OtherOp, Ty, CTMap, TD); + case Instruction::ICmp: { + if (cast<ICmpInst>(I)->getPredicate() == ICmpInst::ICMP_EQ || + cast<ICmpInst>(I)->getPredicate() == ICmpInst::ICMP_NE) { + Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0); + return ExpressionConvertibleToType(OtherOp, Ty, CTMap, TD); + } + return false; } case Instruction::LShr: case Instruction::AShr: @@ -717,9 +720,7 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, } case Instruction::Add: - case Instruction::Sub: - case Instruction::SetEQ: - case Instruction::SetNE: { + case Instruction::Sub: { Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(), Dummy, Dummy, Name); VMC.ExprMap[I] = Res; // Add node to expression eagerly @@ -731,6 +732,19 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, Res->setOperand(OtherIdx, NewOther); break; } + case Instruction::ICmp: { + ICmpInst::Predicate pred = cast<ICmpInst>(I)->getPredicate(); + if (pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_NE) { + Res = new ICmpInst(pred, Dummy, Dummy, Name); + VMC.ExprMap[I] = Res; // Add node to expression eagerly + unsigned OtherIdx = (OldVal == I->getOperand(0)) ? 1 : 0; + Value *OtherOp = I->getOperand(OtherIdx); + Res->setOperand(!OtherIdx, NewVal); + Value *NewOther = ConvertExpressionToType(OtherOp, NewTy, VMC, TD); + Res->setOperand(OtherIdx, NewOther); + } + break; + } case Instruction::Shl: case Instruction::LShr: case Instruction::AShr: diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index 425bcc588b..808a7f805d 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -238,7 +238,7 @@ static bool AnalyzeGlobal(Value *V, GlobalStatus &GS, if (AnalyzeGlobal(I, GS, PHIUsers)) return true; GS.isNotSuitableForSRA = true; GS.HasPHIUser = true; - } else if (isa<SetCondInst>(I)) { + } else if (isa<CmpInst>(I)) { GS.isNotSuitableForSRA = true; } else if (isa<MemCpyInst>(I) || isa<MemMoveInst>(I)) { if (I->getOperand(1) == V) @@ -507,7 +507,7 @@ static bool AllUsesOfValueWillTrapIfNull(Value *V) { if (!AllUsesOfValueWillTrapIfNull(CI)) return false; } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) { if (!AllUsesOfValueWillTrapIfNull(GEPI)) return false; - } else if (isa<SetCondInst>(*UI) && + } else if (isa<ICmpInst>(*UI) && isa<ConstantPointerNull>(UI->getOperand(1))) { // Ignore setcc X, null } else { @@ -720,29 +720,33 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, if (LoadInst *LI = dyn_cast<LoadInst>(GV->use_back())) { while (!LI->use_empty()) { Use &LoadUse = LI->use_begin().getUse(); - if (!isa<SetCondInst>(LoadUse.getUser())) + if (!isa<ICmpInst>(LoadUse.getUser())) LoadUse = RepValue; else { - // Replace the setcc X, 0 with a use of the bool value. - SetCondInst *SCI = cast<SetCondInst>(LoadUse.getUser()); - Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", SCI); + ICmpInst *CI = cast<ICmpInst>(LoadUse.getUser()); + // Replace the cmp X, 0 with a use of the bool value. + Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", CI); InitBoolUsed = true; - switch (SCI->getOpcode()) { - default: assert(0 && "Unknown opcode!"); - case Instruction::SetLT: + switch (CI->getPredicate()) { + default: assert(0 && "Unknown ICmp Predicate!"); + case ICmpInst::ICMP_ULT: + case ICmpInst::ICMP_SLT: LV = ConstantBool::getFalse(); // X < null -> always false break; - case Instruction::SetEQ: - case Instruction::SetLE: - LV = BinaryOperator::createNot(LV, "notinit", SCI); + case ICmpInst::ICMP_ULE: + case ICmpInst::ICMP_SLE: + case ICmpInst::ICMP_EQ: + LV = BinaryOperator::createNot(LV, "notinit", CI); break; - case Instruction::SetNE: - case Instruction::SetGE: - case Instruction::SetGT: + case ICmpInst::ICMP_NE: + case ICmpInst::ICMP_UGE: + case ICmpInst::ICMP_SGE: + case ICmpInst::ICMP_UGT: + case ICmpInst::ICMP_SGT: break; // no change. } - SCI->replaceAllUsesWith(LV); - SCI->eraseFromParent(); + CI->replaceAllUsesWith(LV); + CI->eraseFromParent(); } } LI->eraseFromParent(); @@ -783,7 +787,7 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V, GlobalVariable *GV) { for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI) - if (isa<LoadInst>(*UI) || isa<SetCondInst>(*UI)) { + if (isa<LoadInst>(*UI) || isa<CmpInst>(*UI)) { // Fine, ignore. } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { if (SI->getOperand(0) == V && SI->getOperand(1) != GV) @@ -832,8 +836,8 @@ static bool GlobalLoadUsesSimpleEnoughForHeapSRA(GlobalVariable *GV) { for (Value::use_iterator UI = LI->use_begin(), E = LI->use_end(); UI != E; ++UI) { // Comparison against null is ok. - if (SetCondInst *SCI = dyn_cast<SetCondInst>(*UI)) { - if (!isa<ConstantPointerNull>(SCI->getOperand(1))) + if (ICmpInst *ICI = dyn_cast<ICmpInst>(*UI)) { + if (!isa<ConstantPointerNull>(ICI->getOperand(1))) return false; continue; } @@ -865,7 +869,7 @@ static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Ptr, Instruction *User = Ptr->use_back(); // If this is a comparison against null, handle it. - if (SetCondInst *SCI = dyn_cast<SetCondInst>(User)) { + if (ICmpInst *SCI = dyn_cast<ICmpInst>(User)) { assert(isa<ConstantPointerNull>(SCI->getOperand(1))); // If we have a setcc of the loaded pointer, we can use a setcc of any // field. @@ -877,9 +881,9 @@ static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Ptr, NPtr = InsertedLoadsForPtr.back(); } - Value *New = new SetCondInst(SCI->getOpcode(), NPtr, - Constant::getNullValue(NPtr->getType()), - SCI->getName(), SCI); + Value *New = new ICmpInst(SCI->getPredicate(), NPtr, + Constant::getNullValue(NPtr->getType()), + SCI->getName(), SCI); SCI->replaceAllUsesWith(New); SCI->eraseFromParent(); continue; @@ -959,7 +963,7 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, MallocInst *MI){ // } Value *RunningOr = 0; for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) { - Value *Cond = new SetCondInst(Instruction::SetEQ, FieldMallocs[i], + Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, FieldMallocs[i], Constant::getNullValue(FieldMallocs[i]->getType()), "isnull", MI); if (!RunningOr) @@ -986,9 +990,9 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, MallocInst *MI){ // pointer, because some may be null while others are not. for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) { Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock); - Value *Cmp = new SetCondInst(Instruction::SetNE, GVVal, - Constant::getNullValue(GVVal->getType()), - "tmp", NullPtrBlock); + Value *Cmp = new ICmpInst(ICmpInst::ICMP_NE, GVVal, + Constant::getNullValue(GVVal->getType()), + "tmp", NullPtrBlock); BasicBlock *FreeBlock = new BasicBlock("free_it", OrigBB->getParent()); BasicBlock *NextBlock = new BasicBlock("next", OrigBB->getParent()); new BranchInst(FreeBlock, NextBlock, Cmp, NullPtrBlock); @@ -1710,6 +1714,10 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal, InstResult = ConstantExpr::get(SI->getOpcode(), getVal(Values, SI->getOperand(0)), getVal(Values, SI->getOperand(1))); + } else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) { + InstResult = ConstantExpr::getCompare(CI->getPredicate(), + getVal(Values, CI->getOperand(0)), + getVal(Values, CI->getOperand(1))); } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) { InstResult = ConstantExpr::getCast(CI->getOpcode(), getVal(Values, CI->getOperand(0)), diff --git a/lib/Transforms/IPO/SimplifyLibCalls.cpp b/lib/Transforms/IPO/SimplifyLibCalls.cpp index 7cc1a5bccb..b4e840641b 100644 --- a/lib/Transforms/IPO/SimplifyLibCalls.cpp +++ b/lib/Transforms/IPO/SimplifyLibCalls.cpp @@ -887,8 +887,8 @@ struct StrLenOptimization : public LibCallOptimization { // Does the call to strlen have exactly one use? if (ci->hasOneUse()) - // Is that single use a binary operator? - if (BinaryOperator* bop = dyn_cast<BinaryOperator>(ci->use_back())) + // Is that single use a icmp operator? + if (ICmpInst* bop = dyn_cast<ICmpInst>(ci->use_back())) // Is it compared against a constant integer? if (ConstantInt* CI = dyn_cast<ConstantInt>(bop->getOperand(1))) { @@ -897,15 +897,15 @@ struct StrLenOptimization : public LibCallOptimization { // If its compared against length 0 with == or != if (val == 0 && - (bop->getOpcode() == Instruction::SetEQ || - bop->getOpcode() == Instruction::SetNE)) + (bop->getPredicate() == ICmpInst::ICMP_EQ || + bop->getPredicate() == ICmpInst::ICMP_NE)) { // strlen(x) != 0 -> *x != 0 // strlen(x) == 0 -> *x == 0 LoadInst* load = new LoadInst(str,str->getName()+".first",ci); - BinaryOperator* rbop = BinaryOperator::create(bop->getOpcode(), - load, ConstantInt::get(Type::SByteTy,0), - bop->getName()+".strlen", ci); + ICmpInst* rbop = new ICmpInst(bop->getPredicate(), load, + ConstantInt::get(Type::SByteTy,0), + bop->getName()+".strlen", ci); bop->replaceAllUsesWith(rbop); bop->eraseFromParent(); ci->eraseFromParent(); @@ -933,10 +933,11 @@ static bool IsOnlyUsedInEqualsZeroComparison(Instruction *I) { for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI) { Instruction *User = cast<Instruction>(*UI); - if (User->getOpcode() == Instruction::SetNE || - User->getOpcode() == Instruction::SetEQ) { - if (isa<Constant>(User->getOperand(1)) && - cast<Constant>(User->getOperand(1))->isNullValue()) + if (ICmpInst *IC = dyn_cast<ICmpInst>(User)) { + if ((IC->getPredicate() == ICmpInst::ICMP_NE || + IC->getPredicate() == ICmpInst::ICMP_EQ) && + isa<Constant>(IC->getOperand(1)) && + cast<Constant>(IC->getOperand(1))->isNullValue()) continue; } else if (CastInst *CI = dyn_cast<CastInst>(User)) if (CI->getType() == Type::BoolTy) @@ -1739,7 +1740,7 @@ public: BinaryOperator* sub_inst = BinaryOperator::createSub(cast, ConstantInt::get(Type::UIntTy,0x30), ci->getOperand(1)->getName()+".sub",ci); - SetCondInst* setcond_inst = new SetCondInst(Instruction::SetLE,sub_inst, + ICmpInst* setcond_inst = new ICmpInst(ICmpInst::ICMP_ULE,sub_inst, ConstantInt::get(Type::UIntTy,9), ci->getOperand(1)->getName()+".cmp",ci); CastInst* c2 = new ZExtInst(setcond_inst, Type::IntTy, @@ -1764,12 +1765,9 @@ public: virtual bool OptimizeCall(CallInst *CI, SimplifyLibCalls &SLC) { // isascii(c) -> (unsigned)c < 128 Value *V = CI->getOperand(1); - if (V->getType()->isSigned()) - V = new BitCastInst(V, V->getType()->getUnsignedVersion(), V->getName(), - CI); - Value *Cmp = BinaryOperator::createSetLT(V, ConstantInt::get(V->getType(), - 128), - V->getName()+".isascii", CI); + Value *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, V, + ConstantInt::get(V->getType(), 128), + V->getName()+".isascii", CI); if (Cmp->getType() != CI->getType()) Cmp = new BitCastInst(Cmp, CI->getType(), Cmp->getName(), CI); CI->replaceAllUsesWith(Cmp); @@ -1872,9 +1870,9 @@ public: "tmp", TheCall); V2 = BinaryOperator::createAdd(V2, ConstantInt::get(Type::IntTy, 1), "tmp", TheCall); - Value *Cond = - BinaryOperator::createSetEQ(V, Constant::getNullValue(V->getType()), - "tmp", TheCall); + Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, V, + Constant::getNullValue(V->getType()), "tmp", + TheCall); V2 = new SelectInst(Cond, ConstantInt::get(Type::IntTy, 0), V2, TheCall->getName(), TheCall); TheCall->replaceAllUsesWith(V2); diff --git a/lib/Transforms/Instrumentation/RSProfiling.cpp b/lib/Transforms/Instrumentation/RSProfiling.cpp index ab570e4591..b18d66befb 100644 --- a/lib/Transforms/Instrumentation/RSProfiling.cpp +++ b/lib/Transforms/Instrumentation/RSProfiling.cpp @@ -197,9 +197,9 @@ void GlobalRandomCounter::ProcessChoicePoint(BasicBlock* bb) { //decrement counter LoadInst* l = new LoadInst(Counter, "counter", t); - SetCondInst* s = new SetCondInst(Instruction::SetEQ, l, - ConstantInt::get(T, 0), - "countercc", t); + ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0), + "countercc", t); + Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1), "counternew", t); new StoreInst(nv, Counter, t); @@ -270,9 +270,9 @@ void GlobalRandomCounterOpt::ProcessChoicePoint(BasicBlock* bb) { //decrement counter LoadInst* l = new LoadInst(AI, "counter", t); - SetCondInst* s = new SetCondInst(Instruction::SetEQ, l, - ConstantInt::get(T, 0), - "countercc", t); + ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0), + "countercc", t); + Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1), "counternew", t); new StoreInst(nv, AI, t); @@ -305,9 +305,10 @@ void CycleCounter::ProcessChoicePoint(BasicBlock* bb) { BinaryOperator::createAnd(c, ConstantInt::get(Type::ULongTy, rm), "mrdcc", t); - SetCondInst* s = new SetCondInst(Instruction::SetEQ, b, - ConstantInt::get(Type::ULongTy, 0), - "mrdccc", t); + ICmpInst *s = new ICmpInst(ICmpInst::ICMP_EQ, b, + ConstantInt::get(Type::ULongTy, 0), + "mrdccc", t); + t->setCondition(s); } diff --git a/lib/Transforms/LevelRaise.cpp b/lib/Transforms/LevelRaise.cpp index 9930d1bdf8..f0d39155a3 100644 --- a/lib/Transforms/LevelRaise.cpp +++ b/lib/Transforms/LevelRaise.cpp @@ -393,9 +393,6 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { return false; } - - - bool RPR::DoRaisePass(Function &F) { bool Changed = false; for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) diff --git a/lib/Transforms/Scalar/CorrelatedExprs.cpp b/lib/Transforms/Scalar/CorrelatedExprs.cpp index 7ab999f963..8d369f2870 100644 --- a/lib/Transforms/Scalar/CorrelatedExprs.cpp +++ b/lib/Transforms/Scalar/CorrelatedExprs.cpp @@ -45,36 +45,36 @@ #include <algorithm> using namespace llvm; -STATISTIC(NumSetCCRemoved, "Number of setcc instruction eliminated"); +STATISTIC(NumCmpRemoved, "Number of cmp instruction eliminated"); STATISTIC(NumOperandsCann, "Number of operands canonicalized"); STATISTIC(BranchRevectors, "Number of branches revectored"); namespace { class ValueInfo; class Relation { - Value *Val; // Relation to what value? - Instruction::BinaryOps Rel; // SetCC relation, or Add if no information + Value *Val; // Relation to what value? + unsigned Rel; // SetCC or ICmp relation, or Add if no information public: Relation(Value *V) : Val(V), Rel(Instruction::Add) {} bool operator<(const Relation &R) const { return Val < R.Val; } Value *getValue() const { return Val; } - Instruction::BinaryOps getRelation() const { return Rel; } + unsigned getRelation() const { return Rel; } // contradicts - Return true if the relationship specified by the operand // contradicts already known information. // - bool contradicts(Instruction::BinaryOps Rel, const ValueInfo &VI) const; + bool contradicts(unsigned Rel, const ValueInfo &VI) const; // incorporate - Incorporate information in the argument into this relation // entry. This assumes that the information doesn't contradict itself. If // any new information is gained, true is returned, otherwise false is // returned to indicate that nothing was updated. // - bool incorporate(Instruction::BinaryOps Rel, ValueInfo &VI); + bool incorporate(unsigned Rel, ValueInfo &VI); // KnownResult - Whether or not this condition determines the result of a - // setcc in the program. False & True are intentionally 0 & 1 so we can - // convert to bool by casting after checking for unknown. + // setcc or icmp in the program. False & True are intentionally 0 & 1 + // so we can convert to bool by casting after checking for unknown. // enum KnownResult { KnownFalse = 0, KnownTrue = 1, Unknown = 2 }; @@ -82,7 +82,7 @@ namespace { // the specified relationship is true or false, return that. If we cannot // determine the result required, return Unknown. // - KnownResult getImpliedResult(Instruction::BinaryOps Rel) const; + KnownResult getImpliedResult(unsigned Rel) const; // print - Output this relation to the specified stream void print(std::ostream &OS) const; @@ -269,19 +269,16 @@ namespace { void PropagateBranchInfo(BranchInst *BI); void PropagateSwitchInfo(SwitchInst *SI); void PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI); - void PropagateRelation(Instruction::BinaryOps Opcode, Value *Op0, + void PropagateRelation(unsigned Opcode, Value *Op0, Value *Op1, RegionInfo &RI); void UpdateUsersOfValue(Value *V, RegionInfo &RI); void IncorporateInstruction(Instruction *Inst, RegionInfo &RI); void ComputeReplacements(RegionInfo &RI); - - // getSetCCResult - Given a setcc instruction, determine if the result is + // getCmpResult - Given a icmp instruction, determine if the result is // determined by facts we already know about the region under analysis. - // Return KnownTrue, KnownFalse, or Unknown based on what we can determine. - // - Relation::KnownResult getSetCCResult(SetCondInst *SC, const RegionInfo &RI); - + // Return KnownTrue, KnownFalse, or UnKnown based on what we can determine. + Relation::KnownResult getCmpResult(CmpInst *ICI, const RegionInfo &RI); bool SimplifyBasicBlock(BasicBlock &BB, const RegionInfo &RI); bool SimplifyInstruction(Instruction *Inst, const RegionInfo &RI); @@ -448,12 +445,12 @@ bool CEE::ForwardCorrelatedEdgeDestination(TerminatorInst *TI, unsigned SuccNo, return false; // We can only forward the branch over the block if the block ends with a - // setcc we can determine the outcome for. + // cmp we can determine the outcome for. // // FIXME: we can make this more generic. Code below already handles more // generic case. - SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition()); - if (SCI == 0) return false; + if (!isa<CmpInst>(BI->getCondition())) + return false; // Make a new RegionInfo structure so that we can simulate the effect of the // PHI nodes in the block we are skipping over... @@ -472,10 +469,10 @@ bool CEE::ForwardCorrelatedEdgeDestination(TerminatorInst *TI, unsigned SuccNo, int OpNum = PN->getBasicBlockIndex(BB); assert(OpNum != -1 && "PHI doesn't have incoming edge for predecessor!?"); PropagateEquality(PN, PN->getIncomingValue(OpNum), NewRI); - } else if (SetCondInst *SCI = dyn_cast<SetCondInst>(I)) { - Relation::KnownResult Res = getSetCCResult(SCI, NewRI); + } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) { + Relation::KnownResult Res = getCmpResult(CI, NewRI); if (Res == Relation::Unknown) return false; - PropagateEquality(SCI, ConstantBool::get(Res), NewRI); + PropagateEquality(CI, ConstantBool::get(Res), NewRI); } else { assert(isa<BranchInst>(*I) && "Unexpected instruction type!"); } @@ -827,7 +824,8 @@ void CEE::PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI) { Relation &KnownRelation = VI.getRelation(Op1); // If we already know they're equal, don't reprocess... - if (KnownRelation.getRelation() == Instruction::SetEQ) + if (KnownRelation.getRelation() == FCmpInst::FCMP_OEQ || + KnownRelation.getRelation() == ICmpInst::ICMP_EQ) return; // If this is boolean, check to see if one of the operands is a constant. If @@ -863,32 +861,55 @@ void CEE::PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI) { PropagateEquality(BinaryOperator::getNotArgument(BOp), ConstantBool::get(!CB->getValue()), RI); - // If we know the value of a SetCC instruction, propagate the information + // If we know the value of a FCmp instruction, propagate the information // about the relation into this region as well. // - if (SetCondInst *SCI = dyn_cast<SetCondInst>(Inst)) { + if (FCmpInst *FCI = dyn_cast<FCmpInst>(Inst)) { if (CB->getValue()) { // If we know the condition is true... // Propagate info about the LHS to the RHS & RHS to LHS - PropagateRelation(SCI->getOpcode(), SCI->getOperand(0), - SCI->getOperand(1), RI); - PropagateRelation(SCI->getSwappedCondition(), - SCI->getOperand(1), SCI->getOperand(0), RI); + PropagateRelation(FCI->getPredicate(), FCI->getOperand(0), + FCI->getOperand(1), RI); + PropagateRelation(FCI->getSwappedPredicate(), + FCI->getOperand(1), FCI->getOperand(0), RI); } else { // If we know the condition is false... // We know the opposite of the condition is true... - Instruction::BinaryOps C = SCI->getInverseCondition(); + FCmpInst::Predicate C = FCI->getInversePredicate(); - PropagateRelation(C, SCI->getOperand(0), SCI->getOperand(1), RI); - PropagateRelation(SetCondInst::getSwappedCondition(C), - SCI->getOperand(1), SCI->getOperand(0), RI); + PropagateRelation(C, FCI->getOperand(0), FCI->getOperand(1), RI); + PropagateRelation(FCmpInst::getSwappedPredicate(C), + FCI->getOperand(1), FCI->getOperand(0), RI); + } + } + + // If we know the value of a ICmp instruction, propagate the information + // about the relation into this region as well. + // + if (ICmpInst *ICI = dyn_cast<ICmpInst>(Inst)) { + if (CB->getValue()) { // If we know the condition is true... + // Propagate info about the LHS to the RHS & RHS to LHS + PropagateRelation(ICI->getPredicate(), ICI->getOperand(0), + ICI->getOperand(1), RI); + PropagateRelation(ICI->getSwappedPredicate(), ICI->getOperand(1), + ICI->getOperand(1), RI); + + } else { // If we know the condition is false ... + // We know the opposite of the condition is true... + ICmpInst::Predicate C = ICI->getInversePredicate(); + + PropagateRelation(C, ICI->getOperand(0), ICI->getOperand(1), RI); + PropagateRelation(ICmpInst::getSwappedPredicate(C), + ICI->getOperand(1), ICI->getOperand(0), RI); } } } } // Propagate information about Op0 to Op1 & visa versa - PropagateRelation(Instruction::SetEQ, Op0, Op1, RI); - PropagateRelation(Instruction::SetEQ, Op1, Op0, RI); + PropagateRelation(ICmpInst::ICMP_EQ, Op0, Op1, RI); + PropagateRelation(ICmpInst::ICMP_EQ, Op1, Op0, RI); + PropagateRelation(FCmpInst::FCMP_OEQ, Op0, Op1, RI); + PropagateRelation(FCmpInst::FCMP_OEQ, Op1, Op0, RI); } @@ -896,7 +917,7 @@ void CEE::PropagateEquality(Value *Op0, Value *Op1, RegionInfo &RI) { // blocks in the specified region. Propagate the information about Op0 and // anything derived from it into this region. // -void CEE::PropagateRelation(Instruction::BinaryOps Opcode, Value *Op0, +void CEE::PropagateRelation(unsigned Opcode, Value *Op0, Value *Op1, RegionInfo &RI) { assert(Op0->getType() == Op1->getType() && "Equal types expected!"); @@ -921,7 +942,10 @@ void CEE::PropagateRelation(Instruction::BinaryOps Opcode, Value *Op0, if (Op1R.contradicts(Opcode, VI)) { Op1R.contradicts(Opcode, VI); cerr << "Contradiction found for opcode: " - << Instruction::getOpcodeName(Opcode) << "\n"; + << ((isa<ICmpInst>(Op0)||isa<ICmpInst>(Op1)) ? + Instruction::getOpcodeName(Instruction::ICmp) : + Instruction::getOpcodeName(Opcode)) + << "\n"; Op1R.print(*cerr.stream()); return; } @@ -964,11 +988,11 @@ void CEE::UpdateUsersOfValue(Value *V, RegionInfo &RI) { // value produced by this instruction // void CEE::IncorporateInstruction(Instruction *Inst, RegionInfo &RI) { - if (SetCondInst *SCI = dyn_cast<SetCondInst>(Inst)) { + if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) { // See if we can figure out a result for this instruction... - Relation::KnownResult Result = getSetCCResult(SCI, RI); + Relation::KnownResult Result = getCmpResult(CI, RI); if (Result != Relation::Unknown) { - PropagateEquality(SCI, ConstantBool::get(Result != 0), RI); + PropagateEquality(CI, ConstantBool::get(Result != 0), RI); } } } @@ -1002,7 +1026,14 @@ void CEE::ComputeReplacements(RegionInfo &RI) { // Loop over the relationships known about Op0. const std::vector<Relation> &Relationships = VI.getRelationships(); for (unsigned i = 0, e = Relationships.size(); i != e; ++i) - if (Relationships[i].getRelation() == Instruction::SetEQ) { + if (Relationships[i].getRelation() == FCmpInst::FCMP_OEQ) { + unsigned R = getRank(Relationships[i].getValue()); + if (R < MinRank) { + MinRank = R; + Replacement = Relationships[i].getValue(); + } + } + else if (Relationships[i].getRelation() == ICmpInst::ICMP_EQ) { unsigned R = getRank(Relationships[i].getValue()); if (R < MinRank) { MinRank = R; @@ -1028,16 +1059,17 @@ bool CEE::SimplifyBasicBlock(BasicBlock &BB, const RegionInfo &RI) { // Convert instruction arguments to canonical forms... Changed |= SimplifyInstruction(Inst, RI); - if (SetCondInst *SCI = dyn_cast<SetCondInst>(Inst)) { + if (CmpInst *CI = dyn_cast<CmpInst>(Inst)) { // Try to simplify a setcc instruction based on inherited information - Relation::KnownResult Result = getSetCCResult(SCI, RI); + Relation::KnownResult Result = getCmpResult(CI, RI); if (Result != Relation::Unknown) { - DOUT << "Replacing setcc with " << Result << " constant: " << *SCI; + DEBUG(cerr << "Replacing icmp with " << Result + << " constant: " << *CI); - SCI->replaceAllUsesWith(ConstantBool::get((bool)Result)); + CI->replaceAllUsesWith(ConstantBool::get((bool)Result)); // The instruction is now dead, remove it from the program. - SCI->getParent()->getInstList().erase(SCI); - ++NumSetCCRemoved; + CI->getParent()->getInstList().erase(CI); + ++NumCmpRemoved; Changed = true; } } @@ -1069,33 +1101,35 @@ bool CEE::SimplifyInstruction(Instruction *I, const RegionInfo &RI) { return Changed; } - -// getSetCCResult - Try to simplify a setcc instruction based on information -// inherited from a dominating setcc instruction. V is one of the operands to -// the setcc instruction, and VI is the set of information known about it. We +// getCmpResult - Try to simplify a cmp instruction based on information +// inherited from a dominating icmp instruction. V is one of the operands to +// the icmp instruction, and VI is the set of information known about it. We // take two cases into consideration here. If the comparison is against a // constant value, we can use the constant range to see if the comparison is // possible to succeed. If it is not a comparison against a constant, we check // to see if there is a known relationship between the two values. If so, we // may be able to eliminate the check. // -Relation::KnownResult CEE::getSetCCResult(SetCondInst *SCI, - const RegionInfo &RI) { - Value *Op0 = SCI->getOperand(0), *Op1 = SCI->getOperand(1); - Instruction::BinaryOps Opcode = SCI->getOpcode(); +Relation::KnownResult CEE::getCmpResult(CmpInst *CI, + const RegionInfo &RI) { + Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1); + unsigned short predicate = CI->getPredicate(); if (isa<Constant>(Op0)) { if (isa<Constant>(Op1)) { - if (Constant *Result = ConstantFoldInstruction(SCI)) { - // Wow, this is easy, directly eliminate the SetCondInst. - DOUT << "Replacing setcc with constant fold: " << *SCI; + if (Constant *Result = ConstantFoldInstruction(CI)) { + // Wow, this is easy, directly eliminate the ICmpInst. + DEBUG(cerr << "Replacing cmp with constant fold: " << *CI); return cast<ConstantBool>(Result)->getValue() ? Relation::KnownTrue : Relation::KnownFalse; } } else { // We want to swap this instruction so that operand #0 is the constant. std::swap(Op0, Op1); - Opcode = SCI->getSwappedCondition(); + if (isa<ICmpInst>(CI)) + predicate = cast<ICmpInst>(CI)->getSwappedPredicate(); + else + predicate = cast<FCmpInst>(CI)->getSwappedPredicate(); } } @@ -1107,12 +1141,13 @@ R |