[llvm-commits] CVS: llvm/lib/Transforms/IPO/GlobalOpt.cpp SimplifyLibCalls.cpp

Reid Spencer reid at x10sys.com
Fri Dec 22 22:06:59 PST 2006



Changes in directory llvm/lib/Transforms/IPO:

GlobalOpt.cpp updated: 1.80 -> 1.81
SimplifyLibCalls.cpp updated: 1.79 -> 1.80
---
Log message:

For PR950: http://llvm.org/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.


---
Diffs of the changes:  (+55 -49)

 GlobalOpt.cpp        |   64 ++++++++++++++++++++++++++++-----------------------
 SimplifyLibCalls.cpp |   40 +++++++++++++++----------------
 2 files changed, 55 insertions(+), 49 deletions(-)


Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp
diff -u llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.80 llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.81
--- llvm/lib/Transforms/IPO/GlobalOpt.cpp:1.80	Tue Dec 19 16:09:18 2006
+++ llvm/lib/Transforms/IPO/GlobalOpt.cpp	Sat Dec 23 00:05:41 2006
@@ -238,7 +238,7 @@
           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 @@
       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 @@
     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 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 @@
       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 @@
     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 @@
         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 @@
   //    }
   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 @@
   // 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 @@
       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)),


Index: llvm/lib/Transforms/IPO/SimplifyLibCalls.cpp
diff -u llvm/lib/Transforms/IPO/SimplifyLibCalls.cpp:1.79 llvm/lib/Transforms/IPO/SimplifyLibCalls.cpp:1.80
--- llvm/lib/Transforms/IPO/SimplifyLibCalls.cpp:1.79	Thu Dec 21 01:15:54 2006
+++ llvm/lib/Transforms/IPO/SimplifyLibCalls.cpp	Sat Dec 23 00:05:41 2006
@@ -887,8 +887,8 @@
 
     // 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 @@
 
           // 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 @@
   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 @@
     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 @@
   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 @@
                                      "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);






More information about the llvm-commits mailing list