[llvm-commits] [llvm-gcc-4.0] r41045 - /llvm-gcc-4.0/trunk/gcc/llvm-convert.cpp
Chris Lattner
sabre at nondot.org
Mon Aug 13 09:48:28 PDT 2007
Author: lattner
Date: Mon Aug 13 11:48:27 2007
New Revision: 41045
URL: http://llvm.org/viewvc/llvm-project?rev=41045&view=rev
Log:
When building casts, constant fold with ConstantFoldInstruction instead
of ConstantExpr::getCast. This allows target-data driven constant folding
to happen. In the testcase from PR1602, for example, this results in:
%tmp26 = sub i32 %tmp232425, 1
instead of:
%tmp26 = sub i32 %tmp232425, ptrtoint (i32 (...)** inttoptr (i64 1 to i32 (...)**) to i32)
Modified:
llvm-gcc-4.0/trunk/gcc/llvm-convert.cpp
Modified: llvm-gcc-4.0/trunk/gcc/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.0/trunk/gcc/llvm-convert.cpp?rev=41045&r1=41044&r2=41045&view=diff
==============================================================================
--- llvm-gcc-4.0/trunk/gcc/llvm-convert.cpp (original)
+++ llvm-gcc-4.0/trunk/gcc/llvm-convert.cpp Mon Aug 13 11:48:27 2007
@@ -34,6 +34,7 @@
#include "llvm/InlineAsm.h"
#include "llvm/Instructions.h"
#include "llvm/Module.h"
+#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Target/TargetAsmInfo.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetMachine.h"
@@ -997,16 +998,26 @@
if (V->getType() == Ty)
return V;
+ // If this is a simple constant operand, fold it now. If it is a constant
+ // expr operand, fold it below.
if (Constant *C = dyn_cast<Constant>(V))
- return ConstantExpr::getCast(Instruction::CastOps(opcode), C, Ty);
+ if (!isa<ConstantExpr>(C))
+ return ConstantExpr::getCast(Instruction::CastOps(opcode), C, Ty);
// Handle 'trunc (zext i1 X to T2) to i1' as X, because this occurs all over
// the place.
if (ZExtInst *CI = dyn_cast<ZExtInst>(V))
if (Ty == Type::Int1Ty && CI->getOperand(0)->getType() == Type::Int1Ty)
return CI->getOperand(0);
- return Builder.CreateCast(Instruction::CastOps(opcode), V, Ty,
- V->getName().c_str());
+ Value *Result = Builder.CreateCast(Instruction::CastOps(opcode), V, Ty,
+ V->getNameStart());
+
+ // If this is a constantexpr, fold the instruction with
+ // ConstantFoldInstruction to allow TargetData-driven folding to occur.
+ if (isa<ConstantExpr>(V))
+ Result = ConstantFoldInstruction(cast<Instruction>(Result), &TD);
+
+ return Result;
}
/// CastToAnyType - Cast the specified value to the specified type making no
More information about the llvm-commits
mailing list