llvm-6502/lib/Transforms/Scalar
Chris Lattner 4f1134e51a Fix a HUGE pessimization on X86. The indvars pass was taking this
(familiar) function:

int _strlen(const char *str) {
    int len = 0;
    while (*str++) len++;
    return len;
}

And transforming it to use a ulong induction variable, because the type of
the pointer index was left as a constant long.  This is obviously very bad.

The fix is to shrink long constants in getelementptr instructions to intptr_t,
making the indvars pass insert a uint induction variable, which is much more
efficient.

Here's the before code for this function:

int %_strlen(sbyte* %str) {
entry:
        %tmp.13 = load sbyte* %str              ; <sbyte> [#uses=1]
        %tmp.24 = seteq sbyte %tmp.13, 0                ; <bool> [#uses=1]
        br bool %tmp.24, label %loopexit, label %no_exit

no_exit:                ; preds = %entry, %no_exit
***     %indvar = phi uint [ %indvar.next, %no_exit ], [ 0, %entry ]            ; <uint> [#uses=2]
***     %indvar = phi ulong [ %indvar.next, %no_exit ], [ 0, %entry ]           ; <ulong> [#uses=2]
        %indvar1 = cast ulong %indvar to uint           ; <uint> [#uses=1]
        %inc.02.sum = add uint %indvar1, 1              ; <uint> [#uses=1]
        %inc.0.0 = getelementptr sbyte* %str, uint %inc.02.sum          ; <sbyte*> [#uses=1]
        %tmp.1 = load sbyte* %inc.0.0           ; <sbyte> [#uses=1]
        %tmp.2 = seteq sbyte %tmp.1, 0          ; <bool> [#uses=1]
        %indvar.next = add ulong %indvar, 1             ; <ulong> [#uses=1]
        %indvar.next = add uint %indvar, 1              ; <uint> [#uses=1]
        br bool %tmp.2, label %loopexit.loopexit, label %no_exit

loopexit.loopexit:              ; preds = %no_exit
        %indvar = cast uint %indvar to int              ; <int> [#uses=1]
        %inc.1 = add int %indvar, 1             ; <int> [#uses=1]
        ret int %inc.1

loopexit:               ; preds = %entry
        ret int 0
}


Here's the after code:

int %_strlen(sbyte* %str) {
entry:
        %inc.02 = getelementptr sbyte* %str, uint 1             ; <sbyte*> [#uses=1]
        %tmp.13 = load sbyte* %str              ; <sbyte> [#uses=1]
        %tmp.24 = seteq sbyte %tmp.13, 0                ; <bool> [#uses=1]
        br bool %tmp.24, label %loopexit, label %no_exit

no_exit:                ; preds = %entry, %no_exit
***     %indvar = phi uint [ %indvar.next, %no_exit ], [ 0, %entry ]            ; <uint> [#uses=3]
        %indvar = cast uint %indvar to int              ; <int> [#uses=1]
        %inc.0.0 = getelementptr sbyte* %inc.02, uint %indvar           ; <sbyte*> [#uses=1]
        %inc.1 = add int %indvar, 1             ; <int> [#uses=1]
        %tmp.1 = load sbyte* %inc.0.0           ; <sbyte> [#uses=1]
        %tmp.2 = seteq sbyte %tmp.1, 0          ; <bool> [#uses=1]
        %indvar.next = add uint %indvar, 1              ; <uint> [#uses=1]
        br bool %tmp.2, label %loopexit, label %no_exit

loopexit:               ; preds = %entry, %no_exit
        %len.0.1 = phi int [ 0, %entry ], [ %inc.1, %no_exit ]          ; <int> [#uses=1]
        ret int %len.0.1
}


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13016 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-17 18:16:10 +00:00
..
ADCE.cpp Simplify code a bit, and fix a bug that was breaking perlbmk 2004-04-10 18:06:21 +00:00
BasicBlockPlacement.cpp Remove obsolete comment. Unreachable blocks will automatically be left at the 2004-02-11 05:20:50 +00:00
ConstantProp.cpp Constant propagation should remove the dead instructions 2004-04-13 19:28:20 +00:00
CorrelatedExprs.cpp Remove use of the ConstantHandling interfaces 2004-01-12 19:12:50 +00:00
DCE.cpp Finegrainify namespacification 2004-01-09 06:02:20 +00:00
DecomposeMultiDimRefs.cpp Finegrainify namespacification 2004-01-09 06:02:20 +00:00
GCSE.cpp Add support for removing invoke instructions 2004-04-12 05:15:13 +00:00
IndVarSimplify.cpp Even if there are not any induction variables in the loop, if we can compute 2004-04-17 18:08:33 +00:00
InstructionCombining.cpp Fix a HUGE pessimization on X86. The indvars pass was taking this 2004-04-17 18:16:10 +00:00
LICM.cpp Implement LICM of calls in simple cases. This is sufficient to move around 2004-03-15 04:11:30 +00:00
Makefile Added LLVM copyright notice to Makefiles. 2003-10-20 22:26:57 +00:00
PiNodeInsertion.cpp Finegrainify namespacification 2004-01-09 06:02:20 +00:00
PRE.cpp Finegrainify namespacification 2004-01-09 06:02:20 +00:00
Reassociate.cpp Finegrainify namespacification 2004-01-09 06:02:20 +00:00
ScalarReplAggregates.cpp Support getelementptr instructions which use uint's to index into structure 2004-04-05 01:30:19 +00:00
SCCP.cpp Add SCCP support for constant folding calls, implementing: 2004-04-13 19:43:54 +00:00
SimplifyCFG.cpp Finegrainify namespacification 2004-01-09 06:02:20 +00:00
SymbolStripping.cpp Update obsolete comments 2004-01-10 21:36:49 +00:00
TailDuplication.cpp Fix bug in previous checkin 2004-03-16 23:36:49 +00:00
TailRecursionElimination.cpp Adjust to the new BasicBlock ctor, which requires a function parameter 2004-02-04 03:58:28 +00:00