[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp

Chris Lattner lattner at cs.uiuc.edu
Sat Oct 16 12:45:11 PDT 2004



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.268 -> 1.269
---
Log message:

Implement InstCombine/getelementptr.ll:test9, which is the source of many
ugly and giant constnat exprs in some programs.


---
Diffs of the changes:  (+18 -0)

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.268 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.269
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.268	Sat Oct 16 13:11:37 2004
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Sat Oct 16 14:44:59 2004
@@ -4018,6 +4018,24 @@
 
     // Instruction isn't dead, see if we can constant propagate it...
     if (Constant *C = ConstantFoldInstruction(I)) {
+      if (isa<GetElementPtrInst>(I) &&
+          cast<Constant>(I->getOperand(0))->isNullValue() &&
+          !isa<ConstantPointerNull>(C)) {
+        // If this is a constant expr gep that is effectively computing an
+        // "offsetof", fold it into 'cast int X to T*' instead of 'gep 0, 0, 12'
+        bool isFoldableGEP = true;
+        for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i)
+          if (!isa<ConstantInt>(I->getOperand(i)))
+            isFoldableGEP = false;
+        if (isFoldableGEP) {
+          uint64_t Offset = TD->getIndexedOffset(I->getOperand(0)->getType(),
+                             std::vector<Value*>(I->op_begin()+1, I->op_end()));
+          C = ConstantUInt::get(Type::ULongTy, Offset);
+          C = ConstantUInt::getCast(C, TD->getIntPtrType());
+          C = ConstantExpr::getCast(C, I->getType());
+        }
+      }
+
       // Add operands to the worklist...
       AddUsesToWorkList(*I);
       ReplaceInstUsesWith(*I, C);






More information about the llvm-commits mailing list