mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-08 21:32:39 +00:00
Generalize the previous xform to handle cases where exactly
two elements match or don't match with two comparisons. For example, the testcase compiles into: define i1 @test5(i32 %X) { %1 = icmp eq i32 %X, 2 ; <i1> [#uses=1] %2 = icmp eq i32 %X, 7 ; <i1> [#uses=1] %R = or i1 %1, %2 ; <i1> [#uses=1] ret i1 %R } This generalizes the previous xforms when the array is larger than 64 elements (and this case matches) and generates better code for cases where it overlaps with the magic bitshift case. This generalizes more cases than you might expect. For example, 400.perlbmk has: @PL_utf8skip = constant [256 x i8] c"\01\01\01\... %15 = icmp ult i8 %7, 7 403.gcc has: @rid_to_yy = internal constant [114 x i16] [i16 259, i16 260, ... %18 = icmp eq i16 %16, 295 and xalancbmk has a bunch of examples, such as _ZN11xercesc_2_5L15gCombiningCharsE and _ZN11xercesc_2_5L10gBaseCharsE. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92417 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
33a1ec76c2
commit
bef37376be
@ -6043,15 +6043,16 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
|
|||||||
|
|
||||||
// Variables for our state machines.
|
// Variables for our state machines.
|
||||||
|
|
||||||
// OnlyTrueElement - Used to emit a comparison of "i == 47", where 47 is the
|
// FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
|
||||||
// only index the condition is true for. The values are -1 -> undef,
|
// "i == 47 | i == 87", where 47 is the first index the condition is true for,
|
||||||
// -2 -> overdef, >= 0 -> that index is true.
|
// and 87 is the second (and last) index. FirstTrueElement is -1 when
|
||||||
int OnlyTrueElement = -1;
|
// undefined, otherwise set to the first true element. SecondTrueElement is
|
||||||
|
// -1 when undefined, -2 when overdefined and >= 0 when that index is true.
|
||||||
|
int FirstTrueElement = -1, SecondTrueElement = -1;
|
||||||
|
|
||||||
// OnlyFalseElement - Used to emit a comparison of "i != 47", where 47 is the
|
// FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
|
||||||
// only index the condition is false for. The values are -1 -> undef,
|
// form "i != 47 & i != 87". Same state transitions as for true elements.
|
||||||
// -2 -> overdef, >= 0 -> that index is false.
|
int FirstFalseElement = -1, SecondFalseElement = -1;
|
||||||
int OnlyFalseElement = -1;
|
|
||||||
|
|
||||||
// MagicBitvector - This is a magic bitvector where we set a bit if the
|
// MagicBitvector - This is a magic bitvector where we set a bit if the
|
||||||
// comparison is true for element 'i'. If there are 64 elements or less in
|
// comparison is true for element 'i'. If there are 64 elements or less in
|
||||||
@ -6079,11 +6080,21 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
|
|||||||
|
|
||||||
// State machine for single index comparison.
|
// State machine for single index comparison.
|
||||||
if (IsTrueForElt) {
|
if (IsTrueForElt) {
|
||||||
// If undefined -> defined. Otherwise -> overdefined.
|
// Update the TrueElement state machine.
|
||||||
OnlyTrueElement = OnlyTrueElement == -1 ? i : -2;
|
if (FirstTrueElement == -1)
|
||||||
|
FirstTrueElement = i;
|
||||||
|
else if (SecondTrueElement == -1)
|
||||||
|
SecondTrueElement = i;
|
||||||
|
else
|
||||||
|
SecondTrueElement = -2;
|
||||||
} else {
|
} else {
|
||||||
// If undefined -> defined. Otherwise -> overdefined.
|
// Update the FalseElement state machine.
|
||||||
OnlyFalseElement = OnlyFalseElement == -1 ? i : -2;
|
if (FirstFalseElement == -1)
|
||||||
|
FirstFalseElement = i;
|
||||||
|
else if (SecondFalseElement == -1)
|
||||||
|
SecondFalseElement = i;
|
||||||
|
else
|
||||||
|
SecondFalseElement = -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this element is in range, update our magic bitvector.
|
// If this element is in range, update our magic bitvector.
|
||||||
@ -6091,31 +6102,52 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
|
|||||||
MagicBitvector |= 1ULL << i;
|
MagicBitvector |= 1ULL << i;
|
||||||
|
|
||||||
// If all of our states become overdefined, bail out early.
|
// If all of our states become overdefined, bail out early.
|
||||||
if (i >= 64 && OnlyTrueElement == -2 && OnlyFalseElement == -2)
|
if (i >= 64 && SecondTrueElement == -2 && SecondFalseElement == -2)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now that we've scanned the entire array, emit our new comparison(s). We
|
// Now that we've scanned the entire array, emit our new comparison(s). We
|
||||||
// order the state machines in complexity of the generated code.
|
// order the state machines in complexity of the generated code.
|
||||||
if (OnlyTrueElement != -2) {
|
Value *Idx = GEP->getOperand(2);
|
||||||
|
|
||||||
|
// If the comparison is only true for one or two elements, emit direct
|
||||||
|
// comparisons.
|
||||||
|
if (SecondTrueElement != -2) {
|
||||||
// None true -> false.
|
// None true -> false.
|
||||||
if (OnlyTrueElement == -1)
|
if (FirstTrueElement == -1)
|
||||||
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
|
return ReplaceInstUsesWith(ICI, ConstantInt::getFalse(*Context));
|
||||||
|
|
||||||
|
Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
|
||||||
|
|
||||||
// True for one element -> 'i == 47'.
|
// True for one element -> 'i == 47'.
|
||||||
return new ICmpInst(ICmpInst::ICMP_EQ, GEP->getOperand(2),
|
if (SecondTrueElement == -1)
|
||||||
ConstantInt::get(GEP->getOperand(2)->getType(),
|
return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
|
||||||
OnlyTrueElement));
|
|
||||||
|
// True for two elements -> 'i == 47 | i == 72'.
|
||||||
|
Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
|
||||||
|
Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
|
||||||
|
Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
|
||||||
|
return BinaryOperator::CreateOr(C1, C2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (OnlyFalseElement != -2) {
|
// If the comparison is only false for one or two elements, emit direct
|
||||||
|
// comparisons.
|
||||||
|
if (SecondFalseElement != -2) {
|
||||||
// None false -> true.
|
// None false -> true.
|
||||||
if (OnlyFalseElement == -1)
|
if (FirstFalseElement == -1)
|
||||||
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
|
return ReplaceInstUsesWith(ICI, ConstantInt::getTrue(*Context));
|
||||||
|
|
||||||
return new ICmpInst(ICmpInst::ICMP_NE, GEP->getOperand(2),
|
Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
|
||||||
ConstantInt::get(GEP->getOperand(2)->getType(),
|
|
||||||
OnlyFalseElement));
|
// False for one element -> 'i != 47'.
|
||||||
|
if (SecondFalseElement == -1)
|
||||||
|
return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
|
||||||
|
|
||||||
|
// False for two elements -> 'i != 47 & i != 72'.
|
||||||
|
Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
|
||||||
|
Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement);
|
||||||
|
Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
|
||||||
|
return BinaryOperator::CreateAnd(C1, C2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a 32-bit or 64-bit magic bitvector captures the entire comparison state
|
// If a 32-bit or 64-bit magic bitvector captures the entire comparison state
|
||||||
@ -6128,14 +6160,14 @@ FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
|
|||||||
Ty = Type::getInt32Ty(Init->getContext());
|
Ty = Type::getInt32Ty(Init->getContext());
|
||||||
else
|
else
|
||||||
Ty = Type::getInt64Ty(Init->getContext());
|
Ty = Type::getInt64Ty(Init->getContext());
|
||||||
Value *V = Builder->CreateIntCast(GEP->getOperand(2), Ty, false);
|
Value *V = Builder->CreateIntCast(Idx, Ty, false);
|
||||||
V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
|
V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
|
||||||
V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
|
V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
|
||||||
return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
|
return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Range check, two compares.
|
// TODO: Range check
|
||||||
|
// TODO: GEP 0, i, 4
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,3 +45,15 @@ define i1 @test4(i32 %X) {
|
|||||||
; CHECK-NEXT: %R = icmp ne i32 {{.*}}, 0
|
; CHECK-NEXT: %R = icmp ne i32 {{.*}}, 0
|
||||||
; CHECK-NEXT: ret i1 %R
|
; CHECK-NEXT: ret i1 %R
|
||||||
}
|
}
|
||||||
|
|
||||||
|
define i1 @test5(i32 %X) {
|
||||||
|
%P = getelementptr [10 x i16]* @G16, i32 0, i32 %X
|
||||||
|
%Q = load i16* %P
|
||||||
|
%R = icmp eq i16 %Q, 69
|
||||||
|
ret i1 %R
|
||||||
|
; CHECK: @test5
|
||||||
|
; CHECK-NEXT: icmp eq i32 %X, 2
|
||||||
|
; CHECK-NEXT: icmp eq i32 %X, 7
|
||||||
|
; CHECK-NEXT: %R = or i1
|
||||||
|
; CHECK-NEXT: ret i1 %R
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user