diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index 4c4b0f951d5..c9b8f60e777 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -3234,6 +3234,15 @@ bool SelectionDAGISel::runOnFunction(Function &Fn) { if (GetElementPtrInst *GEPI = dyn_cast(I)) { MadeChange |= OptimizeGEPExpression(GEPI, TLI.getTargetData()); } else if (CastInst *CI = dyn_cast(I)) { + // If the source of the cast is a constant, then this should have + // already been constant folded. The only reason NOT to constant fold + // it is if something (e.g. LSR) was careful to place the constant + // evaluation in a block other than then one that uses it (e.g. to hoist + // the address of globals out of a loop). If this is the case, we don't + // want to forward-subst the cast. + if (isa(CI->getOperand(0))) + continue; + // If this is a noop copy, sink it into user blocks to reduce the number // of virtual registers that must be created and coallesced. MVT::ValueType SrcVT = TLI.getValueType(CI->getOperand(0)->getType());