mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-14 11:32:34 +00:00
Un-brain-dead-ify the lowering of part set for the reverse case.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37071 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
4ef9b11100
commit
76c94b6169
@ -442,57 +442,6 @@ static Instruction *LowerPartSet(CallInst *CI) {
|
||||
|
||||
// If we haven't defined the impl function yet, do so now
|
||||
if (F->isDeclaration()) {
|
||||
// Note: the following code is based on code generated by llvm2cpp with
|
||||
// the following input. This is just *one* example of a generated function.
|
||||
// The functions vary by bit width of result and first two arguments.
|
||||
// The generated code has been changed to deal with any bit width not just
|
||||
// the 32/64 bitwidths used in the above sample.
|
||||
//
|
||||
// define i64 @part_set(i64 %Val, i32 %Rep, i32 %Lo, i32 %Hi) {
|
||||
// entry:
|
||||
// %is_forward = icmp ult i32 %Lo, %Hi
|
||||
// %Lo.pn = select i1 %is_forward, i32 %Hi, i32 %Lo
|
||||
// %Hi.pn = select i1 %is_forward, i32 %Lo, i32 %Hi
|
||||
// %iftmp.16.0 = sub i32 %Lo.pn, %Hi.pn
|
||||
// icmp ult i32 %iftmp.16.0, 32
|
||||
// br i1 %1, label %cond_true11, label %cond_next19
|
||||
// cond_true11:
|
||||
// %tmp13 = sub i32 32, %iftmp.16.0
|
||||
// %tmp14 = lshr i32 -1, %tmp13
|
||||
// %tmp16 = and i32 %tmp14, %Rep
|
||||
// br label %cond_next19
|
||||
// cond_next19:
|
||||
// %iftmp.17.0 = phi i32 [ %tmp16, %cond_true11 ], [ %Rep, %entry ]
|
||||
// %tmp2021 = zext i32 %iftmp.17.0 to i64
|
||||
// icmp ugt i32 %Lo, %Hi
|
||||
// br i1 %2, label %cond_next60, label %cond_true24
|
||||
// cond_true24:
|
||||
// %tmp25.cast = zext i32 %Hi to i64
|
||||
// %tmp26 = lshr i64 -1, %tmp25.cast
|
||||
// %tmp27.cast = zext i32 %Lo to i64
|
||||
// %tmp28 = shl i64 %tmp26, %tmp27.cast
|
||||
// %tmp28not = xor i64 %tmp28, -1
|
||||
// %tmp31 = shl i64 %tmp2021, %tmp27.cast
|
||||
// %tmp34 = and i64 %tmp28not, %Val
|
||||
// %Val_addr.064 = or i64 %tmp31, %tmp34
|
||||
// ret i64 %Val_addr.064
|
||||
// cond_next60:
|
||||
// %tmp39.cast = zext i32 %Lo to i64
|
||||
// %tmp40 = shl i64 -1, %tmp39.cast
|
||||
// %tmp41.cast = zext i32 %Hi to i64
|
||||
// %tmp42 = shl i64 -1, %tmp41.cast
|
||||
// %tmp45.demorgan = or i64 %tmp42, %tmp40
|
||||
// %tmp45 = xor i64 %tmp45.demorgan, -1
|
||||
// %tmp47 = and i64 %tmp45, %Val
|
||||
// %tmp50 = shl i64 %tmp2021, %tmp39.cast
|
||||
// %tmp52 = sub i32 32, %Hi
|
||||
// %tmp52.cast = zext i32 %tmp52 to i64
|
||||
// %tmp54 = lshr i64 %tmp2021, %tmp52.cast
|
||||
// %tmp57 = or i64 %tmp50, %tmp47
|
||||
// %Val_addr.0 = or i64 %tmp57, %tmp54
|
||||
// ret i64 %Val_addr.0
|
||||
// }
|
||||
|
||||
// Get the arguments for the function.
|
||||
Function::arg_iterator args = F->arg_begin();
|
||||
Value* Val = args++; Val->setName("Val");
|
||||
@ -510,27 +459,31 @@ static Instruction *LowerPartSet(CallInst *CI) {
|
||||
ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
|
||||
ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
|
||||
ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
|
||||
ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
|
||||
ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
|
||||
ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
|
||||
ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
|
||||
|
||||
BasicBlock* entry = new BasicBlock("entry",F,0);
|
||||
BasicBlock* large = new BasicBlock("large",F,0);
|
||||
BasicBlock* small = new BasicBlock("small",F,0);
|
||||
BasicBlock* forward = new BasicBlock("forward",F,0);
|
||||
BasicBlock* reverse = new BasicBlock("reverse",F,0);
|
||||
// Basic blocks we fill in below.
|
||||
BasicBlock* entry = new BasicBlock("entry", F, 0);
|
||||
BasicBlock* large = new BasicBlock("large", F, 0);
|
||||
BasicBlock* small = new BasicBlock("small", F, 0);
|
||||
BasicBlock* reverse = new BasicBlock("reverse", F, 0);
|
||||
BasicBlock* result = new BasicBlock("result", F, 0);
|
||||
|
||||
// Block entry (entry)
|
||||
// BASIC BLOCK: entry
|
||||
// First, get the number of bits that we're placing as an i32
|
||||
ICmpInst* is_forward =
|
||||
new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
|
||||
SelectInst* Lo_pn = new SelectInst(is_forward, Hi, Lo, "", entry);
|
||||
SelectInst* Hi_pn = new SelectInst(is_forward, Lo, Hi, "", entry);
|
||||
BinaryOperator* NumBits = BinaryOperator::createSub(Lo_pn, Hi_pn, "",entry);
|
||||
SelectInst* Hi_pn = new SelectInst(is_forward, Hi, Lo, "", entry);
|
||||
SelectInst* Lo_pn = new SelectInst(is_forward, Lo, Hi, "", entry);
|
||||
BinaryOperator* NumBits = BinaryOperator::createSub(Hi_pn, Lo_pn, "",entry);
|
||||
NumBits = BinaryOperator::createAdd(NumBits, One, "", entry);
|
||||
// Now, convert Lo and Hi to ValTy bit width
|
||||
if (ValBits > 32) {
|
||||
Hi = new ZExtInst(Hi, ValTy, "", entry);
|
||||
Lo = new ZExtInst(Lo, ValTy, "", entry);
|
||||
Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
|
||||
} else if (ValBits < 32) {
|
||||
Hi = new TruncInst(Hi, ValTy, "", entry);
|
||||
Lo = new TruncInst(Lo, ValTy, "", entry);
|
||||
Lo = new TruncInst(Lo_pn, ValTy, "", entry);
|
||||
}
|
||||
// Determine if the replacement bits are larger than the number of bits we
|
||||
// are replacing and deal with it.
|
||||
@ -538,7 +491,7 @@ static Instruction *LowerPartSet(CallInst *CI) {
|
||||
new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry);
|
||||
new BranchInst(large, small, is_large, entry);
|
||||
|
||||
// Block "large"
|
||||
// BASIC BLOCK: large
|
||||
Instruction* MaskBits =
|
||||
BinaryOperator::createSub(RepBitWidth, NumBits, "", large);
|
||||
MaskBits = CastInst::createIntegerCast(MaskBits, RepMask->getType(),
|
||||
@ -548,7 +501,7 @@ static Instruction *LowerPartSet(CallInst *CI) {
|
||||
BinaryOperator* Rep2 = BinaryOperator::createAnd(Mask1, Rep, "", large);
|
||||
new BranchInst(small, large);
|
||||
|
||||
// Block "small"
|
||||
// BASIC BLOCK: small
|
||||
PHINode* Rep3 = new PHINode(RepTy, "", small);
|
||||
Rep3->reserveOperandSpace(2);
|
||||
Rep3->addIncoming(Rep2, large);
|
||||
@ -558,37 +511,58 @@ static Instruction *LowerPartSet(CallInst *CI) {
|
||||
Rep4 = new ZExtInst(Rep3, ValTy, "", small);
|
||||
else if (ValBits < RepBits)
|
||||
Rep4 = new TruncInst(Rep3, ValTy, "", small);
|
||||
ICmpInst* is_reverse =
|
||||
new ICmpInst(ICmpInst::ICMP_UGT, Lo, Hi, "", small);
|
||||
new BranchInst(reverse, forward, is_reverse, small);
|
||||
new BranchInst(result, reverse, is_forward, small);
|
||||
|
||||
// Block "forward"
|
||||
Value* t1 = BinaryOperator::createLShr(ValMask, Hi, "", forward);
|
||||
Value* t2 = BinaryOperator::createShl(t1, Lo, "", forward);
|
||||
Value* nott2 = BinaryOperator::createXor(t2, ValMask, "", forward);
|
||||
Value* t3 = BinaryOperator::createShl(Rep4, Lo, "", forward);
|
||||
Value* t4 = BinaryOperator::createAnd(nott2, Val, "", forward);
|
||||
Value* FRslt = BinaryOperator::createOr(t3, t4, "part_set_fwd", forward);
|
||||
new ReturnInst(FRslt, forward);
|
||||
// BASIC BLOCK: reverse (reverses the bits of the replacement)
|
||||
// Set up our loop counter as a PHI so we can decrement on each iteration.
|
||||
// We will loop for the number of bits in the replacement value.
|
||||
PHINode *Count = new PHINode(Type::Int32Ty, "count", reverse);
|
||||
Count->reserveOperandSpace(2);
|
||||
Count->addIncoming(NumBits, small);
|
||||
|
||||
// Block "reverse"
|
||||
Value* t5 = BinaryOperator::createShl(ValMask, Lo, "", reverse);
|
||||
Value* t6 = BinaryOperator::createShl(ValMask, Hi, "", reverse);
|
||||
Value* t7 = BinaryOperator::createOr(t6, t5, "", reverse);
|
||||
Value* t8 = BinaryOperator::createXor(t7, ValMask, "", reverse);
|
||||
Value* t9 = BinaryOperator::createAnd(t8, Val, "", reverse);
|
||||
Value* t10 = BinaryOperator::createShl(Rep4, Lo, "", reverse);
|
||||
if (32 < ValBits)
|
||||
RepBitWidth =
|
||||
cast<ConstantInt>(ConstantExpr::getZExt(RepBitWidth, ValTy));
|
||||
else if (32 > ValBits)
|
||||
RepBitWidth =
|
||||
cast<ConstantInt>(ConstantExpr::getTrunc(RepBitWidth, ValTy));
|
||||
Value* t11 = BinaryOperator::createSub(RepBitWidth, Hi, "", reverse);
|
||||
Value* t13 = BinaryOperator::createLShr(Rep4, t11, "",reverse);
|
||||
Value* t14 = BinaryOperator::createOr(t10, t9, "", reverse);
|
||||
Value* RRslt = BinaryOperator::createOr(t14, t13, "part_set_rvrs", reverse);
|
||||
new ReturnInst(RRslt, reverse);
|
||||
// Get the value that we are shifting bits out of as a PHI because
|
||||
// we'll change this with each iteration.
|
||||
PHINode *BitsToShift = new PHINode(Val->getType(), "val", reverse);
|
||||
BitsToShift->reserveOperandSpace(2);
|
||||
BitsToShift->addIncoming(Rep4, small);
|
||||
|
||||
// Get the result of the last computation or zero on first iteration
|
||||
PHINode *RRes = new PHINode(Val->getType(), "rres", reverse);
|
||||
RRes->reserveOperandSpace(2);
|
||||
RRes->addIncoming(ValZero, small);
|
||||
|
||||
// Decrement the loop counter by one
|
||||
Instruction *Decr = BinaryOperator::createSub(Count, One, "", reverse);
|
||||
Count->addIncoming(Decr, reverse);
|
||||
|
||||
// Get the bit that we want to move into the result
|
||||
Value *Bit = BinaryOperator::createAnd(BitsToShift, ValOne, "", reverse);
|
||||
|
||||
// Compute the new value of the bits to shift for the next iteration.
|
||||
Value *NewVal = BinaryOperator::createLShr(BitsToShift, ValOne,"", reverse);
|
||||
BitsToShift->addIncoming(NewVal, reverse);
|
||||
|
||||
// Shift the bit we extracted into the low bit of the result.
|
||||
Instruction *NewRes = BinaryOperator::createShl(RRes, ValOne, "", reverse);
|
||||
NewRes = BinaryOperator::createOr(NewRes, Bit, "", reverse);
|
||||
RRes->addIncoming(NewRes, reverse);
|
||||
|
||||
// Terminate loop if we've moved all the bits.
|
||||
ICmpInst *Cond = new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "", reverse);
|
||||
new BranchInst(result, reverse, Cond, reverse);
|
||||
|
||||
// BASIC BLOCK: result
|
||||
PHINode *Rplcmnt = new PHINode(Val->getType(), "", result);
|
||||
Rplcmnt->reserveOperandSpace(2);
|
||||
Rplcmnt->addIncoming(NewRes, reverse);
|
||||
Rplcmnt->addIncoming(Rep4, small);
|
||||
Value* t0 = CastInst::createIntegerCast(NumBits,ValTy,false,"",result);
|
||||
Value* t1 = BinaryOperator::createShl(ValMask, t0, "", result);
|
||||
Value* t2 = BinaryOperator::createShl(t1, Lo, "", result);
|
||||
Value* t3 = BinaryOperator::createAnd(t2, Val, "", result);
|
||||
Value* t4 = BinaryOperator::createShl(Rplcmnt, Lo, "", result);
|
||||
Value* Rslt = BinaryOperator::createOr(t3, t4, "part_set", result);
|
||||
new ReturnInst(Rslt, result);
|
||||
}
|
||||
|
||||
// Return a call to the implementation function
|
||||
|
Loading…
Reference in New Issue
Block a user